Files
app_Settings/src/com/android/settings/deviceinfo/storage/SecondaryUserController.java
Arc Wang 044c91cc70 Query storage size instead of calculate size of installed APP
From android S, Storage Settings show files of images/videos/audios
category instead of installed APP of each category. So it's
necessary to change the way to calculate size information.

This change also
- StorageItemPreference shows changing storage size units instead
  of fixed GB. It helps UX for categories of only small size files.
- Query media provider for size of Documents and others.
- Query media provider for size of Trash.

Bug: 170918505
Bug: 177892478
Bug: 179871408
Bug: 184379946
Bug: 186077224
Bug: 187128447
Test: atest com.android.settings.deviceinfo
      atest com.android.settings.deviceinfo.storage
      make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.deviceinfo
      make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.deviceinfo.storage
      manual visual
      Click each file category to count files size is the same as
      displayed in Storage Settings.
Change-Id: I37c7b3a4b5860323cb55581b23a90f583f4af216
2021-05-25 23:44:21 +08:00

218 lines
6.9 KiB
Java

/*
* 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.graphics.drawable.Drawable;
import android.os.UserManager;
import android.util.SparseArray;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceScreen;
import com.android.settings.Utils;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.deviceinfo.StorageItemPreference;
import com.android.settingslib.core.AbstractPreferenceController;
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 AbstractPreferenceController implements
PreferenceControllerMixin, StorageAsyncLoader.ResultHandler,
UserIconLoader.UserIconHandler {
// 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 USER_PROFILE_INSERTION_LOCATION = 6;
private static final int SIZE_NOT_SET = -1;
private @NonNull
UserInfo mUser;
private @Nullable
StorageItemPreference mStoragePreference;
private Drawable mUserIcon;
private long mSize;
private long mTotalSizeBytes;
/**
* 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.
* @param isWorkProfileOnly only shows secondary users of work profile.
* (e.g., it should be true in work profile tab)
*/
public static List<AbstractPreferenceController> getSecondaryUserControllers(
Context context, UserManager userManager, boolean isWorkProfileOnly) {
List<AbstractPreferenceController> 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.isPrimary()) {
continue;
}
if (Utils.isProfileOf(primaryUser, info)) {
continue;
}
if (isWorkProfileOnly && !info.isManagedProfile()) {
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 AbstractPreferenceController}.
* @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 StorageItemPreference(screen.getContext());
PreferenceGroup group =
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, mTotalSizeBytes);
}
group.setVisible(true);
group.addPreference(mStoragePreference);
maybeSetIcon();
}
}
@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, mTotalSizeBytes);
}
}
/**
* Sets the total size for the preference for the progress bar.
*
* @param totalSizeBytes Total size in bytes.
*/
public void setTotalSize(long totalSizeBytes) {
mTotalSizeBytes = totalSizeBytes;
}
@Override
public void handleResult(SparseArray<StorageAsyncLoader.StorageResult> stats) {
final StorageAsyncLoader.StorageResult result = stats.get(getUser().id);
if (result != null) {
setSize(result.externalStats.totalBytes);
}
}
@Override
public void handleUserIcons(SparseArray<Drawable> fetchedIcons) {
mUserIcon = fetchedIcons.get(mUser.id);
maybeSetIcon();
}
private void maybeSetIcon() {
if (mUserIcon != null && mStoragePreference != null) {
mStoragePreference.setIcon(mUserIcon);
}
}
@VisibleForTesting
static class NoSecondaryUserController extends AbstractPreferenceController implements
PreferenceControllerMixin {
public NoSecondaryUserController(Context context) {
super(context);
}
@Override
public void displayPreference(PreferenceScreen screen) {
final PreferenceGroup group = 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;
}
}
}