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 com.android.car.settings.common.FragmentController;
27 import com.android.car.settings.common.Logger;
28 import com.android.car.settings.common.ProgressBarPreference;
29 import com.android.settingslib.deviceinfo.StorageManagerVolumeProvider;
30 import com.android.settingslib.deviceinfo.StorageVolumeProvider;
31 
32 /**
33  * Controller which determines the storage for file category in the storage preference screen.
34  */
35 public class StorageFileCategoryPreferenceController extends StorageUsageBasePreferenceController {
36 
37     private static final Logger LOG = new Logger(StorageFileCategoryPreferenceController.class);
38 
39     private StorageVolumeProvider mStorageVolumeProvider;
40 
StorageFileCategoryPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)41     public StorageFileCategoryPreferenceController(Context context,
42             String preferenceKey, FragmentController fragmentController,
43             CarUxRestrictions uxRestrictions) {
44         super(context, preferenceKey, fragmentController, uxRestrictions);
45         StorageManager sm = context.getSystemService(StorageManager.class);
46         mStorageVolumeProvider = new StorageManagerVolumeProvider(sm);
47     }
48 
49     @Override
onCreateInternal()50     protected void onCreateInternal() {
51         super.onCreateInternal();
52         getPreference().setSelectable(
53                 getFilesIntent().resolveActivity(getContext().getPackageManager()) != null);
54     }
55 
56     @Override
calculateCategoryUsage(SparseArray<StorageAsyncLoader.AppsStorageResult> result, long usedSizeBytes)57     protected long calculateCategoryUsage(SparseArray<StorageAsyncLoader.AppsStorageResult> result,
58             long usedSizeBytes) {
59         StorageAsyncLoader.AppsStorageResult data = result.get(UserHandle.myUserId());
60         return data.getExternalStats().totalBytes - data.getExternalStats().audioBytes
61                 - data.getExternalStats().videoBytes - data.getExternalStats().imageBytes
62                 - data.getExternalStats().appBytes;
63     }
64 
65     @Override
handlePreferenceClicked(ProgressBarPreference preference)66     protected boolean handlePreferenceClicked(ProgressBarPreference preference) {
67         int myUserId = UserHandle.myUserId();
68         Intent intent = getFilesIntent();
69         intent.putExtra(Intent.EXTRA_USER_ID, myUserId);
70         if (intent.resolveActivity(getContext().getPackageManager()) != null) {
71             getContext().startActivityAsUser(intent, UserHandle.of(myUserId));
72         } else {
73             LOG.i("No activity found to handle intent: " + intent);
74         }
75         return true;
76     }
77 
getFilesIntent()78     private Intent getFilesIntent() {
79         return mStorageVolumeProvider.findEmulatedForPrivate(getVolumeInfo()).buildBrowseIntent();
80     }
81 }
82