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.appinfo; 18 19 import android.content.Context; 20 import android.content.pm.ApplicationInfo; 21 import android.content.pm.PackageInfo; 22 import android.content.pm.verify.domain.DomainVerificationManager; 23 import android.content.pm.verify.domain.DomainVerificationUserState; 24 import android.os.UserHandle; 25 26 import androidx.annotation.VisibleForTesting; 27 import androidx.preference.Preference; 28 import androidx.preference.PreferenceScreen; 29 30 import com.android.settings.SettingsPreferenceFragment; 31 import com.android.settings.applications.intentpicker.AppLaunchSettings; 32 import com.android.settings.applications.intentpicker.IntentPickerUtils; 33 import com.android.settingslib.R; 34 import com.android.settingslib.applications.AppUtils; 35 import com.android.settingslib.applications.ApplicationsState; 36 37 public class AppOpenByDefaultPreferenceController extends AppInfoPreferenceControllerBase { 38 39 private final DomainVerificationManager mDomainVerificationManager; 40 private String mPackageName; 41 AppOpenByDefaultPreferenceController(Context context, String key)42 public AppOpenByDefaultPreferenceController(Context context, String key) { 43 super(context, key); 44 mDomainVerificationManager = context.getSystemService(DomainVerificationManager.class); 45 } 46 47 /** Set a package name for this controller. */ setPackageName(String packageName)48 public AppOpenByDefaultPreferenceController setPackageName(String packageName) { 49 mPackageName = packageName; 50 return this; 51 } 52 53 @Override displayPreference(PreferenceScreen screen)54 public void displayPreference(PreferenceScreen screen) { 55 super.displayPreference(screen); 56 final ApplicationsState.AppEntry appEntry = mParent.getAppEntry(); 57 if (appEntry == null || appEntry.info == null) { 58 mPreference.setEnabled(false); 59 } else if ((appEntry.info.flags & ApplicationInfo.FLAG_INSTALLED) == 0 60 || !appEntry.info.enabled) { 61 mPreference.setEnabled(false); 62 } 63 } 64 65 @Override updateState(Preference preference)66 public void updateState(Preference preference) { 67 final PackageInfo packageInfo = mParent.getPackageInfo(); 68 if (packageInfo != null && !AppUtils.isInstant(packageInfo.applicationInfo) 69 && !AppUtils.isBrowserApp(mContext, packageInfo.packageName, 70 UserHandle.myUserId())) { 71 preference.setVisible(true); 72 preference.setSummary(getSubtext()); 73 } else { 74 preference.setVisible(false); 75 } 76 } 77 78 @Override getDetailFragmentClass()79 protected Class<? extends SettingsPreferenceFragment> getDetailFragmentClass() { 80 return AppLaunchSettings.class; 81 } 82 83 @VisibleForTesting getSubtext()84 CharSequence getSubtext() { 85 return mContext.getText(isLinkHandlingAllowed() 86 ? R.string.app_link_open_always : R.string.app_link_open_never); 87 } 88 89 @VisibleForTesting isLinkHandlingAllowed()90 boolean isLinkHandlingAllowed() { 91 final DomainVerificationUserState userState = 92 IntentPickerUtils.getDomainVerificationUserState(mDomainVerificationManager, 93 mPackageName); 94 return userState == null ? false : userState.isLinkHandlingAllowed(); 95 } 96 } 97