1 /* 2 * Copyright (C) 2008 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.intentresolver; 18 19 import android.annotation.Nullable; 20 import android.app.prediction.AppPredictor; 21 import android.app.usage.UsageStatsManager; 22 import android.content.ContentResolver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.IntentFilter; 26 import android.content.pm.ResolveInfo; 27 import android.content.res.Resources; 28 import android.database.Cursor; 29 import android.net.Uri; 30 import android.os.Bundle; 31 import android.os.UserHandle; 32 33 import androidx.lifecycle.ViewModelProvider; 34 35 import com.android.intentresolver.chooser.DisplayResolveInfo; 36 import com.android.intentresolver.chooser.TargetInfo; 37 import com.android.intentresolver.emptystate.CrossProfileIntentsChecker; 38 import com.android.intentresolver.shortcuts.ShortcutLoader; 39 40 import java.util.List; 41 import java.util.function.Consumer; 42 43 /** 44 * Simple wrapper around chooser activity to be able to initiate it under test. For more 45 * information, see {@code com.android.internal.app.ChooserWrapperActivity}. 46 */ 47 public class ChooserWrapperActivity extends ChooserActivity implements IChooserWrapper { 48 static final ChooserActivityOverrideData sOverrides = ChooserActivityOverrideData.getInstance(); 49 private UsageStatsManager mUsm; 50 51 @Override createChooserListAdapter( Context context, List<Intent> payloadIntents, Intent[] initialIntents, List<ResolveInfo> rList, boolean filterLastUsed, ResolverListController resolverListController, UserHandle userHandle, Intent targetIntent, Intent referrerFillInIntent, int maxTargetsPerRow)52 public final ChooserListAdapter createChooserListAdapter( 53 Context context, 54 List<Intent> payloadIntents, 55 Intent[] initialIntents, 56 List<ResolveInfo> rList, 57 boolean filterLastUsed, 58 ResolverListController resolverListController, 59 UserHandle userHandle, 60 Intent targetIntent, 61 Intent referrerFillInIntent, 62 int maxTargetsPerRow) { 63 64 return new ChooserListAdapter( 65 context, 66 payloadIntents, 67 initialIntents, 68 rList, 69 filterLastUsed, 70 createListController(userHandle), 71 userHandle, 72 targetIntent, 73 referrerFillInIntent, 74 this, 75 mPackageManager, 76 getEventLog(), 77 maxTargetsPerRow, 78 userHandle, 79 mTargetDataLoader, 80 null, 81 mFeatureFlags); 82 } 83 84 @Override getAdapter()85 public ChooserListAdapter getAdapter() { 86 return mChooserMultiProfilePagerAdapter.getActiveListAdapter(); 87 } 88 89 @Override getPersonalListAdapter()90 public ChooserListAdapter getPersonalListAdapter() { 91 return mChooserMultiProfilePagerAdapter.getPersonalListAdapter(); 92 } 93 94 @Override getWorkListAdapter()95 public ChooserListAdapter getWorkListAdapter() { 96 return mChooserMultiProfilePagerAdapter.getWorkListAdapter(); 97 } 98 99 @Override getIsSelected()100 public boolean getIsSelected() { 101 return mIsSuccessfullySelected; 102 } 103 104 @Override getUsageStatsManager()105 public UsageStatsManager getUsageStatsManager() { 106 if (mUsm == null) { 107 mUsm = getSystemService(UsageStatsManager.class); 108 } 109 return mUsm; 110 } 111 112 @Override isVoiceInteraction()113 public boolean isVoiceInteraction() { 114 if (sOverrides.isVoiceInteraction != null) { 115 return sOverrides.isVoiceInteraction; 116 } 117 return super.isVoiceInteraction(); 118 } 119 120 @Override createCrossProfileIntentsChecker()121 protected CrossProfileIntentsChecker createCrossProfileIntentsChecker() { 122 if (sOverrides.mCrossProfileIntentsChecker != null) { 123 return sOverrides.mCrossProfileIntentsChecker; 124 } 125 return super.createCrossProfileIntentsChecker(); 126 } 127 128 @Override safelyStartActivityInternal(TargetInfo cti, UserHandle user, @Nullable Bundle options)129 public void safelyStartActivityInternal(TargetInfo cti, UserHandle user, 130 @Nullable Bundle options) { 131 if (sOverrides.onSafelyStartInternalCallback != null 132 && sOverrides.onSafelyStartInternalCallback.apply(cti)) { 133 return; 134 } 135 super.safelyStartActivityInternal(cti, user, options); 136 } 137 138 @Override createListController(UserHandle userHandle)139 public final ChooserListController createListController(UserHandle userHandle) { 140 if (userHandle == UserHandle.SYSTEM) { 141 return sOverrides.resolverListController; 142 } 143 return sOverrides.workResolverListController; 144 } 145 146 @Override getResources()147 public Resources getResources() { 148 if (sOverrides.resources != null) { 149 return sOverrides.resources; 150 } 151 return super.getResources(); 152 } 153 154 @Override createPreviewViewModelFactory()155 protected ViewModelProvider.Factory createPreviewViewModelFactory() { 156 return TestContentPreviewViewModel.Companion.wrap( 157 super.createPreviewViewModelFactory(), 158 sOverrides.imageLoader); 159 } 160 161 @Override queryResolver(ContentResolver resolver, Uri uri)162 public Cursor queryResolver(ContentResolver resolver, Uri uri) { 163 if (sOverrides.resolverCursor != null) { 164 return sOverrides.resolverCursor; 165 } 166 167 if (sOverrides.resolverForceException) { 168 throw new SecurityException("Test exception handling"); 169 } 170 171 return super.queryResolver(resolver, uri); 172 } 173 174 @Override createTestDisplayResolveInfo( Intent originalIntent, ResolveInfo pri, CharSequence pLabel, CharSequence pInfo, Intent replacementIntent)175 public DisplayResolveInfo createTestDisplayResolveInfo( 176 Intent originalIntent, 177 ResolveInfo pri, 178 CharSequence pLabel, 179 CharSequence pInfo, 180 Intent replacementIntent) { 181 return DisplayResolveInfo.newDisplayResolveInfo( 182 originalIntent, 183 pri, 184 pLabel, 185 pInfo, 186 replacementIntent); 187 } 188 189 @Override getCurrentUserHandle()190 public UserHandle getCurrentUserHandle() { 191 return mChooserMultiProfilePagerAdapter.getCurrentUserHandle(); 192 } 193 194 @Override createContextAsUser(UserHandle user, int flags)195 public Context createContextAsUser(UserHandle user, int flags) { 196 // return the current context as a work profile doesn't really exist in these tests 197 return this; 198 } 199 200 @Override createShortcutLoader( Context context, AppPredictor appPredictor, UserHandle userHandle, IntentFilter targetIntentFilter, Consumer<ShortcutLoader.Result> callback)201 protected ShortcutLoader createShortcutLoader( 202 Context context, 203 AppPredictor appPredictor, 204 UserHandle userHandle, 205 IntentFilter targetIntentFilter, 206 Consumer<ShortcutLoader.Result> callback) { 207 ShortcutLoader shortcutLoader = 208 sOverrides.shortcutLoaderFactory.invoke(userHandle, callback); 209 if (shortcutLoader != null) { 210 return shortcutLoader; 211 } 212 return super.createShortcutLoader( 213 context, appPredictor, userHandle, targetIntentFilter, callback); 214 } 215 } 216