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