21 展訊Sprd設定-電池-識別應用類別
阿新 • • 發佈:2018-11-26
1. 目的
攔截應用啟動時,判斷下應用的類別,根據條件選擇是否攔截
public static final int UNKNOWN = -1; public static final int SYSTEM = 0; public static final int GAME = 1; public static final int MUSIC = 2; public static final int MESSAGE = 3; public static final int SOCIAL = 4; public static final int NEWS = 5; public static final int MEDIA = 6; public static final int READER = 7; public static final int NET = 8; public static final int MAP = 9; public static final int MONEY = 10; public static final int STUDY = 11; public static final int SHOPPING = 12; public static final int SPORTS = 13; // such as com.codoon.gps public static final int LIFESERVICE = 14; // such as baiduwaimai public static final int TRAVEL = 15; // such as xiecheng public static final int TRAFFIC = 16; // such as didi / mobike public static final int OTHER = 17; public static final int SPORT = 18; // such as keep
2. 資料庫
//App category list private ArrayMap<String, Integer> mAppCategoryList = new ArrayMap<>(); void checkDB() { PowerDataBaseControl powerDb = new PowerDataBaseControl(mContext); if (!powerDb.checkInfoDB()) { Slog.e(TAG, "CheckinfoDB error!"); return; } powerDb.openDB(); mAppCategoryList = powerDb.queryAll(); powerDb.closeDB(); //dump mAppCategoryList if (DEBUG) { if (mAppCategoryList != null) { for (int i=0;i<mAppCategoryList.size();i++) { String pkgName = mAppCategoryList.keyAt(i); int type = mAppCategoryList.valueAt(i); Slog.d(TAG, "pkgName:" + pkgName + " type:" + type); } } } } private int getAppCategoryTypeInternal(String packageName) { int index = mAppCategoryList.indexOfKey(packageName); if (index >= 0) { int type = mAppCategoryList.valueAt(index); return type; } return PowerDataBaseControl.UNKNOWN; }
2. 原始碼配置資料庫編譯
- 資料庫存放位置 vendor/sprd/platform/frameworks/native/data/etc/power_info.db
- /device/sprd/sharkle/common/DeviceCommon.mk
配置編譯
# Power Framework
PRODUCT_COPY_FILES += \
vendor/sprd/platform/frameworks/native/data/etc/power_info.db:system/etc/power_info.db \
...
3. 資料庫操作工具類
資料庫存放位置:/system/etc/power_info.db
package com.android.server.power;
/**
* To load Power Preset Data Base
*/
public class PowerDataBaseControl {
private static String DB_FILE_NAME = "power_info.db";
private static String assetsdbName = "/system/etc/power_info.db";
private static String TAG="PowerDataBaseControl";
public PowerDataBaseControl(Context context) {
mContext = context;
}
// 若 /data/system/power_info.db存在則返回ture,不存在則從/system/etc/power_info.db拷貝到 /data/system/power_info.db中
public boolean checkInfoDB() {
FileOutputStream localFileOutputStream;
FileInputStream localInputStream;
File file = mContext.getDatabasePath(DB_FILE_NAME);
if (file.exists()) {
return true;
}
// path 為 /data/system/power_info.db
String path = file.getPath();
Log.d(TAG, "Output data base:" + path);
try {
localInputStream = new FileInputStream(assetsdbName/**/system/etc/power_info.db*/);
localFileOutputStream = new FileOutputStream(file);
} catch (Exception e) {
Log.e(TAG, "Exception while copyDataBase");
e.printStackTrace();
return false;
}
// copy DataBase
while (true) {
byte[] arrayOfByte;
int count;
try
{
arrayOfByte = new byte[8192];
count = localInputStream.read(arrayOfByte);
if (count <= 0) {
localInputStream.close();
localFileOutputStream.close();
Log.v(TAG, "copyDataBase = true");
return true;
}
} catch (IOException localIOException){
Log.e(TAG, "IOException while copyDataBase");
localIOException.printStackTrace();
return false;
}
try {
localFileOutputStream.write(arrayOfByte, 0, count);
localFileOutputStream.flush();
} catch (IOException localIOException) {
Log.e(TAG, "IOException while copyDataBase");
localIOException.printStackTrace();
return false;
}
}
}
- 資料庫欄位結構如下
_id pkgname category
1 com.tencent.mm 3
2 com.tencent.mobileqq 3
3 jp.naver.line.android 3
4 com.android.music 2
5 com.baidu.video 6
6 com.android.fmradio 2
7 com.sohu.newsclient 5
8 com.taobao.taobao 12
9 com.sina.weibo 3
10 com.facebook.katana 3
11 com.ximalaya.ting.android 2
12 com.qiyi.video 6
13 com.imangi.templerun2 1
14 com.tencent.mtt 8
15 com.kugou.android 2
16 com.UCMobile 8
17 com.whatsapp 3
18 com.codoon.gps 13
19 com.mobike.mobikeapp 16
20 com.baidu.browser.apps 8
21 com.tencent.qqlive 6
22 com.tencent.qqmusic 2
23 com.baidu.appsearch 17