package com.mjsheng.myappstore.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

import cn.jpush.android.api.JPushInterface;

import com.alibaba.fastjson.JSON;
import com.arialyy.aria.core.Aria;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.EncryptUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PathUtils;
import com.mjsheng.myappstore.BuildConfig;
import com.mjsheng.myappstore.base.MyApplication;
import com.mjsheng.myappstore.comm.CommonDatas;
import com.mjsheng.myappstore.network.Network;
import com.mjsheng.myappstore.utils.ApkUtils;
import com.mjsheng.myappstore.utils.MySQLData;
import com.mjsheng.myappstore.utils.SaveListUtils;
import com.mjsheng.myappstore.utils.Utils;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

import java.util.Iterator;
import java.util.List;

import okhttp3.ResponseBody;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MyJPushReceiver extends BroadcastReceiver {
    private static final String TAG = "MyJPushReceiver";

    private final String ACTION_HrReceiver_JGY = "qch_jgy_network_allow";

    private final String ACTION_HrReceiver_JGY_DIS = "qch_jgy_network_disallow";

    private final String MSG_APPID = "5";

    private final String MSG_BROWSER = "4";

    private final String MSG_DELETE = "1";

    private final String MSG_INSTALL = "9";

    private final String MSG_LOCK = "10";//解锁

    private final String MSG_NET_CONTROL = "6";

    private final String MSG_POWER_ON = "7";

    private final String MSG_RESET = "8";//加锁

    private final String MSG_SETTING = "2";

    private final String MSG_CAMERA = "11";

    private final String MSG_ONEPACKAGES = "12";

    private final String GET_APP_USEDTIME = "13";


    private Context mContext;

    private void SettingSysData(String extras) {
        try {
            JSONObject data = new JSONObject(extras);
            int setting_call = changeNum(data.optInt("setting_call"));
            boolean qch_call_forbid = Settings.System.putInt(mContext.getContentResolver(), "qch_call_forbid", setting_call);
            Log.e("SystemSetting", "qch_call_forbid---------" + setting_call);
            Log.e("SystemSetting", "qch_call_forbid---------" + qch_call_forbid);

            int setting_phone = changeNum(data.optInt("setting_phone"));
            boolean qch_white_list_on = Settings.System.putInt(mContext.getContentResolver(), "qch_white_list_on", setting_phone);
            Log.e("SystemSetting", "qch_call_forbid---------" + setting_phone);
            Log.e("SystemSetting", "qch_white_list_on---------" + qch_white_list_on);

            String setting_phones = data.optString("setting_phones");
            boolean qch_white_list_Array = Settings.System.putString(mContext.getContentResolver(), "qch_white_list_Array", setting_phones);
            Log.e("SystemSetting", "qch_call_forbid---------" + setting_phones);
            //            ToastTool.show("qch_call_forbid::"+setting_call+"----setting_phones::"+setting_phones+"----"+qch_white_list_Array+"---"+qch_call_forbid);
            Log.e("SystemSetting", "qch_white_list_Array---------" + qch_white_list_Array + "---" + setting_phones);

            int setting_memory = changeNum(data.optInt("setting_memory"));
            boolean qch_sdcard_forbid_on = Settings.System.putInt(mContext.getContentResolver(), "qch_sdcard_forbid_on", setting_memory);
            Log.e("SystemSetting", "qch_sdcard_forbid_on---------" + setting_memory);
            Log.e("SystemSetting", "qch_sdcard_forbid_on---------" + qch_sdcard_forbid_on);


            //USB数据功能管控
            //仅充电：usb_charge
            //MTP模式：usb_mtp
            //Midi模式：usb_midi
            String setting_usb = data.optString("setting_usb");
            if (!BuildConfig.DEBUG) {
                boolean qch_usb_choose = Settings.System.putString(mContext.getContentResolver(), "qch_usb_choose", setting_usb);
                Log.e("SystemSetting", "qch_usb_choose---------" + setting_usb);
                Log.e("SystemSetting", "qch_usb_choose---------" + qch_usb_choose);
            String usbStatus = "";
            switch (setting_usb) {
                case "usb_charge":
                    usbStatus = "qch_action_usb_usb_charge";
                    break;
                case "usb_mtp":
                    usbStatus = "qch_action_usb_usb_mtp";
                    break;
                case "usb_midi":
                    usbStatus = "qch_action_usb_usb_midi";
                    break;

            }
            Intent usbIntent = new Intent(usbStatus);
            mContext.sendBroadcast(usbIntent);
            }

            int setting_bluetooth = changeNum(data.optInt("setting_bluetooth"));
            boolean qch_bt_forbid_on = Settings.System.putInt(mContext.getContentResolver(), "qch_bt_forbid_on", setting_bluetooth);
            Log.e("SystemSetting", "qch_bt_forbid_on---------" + setting_bluetooth);
            Log.e("SystemSetting", "qch_bt_forbid_on---------" + qch_bt_forbid_on);
            Log.e("SystemSetting", "qch_bt_forbid_on---------" + Settings.System.getInt(mContext.getContentResolver(), "qch_bt_forbid_on", 0));

            //系统导航条显示开关
            int setting_navigation = changeNum(data.optInt("setting_navigation"));
            boolean qch_hide_navigationBar = Settings.System.putInt(mContext.getContentResolver(), "qch_hide_NavigationBar", setting_navigation);
            Log.e("SystemSetting", "qch_hide_navigationBar---------" + setting_navigation);
            Log.e("SystemSetting", "qch_hide_navigationBar---------" + qch_hide_navigationBar);

            String navigationStatus = "";
            switch (setting_navigation) {
                case 0:
                    navigationStatus = "qch_show_NavigationBar";
                    break;
                case 1:
                    navigationStatus = "qch_hide_NavigationBar";
                    break;

            }
            Intent navIntent = new Intent(navigationStatus);
            mContext.sendBroadcast(navIntent);


            //状态栏显示开关
            int setting_statusbar = changeNum(data.optInt("setting_statusbar"));
            boolean qch_hide_statusBar = Settings.System.putInt(mContext.getContentResolver(), "qch_hide_statusBar", setting_statusbar);
            Log.e("SystemSetting", "qch_hide_statusBar---------" + setting_statusbar);
            Log.e("SystemSetting", "qch_hide_statusBar---------" + qch_hide_statusBar);

            String statusbarStatus = "";
            switch (setting_statusbar) {
                case 0:
                    statusbarStatus = "qch_show_statusBar";
                    break;
                case 1:
                    statusbarStatus = "qch_hide_statusBar";
                    break;

            }
            Intent statusIntent = new Intent(statusbarStatus);
            mContext.sendBroadcast(statusIntent);

            //摄像头开关
            int setting_camera = changeNum(data.optInt("setting_camera"));
            Log.e("SystemSetting", "setting_camera---------" + setting_camera);
            String cameraStatus = "";
            switch (setting_camera) {
                case 0:
                    cameraStatus = "qch_camera_open";
                    break;
                case 1:
                    cameraStatus = "qch_camera_forbid";
                    break;
            }
            Intent cameraIntent = new Intent(cameraStatus);
            mContext.sendBroadcast(cameraIntent);

            //影音管控开关
            int setting_tfmedia = changeNum(data.optInt("setting_tfmedia"));
            Settings.System.putInt(this.mContext.getContentResolver(), "qch_tfmedia_forbid", setting_tfmedia);
            Log.e("SystemSetting", "qch_tfmedia_forbid---------" + setting_tfmedia);

            if (setting_tfmedia == 1) {
                JSONArray jSONArray = null;
                try {
                    jSONArray = data.getJSONArray("setting_tfmedia_format");

                    int i = 0;
                    StringBuffer stringBuffer = new StringBuffer();
                    while (!jSONArray.isNull(i)) {
                        stringBuffer.append(jSONArray.getString(i));
                        stringBuffer.append(",");
                        i++;
                    }
                    stringBuffer.deleteCharAt(stringBuffer.length() - 1);
                    Settings.System.putString(this.mContext.getContentResolver(), "qch_tfmedia_filetypes", stringBuffer.toString());//影音管控
                    Log.e("SystemSetting", "qch_tfmedia_filetypes---------" + stringBuffer.toString());

                } catch (JSONException e) {
                    Log.e("SystemSetting", "qch_tfmedia_filetypes---------" + e.getMessage());

                }
            } else {
                Settings.System.putInt(this.mContext.getContentResolver(), "qch_tfmedia_forbid", 0);
            }
            //added:2019.12.6
            //设置5个app的开关
            //时钟
            int deskclock = changeNum(data.optInt("setting_clock"));
            Settings.System.putInt(this.mContext.getContentResolver(), "qch_app_deskclock", deskclock);
            Log.e("SystemSetting", "qch_app_deskclock" + deskclock);
            //录音机
            int soundrecorder = changeNum(data.optInt("setting_recording"));
            Settings.System.putInt(this.mContext.getContentResolver(), "qch_app_soundrecorder", soundrecorder);
            Log.e("SystemSetting", "qch_app_soundrecorder" + soundrecorder);
            //音乐
            int music = changeNum(data.optInt("setting_music"));
            Settings.System.putInt(this.mContext.getContentResolver(), "qch_app_music", music);
            Log.e("SystemSetting", "qch_app_music" + music);
            //图库
            int gallery = changeNum(data.optInt("setting_picture"));
            Settings.System.putInt(this.mContext.getContentResolver(), "qch_app_gallery", gallery);
            Log.e("SystemSetting", "qch_app_gallery" + gallery);
            //壁纸
            int wallpaper = changeNum(data.optInt("setting_wallpaper"));
            Settings.System.putInt(this.mContext.getContentResolver(), "qch_app_wallpaper", wallpaper);
            Log.e("SystemSetting", "qch_app_wallpaper" + wallpaper);
            //文件管理器
            int filemanager = changeNum(data.optInt("setting_file"));
            Settings.System.putInt(this.mContext.getContentResolver(), "qch_app_filemanager", filemanager);
            Log.e("SystemSetting", "qch_app_filemanager" + filemanager);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("mjhseng", "SettingSysData---error::" + e.getMessage());
        }
    }

    private int changeNum(int paramInt) {
        switch (paramInt) {
            default:
                return 1;
            case 0:
                return 1;
            case 1:
                break;
        }
        return 0;
    }

    private void doDownloadAndInstall(String s) {
        if (TextUtils.isEmpty(s)) {
            Log.e("mjsheng", "doDownloadAndInstall extras is null");
            return;
        }
        try {
            JSONObject jSONObject = new JSONObject(s);
            s = jSONObject.optString("app_package");
            String str = jSONObject.optString("app_url");
            Log.e("mymjsheng", "app_package::" + s);
            Log.e("mymjsheng", "app_url::" + str);
            SaveListUtils.addToList(s);
            List<String> list = SaveListUtils.getList();
            if (list != null) {
                if (!list.contains(s)) {
                    SaveListUtils.addToList(s);
                }
            } else {
                SaveListUtils.addToList(s);
            }

            if (!AppUtils.isAppInstalled(s)) {
                if (!SaveListUtils.isDownLoading(s)) {

                    (Aria.download(this)
                            .load(str)
                            .setDownloadPath(PathUtils.getExternalDownloadsPath() + "/ygj/" + EncryptUtils.encryptMD5ToString(s) + ".apk").setExtendField(s))
                            .start();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }

    private void getAppLimitApi() {
        Network.getAppLimitApi().getAppLimitApi("YTM3YTAxNTJmMmZmNzkyM2E2YzIwZjlhZTc0NzNmMGI=", Utils.getSerial()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<ResponseBody>() {
            public void onComplete() {
            }

            public void onError(Throwable param1Throwable) {
                Log.e("mjsheng", "getAppLimitApi=onError:");
            }

            public void onNext(ResponseBody param1ResponseBody) {
                try {
                    String str1;
                    String str2 = param1ResponseBody.string();
                    Log.e("mjsheng", str2);
                    JSONObject jSONObject = new JSONObject(str2);
                    switch (Integer.valueOf(jSONObject.optInt("code")).intValue()) {
                        case 200:
                            str1 = jSONObject.getJSONObject("data").optString("result");
                            writeAppPackageList(str1);
                            return;
                    }
                    Log.e("mjsheng", "getAppLimitApi---code is -200");
                    return;
                } catch (Exception e) {
                    Log.e("mjsheng", "getAppLimitApi---woring----" + e.getMessage());
                    e.printStackTrace();
                    return;
                }
            }

            public void onSubscribe(Disposable param1Disposable) {
            }
        });
    }

    private int getBatteryLevel() {
        if (Build.VERSION.SDK_INT >= 21)
            return ((BatteryManager) this.mContext.getSystemService(Context.BATTERY_SERVICE)).getIntProperty(4);
        Intent intent = (new ContextWrapper(this.mContext)).registerReceiver(null, new IntentFilter("android.intent.action.BATTERY_CHANGED"));
        return intent.getIntExtra("level", -1) * 100 / intent.getIntExtra("scale", -1);
    }

    private void getDeselectBrowerID() {
        Network.getDeselectBrowserIDApi().getDeselectBrowserIDApi("YTM3YTAxNTJmMmZmNzkyM2E2YzIwZjlhZTc0NzNmMGI=", Utils.getSerial()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<ResponseBody>() {
            public void onComplete() {
                Log.e("mjsheng", "getDeselectBrowerID---onComplete");
            }

            public void onError(Throwable param1Throwable) {
                Log.e("mjsheng", "getDeselectBrowerID=onError:");
            }

            public void onNext(ResponseBody param1ResponseBody) {
                try {
                    String str1;
                    String str2 = param1ResponseBody.string();
                    Log.e("mjsheng", "DeselectBrowserIDApi::" + str2);
                    JSONObject jSONObject = new JSONObject(str2);
                    switch (Integer.valueOf(jSONObject.optInt("code")).intValue()) {
                        case 200:
                            str1 = jSONObject.getJSONObject("data").optString("ids");
                            writeDeselectBrowserIDtoSystem(str1);
                            return;
                    }
                    Log.e("mjsheng", "getDeselectBrowerID---code is -200");
                    return;
                } catch (Exception e) {
                    Log.e("mjsheng", "getDeselectBrowerID---wroing----" + e.getMessage());
                    e.printStackTrace();
                    return;
                }
            }

            public void onSubscribe(Disposable param1Disposable) {
                Log.e("mjsheng", "getDeselectBrowerID---onSubscribe");
            }
        });
    }


    private void getDeselectID() {
        Network.getDeselectIDApi().getDeselectIDApi("YTM3YTAxNTJmMmZmNzkyM2E2YzIwZjlhZTc0NzNmMGI=", Utils.getSerial()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<ResponseBody>() {
            public void onComplete() {
            }

            public void onError(Throwable param1Throwable) {
                Log.e("mjsheng", "DeselectIDApi=onError:");
            }

            public void onNext(ResponseBody param1ResponseBody) {
                try {
                    String str1;
                    String str2 = param1ResponseBody.string();
                    Log.e("mjsheng", str2);
                    JSONObject jSONObject = new JSONObject(str2);
                    switch (Integer.valueOf(jSONObject.optInt("code")).intValue()) {
                        case 200:
                            str1 = jSONObject.getJSONObject("data").optString("ids");
                            str2 = jSONObject.getJSONObject("data").optString("package");
                            writeDeselectIDtoSystem(str1, str2);
                            return;
                    }
                    Log.e("mjsheng", "getDeselectID---code is -200");
                    return;
                } catch (Exception e) {
                    Log.e("mjsheng", "getDeselectID---woring----" + e.getMessage());
                    e.printStackTrace();
                    return;
                }
            }

            public void onSubscribe(Disposable param1Disposable) {
            }
        });
    }


    private static String printBundle(Bundle paramBundle) {
        StringBuilder stringBuilder = new StringBuilder();
        for (String str : paramBundle.keySet()) {
            if (str.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
                stringBuilder.append("\nkey:" + str + ", value:" + paramBundle.getInt(str));
                continue;
            }
            if (str.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
                stringBuilder.append("\nkey:" + str + ", value:" + paramBundle.getBoolean(str));
                continue;
            }
            if (str.equals(JPushInterface.EXTRA_EXTRA)) {
                if (paramBundle.getString(JPushInterface.EXTRA_EXTRA).isEmpty()) {
                    Log.e("MyJPushReceiver", "This message has no Extra data");
                    continue;
                }
                try {
                    JSONObject jSONObject = new JSONObject(paramBundle.getString(JPushInterface.EXTRA_EXTRA));
                    Iterator iterator = jSONObject.keys();
                    while (iterator.hasNext()) {
                        String str1 = ((String) iterator.next());
                        stringBuilder.append("\nkey:" + str + ", value: [" + str1 + " - " + jSONObject.optString(str1) + "]");
                    }
                    continue;
                } catch (JSONException e) {
                    Log.e("MyJPushReceiver", "Get message extra JSON error!");
                    continue;
                }
            }
            stringBuilder.append("\nkey:" + str + ", value:" + paramBundle.getString(str));
        }
        return stringBuilder.toString();
    }

    private void processCustomMessage(final Context context, Bundle bundle) {
        String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
        String title = bundle.getString(JPushInterface.EXTRA_TITLE);
        String type = bundle.getString(JPushInterface.EXTRA_CONTENT_TYPE);
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        String package_name = bundle.getString("package_name");


        LogUtils.e("mjsheng", "message::" + message + "--type::" + type + "--title::" + title + "--extras::" + extras);
//        ToastUtils.showShort("message::" + message + "--type::" + type + "--title::" + title + "--extras::" + extras + "---package_name:" + package_name);
//        Toast.makeText(context, "message::" + message + "--type::" + type + "--title::" + title + "--extras::" + extras + "---package_name:" + package_name, Toast.LENGTH_SHORT).show();
        switch (message) {
            case MSG_DELETE:
                if (!TextUtils.isEmpty(title)) {
                    ApkUtils.deleteApkInSilence(title);
                }
                break;
            case MSG_SETTING:
                if (!TextUtils.isEmpty(extras)) {
                    SettingSysData(extras);
                    Log.e(TAG, "title--------" + extras);
                }
                //                getSystemSetting();
                break;
            case MSG_BROWSER:
                getDeselectBrowerID();
                break;
            case MSG_APPID:
                getDeselectID();
                break;
            case MSG_NET_CONTROL:
                settingNetControl(extras);
                break;
            case MSG_POWER_ON:
                settingPowerOn(extras);
                break;
            case MSG_RESET:
                if (getBatteryLevel() >= CommonDatas.MIN_POWER) {
                    mContext.sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
                } else {
                    MySQLData.SetBooleanData(mContext, CommonDatas.IS_RESET, true);
                }
                break;
            case MSG_INSTALL:
                //                mContext.sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
                getAppLimitApi();
                doDownloadAndInstall(extras);
                break;
            case MSG_LOCK:
                settingLock(extras);
                break;
            case MSG_CAMERA:
                settingCamera(extras);
                break;
            case MSG_ONEPACKAGES:
                if (MyApplication.getInstance().isFinished()) {
                    settingOneNet(extras);
                }
                break;
            case GET_APP_USEDTIME:
                com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(extras);
                String random = jsonObject.getString("random");
                String sendType = jsonObject.getString("type");
                MyApplication.sendAppUsedTime(random, sendType);
                break;

        }
    }


    //    args[1] = message::11--type::11--title::ok--extras::{"ban":"com.lizi.yuwen,com.iyuyan.jp2listensimple,com.netease.edu.study," +
//            "cn.xuexi.android,com.tuixin.pospointsystem"
//            ,"not":"com.duoduo.child.story,com.shanbay.words,com.jiongji.andriod.card,fm.jihua.kecheng,com.duia.duiaapp," +
//            "com.handsgo.jiakao.android,com.chinessjunqi.nb,com.cktiku.gushiwenbibei,com.yangcong345.android.phone"}
    private void settingCamera(String s) {
        if (TextUtils.isEmpty(s)) {
            Intent intent = new Intent("qch_camera_forbid");
            intent.putExtra("camera_package_name", "close");
            this.mContext.sendBroadcast(intent);
            Log.e("mjsheng", "settingNetControl extras is null");
            return;
        }
        try {
            JSONObject jSONObject = new JSONObject(s);
            String s1 = jSONObject.optString("ban");
//            String str = jSONObject.optString("not");
            Log.e("mymjsheng", "ban::" + s1);
//            Log.e("mymjsheng", "not::" + str);
            if (s1.equals("0")) {
                Intent intent2 = new Intent("qch_camera_forbid");
//            intent2.putExtra("camera_package_name", s1);
                this.mContext.sendBroadcast(intent2);
            } else {
                Intent intent1 = new Intent("qch_camera_open");
//            intent1.putExtra("camera_package_name", str);
                this.mContext.sendBroadcast(intent1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void settingOneNet(String s) {
        if (TextUtils.isEmpty(s)) {
            Log.e("mjsheng", "settingNetControl extras is null");
//            return;
        }
        try {
            JSONObject jSONObject = new JSONObject(s);
            Log.e("mymjsheng", "jSONObject::" + jSONObject.toString());

            String packages = jSONObject.optString("package");
            String status = jSONObject.optString("status");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void settingNetControl(String s) {
        if (TextUtils.isEmpty(s)) {
            Log.e("mjsheng", "settingNetControl extras is null");
//            return;
        }
        try {
            JSONObject jSONObject = new JSONObject(s);
            Log.e("mymjsheng", "jSONObject::" + jSONObject.toString());
            String strban = jSONObject.optString("ban");
            String strnot = jSONObject.optString("not");
            String qch_jgy_network_disallow = Settings.System.getString(mContext.getApplicationContext().getContentResolver(), "qch_jgy_network_disallow");
            String qch_jgy_network_allow = Settings.System.getString(mContext.getApplicationContext().getContentResolver(), "qch_jgy_network_allow");
            Log.e("fht", "qch_jgy_network_disallow::" + qch_jgy_network_disallow);
            Log.e("fht", "qch_jgy_network_allow::" + qch_jgy_network_allow);

            if (strban != null && !strban.equals("")) {
                Settings.System.putString(mContext.getContentResolver(), "qch_jgy_network_disallow", strban);
//                Log.e("fht", "qch_jgy_network_disallow::" + Settings.System.getString(mContext.getApplicationContext().getContentResolver(), "qch_jgy_network_disallow"));
//                Log.e("fht", "qch_jgy_network_allow::" + Settings.System.getString(mContext.getApplicationContext().getContentResolver(), "qch_jgy_network_allow"));
                Log.e("fht", "sendban::" + strban);

            } else {
                Settings.System.putString(mContext.getContentResolver(), "qch_jgy_network_disallow", "invalid");
            }
            if (strnot != null && !strnot.equals("")) {
                Settings.System.putString(mContext.getContentResolver(), "qch_jgy_network_allow", strnot);
//                String[] nots = strnot.split(",");
//                Log.e("fht", "qch_jgy_network_disallow::" + Settings.System.getString(mContext.getApplicationContext().getContentResolver(), "qch_jgy_network_disallow"));
//                Log.e("fht", "qch_jgy_network_allow::" + Settings.System.getString(mContext.getApplicationContext().getContentResolver(), "qch_jgy_network_allow"));
                Log.e("fht", "sendnot::" + strnot);
//                new BroadcastThread("qch_jgy_network_allow", nots).start();
            } else {
                Settings.System.putString(mContext.getContentResolver(), "qch_jgy_network_allow", "invalid");
            }
            Log.e("fht", "qch_jgy_network_disallow::" + Settings.System.getString(mContext.getApplicationContext().getContentResolver(), "qch_jgy_network_disallow"));
            Log.e("fht", "qch_jgy_network_allow::" + Settings.System.getString(mContext.getApplicationContext().getContentResolver(), "qch_jgy_network_allow"));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class BroadcastThread extends Thread {
        String[] packagename;
        String action;

        public BroadcastThread(String a, String[] s) {
            super();
            this.packagename = s;
            this.action = a;
        }

        @Override
        public void run() {
            super.run();
            for (String name : packagename) {
                Intent disIntent = new Intent(action);
                disIntent.putExtra("package_name", name);
                Log.e("mymjsheng", "package_name::" + name);
                mContext.sendBroadcast(disIntent);
                try {
                    sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void settingPowerOn(String s) {
        if (TextUtils.isEmpty(s)) {
            Log.e("mjsheng", "settingNetControlAndPowerOn extras is null");
            return;
        }
        try {
            JSONObject jSONObject = new JSONObject(s);
            jSONObject.optString("ban");
            String str = jSONObject.optString("not");
            LogUtils.e("mjsheng", "settingPowerOn������" + str);
            Settings.System.putString(this.mContext.getContentResolver(), "qch_app_power_on", str);
            Log.e("SystemSetting", "qch_app_power_on---------" + str);
            return;
        } catch (JSONException e) {
            e.printStackTrace();
            return;
        }
    }

    private void writeAppPackageList(String s) {
        if (!TextUtils.isEmpty(s)) {
            LogUtils.e(s);
            Settings.System.putString(this.mContext.getContentResolver(), "qch_app_forbid", s);
            Log.e("SystemSetting", "qch_app_forbid__________" + Settings.System.getString(this.mContext.getContentResolver(), "qch_app_forbid"));
            return;
        }
        Log.e("mjsheng", "writeAppPackageList is null:");
    }

    private void writeDeselectBrowserIDtoSystem(String s) {
//        if (!TextUtils.isEmpty(s)) {
        boolean bool = Settings.System.putString(this.mContext.getContentResolver(), "DeselectBrowserArray", s);
        Log.e("SystemSetting", "DeselectBrowserArray---------" + s);
        Log.e("SystemSetting", "DeselectBrowserArray---------" + bool);
//            return;
//        }
//        Log.e("mjsheng", "writeDeselectBrowserIDtoSystem is null:");
    }

    //应用id管控
    private void writeDeselectIDtoSystem(String s1, String s2) {
        if (!TextUtils.isEmpty(s1) && !TextUtils.isEmpty(s2)) {
            Settings.System.putString(this.mContext.getContentResolver(), "qch_app_forbid", s2);
            Settings.System.putString(this.mContext.getContentResolver(), "DeselectViewArray", s1);
            Log.e("SystemSetting", "qch_app_forbid---------" + s2);
            Log.e("SystemSetting", "deselectViewArray---------" + s1);
            return;
        }
        Log.e("mjsheng", "writeDeselectIDtoSystem is null:");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        Bundle bundle = intent.getExtras();

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Log.e(TAG, "[MyReceiver] 接收Registration Id : " + regId);
            //send the Registration Id to your server...
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            //            ToastTool.show("[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE)
            //                    + "---" + bundle.getString(JPushInterface.EXTRA_TITLE) + "---" + bundle.getString(JPushInterface.EXTRA_CONTENT_TYPE)
            //                    + "---" + bundle.getString(JPushInterface.EXTRA_EXTRA) + "----" + bundle.getString("package_name"));
            //            Log.e(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE)
            //                    + "---" + bundle.getString(JPushInterface.EXTRA_TITLE) + "---" + bundle.getString(JPushInterface.EXTRA_CONTENT_TYPE)
            //                    + "---" + bundle.getString(JPushInterface.EXTRA_EXTRA) + "----" + bundle.getString("package_name"));
            processCustomMessage(context, bundle);
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.e(TAG, "[MyReceiver] 接收到推送下来的通知" + bundle.getString(JPushInterface.EXTRA_EXTRA));
            //            doDownloadAndInstall(bundle.getString(JPushInterface.EXTRA_EXTRA));
            //            LogUtils.e(getBatteryLevel()+"-0-iweroiwerewefeerer-----------");
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            Log.e(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);


        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.e(TAG, "[MyReceiver] 用户点击打开了通知");


        } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
            Log.e(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
            //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity， 打开一个网页等..

        } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
            boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
            Log.e(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected);
        } else {
            Log.e(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
        }
    }

    public void settingLock(String s) {
        if (TextUtils.isEmpty(s)) {
            Log.e("mjsheng", "settingLock extras is null");
            return;
        }
        try {
            JSONObject jSONObject = new JSONObject(s);

            int i = changeNum(jSONObject.optInt("lock"));
            Settings.System.putInt(this.mContext.getContentResolver(), "qch_unlock_ipad", i);
            Log.e("SystemSetting", "qch_unlock_ipad---------" + i);

            if (getBatteryLevel() >= CommonDatas.MIN_POWER) {
                mContext.sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
            } else {
                MySQLData.SetBooleanData(mContext, CommonDatas.IS_RESET, true);
            }
            return;

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
