FileDocCategorySizeDatePackage
AppImportanceMonitor.javaAPI DocAndroid 5.1 API5005Thu Mar 12 22:22:10 GMT 2015android.app

AppImportanceMonitor

public class AppImportanceMonitor extends Object
Helper for monitoring the current importance of applications.
hide

Fields Summary
final android.content.Context
mContext
final android.util.SparseArray
mApps
final IProcessObserver
mProcessObserver
static final int
MSG_UPDATE
final android.os.Handler
mHandler
Constructors Summary
public AppImportanceMonitor(android.content.Context context, android.os.Looper looper)


         
        mContext = context;
        mHandler = new Handler(looper) {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG_UPDATE:
                        onImportanceChanged(msg.arg1, msg.arg2&0xffff, msg.arg2>>16);
                        break;
                    default:
                        super.handleMessage(msg);
                }
            }
        };
        ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
        try {
            ActivityManagerNative.getDefault().registerProcessObserver(mProcessObserver);
        } catch (RemoteException e) {
        }
        List<ActivityManager.RunningAppProcessInfo> apps = am.getRunningAppProcesses();
        if (apps != null) {
            for (int i=0; i<apps.size(); i++) {
                ActivityManager.RunningAppProcessInfo app = apps.get(i);
                updateImportanceLocked(app.uid, app.pid, app.importance, false);
            }
        }
    
Methods Summary
public intgetImportance(int uid)

        AppEntry ent = mApps.get(uid);
        if (ent == null) {
            return ActivityManager.RunningAppProcessInfo.IMPORTANCE_GONE;
        }
        return ent.importance;
    
public voidonImportanceChanged(int uid, int importance, int oldImportance)
Report when an app's importance changed. Called on looper given to constructor.

    
voidupdateImportanceLocked(int uid, int pid, int importance, boolean repChange)

        AppEntry ent = mApps.get(uid);
        if (ent == null) {
            ent = new AppEntry(uid);
            mApps.put(uid, ent);
        }
        if (importance >= ActivityManager.RunningAppProcessInfo.IMPORTANCE_GONE) {
            ent.procs.remove(pid);
        } else {
            ent.procs.put(pid, importance);
        }
        updateImportanceLocked(ent, repChange);
    
voidupdateImportanceLocked(android.app.AppImportanceMonitor$AppEntry ent, boolean repChange)

        int appImp = ActivityManager.RunningAppProcessInfo.IMPORTANCE_GONE;
        for (int i=0; i<ent.procs.size(); i++) {
            int procImp = ent.procs.valueAt(i);
            if (procImp < appImp) {
                appImp = procImp;
            }
        }
        if (appImp != ent.importance) {
            int impCode = appImp | (ent.importance<<16);
            ent.importance = appImp;
            if (appImp >= ActivityManager.RunningAppProcessInfo.IMPORTANCE_GONE) {
                mApps.remove(ent.uid);
            }
            if (repChange) {
                mHandler.obtainMessage(MSG_UPDATE, ent.uid, impCode).sendToTarget();
            }
        }