1 /* 2 * Copyright (C) 2018 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.car.settings.applications; 18 19 import android.car.drivingstate.CarUxRestrictions; 20 import android.content.Context; 21 22 import androidx.preference.Preference; 23 24 import com.android.car.settings.R; 25 import com.android.car.settings.common.FragmentController; 26 import com.android.car.settings.common.PreferenceController; 27 import com.android.car.settings.storage.AppStorageSettingsDetailsFragment; 28 import com.android.settingslib.applications.ApplicationsState; 29 30 /** Business logic for the storage entry in the application details settings. */ 31 public class StoragePreferenceController extends PreferenceController<Preference> { 32 33 private String mPackageName; 34 private ApplicationsState.AppEntry mAppEntry; 35 StoragePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)36 public StoragePreferenceController(Context context, String preferenceKey, 37 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 38 super(context, preferenceKey, fragmentController, uxRestrictions); 39 } 40 41 @Override getPreferenceType()42 protected Class<Preference> getPreferenceType() { 43 return Preference.class; 44 } 45 46 /** Sets the {@link ApplicationsState.AppEntry} which is used to load the app size. */ setAppEntry(ApplicationsState.AppEntry appEntry)47 public StoragePreferenceController setAppEntry(ApplicationsState.AppEntry appEntry) { 48 mAppEntry = appEntry; 49 return this; 50 } 51 /** 52 * Set the packageName, which is used to open the AppStorageSettingsDetailsFragment 53 */ setPackageName(String packageName)54 public StoragePreferenceController setPackageName(String packageName) { 55 mPackageName = packageName; 56 return this; 57 } 58 59 @Override checkInitialized()60 protected void checkInitialized() { 61 if (mAppEntry == null || mPackageName == null) { 62 throw new IllegalStateException( 63 "AppEntry and PackageName should be set before calling this function"); 64 } 65 } 66 67 @Override updateState(Preference preference)68 protected void updateState(Preference preference) { 69 preference.setSummary( 70 getContext().getString(R.string.storage_type_internal, mAppEntry.sizeStr)); 71 } 72 73 @Override handlePreferenceClicked(Preference preference)74 protected boolean handlePreferenceClicked(Preference preference) { 75 getFragmentController().launchFragment( 76 AppStorageSettingsDetailsFragment.getInstance(mPackageName)); 77 return true; 78 } 79 } 80