1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.settings.applications.appinfo; 16 17 import android.content.Context; 18 import android.os.Bundle; 19 import android.os.UserManager; 20 import android.support.v7.preference.Preference; 21 import android.text.TextUtils; 22 23 import com.android.internal.logging.nano.MetricsProto; 24 import com.android.settings.R; 25 import com.android.settings.SettingsActivity; 26 import com.android.settings.applications.DefaultAppSettings; 27 import com.android.settings.core.BasePreferenceController; 28 import com.android.settings.core.SubSettingLauncher; 29 30 /* 31 * Abstract base controller for the default app shortcut preferences that launches the default app 32 * settings with the corresponding default app highlighted. 33 */ 34 public abstract class DefaultAppShortcutPreferenceControllerBase extends BasePreferenceController { 35 36 protected final String mPackageName; 37 DefaultAppShortcutPreferenceControllerBase(Context context, String preferenceKey, String packageName)38 public DefaultAppShortcutPreferenceControllerBase(Context context, String preferenceKey, 39 String packageName) { 40 super(context, preferenceKey); 41 mPackageName = packageName; 42 } 43 44 @Override getAvailabilityStatus()45 public int getAvailabilityStatus() { 46 if (UserManager.get(mContext).isManagedProfile()) { 47 return DISABLED_FOR_USER; 48 } 49 return hasAppCapability() ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 50 } 51 52 @Override getSummary()53 public CharSequence getSummary() { 54 int summaryResId = isDefaultApp() ? R.string.yes : R.string.no; 55 return mContext.getText(summaryResId); 56 } 57 58 @Override handlePreferenceTreeClick(Preference preference)59 public boolean handlePreferenceTreeClick(Preference preference) { 60 if (TextUtils.equals(mPreferenceKey, preference.getKey())) { 61 final Bundle bundle = new Bundle(); 62 bundle.putString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY, mPreferenceKey); 63 new SubSettingLauncher(mContext) 64 .setDestination(DefaultAppSettings.class.getName()) 65 .setArguments(bundle) 66 .setTitle(R.string.configure_apps) 67 .setSourceMetricsCategory(MetricsProto.MetricsEvent.VIEW_UNKNOWN) 68 .launch(); 69 return true; 70 } 71 return false; 72 } 73 74 /** 75 * Check whether the app has the default app capability 76 * 77 * @return true if the app has the default app capability 78 */ hasAppCapability()79 protected abstract boolean hasAppCapability(); 80 81 /** 82 * Check whether the app is the default app 83 * 84 * @return true if the app is the default app 85 */ isDefaultApp()86 protected abstract boolean isDefaultApp(); 87 88 } 89