1 /* 2 * Copyright (C) 2015 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.tv.settings.device.apps; 18 19 import static com.android.tv.settings.util.InstrumentationUtils.logEntrySelected; 20 21 import android.app.tvsettings.TvSettingsEnums; 22 import android.content.Context; 23 import android.os.Bundle; 24 import android.text.format.Formatter; 25 26 import androidx.annotation.NonNull; 27 import androidx.leanback.widget.GuidanceStylist; 28 29 import com.android.settingslib.applications.ApplicationsState; 30 import com.android.tv.settings.R; 31 32 public class ClearCachePreference extends AppActionPreference { 33 private boolean mClearingCache; 34 ClearCachePreference(Context context, ApplicationsState.AppEntry entry)35 public ClearCachePreference(Context context, ApplicationsState.AppEntry entry) { 36 super(context, entry); 37 38 refresh(); 39 ConfirmationFragment.prepareArgs(getExtras(), mEntry.info.packageName); 40 } 41 refresh()42 public void refresh() { 43 setTitle(R.string.device_apps_app_management_clear_cache); 44 final Context context = getContext(); 45 setSummary(mClearingCache ? context.getString(R.string.computing_size) 46 : Formatter.formatFileSize(context, mEntry.cacheSize + mEntry.externalCacheSize)); 47 setEnabled(!mClearingCache && mEntry.cacheSize > 0); 48 this.setOnPreferenceClickListener( 49 preference -> { 50 logEntrySelected(TvSettingsEnums.APPS_ALL_APPS_APP_ENTRY_CLEAR_CACHE); 51 return false; 52 }); 53 } 54 setClearingCache(boolean clearingCache)55 public void setClearingCache(boolean clearingCache) { 56 mClearingCache = clearingCache; 57 refresh(); 58 } 59 60 @Override getFragment()61 public String getFragment() { 62 return ConfirmationFragment.class.getName(); 63 } 64 65 public static class ConfirmationFragment extends AppActionPreference.ConfirmationFragment { 66 private static final String ARG_PACKAGE_NAME = "packageName"; 67 prepareArgs(@onNull Bundle args, String packageName)68 private static void prepareArgs(@NonNull Bundle args, String packageName) { 69 args.putString(ARG_PACKAGE_NAME, packageName); 70 } 71 72 @NonNull 73 @Override onCreateGuidance(Bundle savedInstanceState)74 public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) { 75 final AppManagementFragment fragment = (AppManagementFragment) getTargetFragment(); 76 return new GuidanceStylist.Guidance( 77 getString(R.string.device_apps_app_management_clear_cache), 78 null, 79 fragment.getAppName(), 80 fragment.getAppIcon()); 81 } 82 83 @Override onOk()84 public void onOk() { 85 final AppManagementFragment fragment = 86 (AppManagementFragment) getTargetFragment(); 87 fragment.clearCache(); 88 } 89 } 90 } 91