1 /**
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  * <p>http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * <p>Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11  * express or implied. See the License for the specific language governing permissions and
12  * limitations under the License.
13  */
14 package com.android.settings.deletionhelper;
15 
16 import android.content.ContentResolver;
17 import android.content.Context;
18 import android.provider.Settings;
19 import android.text.format.DateUtils;
20 import android.text.format.Formatter;
21 
22 import androidx.preference.Preference;
23 import androidx.preference.PreferenceScreen;
24 
25 import com.android.settings.R;
26 import com.android.settings.core.PreferenceControllerMixin;
27 import com.android.settingslib.Utils;
28 import com.android.settingslib.core.AbstractPreferenceController;
29 
30 /**
31  * Handles the wall of text which appears below the options in the Storage Management settings drill
32  * down.
33  */
34 public class AutomaticStorageManagerDescriptionPreferenceController
35         extends AbstractPreferenceController implements PreferenceControllerMixin {
36     private static final String KEY_FREED = "freed_bytes";
37 
AutomaticStorageManagerDescriptionPreferenceController(Context context)38     public AutomaticStorageManagerDescriptionPreferenceController(Context context) {
39         super(context);
40     }
41 
42     @Override
isAvailable()43     public boolean isAvailable() {
44         return true;
45     }
46 
47     @Override
getPreferenceKey()48     public String getPreferenceKey() {
49         return KEY_FREED;
50     }
51 
52     @Override
displayPreference(PreferenceScreen screen)53     public void displayPreference(PreferenceScreen screen) {
54         Preference preference = screen.findPreference(getPreferenceKey());
55         final Context context = preference.getContext();
56         ContentResolver cr = context.getContentResolver();
57         long freedBytes =
58                 Settings.Secure.getLong(
59                         cr, Settings.Secure.AUTOMATIC_STORAGE_MANAGER_BYTES_CLEARED, 0);
60         long lastRunMillis =
61                 Settings.Secure.getLong(cr, Settings.Secure.AUTOMATIC_STORAGE_MANAGER_LAST_RUN, 0);
62         if (freedBytes == 0 || lastRunMillis == 0 || !Utils.isStorageManagerEnabled(context)) {
63             preference.setSummary(R.string.automatic_storage_manager_text);
64         } else {
65             preference.setSummary(
66                     context.getString(
67                             R.string.automatic_storage_manager_freed_bytes,
68                             Formatter.formatFileSize(context, freedBytes),
69                             DateUtils.formatDateTime(
70                                     context, lastRunMillis, DateUtils.FORMAT_SHOW_DATE)));
71         }
72     }
73 }
74