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 ClearCachePreference(Context context, ApplicationsState.AppEntry entry)31 public ClearCachePreference(Context context, ApplicationsState.AppEntry entry) { 32 super(context, entry); 33 34 refresh(); 35 ConfirmationFragment.prepareArgs(getExtras(), mEntry.info.packageName); 36 } 37 refresh()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 } 44 setClearingCache(boolean clearingCache)45 public void setClearingCache(boolean clearingCache) { 46 mClearingCache = clearingCache; 47 refresh(); 48 } 49 50 @Override getFragment()51 public String getFragment() { 52 return ConfirmationFragment.class.getName(); 53 } 54 55 public static class ConfirmationFragment extends AppActionPreference.ConfirmationFragment { 56 private static final String ARG_PACKAGE_NAME = "packageName"; 57 prepareArgs(@onNull Bundle args, String packageName)58 private static void prepareArgs(@NonNull Bundle args, String packageName) { 59 args.putString(ARG_PACKAGE_NAME, packageName); 60 } 61 62 @NonNull 63 @Override onCreateGuidance(Bundle savedInstanceState)64 public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) { 65 final AppManagementFragment fragment = (AppManagementFragment) getTargetFragment(); 66 return new GuidanceStylist.Guidance( 67 getString(R.string.device_apps_app_management_clear_cache), 68 null, 69 fragment.getAppName(), 70 fragment.getAppIcon()); 71 } 72 73 @Override onOk()74 public void onOk() { 75 final AppManagementFragment fragment = 76 (AppManagementFragment) getTargetFragment(); 77 fragment.clearCache(); 78 } 79 } 80 } 81