• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.text.format.Formatter;
22 
23 import com.android.car.settings.common.FragmentController;
24 import com.android.car.settings.common.PreferenceController;
25 import com.android.settingslib.applications.StorageStatsSource;
26 
27 /**
28  * Controller which have the basic logic to determines the storage size details for a particular
29  * application.
30  */
31 public abstract class StorageSizeBasePreferenceController extends
32         PreferenceController<StorageAppDetailPreference> implements
33         AppsStorageStatsManager.Callback {
34 
35     private StorageStatsSource.AppStorageStats mAppStorageStats;
36     private AppsStorageStatsManager mAppsStorageStatsManager;
37     private Context mContext;
38     private boolean mDataCleared = false;
39     private boolean mCachedCleared = false;
40 
StorageSizeBasePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)41     public StorageSizeBasePreferenceController(Context context, String preferenceKey,
42             FragmentController fragmentController,
43             CarUxRestrictions uxRestrictions) {
44         super(context, preferenceKey, fragmentController, uxRestrictions);
45         mContext = context;
46     }
47 
48     @Override
getPreferenceType()49     protected Class<StorageAppDetailPreference> getPreferenceType() {
50         return StorageAppDetailPreference.class;
51     }
52 
53     /**
54      * Calculates the storage size for a application.
55      *
56      * @return size value in bytes.
57      */
getSize()58     protected abstract long getSize();
59 
60     @Override
onCreateInternal()61     protected void onCreateInternal() {
62         if (mAppsStorageStatsManager == null) {
63             return;
64         }
65         mAppsStorageStatsManager.registerListener(this);
66     }
67 
68     /**
69      * Sets the {@link AppsStorageStatsManager} which will be used to register the controller to the
70      * Listener {@link AppsStorageStatsManager.Callback}.
71      */
setAppsStorageStatsManager(AppsStorageStatsManager appsStorageStatsManager)72     public void setAppsStorageStatsManager(AppsStorageStatsManager appsStorageStatsManager) {
73         mAppsStorageStatsManager = appsStorageStatsManager;
74     }
75 
76     @Override
updateState(StorageAppDetailPreference preference)77     protected void updateState(StorageAppDetailPreference preference) {
78         if (mAppStorageStats == null) {
79             return;
80         }
81         preference.setDetailText(getSizeStr(getSize()));
82     }
83 
84     /**
85      * Sets the {@link StorageStatsSource.AppStorageStats} for a particular application.
86      */
setAppStorageStats(StorageStatsSource.AppStorageStats appStorageStats)87     public void setAppStorageStats(StorageStatsSource.AppStorageStats appStorageStats) {
88         mAppStorageStats = appStorageStats;
89     }
90 
91     /**
92      * Gets the {@link StorageStatsSource.AppStorageStats} for a particular application.
93      */
getAppStorageStats()94     public StorageStatsSource.AppStorageStats getAppStorageStats() {
95         return mAppStorageStats;
96     }
97 
isCachedCleared()98     boolean isCachedCleared() {
99         return mCachedCleared;
100     }
101 
isDataCleared()102     boolean isDataCleared() {
103         return mDataCleared;
104     }
105 
getSizeStr(long size)106     private String getSizeStr(long size) {
107         return Formatter.formatFileSize(mContext, size);
108     }
109 
110     @Override
onDataLoaded(StorageStatsSource.AppStorageStats data, boolean cacheCleared, boolean dataCleared)111     public void onDataLoaded(StorageStatsSource.AppStorageStats data, boolean cacheCleared,
112             boolean dataCleared) {
113         //  Sets if user have cleared the cache and should zero the cache bytes.
114         //  When the cache is cleared, the cache directories are recreated. These directories have
115         //  some size, but are empty. We zero this out to best match user expectations.
116         mCachedCleared = cacheCleared;
117 
118         //  Sets if user have cleared data and should zero the data bytes.
119         //  When the data is cleared, the directory are recreated. Directories have some size,
120         //  but are empty. We zero this out to best match user expectations.
121         mDataCleared = dataCleared;
122         refreshUi();
123     }
124 }
125