Merge "update checkbox based on FAS API" into pi-dev
am: cff99cc2e3

Change-Id: I556fdf252bef54840dc9ec2fe4a43a991517cab2
diff --git a/src/com/android/settings/fuelgauge/BatteryUtils.java b/src/com/android/settings/fuelgauge/BatteryUtils.java
index c8a5d47..639c1fb 100644
--- a/src/com/android/settings/fuelgauge/BatteryUtils.java
+++ b/src/com/android/settings/fuelgauge/BatteryUtils.java
@@ -416,6 +416,11 @@
         mAppOpsManager.setMode(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, uid, packageName, mode);
     }
 
+    public boolean isForceAppStandbyEnabled(int uid, String packageName) {
+        return mAppOpsManager.checkOpNoThrow(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, uid,
+                packageName) == AppOpsManager.MODE_IGNORED;
+    }
+
     public void initBatteryStatsHelper(BatteryStatsHelper statsHelper, Bundle bundle,
             UserManager userManager) {
         statsHelper.create(bundle);
diff --git a/src/com/android/settings/fuelgauge/RestrictedAppDetails.java b/src/com/android/settings/fuelgauge/RestrictedAppDetails.java
index e75112c..96fafb3 100644
--- a/src/com/android/settings/fuelgauge/RestrictedAppDetails.java
+++ b/src/com/android/settings/fuelgauge/RestrictedAppDetails.java
@@ -133,7 +133,8 @@
             try {
                 final ApplicationInfo applicationInfo = mPackageManager.getApplicationInfoAsUser(
                         appInfo.packageName, 0 /* flags */, UserHandle.getUserId(appInfo.uid));
-                checkBoxPreference.setChecked(true);
+                checkBoxPreference.setChecked(
+                        mBatteryUtils.isForceAppStandbyEnabled(appInfo.uid, appInfo.packageName));
                 checkBoxPreference.setTitle(mPackageManager.getApplicationLabel(applicationInfo));
                 checkBoxPreference.setIcon(
                         Utils.getBadgedIcon(mIconDrawableFactory, mPackageManager,
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/BatteryUtilsTest.java b/tests/robotests/src/com/android/settings/fuelgauge/BatteryUtilsTest.java
index 772bb8d..dc34016 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/BatteryUtilsTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/BatteryUtilsTest.java
@@ -565,6 +565,22 @@
     }
 
     @Test
+    public void testIsForceAppStandbyEnabled_enabled_returnTrue() {
+        when(mAppOpsManager.checkOpNoThrow(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, UID,
+                PACKAGE_NAME)).thenReturn(AppOpsManager.MODE_IGNORED);
+
+        assertThat(mBatteryUtils.isForceAppStandbyEnabled(UID, PACKAGE_NAME)).isTrue();
+    }
+
+    @Test
+    public void testIsForceAppStandbyEnabled_disabled_returnFalse() {
+        when(mAppOpsManager.checkOpNoThrow(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, UID,
+                PACKAGE_NAME)).thenReturn(AppOpsManager.MODE_ALLOWED);
+
+        assertThat(mBatteryUtils.isForceAppStandbyEnabled(UID, PACKAGE_NAME)).isFalse();
+    }
+
+    @Test
     public void testIsAppHeavilyUsed_usageMoreThanThreshold_returnTrue() {
         assertThat(mBatteryUtils.isAppHeavilyUsed(mBatteryStatsHelper, mUserManager, UID,
                 10 /* threshold */ )).isTrue();
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/RestrictedAppDetailsTest.java b/tests/robotests/src/com/android/settings/fuelgauge/RestrictedAppDetailsTest.java
index 94a6903..0fad0a7 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/RestrictedAppDetailsTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/RestrictedAppDetailsTest.java
@@ -28,10 +28,12 @@
 import android.content.pm.PackageManager;
 import android.os.Bundle;
 import android.os.UserHandle;
+import android.support.v7.preference.CheckBoxPreference;
 import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceCategory;
 import android.support.v7.preference.PreferenceManager;
 import android.util.IconDrawableFactory;
+import android.widget.CheckBox;
 
 import com.android.settings.SettingsActivity;
 import com.android.settings.core.InstrumentedPreferenceFragment;
@@ -93,7 +95,7 @@
         mRestrictedAppDetails.mAppInfos = new ArrayList<>();
         mRestrictedAppDetails.mAppInfos.add(mAppInfo);
         mRestrictedAppDetails.mRestrictedAppListGroup = spy(new PreferenceCategory(mContext));
-        mRestrictedAppDetails.mBatteryUtils = new BatteryUtils(mContext);
+        mRestrictedAppDetails.mBatteryUtils = spy(new BatteryUtils(mContext));
         doReturn(mPreferenceManager).when(
                 mRestrictedAppDetails.mRestrictedAppListGroup).getPreferenceManager();
     }
@@ -103,13 +105,16 @@
         doReturn(mApplicationInfo).when(mPackageManager)
                 .getApplicationInfoAsUser(PACKAGE_NAME, 0, USER_ID);
         doReturn(APP_NAME).when(mPackageManager).getApplicationLabel(mApplicationInfo);
+        doReturn(true).when(mRestrictedAppDetails.mBatteryUtils).isForceAppStandbyEnabled(UID,
+                PACKAGE_NAME);
 
         mRestrictedAppDetails.refreshUi();
 
         assertThat(mRestrictedAppDetails.mRestrictedAppListGroup.getPreferenceCount()).isEqualTo(1);
-        final Preference preference = mRestrictedAppDetails.mRestrictedAppListGroup.getPreference(
-                0);
+        final CheckBoxPreference preference =
+                (CheckBoxPreference) mRestrictedAppDetails.mRestrictedAppListGroup.getPreference(0);
         assertThat(preference.getTitle()).isEqualTo(APP_NAME);
+        assertThat(preference.isChecked()).isTrue();
     }
 
     @Test