diff --git a/src/com/android/settings/applications/RunningState.java b/src/com/android/settings/applications/RunningState.java index d4abe6c4e28..5429b053c61 100644 --- a/src/com/android/settings/applications/RunningState.java +++ b/src/com/android/settings/applications/RunningState.java @@ -92,27 +92,27 @@ public class RunningState { // entry. final SparseArray> mServiceProcessesByName = new SparseArray>(); - + // Processes that are hosting a service we are interested in, organized // by their pid. These disappear and re-appear as services are restarted. final SparseArray mServiceProcessesByPid = new SparseArray(); - + // Used to sort the interesting processes. final ServiceProcessComparator mServiceProcessComparator = new ServiceProcessComparator(); - + // Additional interesting processes to be shown to the user, even if // there is no service running in them. final ArrayList mInterestingProcesses = new ArrayList(); - + // All currently running processes, for finding dependencies etc. final SparseArray mRunningProcesses = new SparseArray(); - + // The processes associated with services, in sorted order. final ArrayList mProcessItems = new ArrayList(); - + // All processes, used for retrieving memory information. final ArrayList mAllProcessItems = new ArrayList(); @@ -139,69 +139,74 @@ public class RunningState { int mSequence = 0; - final Comparator mBackgroundComparator - = new Comparator() { - @Override - public int compare(MergedItem lhs, MergedItem rhs) { - if (DEBUG_COMPARE) { - Log.i(TAG, "Comparing " + lhs + " with " + rhs); - Log.i(TAG, " Proc " + lhs.mProcess + " with " + rhs.mProcess); - Log.i(TAG, " UserId " + lhs.mUserId + " with " + rhs.mUserId); - } - if (lhs.mUserId != rhs.mUserId) { - if (lhs.mUserId == mMyUserId) return -1; - if (rhs.mUserId == mMyUserId) return 1; - return lhs.mUserId < rhs.mUserId ? -1 : 1; - } - if (lhs.mProcess == rhs.mProcess) { - if (lhs.mLabel == rhs.mLabel) { + final Comparator mBackgroundComparator = + new Comparator() { + @Override + public int compare(MergedItem lhs, MergedItem rhs) { + if (DEBUG_COMPARE) { + Log.i(TAG, "Comparing " + lhs + " with " + rhs); + Log.i(TAG, " Proc " + lhs.mProcess + " with " + rhs.mProcess); + Log.i(TAG, " UserId " + lhs.mUserId + " with " + rhs.mUserId); + } + if (lhs.mUserId != rhs.mUserId) { + if (lhs.mUserId == mMyUserId) return -1; + if (rhs.mUserId == mMyUserId) return 1; + return lhs.mUserId < rhs.mUserId ? -1 : 1; + } + if (lhs.mProcess == rhs.mProcess) { + if (lhs.mLabel == rhs.mLabel) { + return 0; + } + return lhs.mLabel != null ? lhs.mLabel.compareTo(rhs.mLabel) : -1; + } + if (lhs.mProcess == null) return -1; + if (rhs.mProcess == null) return 1; + if (DEBUG_COMPARE) { + Log.i(TAG, " Label " + lhs.mProcess.mLabel + + " with " + rhs.mProcess.mLabel); + } + final ActivityManager.RunningAppProcessInfo lhsInfo = + lhs.mProcess.mRunningProcessInfo; + final ActivityManager.RunningAppProcessInfo rhsInfo = + rhs.mProcess.mRunningProcessInfo; + final boolean lhsBg = lhsInfo.importance + >= ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; + final boolean rhsBg = rhsInfo.importance + >= ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; + if (DEBUG_COMPARE) Log.i(TAG, " Bg " + lhsBg + " with " + rhsBg); + if (lhsBg != rhsBg) { + return lhsBg ? 1 : -1; + } + final boolean lhsA = (lhsInfo.flags + & ActivityManager.RunningAppProcessInfo.FLAG_HAS_ACTIVITIES) != 0; + final boolean rhsA = (rhsInfo.flags + & ActivityManager.RunningAppProcessInfo.FLAG_HAS_ACTIVITIES) != 0; + if (DEBUG_COMPARE) Log.i(TAG, " Act " + lhsA + " with " + rhsA); + if (lhsA != rhsA) { + return lhsA ? -1 : 1; + } + if (DEBUG_COMPARE) { + Log.i(TAG, + " Lru " + lhsInfo.lru + " with " + rhsInfo.lru); + } + if (lhsInfo.lru != rhsInfo.lru) { + return lhsInfo.lru < rhsInfo.lru ? -1 : 1; + } + if (lhs.mProcess.mLabel == rhs.mProcess.mLabel) { return 0; } - return lhs.mLabel != null ? lhs.mLabel.compareTo(rhs.mLabel) : -1; + if (lhs.mProcess.mLabel == null) return 1; + if (rhs.mProcess.mLabel == null) return -1; + return lhs.mProcess.mLabel.compareTo(rhs.mProcess.mLabel); } - if (lhs.mProcess == null) return -1; - if (rhs.mProcess == null) return 1; - if (DEBUG_COMPARE) Log.i(TAG, " Label " + lhs.mProcess.mLabel - + " with " + rhs.mProcess.mLabel); - final ActivityManager.RunningAppProcessInfo lhsInfo - = lhs.mProcess.mRunningProcessInfo; - final ActivityManager.RunningAppProcessInfo rhsInfo - = rhs.mProcess.mRunningProcessInfo; - final boolean lhsBg = lhsInfo.importance - >= ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; - final boolean rhsBg = rhsInfo.importance - >= ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; - if (DEBUG_COMPARE) Log.i(TAG, " Bg " + lhsBg + " with " + rhsBg); - if (lhsBg != rhsBg) { - return lhsBg ? 1 : -1; - } - final boolean lhsA = (lhsInfo.flags - & ActivityManager.RunningAppProcessInfo.FLAG_HAS_ACTIVITIES) != 0; - final boolean rhsA = (rhsInfo.flags - & ActivityManager.RunningAppProcessInfo.FLAG_HAS_ACTIVITIES) != 0; - if (DEBUG_COMPARE) Log.i(TAG, " Act " + lhsA + " with " + rhsA); - if (lhsA != rhsA) { - return lhsA ? -1 : 1; - } - if (DEBUG_COMPARE) Log.i(TAG, " Lru " + lhsInfo.lru + " with " + rhsInfo.lru); - if (lhsInfo.lru != rhsInfo.lru) { - return lhsInfo.lru < rhsInfo.lru ? -1 : 1; - } - if (lhs.mProcess.mLabel == rhs.mProcess.mLabel) { - return 0; - } - if (lhs.mProcess.mLabel == null) return 1; - if (rhs.mProcess.mLabel == null) return -1; - return lhs.mProcess.mLabel.compareTo(rhs.mProcess.mLabel); - } - }; + }; // ----- following protected by mLock ----- - + // Lock for protecting the state that will be shared between the // background update thread and the UI thread. final Object mLock = new Object(); - + boolean mResumed; boolean mHaveData; boolean mWatchingBackgroundItems; @@ -210,7 +215,7 @@ public class RunningState { ArrayList mMergedItems = new ArrayList(); ArrayList mBackgroundItems = new ArrayList(); ArrayList mUserBackgroundItems = new ArrayList(); - + int mNumBackgroundProcesses; long mBackgroundProcessMemory; int mNumForegroundProcesses; @@ -221,6 +226,7 @@ public class RunningState { // ----- BACKGROUND MONITORING THREAD ----- final HandlerThread mBackgroundThread; + final class BackgroundHandler extends Handler { public BackgroundHandler(Looper looper) { super(looper); @@ -372,9 +378,9 @@ public class RunningState { ActivityManager.RunningServiceInfo mRunningService; ServiceInfo mServiceInfo; boolean mShownAsStarted; - + MergedItem mMergedItem; - + public ServiceItem(int userId) { super(false, userId); } @@ -385,17 +391,17 @@ public class RunningState { = new HashMap(); final SparseArray mDependentProcesses = new SparseArray(); - + final int mUid; final String mProcessName; int mPid; - + ProcessItem mClient; int mLastNumDependentProcesses; - + int mRunningSeq; ActivityManager.RunningAppProcessInfo mRunningProcessInfo; - + MergedItem mMergedItem; boolean mInteresting; @@ -404,7 +410,7 @@ public class RunningState { boolean mIsSystem; boolean mIsStarted; long mActiveSince; - + public ProcessItem(Context context, int uid, String processName) { super(true, UserHandle.getUserId(uid)); mDescription = context.getResources().getString( @@ -412,12 +418,12 @@ public class RunningState { mUid = uid; mProcessName = processName; } - + void ensureLabel(PackageManager pm) { if (mLabel != null) { return; } - + try { ApplicationInfo ai = pm.getApplicationInfo(mProcessName, PackageManager.MATCH_ANY_USER); @@ -429,11 +435,11 @@ public class RunningState { } } catch (PackageManager.NameNotFoundException e) { } - + // If we couldn't get information about the overall // process, try to find something about the uid. String[] pkgs = pm.getPackagesForUid(mUid); - + // If there is one package with this uid, that is what we want. if (pkgs.length == 1) { try { @@ -446,7 +452,7 @@ public class RunningState { } catch (PackageManager.NameNotFoundException e) { } } - + // If there are multiple, see if one gives us the official name // for this uid. for (String name : pkgs) { @@ -465,7 +471,7 @@ public class RunningState { } catch (PackageManager.NameNotFoundException e) { } } - + // If still don't have anything to display, just use the // service info. if (mServices.size() > 0) { @@ -476,7 +482,7 @@ public class RunningState { mLabel = mDisplayLabel.toString(); return; } - + // Finally... whatever, just pick the first package's name. try { ApplicationInfo ai = pm.getApplicationInfo(pkgs[0], @@ -544,16 +550,16 @@ public class RunningState { si.mDescription = context.getResources().getString( R.string.service_started_by_app); } - + return changed; } - + boolean updateSize(Context context, long pss, int curSeq) { mSize = pss * 1024; if (mCurSeq == curSeq) { String sizeStr = Formatter.formatShortFileSize( context, mSize); - if (!sizeStr.equals(mSizeStr)){ + if (!sizeStr.equals(mSizeStr)) { mSizeStr = sizeStr; // We update this on the second tick where we update just // the text in the current items, so no need to say we @@ -563,11 +569,11 @@ public class RunningState { } return false; } - + boolean buildDependencyChain(Context context, PackageManager pm, int curSeq) { final int NP = mDependentProcesses.size(); boolean changed = false; - for (int i=0; i dest, ArrayList destProc) { final int NP = mDependentProcesses.size(); - for (int i=0; i mOtherProcesses = new ArrayList(); final ArrayList mServices = new ArrayList(); final ArrayList mChildren = new ArrayList(); - + private int mLastNumProcesses = -1, mLastNumServices = -1; MergedItem(int userId) { @@ -646,7 +652,7 @@ public class RunningState { int numProcesses = 0; int numServices = 0; mActiveSince = -1; - for (int i=0; i 0 ? 1 : 0) + mOtherProcesses.size(), mServices.size()); } - + mActiveSince = -1; - for (int i=0; i= 0 && mActiveSince < si.mActiveSince) { mActiveSince = si.mActiveSince; @@ -678,25 +684,25 @@ public class RunningState { return false; } - + boolean updateSize(Context context) { if (mUser != null) { mSize = 0; - for (int i=0; i= 0) { - label = label.substring(tail+1, label.length()); + label = label.substring(tail + 1, label.length()); } return label; } - + static RunningState getInstance(Context context) { synchronized (sGlobalLock) { if (sInstance == null) { @@ -776,9 +782,9 @@ public class RunningState { private RunningState(Context context) { mApplicationContext = context.getApplicationContext(); - mAm = (ActivityManager)mApplicationContext.getSystemService(Context.ACTIVITY_SERVICE); + mAm = mApplicationContext.getSystemService(ActivityManager.class); mPm = mApplicationContext.getPackageManager(); - mUm = (UserManager)mApplicationContext.getSystemService(Context.USER_SERVICE); + mUm = mApplicationContext.getSystemService(UserManager.class); mMyUserId = UserHandle.myUserId(); UserInfo userInfo = mUm.getUserInfo(mMyUserId); mHideManagedProfiles = userInfo == null || !userInfo.canHaveProfile(); @@ -842,14 +848,14 @@ public class RunningState { } private boolean isInterestingProcess(ActivityManager.RunningAppProcessInfo pi) { - if ((pi.flags&ActivityManager.RunningAppProcessInfo.FLAG_CANT_SAVE_STATE) != 0) { + if ((pi.flags & ActivityManager.RunningAppProcessInfo.FLAG_CANT_SAVE_STATE) != 0) { return true; } - if ((pi.flags&ActivityManager.RunningAppProcessInfo.FLAG_PERSISTENT) == 0 + if ((pi.flags & ActivityManager.RunningAppProcessInfo.FLAG_PERSISTENT) == 0 && pi.importance >= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && pi.importance < ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE && pi.importanceReasonCode - == ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN) { + == ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN) { return true; } return false; @@ -897,15 +903,15 @@ public class RunningState { final PackageManager pm = context.getPackageManager(); mSequence++; - + boolean changed = false; // Retrieve list of services, filtering out anything that definitely // won't be shown in the UI. - List services + List services = am.getRunningServices(MAX_SERVICES); int NS = services != null ? services.size() : 0; - for (int i=0; i 0) { AppProcessInfo ainfo = mTmpAppProcesses.get(si.pid); @@ -954,7 +960,7 @@ public class RunningState { } // Update state we are maintaining about process that are running services. - for (int i=0; i uidToDelete = null; - for (int i=0; i procs = mServiceProcessesByName.valueAt(i); Iterator pit = procs.values().iterator(); while (pit.hasNext()) { @@ -1119,8 +1125,7 @@ public class RunningState { if (pi.mCurSeq == mSequence) { pi.ensureLabel(pm); if (pi.mPid == 0) { - // Sanity: a non-process can't be dependent on - // anything. + // Validation: a non-process can't be dependent on anything. pi.mDependentProcesses.clear(); } } else { @@ -1147,7 +1152,7 @@ public class RunningState { } } } - + if (uidToDelete != null) { for (int i = 0; i < uidToDelete.size(); i++) { int uid = uidToDelete.get(i); @@ -1158,7 +1163,7 @@ public class RunningState { if (changed) { // First determine an order for the services. ArrayList sortedProcesses = new ArrayList(); - for (int i=0; i newItems = new ArrayList(); ArrayList newMergedItems = new ArrayList(); mProcessItems.clear(); - for (int i=0; i 0) { mProcessItems.add(pi); } - + // Now add the services running in it. MergedItem mergedItem = null; boolean haveAllMerged = false; @@ -1216,7 +1221,7 @@ public class RunningState { haveAllMerged = false; } } - + if (!haveAllMerged || mergedItem == null || mergedItem.mServices.size() != pi.mServices.size()) { // Whoops, we need to build a new MergedItem! @@ -1227,11 +1232,11 @@ public class RunningState { } mergedItem.mProcess = pi; mergedItem.mOtherProcesses.clear(); - for (int mpi=firstProc; mpi<(mProcessItems.size()-1); mpi++) { + for (int mpi = firstProc; mpi < (mProcessItems.size() - 1); mpi++) { mergedItem.mOtherProcesses.add(mProcessItems.get(mpi)); } } - + mergedItem.update(context, false); if (mergedItem.mUserId != mMyUserId) { addOtherUserItem(context, newMergedItems, mOtherUserMergedItems, mergedItem); @@ -1243,7 +1248,7 @@ public class RunningState { // Finally, interesting processes need to be shown and will // go at the top. NHP = mInterestingProcesses.size(); - for (int i=0; i= mBackgroundItems.size() || mBackgroundItems.get(bgIndex).mProcess != proc) { newBackgroundItems = new ArrayList(numBackgroundProcesses); - for (int bgi=0; bgi numBackgroundProcesses) { newBackgroundItems = new ArrayList(numBackgroundProcesses); - for (int bgi=0; bgi(); final int NB = newBackgroundItems.size(); - for (int i=0; i, Parcelable { * Check whether data is still make sense. If not, try recover. * @param context used to do validate check */ - public void sanityCheck(Context context) { + public void validateCheck(Context context) { // do nothing } diff --git a/src/com/android/settings/fuelgauge/batterytip/tips/RestrictAppTip.java b/src/com/android/settings/fuelgauge/batterytip/tips/RestrictAppTip.java index 1bd760b6da8..c5f1171a8c7 100644 --- a/src/com/android/settings/fuelgauge/batterytip/tips/RestrictAppTip.java +++ b/src/com/android/settings/fuelgauge/batterytip/tips/RestrictAppTip.java @@ -107,8 +107,8 @@ public class RestrictAppTip extends BatteryTip { } @Override - public void sanityCheck(Context context) { - super.sanityCheck(context); + public void validateCheck(Context context) { + super.validateCheck(context); // Set it invisible if there is no valid app mRestrictAppList.removeIf(AppLabelPredicate.getInstance(context)); diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/RestrictAppTipTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/RestrictAppTipTest.java index b95040a7bd2..f02eec2cb9e 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/RestrictAppTipTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/RestrictAppTipTest.java @@ -34,7 +34,6 @@ import android.os.Parcel; import com.android.internal.logging.nano.MetricsProto; import com.android.settings.R; import com.android.settings.fuelgauge.batterytip.AppInfo; -import com.android.settings.testutils.BatteryTestUtils; import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; import org.junit.After; @@ -45,10 +44,10 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; +import org.robolectric.util.ReflectionHelpers; import java.util.ArrayList; import java.util.List; -import org.robolectric.util.ReflectionHelpers; @RunWith(RobolectricTestRunner.class) public class RestrictAppTipTest { @@ -198,7 +197,7 @@ public class RestrictAppTipTest { appInfos.add(mUninstallAppInfo); final BatteryTip batteryTip = new RestrictAppTip(BatteryTip.StateType.NEW, appInfos); - batteryTip.sanityCheck(mContext); + batteryTip.validateCheck(mContext); assertThat(batteryTip.getState()).isEqualTo(BatteryTip.StateType.INVISIBLE); } @@ -210,7 +209,7 @@ public class RestrictAppTipTest { appInfos.add(mUninstallAppInfo); final BatteryTip batteryTip = new RestrictAppTip(BatteryTip.StateType.NEW, appInfos); - batteryTip.sanityCheck(mContext); + batteryTip.validateCheck(mContext); assertThat(batteryTip.getState()).isEqualTo(BatteryTip.StateType.NEW); }