Merge changes from topic 'secondary-users'

* changes:
  Update the loading of info for the secondary users.
  Add support for visualizing secondary users.
This commit is contained in:
Daniel Nishi
2017-02-21 23:20:29 +00:00
committed by Android (Google) Code Review
13 changed files with 556 additions and 69 deletions

View File

@@ -43,6 +43,9 @@
android:key="pref_system"
android:title="@string/storage_detail_system">
</com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate>
<PreferenceCategory
android:key="pref_secondary_users"
android:title="@string/storage_other_users" />
<Preference
android:key="manage_storage"
android:title="@string/storage_menu_manage"

View File

@@ -16,6 +16,11 @@
package com.android.settings;
import static android.content.Intent.EXTRA_USER;
import static android.content.Intent.EXTRA_USER_ID;
import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
import android.annotation.Nullable;
import android.app.ActivityManager;
import android.app.AlertDialog;
@@ -104,11 +109,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import static android.content.Intent.EXTRA_USER;
import static android.content.Intent.EXTRA_USER_ID;
import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
public final class Utils extends com.android.settingslib.Utils {
private static final String TAG = "Settings";
@@ -1246,4 +1246,16 @@ public final class Utils extends com.android.settingslib.Utils {
&& (Settings.Secure.getInt(context.getContentResolver(),
carrierDemoModeSetting, 0) == 1);
}
/**
* Returns if a given user is a profile of another user.
* @param user The user whose profiles will be checked.
* @param profile The (potential) profile.
* @return if the profile is actually a profile
*/
public static boolean isProfileOf(UserInfo user, UserInfo profile) {
return user.id == profile.id ||
(user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
&& user.profileGroupId == profile.profileGroupId);
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright (C) 2017 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.content.pm.UserInfo;
import java.util.List;
/**
* This interface replicates a subset of the android.os.UserManager. The interface
* exists so that we can use a thin wrapper around the UserManager in production code and a mock in
* tests. We cannot directly mock or shadow the UserManager, because some of the methods we rely on
* are newer than the API version supported by Robolectric or are hidden.
*/
public interface UserManagerWrapper {
UserInfo getPrimaryUser();
List<UserInfo> getUsers();
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright (C) 2017 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.content.pm.UserInfo;
import android.os.UserManager;
import java.util.List;
public class UserManagerWrapperImpl implements UserManagerWrapper {
private UserManager mUserManager;
public UserManagerWrapperImpl(UserManager userManager) {
mUserManager = userManager;
}
@Override
public UserInfo getPrimaryUser() {
return mUserManager.getPrimaryUser();
}
@Override
public List<UserInfo> getUsers() {
return mUserManager.getUsers();
}
}

View File

@@ -228,7 +228,7 @@ public class PrivateVolumeSettings extends SettingsPreferenceFragment {
// Add current user and its profiles first
for (int userIndex = 0; userIndex < userCount; ++userIndex) {
final UserInfo userInfo = allUsers.get(userIndex);
if (isProfileOf(mCurrentUser, userInfo)) {
if (Utils.isProfileOf(mCurrentUser, userInfo)) {
final PreferenceGroup details = showHeaders ?
addCategory(screen, userInfo.name) : screen;
addDetailItems(details, showShared, userInfo.id);
@@ -242,7 +242,7 @@ public class PrivateVolumeSettings extends SettingsPreferenceFragment {
getText(R.string.storage_other_users));
for (int userIndex = 0; userIndex < userCount; ++userIndex) {
final UserInfo userInfo = allUsers.get(userIndex);
if (!isProfileOf(mCurrentUser, userInfo)) {
if (!Utils.isProfileOf(mCurrentUser, userInfo)) {
addItem(otherUsers, /* titleRes */ 0, userInfo.name, userInfo.id);
}
}
@@ -649,12 +649,6 @@ public class PrivateVolumeSettings extends SettingsPreferenceFragment {
pref.setStorageSize(size, mTotalSize);
}
private boolean isProfileOf(UserInfo user, UserInfo profile) {
return user.id == profile.id ||
(user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
&& user.profileGroupId == profile.profileGroupId);
}
private static long totalValues(MeasurementDetails details, int userId, String... keys) {
long total = 0;
HashMap<String, Long> map = details.mediaSize.get(userId);

View File

@@ -16,36 +16,50 @@
package com.android.settings.deviceinfo;
import android.app.LoaderManager;
import android.content.Context;
import android.content.Loader;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.os.storage.StorageManager;
import android.os.storage.VolumeInfo;
import android.provider.SearchIndexableResource;
import android.support.annotation.VisibleForTesting;
import android.util.SparseArray;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.applications.PackageManagerWrapperImpl;
import com.android.settings.applications.UserManagerWrapper;
import com.android.settings.applications.UserManagerWrapperImpl;
import com.android.settings.core.PreferenceController;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.deviceinfo.storage.SecondaryUserController;
import com.android.settings.deviceinfo.storage.StorageAsyncLoader;
import com.android.settings.deviceinfo.storage.StorageItemPreferenceController;
import com.android.settings.deviceinfo.storage.StorageSummaryDonutPreferenceController;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
import com.android.settingslib.applications.StorageStatsSource;
import com.android.settingslib.deviceinfo.StorageManagerVolumeProvider;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class StorageDashboardFragment extends DashboardFragment {
public class StorageDashboardFragment extends DashboardFragment
implements LoaderManager.LoaderCallbacks<SparseArray<StorageAsyncLoader.AppsStorageResult>> {
private static final String TAG = "StorageDashboardFrag";
private static final int APPS_JOB_ID = 0;
private static final int STORAGE_JOB_ID = 0;
private VolumeInfo mVolume;
private StorageSummaryDonutPreferenceController mSummaryController;
private StorageItemPreferenceController mPreferenceController;
private List<PreferenceController> mSecondaryUsers;
private boolean isVolumeValid() {
return (mVolume != null) && (mVolume.getType() == VolumeInfo.TYPE_PRIVATE)
@@ -55,7 +69,29 @@ public class StorageDashboardFragment extends DashboardFragment {
@Override
public void onResume() {
super.onResume();
getLoaderManager().initLoader(APPS_JOB_ID, Bundle.EMPTY, mPreferenceController);
getLoaderManager().initLoader(STORAGE_JOB_ID, Bundle.EMPTY, this);
}
@Override
public Loader<SparseArray<StorageAsyncLoader.AppsStorageResult>> onCreateLoader(int id,
Bundle args) {
Context context = getContext();
return new StorageAsyncLoader(context,
new UserManagerWrapperImpl(context.getSystemService(UserManager.class)),
mVolume.fsUuid,
new StorageStatsSource(context),
new PackageManagerWrapperImpl(context.getPackageManager()));
}
@Override
public void onLoadFinished(Loader<SparseArray<StorageAsyncLoader.AppsStorageResult>> loader,
SparseArray<StorageAsyncLoader.AppsStorageResult> data) {
mPreferenceController.onLoadFinished(data.get(UserHandle.myUserId()));
updateSecondaryUserControllers(mSecondaryUsers, data);
}
@Override
public void onLoaderReset(Loader<SparseArray<StorageAsyncLoader.AppsStorageResult>> loader) {
}
@Override
@@ -110,6 +146,12 @@ public class StorageDashboardFragment extends DashboardFragment {
mPreferenceController = new StorageItemPreferenceController(context, this,
mVolume, new StorageManagerVolumeProvider(sm));
controllers.add(mPreferenceController);
UserManagerWrapper userManager =
new UserManagerWrapperImpl(context.getSystemService(UserManager.class));
mSecondaryUsers = SecondaryUserController.getSecondaryUserControllers(context, userManager);
controllers.addAll(mSecondaryUsers);
controllers.add(new ManageStoragePreferenceController(context));
return controllers;
}
@@ -125,6 +167,24 @@ public class StorageDashboardFragment extends DashboardFragment {
return isVolumeValid();
}
/**
* Updates the secondary user controller sizes.
*/
private void updateSecondaryUserControllers(List<PreferenceController> controllers,
SparseArray<StorageAsyncLoader.AppsStorageResult> stats) {
for (int i = 0, size = controllers.size(); i < size; i++) {
PreferenceController controller = controllers.get(i);
if (controller instanceof SecondaryUserController) {
SecondaryUserController userController = (SecondaryUserController) controller;
int userId = userController.getUser().id;
StorageAsyncLoader.AppsStorageResult result = stats.get(userId);
if (result != null) {
userController.setSize(result.externalStats.totalBytes);
}
}
}
}
/**
* For Search.
*/

View File

@@ -0,0 +1,160 @@
/*
* Copyright (C) 2017 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.deviceinfo.storage;
import android.content.Context;
import android.content.pm.UserInfo;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.PreferenceGroup;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.Utils;
import com.android.settings.applications.UserManagerWrapper;
import com.android.settings.core.PreferenceController;
import java.util.ArrayList;
import java.util.List;
/**
* SecondaryUserController controls the preferences on the Storage screen which had to do with
* secondary users.
*/
public class SecondaryUserController extends PreferenceController {
// PreferenceGroupKey to try to add our preference onto.
private static final String TARGET_PREFERENCE_GROUP_KEY = "pref_secondary_users";
private static final String PREFERENCE_KEY_BASE = "pref_user_";
private static final int SIZE_NOT_SET = -1;
private @NonNull UserInfo mUser;
private @Nullable StorageItemPreferenceAlternate mStoragePreference;
private long mSize;
/**
* Adds the appropriate controllers to a controller list for handling all secondary users on
* a device.
* @param context Context for initializing the preference controllers.
* @param userManager UserManagerWrapper for figuring out which controllers to add.
*/
public static List<PreferenceController> getSecondaryUserControllers(
Context context, UserManagerWrapper userManager) {
List<PreferenceController> controllers = new ArrayList<>();
UserInfo primaryUser = userManager.getPrimaryUser();
boolean addedUser = false;
List<UserInfo> infos = userManager.getUsers();
for (int i = 0, size = infos.size(); i < size; i++) {
UserInfo info = infos.get(i);
if (info == null || Utils.isProfileOf(primaryUser, info)) {
continue;
}
controllers.add(new SecondaryUserController(context, info));
addedUser = true;
}
if (!addedUser) {
controllers.add(new NoSecondaryUserController(context));
}
return controllers;
}
/**
* Constructor for a given secondary user.
* @param context Context to initialize the underlying {@link PreferenceController}.
* @param info {@link UserInfo} for the secondary user which this controllers covers.
*/
@VisibleForTesting
SecondaryUserController(Context context, @NonNull UserInfo info) {
super(context);
mUser = info;
mSize = SIZE_NOT_SET;
}
@Override
public void displayPreference(PreferenceScreen screen) {
if (mStoragePreference == null) {
mStoragePreference = new StorageItemPreferenceAlternate(mContext);
PreferenceGroup group =
(PreferenceGroup) screen.findPreference(TARGET_PREFERENCE_GROUP_KEY);
mStoragePreference.setTitle(mUser.name);
mStoragePreference.setKey(PREFERENCE_KEY_BASE + mUser.id);
if (mSize != SIZE_NOT_SET) {
mStoragePreference.setStorageSize(mSize);
}
group.setVisible(true);
group.addPreference(mStoragePreference);
}
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public String getPreferenceKey() {
return mStoragePreference != null ? mStoragePreference.getKey() : null;
}
/**
* Returns the user for which this is the secondary user controller.
*/
@NonNull
public UserInfo getUser() {
return mUser;
}
/**
* Sets the size for the preference.
* @param size Size in bytes.
*/
public void setSize(long size) {
mSize = size;
if (mStoragePreference != null) {
mStoragePreference.setStorageSize(mSize);
}
}
private static class NoSecondaryUserController extends PreferenceController {
public NoSecondaryUserController(Context context) {
super(context);
}
@Override
public void displayPreference(PreferenceScreen screen) {
PreferenceGroup group =
(PreferenceGroup) screen.findPreference(TARGET_PREFERENCE_GROUP_KEY);
if (group == null) {
return;
}
screen.removePreference(group);
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public String getPreferenceKey() {
return null;
}
}
}

View File

@@ -21,46 +21,61 @@ import static android.content.pm.ApplicationInfo.CATEGORY_GAME;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.UserInfo;
import android.os.UserHandle;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.SparseArray;
import com.android.settings.applications.PackageManagerWrapper;
import com.android.settings.applications.UserManagerWrapper;
import com.android.settings.utils.AsyncLoader;
import com.android.settingslib.applications.StorageStatsSource;
import java.util.List;
import java.util.Map;
/**
* AppsAsyncLoader is a Loader which loads app storage information and categories it by the app's
* specified categorization.
* StorageAsyncLoader is a Loader which loads categorized app information and external stats for all
* users
*/
public class StorageAsyncLoader extends AsyncLoader<StorageAsyncLoader.AppsStorageResult> {
private int mUserId;
public class StorageAsyncLoader
extends AsyncLoader<SparseArray<StorageAsyncLoader.AppsStorageResult>> {
private UserManagerWrapper mUserManager;
private String mUuid;
private StorageStatsSource mStatsManager;
private PackageManagerWrapper mPackageManager;
public StorageAsyncLoader(Context context, int userId, String uuid, StorageStatsSource source,
PackageManagerWrapper pm) {
public StorageAsyncLoader(Context context, UserManagerWrapper userManager,
String uuid, StorageStatsSource source, PackageManagerWrapper pm) {
super(context);
mUserId = userId;
mUserManager = userManager;
mUuid = uuid;
mStatsManager = source;
mPackageManager = pm;
}
@Override
public AppsStorageResult loadInBackground() {
public SparseArray<AppsStorageResult> loadInBackground() {
return loadApps();
}
private AppsStorageResult loadApps() {
AppsStorageResult result = new AppsStorageResult();
ArraySet<Integer> seenUid = new ArraySet<>(); // some apps share a uid
private SparseArray<AppsStorageResult> loadApps() {
SparseArray<AppsStorageResult> result = new SparseArray<>();
List<UserInfo> infos = mUserManager.getUsers();
for (int i = 0, userCount = infos.size(); i < userCount; i++) {
UserInfo info = infos.get(i);
result.put(info.id, getStorageResultForUser(info.id));
}
return result;
}
private AppsStorageResult getStorageResultForUser(int userId) {
List<ApplicationInfo> applicationInfos =
mPackageManager.getInstalledApplicationsAsUser(0, mUserId);
int size = applicationInfos.size();
for (int i = 0; i < size; i++) {
mPackageManager.getInstalledApplicationsAsUser(0, userId);
ArraySet<Integer> seenUid = new ArraySet<>(); // some apps share a uid
AppsStorageResult result = new AppsStorageResult();
for (int i = 0, size = applicationInfos.size(); i < size; i++) {
ApplicationInfo app = applicationInfos.get(i);
if (seenUid.contains(app.uid)) {
continue;
@@ -83,12 +98,12 @@ public class StorageAsyncLoader extends AsyncLoader<StorageAsyncLoader.AppsStora
}
}
result.externalStats = mStatsManager.getExternalStorageStats(mUuid, UserHandle.of(mUserId));
result.externalStats = mStatsManager.getExternalStorageStats(mUuid, UserHandle.of(userId));
return result;
}
@Override
protected void onDiscardResult(AppsStorageResult result) {
protected void onDiscardResult(SparseArray<AppsStorageResult> result) {
}
public static class AppsStorageResult {

View File

@@ -18,10 +18,8 @@ package com.android.settings.deviceinfo.storage;
import android.content.Context;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceViewHolder;
import android.text.format.Formatter;
import android.util.AttributeSet;
import android.view.View;
import com.android.settings.R;

View File

@@ -17,11 +17,9 @@
package com.android.settings.deviceinfo.storage;
import android.app.Fragment;
import android.app.LoaderManager;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.Loader;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
@@ -36,22 +34,21 @@ import com.android.settings.R;
import com.android.settings.Settings;
import com.android.settings.Utils;
import com.android.settings.applications.ManageApplications;
import com.android.settings.applications.PackageManagerWrapperImpl;
import com.android.settings.core.PreferenceController;
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.deviceinfo.StorageMeasurement;
import com.android.settingslib.deviceinfo.StorageVolumeProvider;
import com.android.settingslib.applications.StorageStatsSource;
import java.util.HashMap;
import java.util.Map;
/**
* StorageItemPreferenceController handles the storage line items which summarize the storage
* categorization breakdown.
*/
public class StorageItemPreferenceController extends PreferenceController
implements LoaderManager.LoaderCallbacks<StorageAsyncLoader.AppsStorageResult> {
public class StorageItemPreferenceController extends PreferenceController {
private static final String TAG = "StorageItemPreference";
private static final String IMAGE_MIME_TYPE = "image/*";
@@ -110,6 +107,9 @@ public class StorageItemPreferenceController extends PreferenceController
// TODO: Currently, this reflects the existing behavior for these toggles.
// After the intermediate views are built, swap them in.
Intent intent = null;
if (preference.getKey() == null) {
return false;
}
switch (preference.getKey()) {
case PHOTO_KEY:
intent = getPhotosIntent();
@@ -167,17 +167,7 @@ public class StorageItemPreferenceController extends PreferenceController
mFilePreference = (StorageItemPreferenceAlternate) screen.findPreference(FILES_KEY);
}
@Override
public Loader<StorageAsyncLoader.AppsStorageResult> onCreateLoader(int id,
Bundle args) {
return new StorageAsyncLoader(mContext, UserHandle.myUserId(), mVolume.fsUuid,
new StorageStatsSource(mContext),
new PackageManagerWrapperImpl(mContext.getPackageManager()));
}
@Override
public void onLoadFinished(Loader<StorageAsyncLoader.AppsStorageResult> loader,
StorageAsyncLoader.AppsStorageResult data) {
public void onLoadFinished(StorageAsyncLoader.AppsStorageResult data) {
mPhotoPreference.setStorageSize(
data.externalStats.imageBytes + data.externalStats.videoBytes);
mAudioPreference.setStorageSize(data.musicAppsSize + data.externalStats.audioBytes);
@@ -190,10 +180,6 @@ public class StorageItemPreferenceController extends PreferenceController
mFilePreference.setStorageSize(unattributedBytes);
}
@Override
public void onLoaderReset(Loader<StorageAsyncLoader.AppsStorageResult> loader) {
}
/**
* Sets the system size for the system size preference.
* @param systemSize the size of the system in bytes
@@ -263,7 +249,7 @@ public class StorageItemPreferenceController extends PreferenceController
private static long totalValues(StorageMeasurement.MeasurementDetails details, int userId,
String... keys) {
long total = 0;
HashMap<String, Long> map = details.mediaSize.get(userId);
Map<String, Long> map = details.mediaSize.get(userId);
if (map != null) {
for (String key : keys) {
if (map.containsKey(key)) {

View File

@@ -0,0 +1,148 @@
/*
* Copyright (C) 2017 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.deviceinfo.storage;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.UserInfo;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceGroup;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.applications.UserManagerWrapper;
import com.android.settings.core.PreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.ArrayList;
import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class SecondaryUserControllerTest {
private static final String TEST_NAME = "Fred";
private static final String TARGET_PREFERENCE_GROUP_KEY = "pref_secondary_users";
@Mock
private UserManagerWrapper mUserManager;
private Context mContext;
private SecondaryUserController mController;
private UserInfo mPrimaryUser;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mPrimaryUser = new UserInfo();
mController = new SecondaryUserController(mContext, mPrimaryUser);
}
@Test
public void controllerAddsSecondaryUser() throws Exception {
mPrimaryUser.name = TEST_NAME;
PreferenceScreen screen = mock(PreferenceScreen.class);
PreferenceGroup group = mock(PreferenceGroup.class);
when(screen.findPreference(anyString())).thenReturn(group);
when(group.getKey()).thenReturn(TARGET_PREFERENCE_GROUP_KEY);
mController.displayPreference(screen);
final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class);
verify(group).addPreference(argumentCaptor.capture());
Preference preference = argumentCaptor.getValue();
assertThat(preference.getTitle()).isEqualTo(TEST_NAME);
}
@Test
public void controllerUpdatesSummaryOfNewPreference() throws Exception {
mPrimaryUser.name = TEST_NAME;
PreferenceScreen screen = mock(PreferenceScreen.class);
PreferenceGroup group = mock(PreferenceGroup.class);
when(screen.findPreference(anyString())).thenReturn(group);
when(group.getKey()).thenReturn(TARGET_PREFERENCE_GROUP_KEY);
mController.displayPreference(screen);
mController.setSize(10L);
final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class);
verify(group).addPreference(argumentCaptor.capture());
Preference preference = argumentCaptor.getValue();
assertThat(preference.getSummary()).isEqualTo("10.00B");
}
@Test
public void noSecondaryUserAddedIfNoneExist() throws Exception {
ArrayList<UserInfo> userInfos = new ArrayList<>();
userInfos.add(mPrimaryUser);
when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser);
when(mUserManager.getUsers()).thenReturn(userInfos);
List<PreferenceController> controllers =
SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager);
assertThat(controllers).hasSize(1);
// We should have the NoSecondaryUserController.
assertThat(controllers.get(0) instanceof SecondaryUserController).isFalse();
}
@Test
public void secondaryUserAddedIfHasDistinctId() throws Exception {
ArrayList<UserInfo> userInfos = new ArrayList<>();
UserInfo secondaryUser = new UserInfo();
secondaryUser.id = 10;
secondaryUser.profileGroupId = 101010; // this just has to be something not 0
userInfos.add(mPrimaryUser);
userInfos.add(secondaryUser);
when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser);
when(mUserManager.getUsers()).thenReturn(userInfos);
List<PreferenceController> controllers =
SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager);
assertThat(controllers).hasSize(1);
assertThat(controllers.get(0) instanceof SecondaryUserController).isTrue();
}
@Test
public void profilesOfPrimaryUserAreIgnored() throws Exception {
ArrayList<UserInfo> userInfos = new ArrayList<>();
UserInfo secondaryUser = new UserInfo();
secondaryUser.id = mPrimaryUser.id;
userInfos.add(mPrimaryUser);
userInfos.add(secondaryUser);
when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser);
when(mUserManager.getUsers()).thenReturn(userInfos);
List<PreferenceController> controllers =
SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager);
assertThat(controllers).hasSize(1);
assertThat(controllers.get(0) instanceof SecondaryUserController).isFalse();
}
}

View File

@@ -44,6 +44,7 @@ import com.android.settings.TestConfig;
import com.android.settings.applications.ManageApplications;
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.applications.StorageStatsSource;
import com.android.settingslib.deviceinfo.StorageVolumeProvider;
import org.junit.Before;
@@ -223,7 +224,7 @@ public class StorageItemPreferenceControllerTest {
result.gamesSize = KILOBYTE * 8;
result.otherAppsSize = KILOBYTE * 9;
mController.onLoadFinished(null, result);
mController.onLoadFinished(result);
assertThat(audio.getSummary().toString()).isEqualTo("14.00KB"); // 4KB apps + 10KB files
assertThat(image.getSummary().toString()).isEqualTo("35.00KB"); // 15KB video + 20KB images

View File

@@ -26,10 +26,15 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.UserInfo;
import android.os.UserHandle;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import android.util.SparseArray;
import com.android.settings.applications.PackageManagerWrapper;
import com.android.settings.applications.UserManagerWrapper;
import com.android.settingslib.applications.StorageStatsSource;
import org.junit.Before;
import org.junit.Test;
@@ -39,26 +44,39 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class StorageAsyncLoaderTest {
private static final int PRIMARY_USER_ID = 0;
private static final int SECONDARY_USER_ID = 10;
@Mock
private StorageStatsSource mSource;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@Mock
private PackageManagerWrapper mPackageManager;
ArrayList<ApplicationInfo> mInfo = new ArrayList<>();
@Mock
private UserManagerWrapper mUserManager;
private List<ApplicationInfo> mInfo = new ArrayList<>();
private List<UserInfo> mUsers;
private StorageAsyncLoader mLoader;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mInfo = new ArrayList<>();
mLoader = new StorageAsyncLoader(mContext, 1, "id", mSource, mPackageManager);
mLoader = new StorageAsyncLoader(mContext, mUserManager, "id", mSource, mPackageManager);
when(mPackageManager.getInstalledApplicationsAsUser(anyInt(), anyInt())).thenReturn(mInfo);
UserInfo info = new UserInfo();
mUsers = new ArrayList<>();
mUsers.add(info);
when(mUserManager.getUsers()).thenReturn(mUsers);
}
@Test
@@ -66,20 +84,22 @@ public class StorageAsyncLoaderTest {
addPackage(1001, 0, 1, 10, ApplicationInfo.CATEGORY_UNDEFINED);
addPackage(1002, 0, 100, 1000, ApplicationInfo.CATEGORY_UNDEFINED);
StorageAsyncLoader.AppsStorageResult result = mLoader.loadInBackground();
SparseArray<StorageAsyncLoader.AppsStorageResult> result = mLoader.loadInBackground();
assertThat(result.gamesSize).isEqualTo(0L);
assertThat(result.otherAppsSize).isEqualTo(1111L);
assertThat(result.size()).isEqualTo(1);
assertThat(result.get(PRIMARY_USER_ID).gamesSize).isEqualTo(0L);
assertThat(result.get(PRIMARY_USER_ID).otherAppsSize).isEqualTo(1111L);
}
@Test
public void testGamesAreFiltered() throws Exception {
addPackage(1001, 0, 1, 10, ApplicationInfo.CATEGORY_GAME);
StorageAsyncLoader.AppsStorageResult result = mLoader.loadInBackground();
SparseArray<StorageAsyncLoader.AppsStorageResult> result = mLoader.loadInBackground();
assertThat(result.gamesSize).isEqualTo(11L);
assertThat(result.otherAppsSize).isEqualTo(0);
assertThat(result.size()).isEqualTo(1);
assertThat(result.get(PRIMARY_USER_ID).gamesSize).isEqualTo(11L);
assertThat(result.get(PRIMARY_USER_ID).otherAppsSize).isEqualTo(0);
}
@Test
@@ -87,18 +107,37 @@ public class StorageAsyncLoaderTest {
addPackage(1001, 0, 1, 10, ApplicationInfo.CATEGORY_UNDEFINED);
addPackage(1001, 0, 1, 10, ApplicationInfo.CATEGORY_UNDEFINED);
StorageAsyncLoader.AppsStorageResult result = mLoader.loadInBackground();
SparseArray<StorageAsyncLoader.AppsStorageResult> result = mLoader.loadInBackground();
assertThat(result.otherAppsSize).isEqualTo(11L);
assertThat(result.size()).isEqualTo(1);
assertThat(result.get(PRIMARY_USER_ID).otherAppsSize).isEqualTo(11L);
}
@Test
public void testCacheIsIgnored() throws Exception {
addPackage(1001, 100, 1, 10, ApplicationInfo.CATEGORY_UNDEFINED);
StorageAsyncLoader.AppsStorageResult result = mLoader.loadInBackground();
SparseArray<StorageAsyncLoader.AppsStorageResult> result = mLoader.loadInBackground();
assertThat(result.otherAppsSize).isEqualTo(11L);
assertThat(result.size()).isEqualTo(1);
assertThat(result.get(PRIMARY_USER_ID).otherAppsSize).isEqualTo(11L);
}
@Test
public void testMultipleUsers() throws Exception {
UserInfo info = new UserInfo();
info.id = SECONDARY_USER_ID;
mUsers.add(info);
when(mSource.getExternalStorageStats(anyString(), eq(UserHandle.SYSTEM)))
.thenReturn(new StorageStatsSource.ExternalStorageStats(9, 2, 3, 4));
when(mSource.getExternalStorageStats(anyString(), eq(new UserHandle(SECONDARY_USER_ID))))
.thenReturn(new StorageStatsSource.ExternalStorageStats(10, 3, 3, 4));
SparseArray<StorageAsyncLoader.AppsStorageResult> result = mLoader.loadInBackground();
assertThat(result.size()).isEqualTo(2);
assertThat(result.get(PRIMARY_USER_ID).externalStats.totalBytes).isEqualTo(9L);
assertThat(result.get(SECONDARY_USER_ID).externalStats.totalBytes).isEqualTo(10L);
}
private void addPackage(int uid, long cacheSize, long codeSize, long dataSize, int category) {