Merge "Create a preference controller for "App info""

This commit is contained in:
TreeHugger Robot
2019-02-27 03:02:46 +00:00
committed by Android (Google) Code Review
6 changed files with 190 additions and 95 deletions

View File

@@ -27,7 +27,8 @@
android:key="all_app_info" android:key="all_app_info"
android:title="@string/applications_settings" android:title="@string/applications_settings"
android:order="-999" android:order="-999"
android:fragment="com.android.settings.applications.manageapplications.ManageApplications"/> android:fragment="com.android.settings.applications.manageapplications.ManageApplications"
settings:controller="com.android.settings.applications.AllAppsInfoPreferenceController"/>
<com.android.settingslib.widget.LayoutPreference <com.android.settingslib.widget.LayoutPreference
android:key="recent_open_apps" android:key="recent_open_apps"

View File

@@ -0,0 +1,58 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.applications;
import android.app.usage.UsageStats;
import android.content.Context;
import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import java.util.List;
public class AllAppsInfoPreferenceController extends BasePreferenceController {
private List<UsageStats> mRecentApps;
public AllAppsInfoPreferenceController(Context context, String key) {
super(context, key);
}
public void setRecentApps(List<UsageStats> recentApps) {
mRecentApps = recentApps;
}
@Override
public int getAvailabilityStatus() {
return mRecentApps == null || mRecentApps.isEmpty() ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
// Show total number of installed apps as See all's summary.
new InstalledAppCounter(mContext, InstalledAppCounter.IGNORE_INSTALL_REASON,
mContext.getPackageManager()) {
@Override
protected void onCountComplete(int num) {
preference.setSummary(mContext.getString(R.string.apps_summary, num));
}
}.execute();
}
}

View File

@@ -36,6 +36,10 @@ public class AppAndNotificationDashboardFragment extends DashboardFragment {
private static final String TAG = "AppAndNotifDashboard"; private static final String TAG = "AppAndNotifDashboard";
private boolean mIsFirstLaunch;
private RecentAppsPreferenceController mRecentAppsPreferenceController;
private AllAppsInfoPreferenceController mAllAppsInfoPreferenceController;
@Override @Override
public int getMetricsCategory() { public int getMetricsCategory() {
return SettingsEnums.SETTINGS_APP_NOTIF_CATEGORY; return SettingsEnums.SETTINGS_APP_NOTIF_CATEGORY;
@@ -61,7 +65,26 @@ public class AppAndNotificationDashboardFragment extends DashboardFragment {
super.onAttach(context); super.onAttach(context);
use(SpecialAppAccessPreferenceController.class).setSession(getSettingsLifecycle()); use(SpecialAppAccessPreferenceController.class).setSession(getSettingsLifecycle());
use(RecentAppsPreferenceController.class).setFragment(this /* fragment */); mRecentAppsPreferenceController = use(RecentAppsPreferenceController.class);
mRecentAppsPreferenceController.setFragment(this /* fragment */);
mAllAppsInfoPreferenceController = use(AllAppsInfoPreferenceController.class);
mAllAppsInfoPreferenceController.setRecentApps(
mRecentAppsPreferenceController.getRecentApps());
mIsFirstLaunch = true;
}
@Override
public void onResume() {
if (!mIsFirstLaunch) {
mRecentAppsPreferenceController.reloadData();
mAllAppsInfoPreferenceController.setRecentApps(
mRecentAppsPreferenceController.getRecentApps());
}
super.onResume();
mIsFirstLaunch = false;
} }
@Override @Override

View File

@@ -66,8 +66,6 @@ import java.util.Set;
public class RecentAppsPreferenceController extends BasePreferenceController public class RecentAppsPreferenceController extends BasePreferenceController
implements Comparator<UsageStats> { implements Comparator<UsageStats> {
@VisibleForTesting
static final String KEY_ALL_APP_INFO = "all_app_info";
@VisibleForTesting @VisibleForTesting
static final String KEY_DIVIDER = "recent_apps_divider"; static final String KEY_DIVIDER = "recent_apps_divider";
@@ -79,11 +77,7 @@ public class RecentAppsPreferenceController extends BasePreferenceController
@VisibleForTesting @VisibleForTesting
LayoutPreference mRecentAppsPreference; LayoutPreference mRecentAppsPreference;
@VisibleForTesting @VisibleForTesting
Preference mAllAppPref;
@VisibleForTesting
Preference mDivider; Preference mDivider;
@VisibleForTesting
boolean mIsFirstLaunch;
private final PackageManager mPm; private final PackageManager mPm;
private final UsageStatsManager mUsageStatsManager; private final UsageStatsManager mUsageStatsManager;
@@ -119,7 +113,6 @@ public class RecentAppsPreferenceController extends BasePreferenceController
mPowerManager = mContext.getSystemService(PowerManager.class); mPowerManager = mContext.getSystemService(PowerManager.class);
mUsageStatsManager = mContext.getSystemService(UsageStatsManager.class); mUsageStatsManager = mContext.getSystemService(UsageStatsManager.class);
mRecentApps = new ArrayList<>(); mRecentApps = new ArrayList<>();
mIsFirstLaunch = true;
reloadData(); reloadData();
} }
@@ -129,14 +122,13 @@ public class RecentAppsPreferenceController extends BasePreferenceController
@Override @Override
public int getAvailabilityStatus() { public int getAvailabilityStatus() {
return mRecentApps.isEmpty() ? AVAILABLE_UNSEARCHABLE : AVAILABLE; return mRecentApps.isEmpty() ? CONDITIONALLY_UNAVAILABLE : AVAILABLE;
} }
@Override @Override
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen); super.displayPreference(screen);
mAllAppPref = screen.findPreference(KEY_ALL_APP_INFO);
mDivider = screen.findPreference(KEY_DIVIDER); mDivider = screen.findPreference(KEY_DIVIDER);
mRecentAppsPreference = (LayoutPreference) screen.findPreference(getPreferenceKey()); mRecentAppsPreference = (LayoutPreference) screen.findPreference(getPreferenceKey());
final View view = mRecentAppsPreference.findViewById(R.id.app_entities_header); final View view = mRecentAppsPreference.findViewById(R.id.app_entities_header);
@@ -157,26 +149,18 @@ public class RecentAppsPreferenceController extends BasePreferenceController
@Override @Override
public void updateState(Preference preference) { public void updateState(Preference preference) {
super.updateState(preference); super.updateState(preference);
// In order to improve launch time, we don't load data again at first launch.
if (!mIsFirstLaunch) {
reloadData();
refreshUi(); refreshUi();
}
// Show total number of installed apps as See all's summary. // Show total number of installed apps as See all's summary.
new InstalledAppCounter(mContext, InstalledAppCounter.IGNORE_INSTALL_REASON, new InstalledAppCounter(mContext, InstalledAppCounter.IGNORE_INSTALL_REASON,
mContext.getPackageManager()) { mContext.getPackageManager()) {
@Override @Override
protected void onCountComplete(int num) { protected void onCountComplete(int num) {
if (mHasRecentApps) {
mAppEntitiesController.setHeaderDetails( mAppEntitiesController.setHeaderDetails(
mContext.getString(R.string.see_all_apps_title, num)); mContext.getString(R.string.see_all_apps_title, num));
mAppEntitiesController.apply(); mAppEntitiesController.apply();
} else {
mAllAppPref.setSummary(mContext.getString(R.string.apps_summary, num));
}
} }
}.execute(); }.execute();
mIsFirstLaunch = false;
} }
@Override @Override
@@ -185,14 +169,16 @@ public class RecentAppsPreferenceController extends BasePreferenceController
return Long.compare(b.getLastTimeUsed(), a.getLastTimeUsed()); return Long.compare(b.getLastTimeUsed(), a.getLastTimeUsed());
} }
List<UsageStats> getRecentApps() {
return mRecentApps;
}
@VisibleForTesting @VisibleForTesting
void refreshUi() { void refreshUi() {
if (mRecentApps != null && !mRecentApps.isEmpty()) { if (mRecentApps != null && !mRecentApps.isEmpty()) {
mHasRecentApps = true;
displayRecentApps(); displayRecentApps();
} else { } else {
mHasRecentApps = false; mDivider.setVisible(false);
displayOnlyAppInfo();
} }
} }
@@ -209,13 +195,6 @@ public class RecentAppsPreferenceController extends BasePreferenceController
updateDisplayableRecentAppList(); updateDisplayableRecentAppList();
} }
private void displayOnlyAppInfo() {
mDivider.setVisible(false);
mAllAppPref.setTitle(R.string.applications_settings);
mAllAppPref.setVisible(true);
mRecentAppsPreference.setVisible(false);
}
private void displayRecentApps() { private void displayRecentApps() {
int showAppsCount = 0; int showAppsCount = 0;
@@ -230,8 +209,6 @@ public class RecentAppsPreferenceController extends BasePreferenceController
} }
} }
mAppEntitiesController.apply(); mAppEntitiesController.apply();
mRecentAppsPreference.setVisible(true);
mAllAppPref.setVisible(false);
mDivider.setVisible(true); mDivider.setVisible(true);
} }

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.applications;
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
import static com.google.common.truth.Truth.assertThat;
import android.app.usage.UsageStats;
import android.content.Context;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class AllAppsInfoPreferenceControllerTest {
private AllAppsInfoPreferenceController mController;
@Before
public void setUp() {
final Context context = RuntimeEnvironment.application;
mController = new AllAppsInfoPreferenceController(context, "test_key");
}
@Test
public void getAvailabilityStatus_hasRecentApps_shouldReturnConditionallyUnavailable() {
final List<UsageStats> stats = new ArrayList<>();
final UsageStats stat1 = new UsageStats();
stat1.mLastTimeUsed = System.currentTimeMillis();
stat1.mPackageName = "pkg.class";
stats.add(stat1);
mController.setRecentApps(stats);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void getAvailabilityStatus_noRecentApps_shouldReturnAvailable() {
// No data
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
}

View File

@@ -18,6 +18,7 @@ package com.android.settings.applications;
import static com.android.settings.core.BasePreferenceController.AVAILABLE; import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.AVAILABLE_UNSEARCHABLE; import static com.android.settings.core.BasePreferenceController.AVAILABLE_UNSEARCHABLE;
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
@@ -111,7 +112,6 @@ public class RecentAppsPreferenceControllerTest {
final View appEntitiesHeaderView = LayoutInflater.from(context).inflate( final View appEntitiesHeaderView = LayoutInflater.from(context).inflate(
R.layout.app_entities_header, null /* root */); R.layout.app_entities_header, null /* root */);
final Preference seeAllPreference = new Preference(context);
final Preference dividerPreference = new Preference(context); final Preference dividerPreference = new Preference(context);
mRecentAppsPreference = spy(new LayoutPreference(context, appEntitiesHeaderView)); mRecentAppsPreference = spy(new LayoutPreference(context, appEntitiesHeaderView));
@@ -119,11 +119,8 @@ public class RecentAppsPreferenceControllerTest {
mController.setFragment(mFragment); mController.setFragment(mFragment);
mController.mAppEntitiesController = mock(AppEntitiesHeaderController.class); mController.mAppEntitiesController = mock(AppEntitiesHeaderController.class);
mController.mRecentAppsPreference = mRecentAppsPreference; mController.mRecentAppsPreference = mRecentAppsPreference;
mController.mAllAppPref = seeAllPreference;
mController.mDivider = dividerPreference; mController.mDivider = dividerPreference;
when(mScreen.findPreference(RecentAppsPreferenceController.KEY_ALL_APP_INFO))
.thenReturn(seeAllPreference);
when(mScreen.findPreference(RecentAppsPreferenceController.KEY_DIVIDER)) when(mScreen.findPreference(RecentAppsPreferenceController.KEY_DIVIDER))
.thenReturn(dividerPreference); .thenReturn(dividerPreference);
when(mScreen.findPreference("test_key")).thenReturn(mRecentAppsPreference); when(mScreen.findPreference("test_key")).thenReturn(mRecentAppsPreference);
@@ -152,9 +149,33 @@ public class RecentAppsPreferenceControllerTest {
} }
@Test @Test
public void getAvailabilityStatus_noRecentApps_shouldReturnAvailableUnsearchable() { public void getAvailabilityStatus_noRecentApps_shouldReturnConditionallyUnavailable() {
// No data // No data
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE_UNSEARCHABLE); assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void getAvailabilityStatus_powerSaverModeOn_shouldReturnConditionallyUnavailable() {
when(mPowerManager.isPowerSaveMode()).thenReturn(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.reloadData();
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
} }
@Test @Test
@@ -178,25 +199,6 @@ public class RecentAppsPreferenceControllerTest {
assertThat(mController.mAppEntitiesController).isNotNull(); assertThat(mController.mAppEntitiesController).isNotNull();
} }
@Test
public void updateState_firstLaunch_shouldNotReloadData() {
mController.mIsFirstLaunch = true;
mController.updateState(mRecentAppsPreference);
verify(mController, never()).reloadData();
}
@Test
public void updateState_afterFirstLaunch_shouldReloadDataAndRefreshUi() {
mController.mIsFirstLaunch = false;
mController.updateState(mRecentAppsPreference);
verify(mController).reloadData();
verify(mController).refreshUi();
}
@Test @Test
public void updateState_threeValidRecentOpenAppsSet_setAppEntityThreeTime() { public void updateState_threeValidRecentOpenAppsSet_setAppEntityThreeTime() {
final List<UsageStats> stats = new ArrayList<>(); final List<UsageStats> stats = new ArrayList<>();
@@ -227,7 +229,7 @@ public class RecentAppsPreferenceControllerTest {
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
.thenReturn(stats); .thenReturn(stats);
mAppEntry.info = mApplicationInfo; mAppEntry.info = mApplicationInfo;
mController.mIsFirstLaunch = false; mController.reloadData();
mController.updateState(mRecentAppsPreference); mController.updateState(mRecentAppsPreference);
@@ -235,7 +237,6 @@ public class RecentAppsPreferenceControllerTest {
.setAppEntity(anyInt(), any(AppEntityInfo.class)); .setAppEntity(anyInt(), any(AppEntityInfo.class));
assertThat(mController.mRecentAppsPreference.isVisible()).isTrue(); assertThat(mController.mRecentAppsPreference.isVisible()).isTrue();
assertThat(mController.mDivider.isVisible()).isTrue(); assertThat(mController.mDivider.isVisible()).isTrue();
assertThat(mController.mAllAppPref.isVisible()).isFalse();
} }
@Test @Test
@@ -268,7 +269,7 @@ public class RecentAppsPreferenceControllerTest {
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
.thenReturn(stats); .thenReturn(stats);
mAppEntry.info = mApplicationInfo; mAppEntry.info = mApplicationInfo;
mController.mIsFirstLaunch = false; mController.reloadData();
mController.updateState(mRecentAppsPreference); mController.updateState(mRecentAppsPreference);
@@ -278,35 +279,6 @@ public class RecentAppsPreferenceControllerTest {
.setAppEntity(anyInt(), any(AppEntityInfo.class)); .setAppEntity(anyInt(), any(AppEntityInfo.class));
assertThat(mController.mRecentAppsPreference.isVisible()).isTrue(); assertThat(mController.mRecentAppsPreference.isVisible()).isTrue();
assertThat(mController.mDivider.isVisible()).isTrue(); assertThat(mController.mDivider.isVisible()).isTrue();
assertThat(mController.mAllAppPref.isVisible()).isFalse();
}
@Test
public void updateState_powerSaverModeOn_headerIsNotVisible() {
when(mPowerManager.isPowerSaveMode()).thenReturn(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.mIsFirstLaunch = false;
mController.updateState(mRecentAppsPreference);
assertThat(mController.mRecentAppsPreference.isVisible()).isFalse();
assertThat(mController.mDivider.isVisible()).isFalse();
assertThat(mController.mAllAppPref.isVisible()).isTrue();
} }
@Test @Test
@@ -341,7 +313,7 @@ public class RecentAppsPreferenceControllerTest {
// Make sure stat2 is considered an instant app. // Make sure stat2 is considered an instant app.
ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider", ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider",
(InstantAppDataProvider) (ApplicationInfo info) -> info == stat2Entry.info); (InstantAppDataProvider) (ApplicationInfo info) -> info == stat2Entry.info);
mController.mIsFirstLaunch = false; mController.reloadData();
mController.updateState(mRecentAppsPreference); mController.updateState(mRecentAppsPreference);
@@ -417,7 +389,7 @@ public class RecentAppsPreferenceControllerTest {
.thenReturn(new ResolveInfo()); .thenReturn(new ResolveInfo());
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
.thenReturn(stats); .thenReturn(stats);
mController.mIsFirstLaunch = false; mController.reloadData();
mController.updateState(mRecentAppsPreference); mController.updateState(mRecentAppsPreference);