1 /* 2 * Copyright (C) 2019 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.car.settings.applications.managedomainurls; 18 19 import android.car.drivingstate.CarUxRestrictions; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.pm.PackageManager; 23 import android.content.pm.ResolveInfo; 24 import android.content.pm.verify.domain.DomainVerificationUserState; 25 import android.net.Uri; 26 import android.os.UserHandle; 27 28 import androidx.annotation.VisibleForTesting; 29 import androidx.preference.Preference; 30 31 import com.android.car.settings.common.FragmentController; 32 import com.android.car.settings.common.PreferenceController; 33 import com.android.settingslib.applications.ApplicationsState; 34 35 import java.util.List; 36 37 /** 38 * Shared logic for preference controllers related to app launch settings. 39 * 40 * @param <V> the upper bound on the type of {@link Preference} on which the controller 41 * expects to operate. 42 */ 43 public abstract class AppLaunchSettingsBasePreferenceController<V extends Preference> extends 44 PreferenceController<V> { 45 46 @VisibleForTesting 47 static final Intent sBrowserIntent = new Intent() 48 .setAction(Intent.ACTION_VIEW) 49 .addCategory(Intent.CATEGORY_BROWSABLE) 50 .setData(Uri.parse("http:")); 51 52 protected final PackageManager mPm; 53 private ApplicationsState.AppEntry mAppEntry; 54 private CarDomainVerificationManager mDomainVerificationManager; 55 private CarDomainVerificationManager.DomainVerificationStateChangeListener mStateChangeListener; 56 AppLaunchSettingsBasePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)57 public AppLaunchSettingsBasePreferenceController(Context context, String preferenceKey, 58 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 59 this(context, preferenceKey, fragmentController, uxRestrictions, 60 context.getPackageManager()); 61 } 62 63 @VisibleForTesting AppLaunchSettingsBasePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions, PackageManager packageManager)64 AppLaunchSettingsBasePreferenceController(Context context, String preferenceKey, 65 FragmentController fragmentController, CarUxRestrictions uxRestrictions, 66 PackageManager packageManager) { 67 super(context, preferenceKey, fragmentController, uxRestrictions); 68 mPm = packageManager; 69 mDomainVerificationManager = new CarDomainVerificationManager(context); 70 } 71 72 @Override onCreateInternal()73 protected void onCreateInternal() { 74 super.onCreateInternal(); 75 if (mStateChangeListener != null) { 76 mDomainVerificationManager.addListener(mStateChangeListener); 77 } 78 } 79 80 /** 81 * Sets a {@link CarDomainVerificationManager.DomainVerificationStateChangeListener} to listen 82 * to the DomainVerificationState change. 83 */ setDomainVerificationStateListener( CarDomainVerificationManager.DomainVerificationStateChangeListener listener)84 public void setDomainVerificationStateListener( 85 CarDomainVerificationManager.DomainVerificationStateChangeListener listener) { 86 mStateChangeListener = listener; 87 } 88 89 /** Sets the app entry associated with this settings screen. */ setAppEntry(ApplicationsState.AppEntry entry)90 public void setAppEntry(ApplicationsState.AppEntry entry) { 91 mAppEntry = entry; 92 } 93 94 /** Returns the app entry. */ getAppEntry()95 public ApplicationsState.AppEntry getAppEntry() { 96 return mAppEntry; 97 } 98 99 /** Returns the package name. */ getPackageName()100 public String getPackageName() { 101 return mAppEntry.info.packageName; 102 } 103 104 /** 105 * Returns the DomainVerificationManager. 106 */ getDomainVerificationManager()107 public CarDomainVerificationManager getDomainVerificationManager() { 108 return mDomainVerificationManager; 109 } 110 111 /** Returns the current user id. */ getCurrentUserId()112 protected int getCurrentUserId() { 113 return UserHandle.myUserId(); 114 } 115 116 /** Returns {@code true} if the current package is a browser app. */ isBrowserApp()117 protected boolean isBrowserApp() { 118 sBrowserIntent.setPackage(getPackageName()); 119 List<ResolveInfo> list = mPm.queryIntentActivitiesAsUser(sBrowserIntent, 120 PackageManager.MATCH_ALL, getCurrentUserId()); 121 for (ResolveInfo info : list) { 122 if (info.activityInfo != null && info.handleAllWebDataURI) { 123 return true; 124 } 125 } 126 return false; 127 } 128 129 /** 130 * Return whether the link handling is enabled. 131 */ isLinkHandlingEnabled()132 public boolean isLinkHandlingEnabled() { 133 DomainVerificationUserState domainVerificationUserState = 134 getDomainVerificationManager().getDomainVerificationUserState(getPackageName()); 135 return domainVerificationUserState != null 136 ? domainVerificationUserState.isLinkHandlingAllowed() : false; 137 } 138 139 /** Get the number of the specified links */ getLinksNumber(@omainVerificationUserState.DomainState int state)140 public int getLinksNumber(@DomainVerificationUserState.DomainState int state) { 141 final List<String> linkList = mDomainVerificationManager.getLinksList(getPackageName(), 142 state); 143 if (linkList == null) { 144 return 0; 145 } 146 return linkList.size(); 147 } 148 } 149