package com.android.launcher3.settings;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;

import com.secspace.lib.common.event.RefreshWorkSpaceEvent;
import com.secspace.lib.common.function.SwitchInMdm;
import com.secspace.lib.common.utils.Prefs;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class AppInstallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
            String packageName = intent.getData().getSchemeSpecificPart();
            if (!TextUtils.isEmpty(packageName)) {
                if (!TextUtils.isEmpty(packageName) && !TextUtils.equals(packageName, context.getPackageName())) {
                    if (Prefs.isAllowNewApp(context)) {
                        List<String> pkgs = Prefs.getUserShowAppPkgs(context);
                        pkgs.add(packageName);
                        Prefs.setUserShowAppPkgs(context, pkgs);
                        SwitchInMdm.INSTANCE.addDisallowedRunningApp(context);
                        EventBus.getDefault().post(new RefreshWorkSpaceEvent());
                    }
                    EventBus.getDefault().post(new RefreshWorkSpaceEvent());
                }
            }
        }
    }
}
