BootReceiver.java 4.12 KB
/*
 *  Copyright (c)  2016.  wugian
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package com.xgimi.gimicinema.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.gimi.common.cinema.model.Constant;
import com.gimi.common.cinema.utils.SystemUtils;
import com.gimi.common.cinema.utils.WifiApManger;
import com.xgimi.gimicinema.activity.StartActivity;
import com.xgimi.gimicinema.service.AskService;

/**
 * Created by pc on 2015/1/8
 */
public class BootReceiver extends BroadcastReceiver {
    static final String ACTION = "android.intent.action.BOOT_COMPLETED";
    private int workMode;
    private boolean startOpen;
    private boolean openAp;

    public void onReceiveIntent(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION)) {
            SharedPreferences sharedPreferences = context.getSharedPreferences(
                    Constant.XML_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean("has_enter_pwd", false);
            editor.apply();
            startOpen = sharedPreferences.getBoolean("start_open", false);
            openAp = sharedPreferences.getBoolean("open_ap", false);
            workMode = new SystemUtils().getYbProp();
            mConnectivityManager = (ConnectivityManager)
                    context.getSystemService(Context.CONNECTIVITY_SERVICE);
            netInfo = mConnectivityManager.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isAvailable()) {
                bootInit(context);
            } else {
                IntentFilter mFilter = new IntentFilter();
                mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
                context.getApplicationContext()
                        .registerReceiver(myNetReceiver, mFilter);
            }
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        onReceiveIntent(context, intent);
    }

    private ConnectivityManager mConnectivityManager;
    private NetworkInfo netInfo;
    private BroadcastReceiver myNetReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                return;
            }
            mConnectivityManager = (ConnectivityManager)
                    context.getSystemService(Context.CONNECTIVITY_SERVICE);
            netInfo = mConnectivityManager.getActiveNetworkInfo();
            if (myNetReceiver != null) {
                try {
                    context.unregisterReceiver(myNetReceiver);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (netInfo != null && netInfo.isAvailable()) {
                bootInit(context);
            }
        }
    };

    private void bootInit(Context context) {
        if (workMode != 3) {
            Intent intents = new Intent(context, AskService.class);
            context.startService(intents);
        }
        if (startOpen && !Constant.userOpen) {
            Intent openIntent = new Intent(context, StartActivity.class);
            openIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(openIntent);
        }
        if (openAp) {
            WifiApManger.openWifiAp(context);
        }
    }
}