package com.uiui.zyappstore.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.uiui.zyappstore.utils.ApkUtils;
import com.uiui.zyappstore.utils.LogUtils;
import com.uiui.zyappstore.utils.SPUtils;
import com.uiui.zyappstore.utils.ToastUtil;

import java.util.Arrays;

public class AppManagerReceiver extends BroadcastReceiver {

    private static final String TAG = "AppManagerReceiver";
    private String action = null;

    @Override
    public void onReceive(Context context, Intent intent) {
        int admin_id = (int) SPUtils.get(context, "admin_id", -1);
        int sn_id = (int) SPUtils.get(context, "sn_id", -1);
        Log.e("BootReceiver", intent.getAction());

        action = intent.getAction();
        //接收安装广播
        if (action.equals(Intent.ACTION_PACKAGE_ADDED) || action.equals(Intent.ACTION_PACKAGE_REPLACED)) {
            String packageName = intent.getDataString().replace("package:", "");
            if (action.equals(Intent.ACTION_PACKAGE_ADDED)) {
                Intent intents = new Intent("com.uiui.zyappstore.PACKAGE_ADDED");
                intents.putExtra("package", packageName);
                context.sendBroadcast(intents);
            } else {
                Intent intents = new Intent("com.uiui.zyappstore.PACKAGE_REPLACED");
                intents.putExtra("package", packageName);
                context.sendBroadcast(intents);
            }
            String name = ApkUtils.getApplicationName(context, packageName);
            ToastUtil.show(name + ":安装成功");
            LogUtils.e(TAG, "安装了:" + packageName + "包名的程序");
//            ToastTool.show("安装成功");
            if (!packageName.equals(context.getPackageName()) && !Arrays.asList(packages).contains(packageName)) {
                if (admin_id == -1 || sn_id == -1) {
//                    OKGO.getUserInfo(context);
                } else {
//                    OKGO.setAppinstallInfo(member_id, sn_id, name, packageName);
                }
            }
        }

        //接收卸载广播
        if (action.equals(Intent.ACTION_PACKAGE_REMOVED)) {
            String packageName = intent.getDataString().replace("package:", "");
            LogUtils.e(TAG, "卸载了:" + packageName + "包名的程序");
            Intent intents = new Intent("com.uiui.zyappstore.PACKAGE_REMOVED");
            context.sendBroadcast(intents);
            if (!packageName.equals(context.getPackageName()) && !Arrays.asList(packages).contains(packageName)) {
                if (admin_id == -1 || sn_id == -1) {
//                    OKGO.getUserInfo(context);
                } else {
//                    OKGO.setAppuninstallInfo(sn_id, packageName);
                }
            }
        }
        if (action.equals(Intent.ACTION_USER_PRESENT)) {
            LogUtils.e(TAG, action);
        }


    }

    String[] packages = {
            "com.uiui.zyappstore",
            "com.uiuios.updatetools",
            "com.uiui.sn",
            "com.android.uiuios",
            "com.easyold.uiuios",
            "com.jiaoguanyi.store"

    };

}
