21 展讯Sprd设置-电池-识别应用类别

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/su749520/article/details/84338847

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

猜你喜欢

转载自blog.csdn.net/su749520/article/details/84338847
21
21)