1 /* 2 * Copyright (C) 2018 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.packageinstaller.role.model; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.content.pm.PackageManager; 24 import android.os.Process; 25 26 import androidx.annotation.NonNull; 27 28 import java.util.List; 29 import java.util.Objects; 30 31 /** 32 * Specifies a preferred {@code Activity} configuration to be configured by a {@link Role}. 33 */ 34 public class PreferredActivity { 35 36 /** 37 * The specification of the {@code Activity} to be preferred. 38 */ 39 @NonNull 40 private final RequiredActivity mActivity; 41 42 /** 43 * The list of {@code IntentFilter} specifications to be configured to prefer this 44 * {@code Activity}. 45 */ 46 @NonNull 47 private final List<IntentFilterData> mIntentFilterDatas; 48 PreferredActivity(@onNull RequiredActivity activity, @NonNull List<IntentFilterData> intentFilterDatas)49 public PreferredActivity(@NonNull RequiredActivity activity, 50 @NonNull List<IntentFilterData> intentFilterDatas) { 51 mActivity = activity; 52 mIntentFilterDatas = intentFilterDatas; 53 } 54 55 @NonNull getActivity()56 public RequiredActivity getActivity() { 57 return mActivity; 58 } 59 60 @NonNull getIntentFilterDatas()61 public List<IntentFilterData> getIntentFilterDatas() { 62 return mIntentFilterDatas; 63 } 64 65 /** 66 * Configure this preferred activity specification for an application. 67 * 68 * @param packageName the package name of the application 69 * @param context the {@code Context} to retrieve system services 70 */ configure(@onNull String packageName, @NonNull Context context)71 public void configure(@NonNull String packageName, @NonNull Context context) { 72 PackageManager packageManager = context.getPackageManager(); 73 ComponentName packageActivity = mActivity.getQualifyingComponentForPackage( 74 packageName, context); 75 if (packageActivity == null) { 76 // We might be running into some race condition here, but we can't do anything about it. 77 // This should be handled by a future reconciliation started by the package change. 78 return; 79 } 80 81 int intentFilterDatasSize = mIntentFilterDatas.size(); 82 for (int i = 0; i < intentFilterDatasSize; i++) { 83 IntentFilterData intentFilterData = mIntentFilterDatas.get(i); 84 85 IntentFilter intentFilter = intentFilterData.createIntentFilter(); 86 intentFilter.addCategory(Intent.CATEGORY_DEFAULT); 87 // PackageManager.replacePreferredActivity() expects filter to have no data authorities, 88 // paths, or types; and at most one scheme. 89 int match = intentFilterData.getDataScheme() != null 90 ? IntentFilter.MATCH_CATEGORY_SCHEME : IntentFilter.MATCH_CATEGORY_EMPTY; 91 List<ComponentName> activities = mActivity.getQualifyingComponentsAsUser( 92 Process.myUserHandle(), context); 93 packageManager.replacePreferredActivity(intentFilter, match, activities, 94 packageActivity); 95 } 96 } 97 98 @Override toString()99 public String toString() { 100 return "PreferredActivity{" 101 + "mActivity=" + mActivity 102 + ", mIntentFilterDatas=" + mIntentFilterDatas 103 + '}'; 104 } 105 106 @Override equals(Object object)107 public boolean equals(Object object) { 108 if (this == object) { 109 return true; 110 } 111 if (object == null || getClass() != object.getClass()) { 112 return false; 113 } 114 PreferredActivity that = (PreferredActivity) object; 115 return Objects.equals(mActivity, that.mActivity) 116 && Objects.equals(mIntentFilterDatas, that.mIntentFilterDatas); 117 } 118 119 @Override hashCode()120 public int hashCode() { 121 return Objects.hash(mActivity, mIntentFilterDatas); 122 } 123 } 124