手机自启动管理

为了解决功耗问题,手机里面需要实现自启动管理功能,防止应用关闭后自己在后台启动,影响功耗问题,下面是自启动的代码实现:

--- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
@@ -2889,7 +2889,7 @@ public class SettingsProvider extends ContentProvider {
         }
 
         private final class UpgradeController {
-            private static final int SETTINGS_VERSION = 148;
+            private static final int SETTINGS_VERSION = 149;
 
             private final int mUserId;
 
@@ -3450,20 +3450,21 @@ public class SettingsProvider extends ContentProvider {
                 }
 
                 // vos-add-for modify default IME -18/10/15 start
-                if(currentVersion == 148) {
-                    final SettingsState secureSettings = getSecureSettingsLocked(userId);
-                    final String defaultValue = getContext().getResources().getString(
-                            R.string.default_input_method);
-                    if (defaultValue != null) {
-                        secureSettings.insertSettingLocked(Settings.Secure.DEFAULT_INPUT_METHOD,
-                                defaultValue, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
-                    }
+                if(currentVersion == 148){
+                    if (userId == UserHandle.USER_SYSTEM) {
+                        final SettingsState secureSettings = getSecureSettingsLocked(userId);
+                        final String defaultValue = getContext().getResources().getString(
+                                R.string.default_input_method);
+                        if (defaultValue != null) {
+                            secureSettings.insertSettingLocked(Settings.Secure.DEFAULT_INPUT_METHOD,
+                                    defaultValue, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
+                        }
 
-                    if (defaultValue != null) {
-                        secureSettings.insertSettingLocked(Settings.Secure.ENABLED_INPUT_METHODS,
-                                defaultValue, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
+                        if (defaultValue != null) {
+                            secureSettings.insertSettingLocked(Settings.Secure.ENABLED_INPUT_METHODS,
+                                    defaultValue, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
+                        }
                     }
-
                     currentVersion = 149;
                 }
                 // vos-add-for modify default IME -18/10/15 end

ActivityManagerService.java的修改如下:


 public class ActivityManagerService extends IActivityManager.Stub
         implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
 
@@ -651,6 +650,16 @@ public class ActivityManagerService extends IActivityManager.Stub
     // Whether we should use SCHED_FIFO for UI and RenderThreads.
     private boolean mUseFifoUiScheduling = false;
 
+    // FEATURE-autostrart-181020 start
+    private static final String TAG2 = "ams";
+
+    private static final Uri URI = Uri.parse("content://com.android.apps/autostart");
+
+    private static final String COLUNMN_APPS = "package_name";
+
+    private static HashSet<String> mDisallowList;
+    // FEATURE-autostrart-181020 end
+
     BroadcastQueue mFgBroadcastQueue;
     BroadcastQueue mBgBroadcastQueue;
     // Convenient for easy iteration over the queues. Foreground is first
@@ -3703,12 +3712,100 @@ public class ActivityManagerService extends IActivityManager.Stub
             ApplicationInfo info, boolean knownToBeDead, int intentFlags,
             String hostingType, ComponentName hostingName, boolean allowWhileBooting,
             boolean isolated, boolean keepIfLarge) {
+
+        // FEATURE-autostart-181020 start
+        if (isProcessLocked(info, hostingType,hostingName)) {
+            return null;
+        }
+        // FEATURE-autostart-181020 end
         return startProcessLocked(processName, info, knownToBeDead, intentFlags, hostingType,
                 hostingName, allowWhileBooting, isolated, 0 /* isolatedUid */, keepIfLarge,
                 null /* ABI override */, null /* entryPoint */, null /* entryPointArgs */,
                 null /* crashHandler */);
     }
 
+    // FEATURE-autostart-181020 start
+    private String mCachePkg;
+    final boolean isProcessLocked(ApplicationInfo info, String hostingType,ComponentName hostingName) {
+        String launchPkg = info.packageName;
+        Log.d(TAG2, "Package name ["+launchPkg+"] isSystemApp: " + info.isSystemApp());
+        boolean isRunningApp = getRunningProcessRecordIndex(launchPkg) != -1;
+        if(info.isSystemApp() || isRunningApp || "android".equals(info.packageName) || "system".equals(info.packageName)){
+            Log.d(TAG2, "Launch: " + launchPkg  + " " + hostingType);
+            return false;
+        }
+        if(launchPkg.equals(mCachePkg)){
+            Log.d(TAG2, "Launch for cache: " + launchPkg  + " " + hostingType);
+            return false;
+        }
+        if ("service".equals(hostingType) || "broadcast".equals(hostingType) || "content provider".equals(hostingType)) {
+            List<ActivityManager.RunningAppProcessInfo> infoList = getRunningAppProcesses();
+            if(mDisallowList==null){
+                Log.d(TAG2, "mDisallowList is null");
+                return true;
+            }
+
+            if (mDisallowList.contains(launchPkg)) {
+                if("com.uc.application.desktopwidget.UCWidgetProvider".equals(hostingName.getClassName())){
+                    Log.d(TAG2,"allowed: "+hostingName.getClassName());
+                    return false;
+                }
+                Log.d(TAG2, "intercept: " + launchPkg + "  " + hostingType + "  " + info.processName);
+                return true;
+            } else {
+                Log.d(TAG2, "allowed: " + launchPkg + "  " + hostingType + "  " + info.processName);
+                return false;
+            }
+
+        } /*else if(MAIN_TYPE.equals(hostingType)) {
+            String hostingNameStr = hostingName != null ? hostingName.flattenToShortString() : null;
+            String launchName = hostingNameStr.split("/")[1];
+            if("com.igexin.sdk.GActivity".equals(launchName)){
+                Log.d(TAG2, "forks intercept: " + info.packageName + "  " + hostingType+" "+launchName);
+                return true;
+            }
+            Log.d(TAG2, "forks: " + info.packageName + "  " + hostingType);
+        }*/
+        mCachePkg = launchPkg;
+        return false;
+    }
+
+    private final int getRunningProcessRecordIndex(String packageName) {
+        // Find the application record.
+        if(packageName == null){
+            return -1;
+        }
+        for (int i=mLruProcesses.size()-1; i>=0; i--) {
+            ProcessRecord rec = mLruProcesses.get(i);
+            if (rec.thread != null && packageName.equals(rec.processName)) {
+                return i;
+            }
+        }
+        return -1;
+    }
+
+
+    private void getDisallowList() {
+        mDisallowList.clear();
+        Cursor cursor = null;
+        try {
+            cursor = mContext.getContentResolver().query(URI, null, null, null, null);
+            while (cursor != null && cursor.moveToNext()) {
+                String package_name = cursor.getString(cursor.getColumnIndex(COLUNMN_APPS));
+                mDisallowList.add(package_name);
+            }
+        } catch (Exception e) {
+            // ignore Attempt to launch content provider before system ready
+            e.printStackTrace();
+        } finally {
+            if (cursor != null) {
+                cursor.close();
+            }
+        }
+    }
+    // FEATURE-autostart-181020 end
+
     final ProcessRecord startProcessLocked(String processName, ApplicationInfo info,
             boolean knownToBeDead, int intentFlags, String hostingType, ComponentName hostingName,
             boolean allowWhileBooting, boolean isolated, int isolatedUid, boolean keepIfLarge,
@@ -7411,6 +7508,25 @@ public class ActivityManagerService extends IActivityManager.Stub
     }
 
     final void finishBooting() {
+        // FEATURE-autostart-181020 start
+        if(mDisallowList == null){
+            mDisallowList = new HashSet<String> ();
+            getDisallowList();
+            Log.d(TAG2,"finishBooting  getDisallowList: "+ mDisallowList.size());
+
+            IntentFilter app = new IntentFilter();
+            app.addAction("com.homecare.autostart");
+            mContext.registerReceiver(new BroadcastReceiver() {
+                @Override
+                public void onReceive(Context context, Intent intent) {
+                    getDisallowList();
+                    Log.d(TAG2,"autostart onReceive "+mDisallowList.size());
+                }
+            }, app);
+
+            HCSmartCleaner.registerScreenOnReceriver(mContext);
+        }
+        // FEATURE-autostart-181020 end
         synchronized (this) {
             if (!mBootAnimationComplete) {
                 mCallFinishBooting = true;



[sdm660_8.1/frameworks/base/.git] / services / core / java / com / android / server / utils / HCSmartCleaner.java
   1 package com.android.server.utils;
   2
   3 import android.app.ActivityManager;
   4 import android.content.BroadcastReceiver;
   5 import android.content.Context;
   6 import android.content.IntentFilter;
   7 import android.content.pm.ApplicationInfo;
   8 import android.content.pm.PackageManager;
   9 import android.database.Cursor;
  10 import android.media.AudioManager;
  11 import android.os.UserHandle;
  12 import android.provider.Settings;
  13 import android.text.TextUtils;
  14 import android.util.Log;
  15
  16 import java.util.HashSet;
  17 import java.util.List;
  18 import java.util.concurrent.Executors;
  19 import java.util.concurrent.ScheduledExecutorService;
  20 import java.util.concurrent.TimeUnit;
  21
  22
  23  
  24 
  25 
  26  
  27  /**
  28  * @PROJECT_NAME: android
  29  * @PACKAGE_NAME: com.android.server
  30  * @Description: TODO
  31  */
  32 public class HCSmartCleaner {
  33
  34     private static final String TAG = "HCSmartCleaner";
  35
  36     private static HashSet<String> mProtectList = new HashSet<String>();
  37
  38     private static final android.net.Uri URI = android.net.Uri.parse("content://com.android.apps/protect");
  39
  40     private static final String COLUNMN_APPS = "package_name";
  41
  42     private static Context mContent;
  43
  44     private static ScheduledExecutorService mScheduledExecutorService;
  45
  46     private static boolean isRun;
  47
  48     private static String sAudioFocusPkg;
  49
  50     private static final Runnable LIGHT_RUNNABEL = new Runnable() {
  51         public void run() {
  52             Log.d(TAG, "LIGHT_RUNNABEL");
  53             cleanProcessByCount(mContent, 5);
  54         }
  55     };
  56
  57     private static boolean mDeepMode = false;
  58     private static final Runnable DARK_RUNNABEL = new Runnable() {
  59         public void run() {
  60             Log.d(TAG, "DARK_RUNNABEL  DeepMode: " + mDeepMode);
  61             if (mDeepMode) {
  62                 cleanProcessByCount(mContent, 1);
  63                 mDeepMode = false;
  64                 mScheduledExecutorService.shutdown();
  65             } else {
  66                 cleanProcessByCount(mContent, 3);
  67                 mDeepMode = true;
  68             }
  69         }
  70     };
  71
  72
  73     public static void registerScreenOnReceriver(final Context context) {
  74         mContent = context;
  75         registerAudioFocusReceiver();
  76         Log.d(TAG, "registerScreenOnReceriver");
  77         mScheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
  78         mScheduledExecutorService.scheduleAtFixedRate(LIGHT_RUNNABEL, 10, 10, TimeUnit.MINUTES);
  79         isRun = true;
  80         IntentFilter clean = new IntentFilter();
  81         clean.addAction(android.content.Intent.ACTION_SCREEN_ON);
  82         clean.addAction(android.content.Intent.ACTION_SCREEN_OFF);
  83         context.registerReceiver(new BroadcastReceiver() {
  84             @Override
  85             public void onReceive(Context context, android.content.Intent intent) {
  86                 if (android.content.Intent.ACTION_SCREEN_ON.equals(intent.getAction())) {
  87                     Log.d(TAG, "ACTION_SCREEN_ON  " + isRun);
  88                     mDeepMode = false;
  89                     if (!isRun) {
  90                         mScheduledExecutorService.shutdownNow();
  91                         mScheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
  92                         mScheduledExecutorService.scheduleAtFixedRate(LIGHT_RUNNABEL, 10, 10, TimeUnit.MINUTES);
  93                         isRun = true;
  94                     }
  95
  96
  97                 } else if (android.content.Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
  98                     Log.d(TAG, "ACTION_SCREEN_OFF  " + isRun);
  99                     if (isRun) {
 100                         mScheduledExecutorService.shutdownNow();
 101                         mScheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
 102                         mScheduledExecutorService.scheduleAtFixedRate(DARK_RUNNABEL, 10, 10, TimeUnit.MINUTES);
 103                         isRun = false;
 104                     }
 105                 }
 106             }
 107         }, clean);
 108     }
 109
 110     private static void cleanProcessByCount(Context context, int count) {
 111         try {
 112             getProtectList(context);
 113
 114             AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
 115             if (audio.isMusicActive()) {
 116 //                String focusPkg = Settings.System.getString(context.getContentResolver(), "AudioFocus");
 117                 Log.d(TAG, "AudioFocus: " + sAudioFocusPkg);
 118                 mProtectList.add(sAudioFocusPkg);
 119             }
 120
 121             ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
 122             List<ActivityManager.RunningAppProcessInfo> infoList = am.getRunningAppProcesses();
 123             List<ActivityManager.RunningTaskInfo> runningTasks = am.getRunningTasks(12);
 124
 125             int number = 0;
 126             for (ActivityManager.RunningTaskInfo info : runningTasks) {
 127                 String packageName = info.baseActivity.getPackageName();
 128                 ApplicationInfo appInfo = android.app.ActivityThread.getPackageManager().getApplicationInfo(
 129                         packageName, PackageManager.GET_UNINSTALLED_PACKAGES, context.getUserId());
 130                 // ignore system
 131                 if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
 132                     Log.d(TAG, "cleanProcessByCount  FLAG_SYSTEM: " + packageName);
 133                     continue;
 134                 }
 135                 // ignore protect
 136                 if (mProtectList.contains(packageName)) {
 137                     continue;
 138                 }
 139                 if (number >= count) {
 140                     break;
 141                 }
 142                 Log.d(TAG, "cleanProcessByCount  ProtectList add: " + packageName);
 143                 mProtectList.add(packageName);
 144                 number++;
 145             }
 146             for (ActivityManager.RunningAppProcessInfo processInfo : infoList) {
 147                 String[] pkgList = processInfo.pkgList;
 148                 for (String packageName : pkgList) {
 149                     ApplicationInfo appInfo = android.app.ActivityThread.getPackageManager().getApplicationInfo(
 150                             packageName, PackageManager.GET_UNINSTALLED_PACKAGES, UserHandle.getUserId(processInfo.uid));
 151                     // Don't kill system process
 152                     if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
 153                         break;
 154                     } else {
 155                         if (mProtectList.contains(packageName)) {
 156                             Log.d(TAG, "ignore ProtectList: " + packageName);
 157                             break;
 158                         }
 159                         if ("com.qihoo.appstore".equals(packageName)) {
 160                             am.killBackgroundProcesses(packageName);
 161                             Log.d(TAG, "killBackgroundProcesses processName :" + processInfo.processName);
 162                         } else {
 163                             am.forceStopPackage(packageName);
 164                             Log.d(TAG, "forceStopPackage: " + packageName);
 165                         }
 166                     }
 167                 }
 168             }
 169         } catch (Exception e) {
 170             e.printStackTrace();
 171             Log.d(TAG, e.getMessage());
 172         }
 173     }
 174
 175 
 176     private static void getProtectList(Context context) {
 177         mProtectList.clear();
 178         Cursor cursor = null;
 179         try {
 180             cursor = context.getContentResolver().query(URI, null, null, null, null);
 181             while (cursor != null && cursor.moveToNext()) {
 182                 String package_name = cursor.getString(cursor.getColumnIndex(COLUNMN_APPS));
 183                 mProtectList.add(package_name);
 184             }
 185         } catch (Exception e) {
 186             // ignore Attempt to launch content provider before system ready
 187             e.printStackTrace();
 188         } finally {
 189             if (cursor != null) {
 190                 cursor.close();
 191             }
 192         }
 193         String defaultInputMethodCls = Settings.Secure.getString(context.getContentResolver(),
 194                 Settings.Secure.DEFAULT_INPUT_METHOD);
 195         if (!TextUtils.isEmpty(defaultInputMethodCls)) {
 196             defaultInputMethodCls = defaultInputMethodCls.split("/")[0];
 197             mProtectList.add(defaultInputMethodCls);
 198             Log.d(TAG,"defaultInputMethodPkg: "+defaultInputMethodCls);
 199         }
 200
 201     }
 202
 203     private static void registerAudioFocusReceiver() {
 204         BroadcastReceiver audioFocusReceiver = new BroadcastReceiver() {
 205             @Override
 206             public void onReceive(Context context, android.content.Intent intent) {
 207                 sAudioFocusPkg = intent.getStringExtra("AudioFocus");
 208                 Settings.System.putString(mContent.getContentResolver(), "AudioFocus", sAudioFocusPkg);
 209                 Log.d(TAG, "audioFocusReceiver: " + sAudioFocusPkg);
 210
 211             }
 212         };
 213         IntentFilter intentFilter = new IntentFilter("android.action.AudioFocus");
 214         mContent.registerReceiver(audioFocusReceiver, intentFilter);
 215
 216     }
 217
 218
 219 }

 

展开阅读全文

没有更多推荐了,返回首页