DefaultAppPreferenceController.java revision a278962dbc545f69ca3712159d438f2e50dab0a5
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.os.UserHandle; 22import android.os.UserManager; 23import android.support.v7.preference.Preference; 24import android.text.TextUtils; 25import android.util.Log; 26 27import com.android.settings.R; 28import com.android.settings.applications.PackageManagerWrapper; 29import com.android.settings.applications.PackageManagerWrapperImpl; 30import com.android.settings.core.PreferenceController; 31import com.android.settings.widget.GearPreference; 32 33public 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 = null; 53 if (app != null) { 54 defaultAppLabel = app.loadLabel(); 55 } 56 if (!TextUtils.isEmpty(defaultAppLabel)) { 57 preference.setSummary(defaultAppLabel); 58 } else { 59 Log.d(TAG, "No default app"); 60 preference.setSummary(R.string.app_list_preference_none); 61 } 62 mayUpdateGearIcon(app, preference); 63 } 64 65 private void mayUpdateGearIcon(DefaultAppInfo app, Preference preference) { 66 if (!(preference instanceof GearPreference)) { 67 return; 68 } 69 final Intent settingIntent = getSettingIntent(app); 70 if (settingIntent != null) { 71 ((GearPreference) preference).setOnGearClickListener( 72 p -> mContext.startActivity(settingIntent)); 73 } else { 74 ((GearPreference) preference).setOnGearClickListener(null); 75 } 76 } 77 78 protected abstract DefaultAppInfo getDefaultAppInfo(); 79 80 /** 81 * Returns an optional intent that will be launched when clicking "gear" icon. 82 */ 83 protected Intent getSettingIntent(DefaultAppInfo info) { 84 //By default return null. It's up to subclasses to provide logic. 85 return null; 86 } 87} 88