Disable recently used apps in battery saver mode.

UsageStats query is too slow.

Change-Id: I53fa13924514e71221d3a6407139ff00ae5eb378
Fixes: 114788699
Test: robotests
This commit is contained in:
Fan Zhang
2018-09-21 14:14:16 -07:00
parent a114c09c7a
commit 2a1e59063d
2 changed files with 44 additions and 6 deletions

View File

@@ -25,6 +25,7 @@ import android.app.usage.UsageStatsManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.PowerManager;
import android.os.UserHandle;
import android.text.TextUtils;
import android.util.ArrayMap;
@@ -78,6 +79,7 @@ public class RecentAppsPreferenceController extends AbstractPreferenceController
private final ApplicationsState mApplicationsState;
private final int mUserId;
private final IconDrawableFactory mIconDrawableFactory;
private final PowerManager mPowerManager;
private Calendar mCal;
private List<UsageStats> mStats;
@@ -108,6 +110,8 @@ public class RecentAppsPreferenceController extends AbstractPreferenceController
mIconDrawableFactory = IconDrawableFactory.newInstance(context);
mUserId = UserHandle.myUserId();
mPm = context.getPackageManager();
mPowerManager = context.getSystemService(PowerManager.class);
mHost = host;
mUsageStatsManager =
(UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
@@ -183,9 +187,11 @@ public class RecentAppsPreferenceController extends AbstractPreferenceController
void reloadData() {
mCal = Calendar.getInstance();
mCal.add(Calendar.DAY_OF_YEAR, -1);
mStats = mUsageStatsManager.queryUsageStats(
UsageStatsManager.INTERVAL_BEST, mCal.getTimeInMillis(),
System.currentTimeMillis());
mStats = mPowerManager.isPowerSaveMode()
? new ArrayList<>()
: mUsageStatsManager.queryUsageStats(
UsageStatsManager.INTERVAL_BEST, mCal.getTimeInMillis(),
System.currentTimeMillis());
}
private void displayOnlyAppInfo() {
@@ -244,8 +250,8 @@ public class RecentAppsPreferenceController extends AbstractPreferenceController
pref.setOrder(i);
pref.setOnPreferenceClickListener(preference -> {
AppInfoBase.startAppInfoFragment(AppInfoDashboardFragment.class,
R.string.application_info_label, pkgName, appEntry.info.uid, mHost,
1001 /*RequestCode*/, SETTINGS_APP_NOTIF_CATEGORY);
R.string.application_info_label, pkgName, appEntry.info.uid, mHost,
1001 /*RequestCode*/, SETTINGS_APP_NOTIF_CATEGORY);
return true;
});
if (!rebindPref) {
@@ -301,7 +307,8 @@ public class RecentAppsPreferenceController extends AbstractPreferenceController
private boolean shouldIncludePkgInRecents(UsageStats stat) {
final String pkgName = stat.getPackageName();
if (stat.getLastTimeUsed() < mCal.getTimeInMillis()) {
Log.d(TAG, "Invalid timestamp, skipping " + pkgName);
Log.d(TAG, "Invalid timestamp (usage time is more than 24 hours ago), skipping "
+ pkgName);
return false;
}

View File

@@ -40,6 +40,7 @@ import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.PowerManager;
import android.os.UserHandle;
import android.os.UserManager;
import android.text.TextUtils;
@@ -50,6 +51,7 @@ import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowPowerManager;
import com.android.settingslib.applications.AppUtils;
import com.android.settingslib.applications.ApplicationsState;
import com.android.settingslib.applications.instantapps.InstantAppDataProvider;
@@ -195,6 +197,35 @@ public class RecentAppsPreferenceControllerTest {
verify(mDivider).setVisible(true);
}
@Test
public void display_powerSaverMode_showNoRecents() {
mContext.getSystemService(PowerManager.class).setPowerSaveMode(true);
final List<UsageStats> stats = new ArrayList<>();
final UsageStats stat1 = new UsageStats();
stat1.mLastTimeUsed = System.currentTimeMillis();
stat1.mPackageName = "pkg.class";
stats.add(stat1);
// stat1, stat2 are valid apps. stat3 is invalid.
when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId()))
.thenReturn(mAppEntry);
when(mPackageManager.resolveActivity(any(Intent.class), anyInt()))
.thenReturn(new ResolveInfo());
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
.thenReturn(stats);
mAppEntry.info = mApplicationInfo;
mController.displayPreference(mScreen);
verify(mCategory, never()).addPreference(any(Preference.class));
verify(mCategory).setTitle(null);
verify(mSeeAllPref).setTitle(R.string.applications_settings);
verify(mSeeAllPref).setIcon(null);
verify(mDivider).setVisible(false);
}
@Test
public void display_showRecentsWithInstantApp() {
// Regular app.