DefaultAppShortcutPreferenceControllerBase.java revision 86daa8f4c6a3ce24917f098f515661a9251e2098
1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 *      http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.settings.applications.appinfo;
16
17import android.app.slice.Slice;
18import android.content.Context;
19import android.os.Bundle;
20import android.os.UserManager;
21import android.support.v7.preference.Preference;
22import android.text.TextUtils;
23
24import com.android.internal.logging.nano.MetricsProto;
25import com.android.settings.R;
26import com.android.settings.SettingsActivity;
27import com.android.settings.Utils;
28import com.android.settings.applications.DefaultAppSettings;
29import com.android.settings.core.BasePreferenceController;
30
31/*
32 * Abstract base controller for the default app shortcut preferences that launches the default app
33 * settings with the corresponding default app highlighted.
34 */
35public abstract class DefaultAppShortcutPreferenceControllerBase extends BasePreferenceController {
36
37    protected final String mPackageName;
38
39    public DefaultAppShortcutPreferenceControllerBase(Context context, String preferenceKey,
40            String packageName) {
41        super(context, preferenceKey);
42        mPackageName = packageName;
43    }
44
45    @Override
46    public int getAvailabilityStatus() {
47        if (UserManager.get(mContext).isManagedProfile()) {
48            return DISABLED_FOR_USER;
49        }
50        return hasAppCapability() ? AVAILABLE : DISABLED_UNSUPPORTED;
51    }
52
53    @Override
54    public Slice getSettingSlice() {
55        return null;
56    }
57
58    @Override
59    public void updateState(Preference preference) {
60        preference.setSummary(isDefaultApp() ? R.string.yes : R.string.no);
61    }
62
63    @Override
64    public boolean handlePreferenceTreeClick(Preference preference) {
65        if (TextUtils.equals(mPreferenceKey, preference.getKey())) {
66            Bundle bundle = new Bundle();
67            bundle.putString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY, mPreferenceKey);
68            Utils.startWithFragment(mContext, DefaultAppSettings.class.getName(), bundle, null, 0,
69                    R.string.configure_apps, null, MetricsProto.MetricsEvent.VIEW_UNKNOWN);
70            return true;
71        }
72        return false;
73    }
74
75    /**
76     * Check whether the app has the default app capability
77     * @return true if the app has the default app capability
78     */
79    protected abstract boolean hasAppCapability();
80
81    /**
82     * Check whether the app is the default app
83     * @return true if the app is the default app
84     */
85    protected abstract boolean isDefaultApp();
86
87}
88