Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 63558780 authored by Daniel Nishi's avatar Daniel Nishi Committed by Android (Google) Code Review
Browse files

Merge "Add support for user profiles to the Storage Settings."

parents ee2a238e 9f60f42a
Loading
Loading
Loading
Loading
+20 −10
Original line number Diff line number Diff line
@@ -16,40 +16,50 @@

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="@string/storage_settings">
    android:title="@string/storage_settings"
    android:orderingFromXml="false">
    <com.android.settings.deviceinfo.storage.StorageSummaryDonutPreference
        android:key="pref_summary" />
        android:key="pref_summary"
        android:order="0" />
    <com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate
        android:key="pref_photos_videos"
        android:title="@string/storage_photos_videos">
        android:title="@string/storage_photos_videos"
        android:order="1" >
    </com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate>
    <com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate
        android:key="pref_music_audio"
        android:title="@string/storage_music_audio">
        android:title="@string/storage_music_audio"
        android:order="2" >
    </com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate>
    <com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate
        android:key="pref_games"
        android:title="@string/storage_games">
        android:title="@string/storage_games"
        android:order="3" >
    </com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate>
    <com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate
        android:key="pref_other_apps"
        android:title="@string/storage_other_apps">
        android:title="@string/storage_other_apps"
        android:order="4" >
    </com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate>
    <com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate
        android:key="pref_files"
        android:title="@string/storage_files">
        android:title="@string/storage_files"
        android:order="5" >
    </com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate>
    <com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate
        android:key="pref_system"
        android:title="@string/storage_detail_system">
        android:title="@string/storage_detail_system"
        android:order="100" >
    </com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate>
    <PreferenceCategory
        android:key="pref_secondary_users"
        android:title="@string/storage_other_users" />
        android:title="@string/storage_other_users"
        android:order="200" />
    <Preference
        android:key="manage_storage"
        android:title="@string/storage_menu_manage"
        android:icon="@drawable/ic_settings_storage"
        android:fragment="com.android.settings.deletionhelper.AutomaticStorageManagerSettings">
        android:fragment="com.android.settings.deletionhelper.AutomaticStorageManagerSettings"
        android:order="300" >
    </Preference>
</PreferenceScreen>
 No newline at end of file
+41 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->

<!-- Layout for the storage breakdown for a profile of the primary user. -->
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="@string/storage_settings">
    <com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate
        android:key="pref_photos_videos"
        android:title="@string/storage_photos_videos">
    </com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate>
    <com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate
        android:key="pref_music_audio"
        android:title="@string/storage_music_audio">
    </com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate>
    <com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate
        android:key="pref_games"
        android:title="@string/storage_games">
    </com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate>
    <com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate
        android:key="pref_other_apps"
        android:title="@string/storage_other_apps">
    </com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate>
    <com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate
        android:key="pref_files"
        android:title="@string/storage_files">
    </com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate>
</PreferenceScreen>
+18 −0
Original line number Diff line number Diff line
@@ -65,6 +65,7 @@ import android.os.ServiceManager;
import android.os.UserHandle;
import android.os.UserManager;
import android.os.storage.StorageManager;
import android.os.storage.VolumeInfo;
import android.preference.PreferenceFrameLayout;
import android.provider.ContactsContract.CommonDataKinds;
import android.provider.ContactsContract.Contacts;
@@ -1258,4 +1259,21 @@ public final class Utils extends com.android.settingslib.Utils {
                (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
                        && user.profileGroupId == profile.profileGroupId);
    }

    /**
     * Tries to initalize a volume with the given bundle. If it is a valid, private, and readable
     * {@link VolumeInfo}, it is returned. If it is not valid, null is returned.
     */
    @Nullable
    public static VolumeInfo maybeInitializeVolume(StorageManager sm, Bundle bundle) {
        final String volumeId = bundle.getString(VolumeInfo.EXTRA_VOLUME_ID,
                VolumeInfo.ID_PRIVATE_INTERNAL);
        VolumeInfo volume = sm.findVolumeById(volumeId);
        return isVolumeValid(volume) ? volume : null;
    }

    private static boolean isVolumeValid(VolumeInfo volume) {
        return (volume != null) && (volume.getType() == VolumeInfo.TYPE_PRIVATE)
                && volume.isMountedReadable();
    }
}
+7 −26
Original line number Diff line number Diff line
@@ -25,11 +25,11 @@ 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.Utils;
import com.android.settings.applications.PackageManagerWrapperImpl;
import com.android.settings.applications.UserManagerWrapper;
import com.android.settings.applications.UserManagerWrapperImpl;
@@ -48,7 +48,6 @@ 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
    implements LoaderManager.LoaderCallbacks<SparseArray<StorageAsyncLoader.AppsStorageResult>> {
@@ -61,11 +60,6 @@ public class StorageDashboardFragment extends DashboardFragment
    private StorageItemPreferenceController mPreferenceController;
    private List<PreferenceController> mSecondaryUsers;

    private boolean isVolumeValid() {
        return (mVolume != null) && (mVolume.getType() == VolumeInfo.TYPE_PRIVATE)
                && mVolume.isMountedReadable();
    }

    @Override
    public void onResume() {
        super.onResume();
@@ -101,7 +95,8 @@ public class StorageDashboardFragment extends DashboardFragment
        // Initialize the storage sizes that we can quickly calc.
        final Context context = getActivity();
        StorageManager sm = context.getSystemService(StorageManager.class);
        if (!initializeVolume(sm, getArguments())) {
        mVolume = Utils.maybeInitializeVolume(sm, getArguments());
        if (mVolume == null) {
            getActivity().finish();
            return;
        }
@@ -156,17 +151,6 @@ public class StorageDashboardFragment extends DashboardFragment
        return controllers;
    }

    /**
     * Initializes the volume with a given bundle and returns if the volume is valid.
     */
    @VisibleForTesting
    boolean initializeVolume(StorageManager sm, Bundle bundle) {
        String volumeId = bundle.getString(VolumeInfo.EXTRA_VOLUME_ID,
                VolumeInfo.ID_PRIVATE_INTERNAL);
        mVolume = sm.findVolumeById(volumeId);
        return isVolumeValid();
    }

    /**
     * Updates the secondary user controller sizes.
     */
@@ -174,13 +158,10 @@ public class StorageDashboardFragment extends DashboardFragment
            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);
                }
            if (controller instanceof StorageAsyncLoader.ResultHandler) {
                StorageAsyncLoader.ResultHandler userController =
                        (StorageAsyncLoader.ResultHandler) controller;
                userController.handleResult(stats);
            }
        }
    }
+128 −0
Original line number Diff line number Diff line
/*
 * 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;

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.util.SparseArray;

import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.applications.PackageManagerWrapperImpl;
import com.android.settings.applications.UserManagerWrapperImpl;
import com.android.settings.core.PreferenceController;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.deviceinfo.storage.StorageAsyncLoader;
import com.android.settings.deviceinfo.storage.StorageAsyncLoader.AppsStorageResult;
import com.android.settings.deviceinfo.storage.StorageItemPreferenceController;
import com.android.settingslib.applications.StorageStatsSource;
import com.android.settingslib.deviceinfo.StorageManagerVolumeProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * StorageProfileFragment is a fragment which shows the storage results for a profile of the
 * primary user.
 */
public class StorageProfileFragment extends DashboardFragment
        implements LoaderManager.LoaderCallbacks<SparseArray<AppsStorageResult>> {
    private static final String TAG = "StorageProfileFragment";
    public static final String USER_ID_EXTRA = "userId";
    private static final int APPS_JOB_ID = 0;

    private VolumeInfo mVolume;
    private int mUserId;
    private StorageItemPreferenceController mPreferenceController;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        final Bundle args = getArguments();

        // Initialize the storage sizes that we can quickly calc.
        final Context context = getActivity();
        final StorageManager sm = context.getSystemService(StorageManager.class);
        mVolume = Utils.maybeInitializeVolume(sm, args);
        if (mVolume == null) {
            getActivity().finish();
            return;
        }

        mPreferenceController.setVolume(mVolume);
        mUserId = args.getInt(USER_ID_EXTRA, UserHandle.myUserId());
        mPreferenceController.setUserId(mUserId);
    }

    @Override
    public void onResume() {
        super.onResume();
        getLoaderManager().initLoader(APPS_JOB_ID, Bundle.EMPTY, this);
    }

    @Override
    public int getMetricsCategory() {
        return MetricsProto.MetricsEvent.SETTINGS_STORAGE_PROFILE;
    }

    @Override
    protected String getLogTag() {
        return TAG;
    }

    @Override
    protected int getPreferenceScreenResId() {
        return R.xml.storage_profile_fragment;
    }

    @Override
    protected List<PreferenceController> getPreferenceControllers(Context context) {
        final List<PreferenceController> controllers = new ArrayList<>();
        final StorageManager sm = context.getSystemService(StorageManager.class);
        mPreferenceController = new StorageItemPreferenceController(context, this,
                mVolume, new StorageManagerVolumeProvider(sm));
        controllers.add(mPreferenceController);
        return controllers;
    }

    @Override
    public Loader<SparseArray<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<AppsStorageResult>> loader,
            SparseArray<AppsStorageResult> result) {
        mPreferenceController.onLoadFinished(result.get(mUserId));
    }

    @Override
    public void onLoaderReset(Loader<SparseArray<AppsStorageResult>> loader) {
    }
}
Loading