| /* |
| * Copyright (C) 2019 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.car.settings.storage; |
| |
| import android.car.drivingstate.CarUxRestrictions; |
| import android.content.Context; |
| import android.content.Intent; |
| import android.os.UserHandle; |
| import android.os.storage.StorageManager; |
| import android.util.SparseArray; |
| |
| import androidx.annotation.VisibleForTesting; |
| |
| import com.android.car.settings.common.FragmentController; |
| import com.android.car.settings.common.Logger; |
| import com.android.car.settings.common.ProgressBarPreference; |
| import com.android.settingslib.deviceinfo.StorageManagerVolumeProvider; |
| import com.android.settingslib.deviceinfo.StorageVolumeProvider; |
| |
| /** |
| * Controller which determines the storage for file category in the storage preference screen. |
| */ |
| public class StorageFileCategoryPreferenceController extends StorageUsageBasePreferenceController { |
| |
| private static final Logger LOG = new Logger(StorageFileCategoryPreferenceController.class); |
| |
| private StorageVolumeProvider mStorageVolumeProvider; |
| |
| public StorageFileCategoryPreferenceController(Context context, |
| String preferenceKey, FragmentController fragmentController, |
| CarUxRestrictions uxRestrictions) { |
| this(context, preferenceKey, fragmentController, uxRestrictions, |
| new StorageManagerVolumeProvider(context.getSystemService(StorageManager.class))); |
| } |
| |
| @VisibleForTesting |
| StorageFileCategoryPreferenceController(Context context, |
| String preferenceKey, FragmentController fragmentController, |
| CarUxRestrictions uxRestrictions, StorageVolumeProvider storageVolumeProvider) { |
| super(context, preferenceKey, fragmentController, uxRestrictions); |
| mStorageVolumeProvider = storageVolumeProvider; |
| } |
| |
| @Override |
| protected void onCreateInternal() { |
| super.onCreateInternal(); |
| getPreference().setSelectable( |
| getFilesIntent().resolveActivity(getContext().getPackageManager()) != null); |
| } |
| |
| @Override |
| protected long calculateCategoryUsage(SparseArray<StorageAsyncLoader.AppsStorageResult> result, |
| long usedSizeBytes) { |
| StorageAsyncLoader.AppsStorageResult data = result.get(UserHandle.myUserId()); |
| return data.getExternalStats().totalBytes - data.getExternalStats().audioBytes |
| - data.getExternalStats().videoBytes - data.getExternalStats().imageBytes |
| - data.getExternalStats().appBytes; |
| } |
| |
| @Override |
| protected boolean handlePreferenceClicked(ProgressBarPreference preference) { |
| int myUserId = UserHandle.myUserId(); |
| Intent intent = getFilesIntent(); |
| intent.putExtra(Intent.EXTRA_USER_ID, myUserId); |
| if (intent.resolveActivity(getContext().getPackageManager()) != null) { |
| getContext().startActivityAsUser(intent, UserHandle.of(myUserId)); |
| } else { |
| LOG.i("No activity found to handle intent: " + intent); |
| } |
| return true; |
| } |
| |
| private Intent getFilesIntent() { |
| return mStorageVolumeProvider.findEmulatedForPrivate(getVolumeInfo()).buildBrowseIntent(); |
| } |
| } |