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.support.v7.preference.Preference; 21 import android.support.v7.preference.Preference.OnPreferenceChangeListener; 22 import android.support.v7.preference.CheckBoxPreference; 23 import android.util.AttributeSet; 24 25 import com.android.storagemanager.deletionhelper.DeletionType; 26 import com.android.storagemanager.R; 27 28 /** 29 * Preference to handle the deletion of various data types in the Deletion Helper. 30 */ 31 public abstract class DeletionPreference extends CheckBoxPreference implements 32 DeletionType.FreeableChangedListener, OnPreferenceChangeListener { 33 private DeletionType.FreeableChangedListener mListener; 34 private long mFreeableBytes; 35 private int mFreeableItems; 36 private DeletionType mDeletionService; 37 38 public DeletionPreference(Context context, AttributeSet attrs) { 39 super(context, attrs); 40 setLayoutResource(R.layout.deletion_preference); 41 setOnPreferenceChangeListener(this); 42 setPersistent(false); 43 } 44 45 /** 46 * Returns the number of bytes which can be cleared by the deletion service. 47 * @return The number of bytes. 48 */ 49 public long getFreeableBytes() { 50 return isChecked() ? mFreeableBytes : 0; 51 } 52 53 /** 54 * Register a listener to be called back on when the freeable bytes have changed. 55 * @param listener The callback listener. 56 */ 57 public void registerFreeableChangedListener(DeletionType.FreeableChangedListener listener) { 58 mListener = listener; 59 } 60 61 /** 62 * Registers a deletion service to update the preference's information. 63 * @param deletionService A photo/video deletion service. 64 */ 65 public void registerDeletionService(DeletionType deletionService) { 66 mDeletionService = deletionService; 67 if (mDeletionService != null) { 68 mDeletionService.registerFreeableChangedListener(this); 69 } 70 } 71 72 /** 73 * Returns the deletion service powering the preference. 74 * @return The deletion service. 75 */ 76 public DeletionType getDeletionService() { 77 return mDeletionService; 78 } 79 80 @Override 81 public void onFreeableChanged(int numItems, long freeableBytes) { 82 mFreeableItems = numItems; 83 mFreeableBytes = freeableBytes; 84 maybeUpdateListener(); 85 } 86 87 @Override 88 public boolean onPreferenceChange(Preference preference, Object newValue) { 89 setChecked((boolean) newValue); 90 maybeUpdateListener(); 91 return true; 92 } 93 94 private void maybeUpdateListener() { 95 if (mListener != null) { 96 mListener.onFreeableChanged(mFreeableItems, getFreeableBytes()); 97 } 98 } 99 } 100