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