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