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 ClearDataPreference extends AppActionPreference { 33 private boolean mClearingData; 34 ClearDataPreference(Context context, ApplicationsState.AppEntry entry)35 public ClearDataPreference(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_data); 44 final Context context = getContext(); 45 setSummary(mClearingData ? context.getString(R.string.computing_size) : 46 Formatter.formatFileSize(context, mEntry.dataSize + mEntry.externalDataSize)); 47 this.setOnPreferenceClickListener( 48 preference -> { 49 logEntrySelected(TvSettingsEnums.APPS_ALL_APPS_APP_ENTRY_CLEAR_DATA); 50 return false; 51 }); 52 } 53 setClearingData(boolean clearingData)54 public void setClearingData(boolean clearingData) { 55 mClearingData = clearingData; 56 refresh(); 57 } 58 59 @Override getFragment()60 public String getFragment() { 61 return ConfirmationFragment.class.getName(); 62 } 63 64 public static class ConfirmationFragment extends AppActionPreference.ConfirmationFragment { 65 private static final String ARG_PACKAGE_NAME = "packageName"; 66 prepareArgs(@onNull Bundle args, String packageName)67 private static void prepareArgs(@NonNull Bundle args, String packageName) { 68 args.putString(ARG_PACKAGE_NAME, packageName); 69 } 70 71 @NonNull 72 @Override onCreateGuidance(Bundle savedInstanceState)73 public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) { 74 final AppManagementFragment fragment = (AppManagementFragment) getTargetFragment(); 75 return new GuidanceStylist.Guidance( 76 getString(R.string.device_apps_app_management_clear_data), 77 getString(R.string.device_apps_app_management_clear_data_desc), 78 fragment.getAppName(), 79 fragment.getAppIcon()); 80 } 81 82 @Override onOk()83 public void onOk() { 84 final AppManagementFragment fragment = 85 (AppManagementFragment) getTargetFragment(); 86 fragment.clearData(); 87 } 88 } 89 } 90