Home | History | Annotate | Download | only in deletionhelper
      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.support.v7.preference.Preference;
     20 import android.support.v7.preference.PreferenceScreen;
     21 import android.text.format.DateUtils;
     22 import android.text.format.Formatter;
     23 
     24 import com.android.settings.R;
     25 import com.android.settings.core.PreferenceControllerMixin;
     26 import com.android.settingslib.Utils;
     27 import com.android.settingslib.core.AbstractPreferenceController;
     28 
     29 /**
     30  * Handles the wall of text which appears below the options in the Storage Management settings drill
     31  * down.
     32  */
     33 public class AutomaticStorageManagerDescriptionPreferenceController
     34         extends AbstractPreferenceController implements PreferenceControllerMixin {
     35     private static final String KEY_FREED = "freed_bytes";
     36 
     37     public AutomaticStorageManagerDescriptionPreferenceController(Context context) {
     38         super(context);
     39     }
     40 
     41     @Override
     42     public boolean isAvailable() {
     43         return true;
     44     }
     45 
     46     @Override
     47     public String getPreferenceKey() {
     48         return KEY_FREED;
     49     }
     50 
     51     @Override
     52     public void displayPreference(PreferenceScreen screen) {
     53         Preference preference = screen.findPreference(getPreferenceKey());
     54         final Context context = preference.getContext();
     55         ContentResolver cr = context.getContentResolver();
     56         long freedBytes =
     57                 Settings.Secure.getLong(
     58                         cr, Settings.Secure.AUTOMATIC_STORAGE_MANAGER_BYTES_CLEARED, 0);
     59         long lastRunMillis =
     60                 Settings.Secure.getLong(cr, Settings.Secure.AUTOMATIC_STORAGE_MANAGER_LAST_RUN, 0);
     61         if (freedBytes == 0 || lastRunMillis == 0 || !Utils.isStorageManagerEnabled(context)) {
     62             preference.setSummary(R.string.automatic_storage_manager_text);
     63         } else {
     64             preference.setSummary(
     65                     context.getString(
     66                             R.string.automatic_storage_manager_freed_bytes,
     67                             Formatter.formatFileSize(context, freedBytes),
     68                             DateUtils.formatDateTime(
     69                                     context, lastRunMillis, DateUtils.FORMAT_SHOW_DATE)));
     70         }
     71     }
     72 }
     73