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.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.TwoTargetPreference; 37 import com.android.settingslib.applications.DefaultAppInfo; 38 import com.android.settingslib.core.AbstractPreferenceController; 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 preference.setSummary(defaultAppLabel); 69 Utils.setSafeIcon(preference, getDefaultAppIcon()); 70 } else { 71 Log.d(TAG, "No default app"); 72 preference.setSummary(R.string.app_list_preference_none); 73 preference.setIcon(null); 74 } 75 mayUpdateGearIcon(app, preference); 76 } 77 mayUpdateGearIcon(DefaultAppInfo app, Preference preference)78 private void mayUpdateGearIcon(DefaultAppInfo app, Preference preference) { 79 if (!(preference instanceof GearPreference)) { 80 return; 81 } 82 final Intent settingIntent = getSettingIntent(app); 83 if (settingIntent != null) { 84 ((GearPreference) preference).setOnGearClickListener( 85 p -> startActivity(settingIntent)); 86 } else { 87 ((GearPreference) preference).setOnGearClickListener(null); 88 } 89 } 90 startActivity(Intent intent)91 protected void startActivity(Intent intent) { 92 mContext.startActivity(intent); 93 } 94 getDefaultAppInfo()95 protected abstract DefaultAppInfo getDefaultAppInfo(); 96 97 /** 98 * Returns an optional intent that will be launched when clicking "gear" icon. 99 */ getSettingIntent(DefaultAppInfo info)100 protected Intent getSettingIntent(DefaultAppInfo info) { 101 //By default return null. It's up to subclasses to provide logic. 102 return null; 103 } 104 getDefaultAppIcon()105 public Drawable getDefaultAppIcon() { 106 if (!isAvailable()) { 107 return null; 108 } 109 final DefaultAppInfo app = getDefaultAppInfo(); 110 if (app != null) { 111 return app.loadIcon(); 112 } 113 return null; 114 } 115 getDefaultAppLabel()116 public CharSequence getDefaultAppLabel() { 117 if (!isAvailable()) { 118 return null; 119 } 120 final DefaultAppInfo app = getDefaultAppInfo(); 121 if (app != null) { 122 return app.loadLabel(); 123 } 124 return null; 125 } 126 } 127