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