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.internal.app; 18 19 import static org.mockito.Mockito.mock; 20 import static org.mockito.Mockito.when; 21 22 import android.app.usage.UsageStatsManager; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.pm.PackageManager; 26 import android.content.pm.ResolveInfo; 27 import android.os.Bundle; 28 import android.os.UserHandle; 29 30 import com.android.internal.app.chooser.TargetInfo; 31 32 import java.util.List; 33 import java.util.function.Function; 34 35 /* 36 * Simple wrapper around chooser activity to be able to initiate it under test 37 */ 38 public class ResolverWrapperActivity extends ResolverActivity { 39 static final OverrideData sOverrides = new OverrideData(); 40 private UsageStatsManager mUsm; 41 42 @Override createResolverListAdapter(Context context, List<Intent> payloadIntents, Intent[] initialIntents, List<ResolveInfo> rList, boolean filterLastUsed, UserHandle userHandle)43 public ResolverListAdapter createResolverListAdapter(Context context, 44 List<Intent> payloadIntents, Intent[] initialIntents, List<ResolveInfo> rList, 45 boolean filterLastUsed, UserHandle userHandle) { 46 return new ResolverWrapperAdapter(context, payloadIntents, initialIntents, rList, 47 filterLastUsed, createListController(userHandle), this); 48 } 49 50 @Override createMultiProfilePagerAdapter( Intent[] initialIntents, List<ResolveInfo> rList, boolean filterLastUsed)51 protected AbstractMultiProfilePagerAdapter createMultiProfilePagerAdapter( 52 Intent[] initialIntents, List<ResolveInfo> rList, boolean filterLastUsed) { 53 AbstractMultiProfilePagerAdapter multiProfilePagerAdapter = 54 super.createMultiProfilePagerAdapter(initialIntents, rList, filterLastUsed); 55 multiProfilePagerAdapter.setInjector(sOverrides.multiPagerAdapterInjector); 56 return multiProfilePagerAdapter; 57 } 58 getAdapter()59 ResolverWrapperAdapter getAdapter() { 60 return (ResolverWrapperAdapter) mMultiProfilePagerAdapter.getActiveListAdapter(); 61 } 62 getPersonalListAdapter()63 ResolverListAdapter getPersonalListAdapter() { 64 return ((ResolverListAdapter) mMultiProfilePagerAdapter.getAdapterForIndex(0)); 65 } 66 getWorkListAdapter()67 ResolverListAdapter getWorkListAdapter() { 68 if (mMultiProfilePagerAdapter.getInactiveListAdapter() == null) { 69 return null; 70 } 71 return ((ResolverListAdapter) mMultiProfilePagerAdapter.getAdapterForIndex(1)); 72 } 73 74 @Override isVoiceInteraction()75 public boolean isVoiceInteraction() { 76 if (sOverrides.isVoiceInteraction != null) { 77 return sOverrides.isVoiceInteraction; 78 } 79 return super.isVoiceInteraction(); 80 } 81 82 @Override safelyStartActivity(TargetInfo cti)83 public void safelyStartActivity(TargetInfo cti) { 84 if (sOverrides.onSafelyStartCallback != null && 85 sOverrides.onSafelyStartCallback.apply(cti)) { 86 return; 87 } 88 super.safelyStartActivity(cti); 89 } 90 91 @Override createListController(UserHandle userHandle)92 protected ResolverListController createListController(UserHandle userHandle) { 93 if (userHandle == UserHandle.SYSTEM) { 94 when(sOverrides.resolverListController.getUserHandle()).thenReturn(UserHandle.SYSTEM); 95 return sOverrides.resolverListController; 96 } 97 when(sOverrides.workResolverListController.getUserHandle()).thenReturn(userHandle); 98 return sOverrides.workResolverListController; 99 } 100 101 @Override getPackageManager()102 public PackageManager getPackageManager() { 103 if (sOverrides.createPackageManager != null) { 104 return sOverrides.createPackageManager.apply(super.getPackageManager()); 105 } 106 return super.getPackageManager(); 107 } 108 getCurrentUserHandle()109 protected UserHandle getCurrentUserHandle() { 110 return mMultiProfilePagerAdapter.getCurrentUserHandle(); 111 } 112 113 @Override getWorkProfileUserHandle()114 protected UserHandle getWorkProfileUserHandle() { 115 return sOverrides.workProfileUserHandle; 116 } 117 118 @Override startActivityAsUser(Intent intent, Bundle options, UserHandle user)119 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) { 120 super.startActivityAsUser(intent, options, user); 121 } 122 123 /** 124 * We cannot directly mock the activity created since instrumentation creates it. 125 * <p> 126 * Instead, we use static instances of this object to modify behavior. 127 */ 128 static class OverrideData { 129 @SuppressWarnings("Since15") 130 public Function<PackageManager, PackageManager> createPackageManager; 131 public Function<TargetInfo, Boolean> onSafelyStartCallback; 132 public ResolverListController resolverListController; 133 public ResolverListController workResolverListController; 134 public Boolean isVoiceInteraction; 135 public UserHandle workProfileUserHandle; 136 public boolean hasCrossProfileIntents; 137 public boolean isQuietModeEnabled; 138 public AbstractMultiProfilePagerAdapter.Injector multiPagerAdapterInjector; 139 reset()140 public void reset() { 141 onSafelyStartCallback = null; 142 isVoiceInteraction = null; 143 createPackageManager = null; 144 resolverListController = mock(ResolverListController.class); 145 workResolverListController = mock(ResolverListController.class); 146 workProfileUserHandle = null; 147 hasCrossProfileIntents = true; 148 isQuietModeEnabled = false; 149 multiPagerAdapterInjector = new AbstractMultiProfilePagerAdapter.Injector() { 150 @Override 151 public boolean hasCrossProfileIntents(List<Intent> intents, int sourceUserId, 152 int targetUserId) { 153 return hasCrossProfileIntents; 154 } 155 156 @Override 157 public boolean isQuietModeEnabled(UserHandle workProfileUserHandle) { 158 return isQuietModeEnabled; 159 } 160 161 @Override 162 public void requestQuietModeEnabled(boolean enabled, 163 UserHandle workProfileUserHandle) { 164 isQuietModeEnabled = enabled; 165 } 166 }; 167 } 168 } 169 } 170