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.widget.TwoTargetPreference.ICON_SIZE_MEDIUM; 20 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.PackageManager; 24 import android.graphics.drawable.Drawable; 25 import android.os.UserHandle; 26 import android.os.UserManager; 27 import android.text.TextUtils; 28 import android.util.Log; 29 30 import androidx.annotation.Nullable; 31 import androidx.preference.Preference; 32 33 import com.android.settings.R; 34 import com.android.settings.Utils; 35 import com.android.settings.core.PreferenceControllerMixin; 36 import com.android.settings.widget.GearPreference; 37 import com.android.settingslib.applications.DefaultAppInfo; 38 import com.android.settingslib.core.AbstractPreferenceController; 39 import com.android.settingslib.widget.TwoTargetPreference; 40 41 public abstract class DefaultAppPreferenceController extends AbstractPreferenceController 42 implements PreferenceControllerMixin { 43 44 private static final String TAG = "DefaultAppPrefControl"; 45 46 protected final PackageManager mPackageManager; 47 protected final UserManager mUserManager; 48 49 protected int mUserId; 50 DefaultAppPreferenceController(Context context)51 public DefaultAppPreferenceController(Context context) { 52 super(context); 53 mPackageManager = context.getPackageManager(); 54 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 55 mUserId = UserHandle.myUserId(); 56 } 57 58 @Override updateState(Preference preference)59 public void updateState(Preference preference) { 60 final DefaultAppInfo app = getDefaultAppInfo(); 61 CharSequence defaultAppLabel = getDefaultAppLabel(); 62 if (preference instanceof TwoTargetPreference) { 63 // For use small icon because we are displaying an app preference. 64 // We only need to do this for TwoTargetPreference because the other prefs are 65 // already using AppPreference so their icon is already normalized. 66 ((TwoTargetPreference) preference).setIconSize(ICON_SIZE_MEDIUM); 67 } 68 if (!TextUtils.isEmpty(defaultAppLabel)) { 69 if (showLabelAsTitle() && showAppSummary()) { 70 preference.setTitle(defaultAppLabel); 71 preference.setSummary(getDefaultAppSummary()); 72 } else if (showLabelAsTitle()) { 73 preference.setTitle(defaultAppLabel); 74 } else { 75 preference.setSummary(defaultAppLabel); 76 } 77 78 preference.setIcon(Utils.getSafeIcon(getDefaultAppIcon())); 79 } else { 80 Log.d(TAG, "No default app"); 81 if (showLabelAsTitle()) { 82 preference.setTitle(R.string.app_list_preference_none); 83 preference.setSummary(null); 84 } else { 85 preference.setSummary(R.string.app_list_preference_none); 86 } 87 preference.setIcon(null); 88 } 89 mayUpdateGearIcon(app, preference); 90 } 91 mayUpdateGearIcon(DefaultAppInfo app, Preference preference)92 private void mayUpdateGearIcon(DefaultAppInfo app, Preference preference) { 93 if (!(preference instanceof GearPreference)) { 94 return; 95 } 96 final Intent settingIntent = getSettingIntent(app); 97 if (settingIntent != null) { 98 ((GearPreference) preference).setOnGearClickListener(p -> startActivity(settingIntent)); 99 } else { 100 ((GearPreference) preference).setOnGearClickListener(null); 101 } 102 } 103 startActivity(Intent intent)104 protected void startActivity(Intent intent) { 105 mContext.startActivity(intent); 106 } 107 getDefaultAppInfo()108 protected abstract DefaultAppInfo getDefaultAppInfo(); 109 110 /** Returns an optional intent that will be launched when clicking "gear" icon. */ getSettingIntent(DefaultAppInfo info)111 protected Intent getSettingIntent(DefaultAppInfo info) { 112 // By default return null. It's up to subclasses to provide logic. 113 return null; 114 } 115 116 /** Whether to show the default app label as the title, instead of as the summary. */ showLabelAsTitle()117 protected boolean showLabelAsTitle() { 118 return false; 119 } 120 121 /** Whether to show the app summary. */ showAppSummary()122 protected boolean showAppSummary() { 123 return false; 124 } 125 getDefaultAppIcon()126 public Drawable getDefaultAppIcon() { 127 if (!isAvailable()) { 128 return null; 129 } 130 final DefaultAppInfo app = getDefaultAppInfo(); 131 if (app != null) { 132 return app.loadIcon(); 133 } 134 return null; 135 } 136 getDefaultAppLabel()137 public CharSequence getDefaultAppLabel() { 138 if (!isAvailable()) { 139 return null; 140 } 141 final DefaultAppInfo app = getDefaultAppInfo(); 142 if (app != null) { 143 return app.loadLabel(); 144 } 145 return null; 146 } 147 getDefaultAppSummary()148 private @Nullable CharSequence getDefaultAppSummary() { 149 if (!isAvailable()) { 150 return null; 151 } 152 final DefaultAppInfo app = getDefaultAppInfo(); 153 if (app != null) { 154 return app.getSummary(); 155 } 156 return null; 157 } 158 } 159