1 /* 2 * Copyright (C) 2014 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 com.android.tv.settings.R; 20 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.PackageManager; 24 import android.content.pm.IPackageDataObserver; 25 26 /** 27 * Handles clearing an application's cache. 28 */ 29 class CacheClearer { 30 31 interface Listener { 32 void cacheCleared(boolean succeeded); 33 } 34 35 private final Listener mListener; 36 private final AppInfo mAppInfo; 37 private boolean mClearingCache; 38 39 CacheClearer(Listener listener, AppInfo appInfo) { 40 mListener = listener; 41 mAppInfo = appInfo; 42 mClearingCache = false; 43 } 44 45 void clearCache(Context context) { 46 PackageManager packageManager = context.getPackageManager(); 47 48 mClearingCache = true; 49 packageManager.deleteApplicationCacheFiles(mAppInfo.getPackageName(), 50 new IPackageDataObserver.Stub() { 51 public void onRemoveCompleted(final String packageName, 52 final boolean succeeded) { 53 mClearingCache = false; 54 mListener.cacheCleared(succeeded); 55 } 56 }); 57 58 // Send out broadcast to clear canvas disk cache. 59 Intent intent = new Intent(); 60 intent.setAction("com.google.android.canvas.data.ClusterDiskCache.CLEAR_CACHE_APP"); 61 intent.putExtra("packageName", mAppInfo.getPackageName()); 62 context.sendBroadcast(intent); 63 } 64 65 String getCacheSize(Context context) { 66 return (mClearingCache) ? context.getString(R.string.computing_size) 67 : mAppInfo.getCacheSize(); 68 } 69 } 70