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 androidx.preference.Preference; 21 import androidx.preference.Preference.OnPreferenceChangeListener; 22 import androidx.preference.CheckBoxPreference; 23 import androidx.preference.PreferenceViewHolder; 24 import android.util.AttributeSet; 25 26 import android.view.View; 27 import android.widget.ProgressBar; 28 import android.widget.TextView; 29 import com.android.storagemanager.R; 30 31 /** 32 * Preference to handle the deletion of various data types in the Deletion Helper. 33 */ 34 public abstract class DeletionPreference extends CheckBoxPreference implements 35 DeletionType.FreeableChangedListener, OnPreferenceChangeListener { 36 private DeletionType.FreeableChangedListener mListener; 37 private long mFreeableBytes; 38 private int mFreeableItems; 39 private DeletionType mDeletionService; 40 private TextView mSummary; 41 private View mWidget; 42 private ProgressBar mProgressBar; 43 private boolean mLoaded; 44 DeletionPreference(Context context, AttributeSet attrs)45 public DeletionPreference(Context context, AttributeSet attrs) { 46 super(context, attrs); 47 setLayoutResource(R.layout.deletion_preference); 48 setOnPreferenceChangeListener(this); 49 setPersistent(false); 50 } 51 52 /** 53 * Returns the number of bytes which can be cleared by the deletion service. 54 * 55 * @return The number of bytes. 56 */ getFreeableBytes(boolean countUnchecked)57 public long getFreeableBytes(boolean countUnchecked) { 58 return (isChecked() || countUnchecked) ? mFreeableBytes : 0; 59 } 60 61 /** 62 * Register a listener to be called back on when the freeable bytes have changed. 63 * @param listener The callback listener. 64 */ registerFreeableChangedListener(DeletionType.FreeableChangedListener listener)65 public void registerFreeableChangedListener(DeletionType.FreeableChangedListener listener) { 66 mListener = listener; 67 } 68 69 /** 70 * Registers a deletion service to update the preference's information. 71 * @param deletionService A photo/video deletion service. 72 */ registerDeletionService(DeletionType deletionService)73 public void registerDeletionService(DeletionType deletionService) { 74 mDeletionService = deletionService; 75 if (mDeletionService != null) { 76 mDeletionService.registerFreeableChangedListener(this); 77 } 78 } 79 80 /** 81 * Returns the deletion service powering the preference. 82 * @return The deletion service. 83 */ getDeletionService()84 public DeletionType getDeletionService() { 85 return mDeletionService; 86 } 87 88 @Override onFreeableChanged(int numItems, long freeableBytes)89 public void onFreeableChanged(int numItems, long freeableBytes) { 90 mFreeableItems = numItems; 91 mFreeableBytes = freeableBytes; 92 mLoaded = true; 93 if (mDeletionService != null) { 94 setEnabled(!mDeletionService.isEmpty()); 95 } 96 if (!isEnabled()) { 97 setChecked(false); 98 } 99 if (mWidget != null) { 100 mWidget.setVisibility(View.VISIBLE); 101 } 102 if (mProgressBar != null) { 103 mProgressBar.setVisibility(View.GONE); 104 } 105 maybeUpdateListener(); 106 } 107 108 @Override onBindViewHolder(PreferenceViewHolder holder)109 public void onBindViewHolder(PreferenceViewHolder holder) { 110 super.onBindViewHolder(holder); 111 mSummary = (TextView) holder.findViewById(android.R.id.summary); 112 mProgressBar = (ProgressBar) holder.findViewById(R.id.progress_bar); 113 mProgressBar.setVisibility(mLoaded ? View.GONE : View.VISIBLE); 114 mWidget = holder.findViewById(android.R.id.widget_frame); 115 mWidget.setVisibility(mLoaded ? View.VISIBLE : View.GONE); 116 } 117 118 @Override onPreferenceChange(Preference preference, Object newValue)119 public boolean onPreferenceChange(Preference preference, Object newValue) { 120 setChecked((boolean) newValue); 121 mSummary.setActivated((boolean) newValue); 122 maybeUpdateListener(); 123 return true; 124 } 125 maybeUpdateListener()126 private void maybeUpdateListener() { 127 if (mListener != null) { 128 mListener.onFreeableChanged( 129 mFreeableItems, getFreeableBytes(DeletionHelperSettings.COUNT_CHECKED_ONLY)); 130 } 131 } 132 } 133