package com.xypower.common;

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

import com.dev.devapi.api.SysApi;

public class UpdateReceiver extends BroadcastReceiver {

    private static final String TAG = UpdateReceiver.class.getSimpleName();

    private static final String APP_PACKAGE_MPMASTER = "com.xypower.mpmaster";

    @Override
    public void onReceive(Context context, Intent intent) {
        String packageName = intent.getDataString();

        packageChanged(context, intent.getAction(), packageName, APP_PACKAGE_MPMASTER);
        packageChanged(context, intent.getAction(), packageName, context.getPackageName());
    }

    private void packageChanged(Context context, String action, String packageName, String targetPackageName) {
        if (action.equals(Intent.ACTION_PACKAGE_REPLACED)) { // Upgrade Broadcast
            // Logger.e(TAG, "onReceive:升级了一个安装包，重新启动此程序");
            if (packageName.equals("package:" + targetPackageName)) {
                SysApi.enableApp(context, targetPackageName);
            }
        } else if (action.equals(Intent.ACTION_PACKAGE_ADDED)) {// Install broadcast
            // Logger.e(TAG, "onReceive:安装了" + packageName);
            if (packageName.equals("package:" + targetPackageName)) {
                /*SystemUtil.reBootDevice();*/
                SysApi.enableApp(context, targetPackageName);
            }
        } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)) { // Uninstall
            // Logger.e(TAG, "onReceive:uninstall" + packageName);
        }
    }
}