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