1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.car.settings.storage; 18 19 import android.car.drivingstate.CarUxRestrictions; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.os.UserHandle; 23 import android.os.storage.StorageManager; 24 import android.util.SparseArray; 25 26 import androidx.annotation.VisibleForTesting; 27 28 import com.android.car.settings.common.FragmentController; 29 import com.android.car.settings.common.Logger; 30 import com.android.car.settings.common.ProgressBarPreference; 31 import com.android.settingslib.deviceinfo.StorageManagerVolumeProvider; 32 import com.android.settingslib.deviceinfo.StorageVolumeProvider; 33 34 /** 35 * Controller which determines the storage for file category in the storage preference screen. 36 */ 37 public class StorageFileCategoryPreferenceController extends StorageUsageBasePreferenceController { 38 39 private static final Logger LOG = new Logger(StorageFileCategoryPreferenceController.class); 40 41 private StorageVolumeProvider mStorageVolumeProvider; 42 StorageFileCategoryPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)43 public StorageFileCategoryPreferenceController(Context context, 44 String preferenceKey, FragmentController fragmentController, 45 CarUxRestrictions uxRestrictions) { 46 this(context, preferenceKey, fragmentController, uxRestrictions, 47 new StorageManagerVolumeProvider(context.getSystemService(StorageManager.class))); 48 } 49 50 @VisibleForTesting StorageFileCategoryPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions, StorageVolumeProvider storageVolumeProvider)51 StorageFileCategoryPreferenceController(Context context, 52 String preferenceKey, FragmentController fragmentController, 53 CarUxRestrictions uxRestrictions, StorageVolumeProvider storageVolumeProvider) { 54 super(context, preferenceKey, fragmentController, uxRestrictions); 55 mStorageVolumeProvider = storageVolumeProvider; 56 } 57 58 @Override onCreateInternal()59 protected void onCreateInternal() { 60 super.onCreateInternal(); 61 getPreference().setSelectable( 62 getFilesIntent().resolveActivity(getContext().getPackageManager()) != null); 63 } 64 65 @Override calculateCategoryUsage(SparseArray<StorageAsyncLoader.AppsStorageResult> result, long usedSizeBytes)66 protected long calculateCategoryUsage(SparseArray<StorageAsyncLoader.AppsStorageResult> result, 67 long usedSizeBytes) { 68 StorageAsyncLoader.AppsStorageResult data = result.get(UserHandle.myUserId()); 69 return data.getExternalStats().totalBytes - data.getExternalStats().audioBytes 70 - data.getExternalStats().videoBytes - data.getExternalStats().imageBytes 71 - data.getExternalStats().appBytes; 72 } 73 74 @Override handlePreferenceClicked(ProgressBarPreference preference)75 protected boolean handlePreferenceClicked(ProgressBarPreference preference) { 76 int myUserId = UserHandle.myUserId(); 77 Intent intent = getFilesIntent(); 78 intent.putExtra(Intent.EXTRA_USER_ID, myUserId); 79 if (intent.resolveActivity(getContext().getPackageManager()) != null) { 80 getContext().startActivityAsUser(intent, UserHandle.of(myUserId)); 81 } else { 82 LOG.i("No activity found to handle intent: " + intent); 83 } 84 return true; 85 } 86 getFilesIntent()87 private Intent getFilesIntent() { 88 return mStorageVolumeProvider.findEmulatedForPrivate(getVolumeInfo()).buildBrowseIntent(); 89 } 90 } 91