1 /* 2 * Copyright (C) 2017 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.settings.applications.appinfo; 18 19 import android.content.Context; 20 import android.os.Bundle; 21 import android.text.TextUtils; 22 23 import androidx.preference.Preference; 24 import androidx.preference.PreferenceScreen; 25 26 import com.android.settings.SettingsPreferenceFragment; 27 import com.android.settings.core.BasePreferenceController; 28 import com.android.settingslib.applications.ApplicationsState; 29 30 /* 31 * Abstract base controller for the app detail preferences that refresh the state when the app state 32 * changes and launch a specific detail fragment when the preference is clicked. 33 */ 34 public abstract class AppInfoPreferenceControllerBase extends BasePreferenceController 35 implements AppInfoDashboardFragment.Callback { 36 37 protected AppInfoDashboardFragment mParent; 38 protected Preference mPreference; 39 protected ApplicationsState.AppEntry mAppEntry; 40 41 private final Class<? extends SettingsPreferenceFragment> mDetailFragmentClass; 42 AppInfoPreferenceControllerBase(Context context, String preferenceKey)43 public AppInfoPreferenceControllerBase(Context context, String preferenceKey) { 44 super(context, preferenceKey); 45 mDetailFragmentClass = getDetailFragmentClass(); 46 } 47 48 @Override getAvailabilityStatus()49 public int getAvailabilityStatus() { 50 return AVAILABLE; 51 } 52 53 @Override displayPreference(PreferenceScreen screen)54 public void displayPreference(PreferenceScreen screen) { 55 super.displayPreference(screen); 56 mPreference = screen.findPreference(getPreferenceKey()); 57 } 58 59 @Override handlePreferenceTreeClick(Preference preference)60 public boolean handlePreferenceTreeClick(Preference preference) { 61 if (TextUtils.equals(preference.getKey(), mPreferenceKey) && mDetailFragmentClass != null) { 62 AppInfoDashboardFragment.startAppInfoFragment( 63 mDetailFragmentClass, -1, getArguments(), mParent, mParent.getAppEntry()); 64 return true; 65 } 66 return false; 67 } 68 69 @Override refreshUi()70 public void refreshUi() { 71 updateState(mPreference); 72 } 73 setParentFragment(AppInfoDashboardFragment parent)74 public void setParentFragment(AppInfoDashboardFragment parent) { 75 mParent = parent; 76 parent.addToCallbackList(this); 77 mAppEntry = mParent.getAppEntry(); 78 } 79 80 /** 81 * Gets the fragment class to be launched when the preference is clicked. 82 * 83 * @return the fragment to launch 84 */ getDetailFragmentClass()85 protected Class<? extends SettingsPreferenceFragment> getDetailFragmentClass() { 86 return null; 87 } 88 89 /** 90 * Gets any extras that should be passed to the fragment class when the preference is clicked. 91 * 92 * @return a bundle of extras to include in the launch intent 93 */ getArguments()94 protected Bundle getArguments() { 95 return null; 96 } 97 } 98