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 import android.os.UserHandle; 22 23 import androidx.annotation.VisibleForTesting; 24 import androidx.preference.Preference; 25 26 import com.android.car.settings.R; 27 import com.android.car.settings.common.FragmentController; 28 import com.android.car.settings.common.PreferenceController; 29 import com.android.car.settings.storage.AppStorageSettingsDetailsFragment; 30 import com.android.settingslib.applications.ApplicationsState; 31 32 import java.util.ArrayList; 33 34 /** Business logic for the storage entry in the application details settings. */ 35 public class StoragePreferenceController extends PreferenceController<Preference> { 36 37 private ApplicationsState mApplicationsState; 38 private ApplicationsState.AppEntry mAppEntry; 39 private ApplicationsState.Session mSession; 40 private String mPackageName; 41 42 @VisibleForTesting 43 final ApplicationsState.Callbacks mApplicationStateCallbacks = 44 new ApplicationsState.Callbacks() { 45 @Override 46 public void onRunningStateChanged(boolean running) { 47 } 48 49 @Override 50 public void onPackageListChanged() { 51 } 52 53 @Override 54 public void onRebuildComplete(ArrayList<ApplicationsState.AppEntry> apps) { 55 } 56 57 @Override 58 public void onPackageIconChanged() { 59 } 60 61 @Override 62 public void onPackageSizeChanged(String packageName) { 63 if (packageName.equals(mPackageName)) { 64 refreshUi(); 65 } 66 } 67 68 @Override 69 public void onAllSizesComputed() { 70 refreshUi(); 71 } 72 73 @Override 74 public void onLauncherInfoChanged() { 75 } 76 77 @Override 78 public void onLoadEntriesCompleted() { 79 } 80 }; 81 StoragePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)82 public StoragePreferenceController(Context context, String preferenceKey, 83 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 84 super(context, preferenceKey, fragmentController, uxRestrictions); 85 } 86 87 @Override getPreferenceType()88 protected Class<Preference> getPreferenceType() { 89 return Preference.class; 90 } 91 92 93 /** Sets the {@link ApplicationsState.AppEntry} which is used to load the app size. */ setAppEntry(ApplicationsState.AppEntry appEntry)94 public StoragePreferenceController setAppEntry(ApplicationsState.AppEntry appEntry) { 95 mAppEntry = appEntry; 96 return this; 97 } 98 99 /** Sets the {@link ApplicationsState} which is used to load the app size. */ setAppState(ApplicationsState applicationsState)100 public StoragePreferenceController setAppState(ApplicationsState applicationsState) { 101 mApplicationsState = applicationsState; 102 return this; 103 } 104 105 /** 106 * Set the packageName, which is used to open the AppStorageSettingsDetailsFragment 107 */ setPackageName(String packageName)108 public StoragePreferenceController setPackageName(String packageName) { 109 mPackageName = packageName; 110 return this; 111 } 112 113 @Override checkInitialized()114 protected void checkInitialized() { 115 if (mAppEntry == null || mApplicationsState == null || mPackageName == null) { 116 throw new IllegalStateException( 117 "AppEntry, ApplicationsState and PackageName should be set before calling this " 118 + "function"); 119 } 120 } 121 122 @Override onCreateInternal()123 protected void onCreateInternal() { 124 mSession = mApplicationsState.newSession(mApplicationStateCallbacks); 125 } 126 127 @Override onStartInternal()128 protected void onStartInternal() { 129 mSession.onResume(); 130 } 131 132 @Override onStopInternal()133 protected void onStopInternal() { 134 mSession.onPause(); 135 } 136 137 @Override updateState(Preference preference)138 protected void updateState(Preference preference) { 139 refreshAppEntry(); 140 if (mAppEntry == null) { 141 getFragmentController().goBack(); 142 } else if (mAppEntry.sizeStr == null) { 143 preference.setSummary( 144 getContext().getString(R.string.memory_calculating_size)); 145 } else { 146 preference.setSummary( 147 getContext().getString(R.string.storage_type_internal, mAppEntry.sizeStr)); 148 } 149 } 150 151 @Override handlePreferenceClicked(Preference preference)152 protected boolean handlePreferenceClicked(Preference preference) { 153 getFragmentController().launchFragment( 154 AppStorageSettingsDetailsFragment.getInstance(mPackageName)); 155 return true; 156 } 157 158 // TODO(b/201351382): Remove after SettingsLib investigation refreshAppEntry()159 private void refreshAppEntry() { 160 mAppEntry = mApplicationsState.getEntry(mPackageName, UserHandle.myUserId()); 161 } 162 } 163