Use the fast track calculation for apps.

This replaces the calculation of the apps category
and add in a calculation for the games category using
the faster GID calculation. This should result in app
sizes loading roughly 10 times after.

Bug: 34204877
Test: Settings unit & robo tests
Change-Id: I78044a8d50f695f8c0a7e04183030232a9719260
This commit is contained in:
Daniel Nishi
2017-01-24 17:36:43 -08:00
parent d1172942ee
commit 0905600b7a
6 changed files with 279 additions and 15 deletions

View File

@@ -17,7 +17,9 @@
package com.android.settings.deviceinfo;
import android.content.Context;
import android.content.Loader;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.storage.StorageManager;
import android.os.storage.VolumeInfo;
import android.provider.SearchIndexableResource;
@@ -27,6 +29,7 @@ import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.core.PreferenceController;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.deviceinfo.storage.AppsAsyncLoader;
import com.android.settings.deviceinfo.storage.StorageItemPreferenceController;
import com.android.settings.deviceinfo.storage.StorageSummaryDonutPreferenceController;
import com.android.settings.overlay.FeatureFactory;
@@ -42,6 +45,7 @@ import java.util.List;
public class StorageDashboardFragment extends DashboardFragment {
private static final String TAG = "StorageDashboardFrag";
private static final int APPS_JOB_ID = 0;
private VolumeInfo mVolume;
@@ -53,6 +57,12 @@ public class StorageDashboardFragment extends DashboardFragment {
&& mVolume.isMountedReadable();
}
@Override
public void onResume() {
super.onResume();
getLoaderManager().initLoader(APPS_JOB_ID, Bundle.EMPTY, mPreferenceController);
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

View File

@@ -0,0 +1,86 @@
/*
* 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.ApplicationInfo;
import android.util.ArraySet;
import com.android.settings.applications.PackageManagerWrapper;
import com.android.settings.utils.AsyncLoader;
import java.util.List;
/**
* AppsAsyncLoader is a Loader which loads app storage information and categories it by the app's
* specified categorization.
*/
public class AppsAsyncLoader extends AsyncLoader<AppsAsyncLoader.AppsStorageResult> {
private int mUserId;
private String mUuid;
private StorageStatsSource mStatsManager;
private PackageManagerWrapper mPackageManager;
public AppsAsyncLoader(Context context, int userId, String uuid, StorageStatsSource source,
PackageManagerWrapper pm) {
super(context);
mUserId = userId;
mUuid = uuid;
mStatsManager = source;
mPackageManager = pm;
}
@Override
public AppsStorageResult loadInBackground() {
return loadApps();
}
private AppsStorageResult loadApps() {
AppsStorageResult result = new AppsStorageResult();
ArraySet<Integer> seenUid = new ArraySet<>(); // some apps share a uid
List<ApplicationInfo> applicationInfos =
mPackageManager.getInstalledApplicationsAsUser(0, mUserId);
int size = applicationInfos.size();
for (int i = 0; i < size; i++) {
ApplicationInfo app = applicationInfos.get(i);
if (seenUid.contains(app.uid)) {
continue;
}
seenUid.add(app.uid);
StorageStatsSource.AppStorageStats stats = mStatsManager.getStatsForUid(mUuid, app.uid);
// Note: This omits cache intentionally -- we are not attributing it to the apps.
long appSize = stats.getCodeBytes() + stats.getDataBytes();
if (app.category == ApplicationInfo.CATEGORY_GAME) {
result.gamesSize += appSize;
} else {
result.otherAppsSize += appSize;
}
}
return result;
}
@Override
protected void onDiscardResult(AppsStorageResult result) {
}
public static class AppsStorageResult {
public long gamesSize;
public long otherAppsSize;
}
}

View File

@@ -17,9 +17,12 @@
package com.android.settings.deviceinfo.storage;
import android.app.Fragment;
import android.app.LoaderManager;
import android.app.usage.StorageStatsManager;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.Loader;
import android.os.Bundle;
import android.os.Environment;
import android.os.UserHandle;
@@ -35,6 +38,7 @@ 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.lifecycle.Lifecycle;
import com.android.settings.core.lifecycle.LifecycleObserver;
@@ -51,10 +55,12 @@ import java.util.HashMap;
* categorization breakdown.
*/
public class StorageItemPreferenceController extends PreferenceController
implements StorageMeasurement.MeasurementReceiver, LifecycleObserver, OnDestroy {
implements StorageMeasurement.MeasurementReceiver, LifecycleObserver, OnDestroy,
LoaderManager.LoaderCallbacks<AppsAsyncLoader.AppsStorageResult> {
private static final String TAG = "StorageItemPreference";
private static final String IMAGE_MIME_TYPE = "image/*";
@VisibleForTesting
static final String PHOTO_KEY = "pref_photos_videos";
@VisibleForTesting
@@ -179,15 +185,6 @@ public class StorageItemPreferenceController extends PreferenceController
mAudioPreference.setStorageSize(audioSize);
}
if (mGamePreference != null) {
mGamePreference.setStorageSize(0);
}
final long appSize = details.appsSize.get(mUserId);
if (mAppPreference != null) {
mAppPreference.setStorageSize(appSize);
}
if (mSystemPreference != null) {
mSystemPreference.setStorageSize(mSystemSize);
}
@@ -216,6 +213,25 @@ public class StorageItemPreferenceController extends PreferenceController
mFilePreference = (StorageItemPreferenceAlternate) screen.findPreference(FILES_KEY);
}
@Override
public Loader<AppsAsyncLoader.AppsStorageResult> onCreateLoader(int id,
Bundle args) {
return new AppsAsyncLoader(mContext, UserHandle.myUserId(), mVolume.fsUuid,
new StorageStatsSource(mContext),
new PackageManagerWrapperImpl(mContext.getPackageManager()));
}
@Override
public void onLoadFinished(Loader<AppsAsyncLoader.AppsStorageResult> loader,
AppsAsyncLoader.AppsStorageResult data) {
mGamePreference.setStorageSize(data.gamesSize);
mAppPreference.setStorageSize(data.otherAppsSize);
}
@Override
public void onLoaderReset(Loader<AppsAsyncLoader.AppsStorageResult> loader) {
}
/**
* Begins an asynchronous storage measurement task for the preferences.
*/

View File

@@ -16,6 +16,7 @@
package com.android.settings.deviceinfo.storage;
import android.app.usage.StorageStats;
import android.app.usage.StorageStatsManager;
import android.content.Context;
import android.os.UserHandle;
@@ -24,14 +25,19 @@ import android.os.UserHandle;
* StorageStatsSource wraps the StorageStatsManager for testability purposes.
*/
public class StorageStatsSource {
private StorageStatsManager mSsm;
private StorageStatsManager mStorageStatsManager;
public StorageStatsSource(Context context) {
mSsm = context.getSystemService(StorageStatsManager.class);
mStorageStatsManager = context.getSystemService(StorageStatsManager.class);
}
public ExternalStorageStats getExternalStorageStats(String volumeUuid, UserHandle user) {
return new ExternalStorageStats(mSsm.queryExternalStatsForUser(volumeUuid, user));
return new ExternalStorageStats(
mStorageStatsManager.queryExternalStatsForUser(volumeUuid, user));
}
public AppStorageStats getStatsForUid(String volumeUuid, int uid) {
return new AppStorageStatsImpl(mStorageStatsManager.queryStatsForUid(volumeUuid, uid));
}
public static class ExternalStorageStats {
@@ -55,4 +61,30 @@ public class StorageStatsSource {
imageBytes = stats.getImageBytes();
}
}
public interface AppStorageStats {
long getCodeBytes();
long getDataBytes();
long getCacheBytes();
}
public static class AppStorageStatsImpl implements AppStorageStats {
private StorageStats mStats;
public AppStorageStatsImpl(StorageStats stats) {
mStats = stats;
}
public long getCodeBytes() {
return mStats.getCodeBytes();
}
public long getDataBytes() {
return mStats.getDataBytes();
}
public long getCacheBytes() {
return mStats.getCacheBytes();
}
}
}