DefaultAppPreferenceController.java revision 1c3a4de93dc1a1a69c50714582faad441e4a626f
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.defaultapps;
18
19import android.content.Context;
20import android.content.Intent;
21import android.graphics.drawable.Drawable;
22import android.os.UserHandle;
23import android.os.UserManager;
24import android.support.v7.preference.Preference;
25import android.text.TextUtils;
26import android.util.Log;
27
28import com.android.settings.R;
29import com.android.settings.Utils;
30import com.android.settings.core.PreferenceControllerMixin;
31import com.android.settings.widget.GearPreference;
32import com.android.settingslib.TwoTargetPreference;
33import com.android.settingslib.core.AbstractPreferenceController;
34import com.android.settingslib.wrapper.PackageManagerWrapper;
35
36public abstract class DefaultAppPreferenceController extends AbstractPreferenceController
37        implements PreferenceControllerMixin {
38
39    private static final String TAG = "DefaultAppPrefControl";
40
41    protected final PackageManagerWrapper mPackageManager;
42    protected final UserManager mUserManager;
43
44    protected int mUserId;
45
46    public DefaultAppPreferenceController(Context context) {
47        super(context);
48        mPackageManager = new PackageManagerWrapper(context.getPackageManager());
49        mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
50        mUserId = UserHandle.myUserId();
51    }
52
53    @Override
54    public void updateState(Preference preference) {
55        final DefaultAppInfo app = getDefaultAppInfo();
56        CharSequence defaultAppLabel = getDefaultAppLabel();
57        if (preference instanceof TwoTargetPreference) {
58            // For use small icon because we are displaying an app preference.
59            // We only need to do this for TwoTargetPreference because the other prefs are
60            // already using AppPreference so their icon is already normalized.
61            ((TwoTargetPreference) preference).setUseSmallIcon(true);
62        }
63        if (!TextUtils.isEmpty(defaultAppLabel)) {
64            preference.setSummary(defaultAppLabel);
65            Utils.setSafeIcon(preference, getDefaultAppIcon());
66        } else {
67            Log.d(TAG, "No default app");
68            preference.setSummary(R.string.app_list_preference_none);
69            preference.setIcon(null);
70        }
71        mayUpdateGearIcon(app, preference);
72    }
73
74    private void mayUpdateGearIcon(DefaultAppInfo app, Preference preference) {
75        if (!(preference instanceof GearPreference)) {
76            return;
77        }
78        final Intent settingIntent = getSettingIntent(app);
79        if (settingIntent != null) {
80            ((GearPreference) preference).setOnGearClickListener(
81                    p -> mContext.startActivity(settingIntent));
82        } else {
83            ((GearPreference) preference).setOnGearClickListener(null);
84        }
85    }
86
87    protected abstract DefaultAppInfo getDefaultAppInfo();
88
89    /**
90     * Returns an optional intent that will be launched when clicking "gear" icon.
91     */
92    protected Intent getSettingIntent(DefaultAppInfo info) {
93        //By default return null. It's up to subclasses to provide logic.
94        return null;
95    }
96
97    public Drawable getDefaultAppIcon() {
98        if (!isAvailable()) {
99            return null;
100        }
101        final DefaultAppInfo app = getDefaultAppInfo();
102        if (app != null) {
103            return app.loadIcon();
104        }
105        return null;
106    }
107
108    public CharSequence getDefaultAppLabel() {
109        if (!isAvailable()) {
110            return null;
111        }
112        final DefaultAppInfo app = getDefaultAppInfo();
113        if (app != null) {
114            return app.loadLabel();
115        }
116        return null;
117    }
118}
119