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.applications.PackageManagerWrapper; 30import com.android.settings.applications.PackageManagerWrapperImpl; 31import com.android.settings.core.PreferenceControllerMixin; 32import com.android.settings.widget.GearPreference; 33import com.android.settingslib.core.AbstractPreferenceController; 34 35public abstract class DefaultAppPreferenceController extends AbstractPreferenceController 36 implements PreferenceControllerMixin { 37 38 private static final String TAG = "DefaultAppPrefControl"; 39 40 protected final PackageManagerWrapper mPackageManager; 41 protected final UserManager mUserManager; 42 43 protected int mUserId; 44 45 public DefaultAppPreferenceController(Context context) { 46 super(context); 47 mPackageManager = new PackageManagerWrapperImpl(context.getPackageManager()); 48 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 49 mUserId = UserHandle.myUserId(); 50 } 51 52 @Override 53 public void updateState(Preference preference) { 54 final DefaultAppInfo app = getDefaultAppInfo(); 55 CharSequence defaultAppLabel = getDefaultAppLabel(); 56 if (!TextUtils.isEmpty(defaultAppLabel)) { 57 preference.setSummary(defaultAppLabel); 58 preference.setIcon(getDefaultAppIcon()); 59 } else { 60 Log.d(TAG, "No default app"); 61 preference.setSummary(R.string.app_list_preference_none); 62 preference.setIcon(null); 63 } 64 mayUpdateGearIcon(app, preference); 65 } 66 67 private void mayUpdateGearIcon(DefaultAppInfo app, Preference preference) { 68 if (!(preference instanceof GearPreference)) { 69 return; 70 } 71 final Intent settingIntent = getSettingIntent(app); 72 if (settingIntent != null) { 73 ((GearPreference) preference).setOnGearClickListener( 74 p -> mContext.startActivity(settingIntent)); 75 } else { 76 ((GearPreference) preference).setOnGearClickListener(null); 77 } 78 } 79 80 protected abstract DefaultAppInfo getDefaultAppInfo(); 81 82 /** 83 * Returns an optional intent that will be launched when clicking "gear" icon. 84 */ 85 protected Intent getSettingIntent(DefaultAppInfo info) { 86 //By default return null. It's up to subclasses to provide logic. 87 return null; 88 } 89 90 public Drawable getDefaultAppIcon() { 91 if (!isAvailable()) { 92 return null; 93 } 94 final DefaultAppInfo app = getDefaultAppInfo(); 95 if (app != null) { 96 return app.loadIcon(); 97 } 98 return null; 99 } 100 101 public CharSequence getDefaultAppLabel() { 102 if (!isAvailable()) { 103 return null; 104 } 105 final DefaultAppInfo app = getDefaultAppInfo(); 106 if (app != null) { 107 return app.loadLabel(); 108 } 109 return null; 110 } 111} 112