本文主要是介绍10 展讯Sprd设置-电池-AppStateInfoCollector 接口介绍,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. AppStateInfoCollector 接口介绍
由于应用场景调用地方多样,所以该接口主要更新应用状态信息。这里重点 public接口的被调用状况
2. 实例化
package com.android.server.power;public class PowerController {// App state infoprivate AppStateInfoCollector mAppStateInfoCollector;// SystemServer.startOtherServices()public PowerController(Context context, IActivityManager activityManager) {...mAppStateInfoCollector = AppStateInfoCollector.getInstance(mContext);}
3. AppStateInfoCollector 接口介绍
3.1 单例模式调用
public static AppStateInfoCollector getInstance(Context context) {synchronized (AppStateInfoCollector.class) {if (sInstance == null) {sInstance = new AppStateInfoCollector(context);}return sInstance;}}public AppStateInfoCollector(Context context) {mContext = context;}
3.2 AppStateInfoCollector.reportAppStateEventInfo
更新应用状态事件信息
// 更新应用状态事件信息// return true: for new app state// false: for otherspublic boolean reportAppStateEventInfo(String packageName, int userId, int stateEvent) {// 获取应用状态列表ArrayMap<String, AppState> mAppStateInfoList = getAppStateInfoList(userId);//update mAppStateInfoListint index = mAppStateInfoList.indexOfKey(packageName);AppState appState = null;boolean ret = true;if (DEBUG) Slog.d(TAG, "- reportAppStateEventInfo() E -");if (index >= 0) {appState = mAppStateInfoList.valueAt(index);// 更新应用状态appState.updateAppState(stateEvent);ret = false;} else {// 新建一个应用状态appState = buildAppState(packageName, userId, stateEvent);mAppStateInfoList.put(packageName, appState);}return ret;}
3.3 AppStateInfoCollector.reportAppProcStateInfo
更新应用进程状态信息
// 更新应用进程状态信息// return true: for new app state// false: for otherspublic boolean reportAppProcStateInfo(String packageName, int uid, int procState) {int userId = UserHandle.getUserId(uid);ArrayMap<String, AppState> mAppStateInfoList = getAppStateInfoList(userId);//update mAppStateInfoListint index = mAppStateInfoList.indexOfKey(packageName);AppState appState = null;boolean ret = true;if (DEBUG) Slog.d(TAG, "- reportAppProcStateInfo() E -");if (index >= 0) {appState = mAppStateInfoList.valueAt(index);// update procStateappState.mProcState = procState;if (uid != appState.mUid) appState.mUid = uid;ret = false;} else {if (DEBUG) Slog.d(TAG, "reportAppProcStateInfo: appName:" + packageName + " uid:" + uid + " is not exist, create it");appState = buildAppState(packageName, userId, Event.NONE);mAppStateInfoList.put(packageName, appState);}return ret;}
3.4 获取某个应用状态 AppStateInfoCollector.getAppState
根据uid或者包名,获取应用状态,非常实用,方便上层调用
public AppState getAppState(String pkgName, int userId) {ArrayMap<String, AppState> mAppStateInfoList = getAppStateInfoList(userId);int index = mAppStateInfoList.indexOfKey(pkgName);if (index >= 0) {AppState appState = mAppStateInfoList.valueAt(index);return appState;} else {return null;}}public AppState getAppState(int uid) {int userId = UserHandle.getUserId(uid);ArrayMap<String, AppState> mAppStateInfoList = getAppStateInfoList(userId);for (int i=0;i<mAppStateInfoList.size();i++) {AppState appState = mAppStateInfoList.valueAt(i);if (appState.mUid == uid)return appState;}return null;}
3.5 AppStateInfoCollector.getCountOfActiveLaunchedApp
获取当前被启动的进程数量
// to sync with mAppStateInfoList if this api is called by other thread ??public int getCountOfActiveLaunchedApps(int userId) {ArrayMap<String, AppState> mAppStateInfoList = getAppStateInfoList(userId);int count = 0;for (int i=0;i<mAppStateInfoList.size();i++) {AppState appState = mAppStateInfoList.valueAt(i);if (appState.mLaunchCount > 0&& appState.mProcState != ActivityManager.PROCESS_STATE_CACHED_EMPTY&& appState.mProcState != ActivityManager.PROCESS_STATE_NONEXISTENT) {count++;}}return count;}
3.6 更新输入法状态信息
// update the Input Method identify state of this apppublic void updateAppInputMethodState(String pkgName, boolean isInputMethod, int userId) {ArrayMap<String, AppState> mAppStateInfoList = getAppStateInfoList(userId);int index = mAppStateInfoList.indexOfKey(pkgName);if (index >= 0) {AppState appState = mAppStateInfoList.valueAt(index);appState.mIsEnabledInputMethod = isInputMethod;}}// update the Input Method identify state of this apppublic void setDefaultInputMethodApp(String pkgName, int userId) {ArrayMap<String, AppState> mAppStateInfoList = getAppStateInfoList(userId);String mDefaultIMEAppName = mDefaultIMEAppNameForUsers.get(userId);if (mDefaultIMEAppName != null&& mDefaultIMEAppName.equals(pkgName)) {return;}// clear orignalif (mDefaultIMEAppName != null) {int index = mAppStateInfoList.indexOfKey(mDefaultIMEAppName);if (index >= 0) {AppState appState = mAppStateInfoList.valueAt(index);appState.mIsDefaultInputMethod = false;}}// set newint index = mAppStateInfoList.indexOfKey(pkgName);if (index >= 0) {AppState appState = mAppStateInfoList.valueAt(index);appState.mIsDefaultInputMethod = true;}mDefaultIMEAppName = pkgName;mDefaultIMEAppNameForUsers.put(userId, mDefaultIMEAppName);}// if this app is a input Methodprivate boolean isEnabledIMEApp(String pkgName){if (pkgName == null) return false;IInputMethodManager service = IInputMethodManager.Stub.asInterface(ServiceManager.getService(Context.INPUT_METHOD_SERVICE));List<InputMethodInfo> inputMethods;try {inputMethods = service.getEnabledInputMethodList();} catch (RemoteException e) {return false;}if (inputMethods == null || inputMethods.size() == 0) return false;for (InputMethodInfo info : inputMethods){if (info == null || info.getPackageName() == null) continue;if (info.getPackageName().equals(pkgName)) return true;}return false;}
3.7 更新进程状态信息
public void updateUidState(int uid, int uidState) {synchronized (mUidStateLock) {final int oldUidState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY);if (oldUidState != uidState) {// state changed, push updated rulesmUidState.put(uid, uidState);}}}public void removeUidState(int uid) {synchronized (mUidStateLock) {final int index = mUidState.indexOfKey(uid);if (index >= 0) {final int oldUidState = mUidState.valueAt(index);mUidState.removeAt(index);}}}
3.8 创建一个新的应用状态
private AppState buildAppState(String packageName, int userId, int stateEvent) {ApplicationInfo app = null;int uid = 0;int procState = ActivityManager.PROCESS_STATE_CACHED_EMPTY;int flags = 0;try {app = AppGlobals.getPackageManager().getApplicationInfo(packageName, 0, userId);} catch (RemoteException e) {// can't happen; package manager is process-local}if (app != null) {uid = app.uid;flags = app.flags;synchronized (mUidStateLock) {procState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY);}}AppState retVal = new AppState(packageName, userId, uid, stateEvent, procState, flags);// check if is input methodretVal.mIsEnabledInputMethod = isEnabledIMEApp(packageName);//if (DEBUG) Slog.d(TAG, "- buildAppState() :" + packageName);return retVal;}
3.9 获取应用状态列表
public ArrayMap<String, AppState> getAppStateInfoList(int userId) {ArrayMap<String, AppState> mAppStateInfoList = mUserAppStateInfoList.get(userId);if (mAppStateInfoList == null) {mAppStateInfoList = new ArrayMap<>();mUserAppStateInfoList.put(userId, mAppStateInfoList);}return mAppStateInfoList;}
即上述 reportAppStateEventInfo 和 reportAppProcStateInfo 和 updateUidState 作为外部调用接口,可以间接更新应用状态AppState
这篇关于10 展讯Sprd设置-电池-AppStateInfoCollector 接口介绍的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!