blob: 93357af406e888f4c773a81ae0266f05b10a0e02 [file] [log] [blame]
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -08001/*
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
17package com.android.internal.app;
18
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -080019import static org.mockito.Mockito.mock;
20
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090021import android.app.usage.UsageStatsManager;
arangelovb0802dc2019-10-18 18:03:44 +010022import android.content.Context;
23import android.content.Intent;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090024import android.content.pm.PackageManager;
arangelovb0802dc2019-10-18 18:03:44 +010025import android.content.pm.ResolveInfo;
arangelov2c1c37a2019-12-06 14:43:34 +000026import android.os.UserHandle;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090027
arangelovb0802dc2019-10-18 18:03:44 +010028import com.android.internal.app.chooser.TargetInfo;
Zhen Zhangcb55c8a2019-10-22 14:49:09 -070029
arangelovb0802dc2019-10-18 18:03:44 +010030import java.util.List;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090031import java.util.function.Function;
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -080032
33/*
34 * Simple wrapper around chooser activity to be able to initiate it under test
35 */
36public class ResolverWrapperActivity extends ResolverActivity {
37 static final OverrideData sOverrides = new OverrideData();
38 private UsageStatsManager mUsm;
Zhen Zhangcb55c8a2019-10-22 14:49:09 -070039
arangelovb0802dc2019-10-18 18:03:44 +010040 @Override
arangelov2c1c37a2019-12-06 14:43:34 +000041 public ResolverListAdapter createResolverListAdapter(Context context,
42 List<Intent> payloadIntents, Intent[] initialIntents, List<ResolveInfo> rList,
43 boolean filterLastUsed, boolean useLayoutForBrowsables, UserHandle userHandle) {
arangelovb0802dc2019-10-18 18:03:44 +010044 return new ResolverWrapperAdapter(context, payloadIntents, initialIntents, rList,
arangelov2c1c37a2019-12-06 14:43:34 +000045 filterLastUsed, createListController(userHandle), useLayoutForBrowsables, this);
Zhen Zhangcb55c8a2019-10-22 14:49:09 -070046 }
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -080047
arangelovb0802dc2019-10-18 18:03:44 +010048 ResolverWrapperAdapter getAdapter() {
arangelov2c1c37a2019-12-06 14:43:34 +000049 return (ResolverWrapperAdapter) mMultiProfilePagerAdapter.getCurrentListAdapter();
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -080050 }
51
52 @Override
53 public boolean isVoiceInteraction() {
54 if (sOverrides.isVoiceInteraction != null) {
55 return sOverrides.isVoiceInteraction;
56 }
57 return super.isVoiceInteraction();
58 }
59
60 @Override
61 public void safelyStartActivity(TargetInfo cti) {
62 if (sOverrides.onSafelyStartCallback != null &&
63 sOverrides.onSafelyStartCallback.apply(cti)) {
64 return;
65 }
66 super.safelyStartActivity(cti);
67 }
68
69 @Override
arangelov2c1c37a2019-12-06 14:43:34 +000070 protected ResolverListController createListController(UserHandle userHandle) {
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -080071 return sOverrides.resolverListController;
72 }
73
74 @Override
75 public PackageManager getPackageManager() {
76 if (sOverrides.createPackageManager != null) {
77 return sOverrides.createPackageManager.apply(super.getPackageManager());
78 }
79 return super.getPackageManager();
80 }
81
82 /**
83 * We cannot directly mock the activity created since instrumentation creates it.
84 * <p>
85 * Instead, we use static instances of this object to modify behavior.
86 */
87 static class OverrideData {
88 @SuppressWarnings("Since15")
89 public Function<PackageManager, PackageManager> createPackageManager;
90 public Function<TargetInfo, Boolean> onSafelyStartCallback;
91 public ResolverListController resolverListController;
92 public Boolean isVoiceInteraction;
93
94 public void reset() {
95 onSafelyStartCallback = null;
96 isVoiceInteraction = null;
97 createPackageManager = null;
98 resolverListController = mock(ResolverListController.class);
99 }
100 }
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -0800101}