1 /* 2 * Copyright (C) 2016 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.storagemanager.deletionhelper; 18 19 import android.content.Context; 20 import android.os.Handler; 21 import androidx.preference.Preference; 22 import androidx.preference.PreferenceViewHolder; 23 import android.util.AttributeSet; 24 import android.text.format.Formatter; 25 26 import android.view.View; 27 import com.android.internal.logging.MetricsLogger; 28 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 29 import com.android.storagemanager.R; 30 31 /** 32 * Preference to handle the deletion of photos and videos in the Deletion Helper. 33 */ 34 public class PhotosDeletionPreference extends DeletionPreference { 35 public static final int DAYS_TO_KEEP_DEFAULT = 30; 36 public static final int DAYS_TO_KEEP_MIN = 0; 37 private int mDaysToKeep; 38 private boolean mLoaded; 39 PhotosDeletionPreference(Context context, AttributeSet attrs)40 public PhotosDeletionPreference(Context context, AttributeSet attrs) { 41 super(context, attrs); 42 mDaysToKeep = DAYS_TO_KEEP_DEFAULT; 43 updatePreferenceText(0, 0); 44 setTitle(R.string.deletion_helper_photos_loading_title); 45 setSummary(R.string.deletion_helper_photos_loading_summary); 46 } 47 48 /** 49 * Updates the title and summary of the preference with fresh information. 50 */ updatePreferenceText(int items, long bytes)51 public void updatePreferenceText(int items, long bytes) { 52 Context context = getContext(); 53 setTitle(context.getString(R.string.deletion_helper_photos_title)); 54 mLoaded = true; 55 setSummary( 56 context.getString( 57 R.string.deletion_helper_photos_summary, 58 Formatter.formatFileSize(context, bytes), 59 mDaysToKeep)); 60 } 61 setDaysToKeep(int daysToKeep)62 public void setDaysToKeep(int daysToKeep) { 63 mDaysToKeep = daysToKeep; 64 updatePreferenceText(0, 0); 65 } 66 67 @Override onFreeableChanged(int items, long bytes)68 public void onFreeableChanged(int items, long bytes) { 69 // Because these operations may cause UI churn, we need to ensure they run on the main 70 // thread. 71 new Handler(getContext().getMainLooper()).post(new Runnable() { 72 @Override 73 public void run() { 74 PhotosDeletionPreference.super.onFreeableChanged(items, bytes); 75 updatePreferenceText(items, bytes); 76 } 77 }); 78 } 79 80 @Override onPreferenceChange(Preference preference, Object newValue)81 public boolean onPreferenceChange(Preference preference, Object newValue) { 82 boolean checked = (boolean) newValue; 83 MetricsLogger.action(getContext(), MetricsEvent.ACTION_DELETION_SELECTION_PHOTOS, checked); 84 return super.onPreferenceChange(preference, newValue); 85 } 86 87 @Override onBindViewHolder(PreferenceViewHolder holder)88 public void onBindViewHolder(PreferenceViewHolder holder) { 89 super.onBindViewHolder(holder); 90 holder.findViewById(com.android.internal.R.id.icon).setVisibility(View.GONE); 91 } 92 isLoaded()93 public boolean isLoaded() { 94 return mLoaded; 95 } 96 } 97