Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 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 | |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 19 | import static org.mockito.Mockito.mock; |
| 20 | |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 21 | import android.app.usage.UsageStatsManager; |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 25 | import android.content.pm.ResolveInfo; |
arangelov | 2c1c37a | 2019-12-06 14:43:34 +0000 | [diff] [blame] | 26 | import android.os.UserHandle; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 27 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 28 | import com.android.internal.app.chooser.TargetInfo; |
Zhen Zhang | cb55c8a | 2019-10-22 14:49:09 -0700 | [diff] [blame] | 29 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 30 | import java.util.List; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 31 | import java.util.function.Function; |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Simple wrapper around chooser activity to be able to initiate it under test |
| 35 | */ |
| 36 | public class ResolverWrapperActivity extends ResolverActivity { |
| 37 | static final OverrideData sOverrides = new OverrideData(); |
| 38 | private UsageStatsManager mUsm; |
Zhen Zhang | cb55c8a | 2019-10-22 14:49:09 -0700 | [diff] [blame] | 39 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 40 | @Override |
arangelov | 2c1c37a | 2019-12-06 14:43:34 +0000 | [diff] [blame] | 41 | public ResolverListAdapter createResolverListAdapter(Context context, |
| 42 | List<Intent> payloadIntents, Intent[] initialIntents, List<ResolveInfo> rList, |
| 43 | boolean filterLastUsed, boolean useLayoutForBrowsables, UserHandle userHandle) { |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 44 | return new ResolverWrapperAdapter(context, payloadIntents, initialIntents, rList, |
arangelov | 2c1c37a | 2019-12-06 14:43:34 +0000 | [diff] [blame] | 45 | filterLastUsed, createListController(userHandle), useLayoutForBrowsables, this); |
Zhen Zhang | cb55c8a | 2019-10-22 14:49:09 -0700 | [diff] [blame] | 46 | } |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 47 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 48 | ResolverWrapperAdapter getAdapter() { |
arangelov | 2c1c37a | 2019-12-06 14:43:34 +0000 | [diff] [blame] | 49 | return (ResolverWrapperAdapter) mMultiProfilePagerAdapter.getCurrentListAdapter(); |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 50 | } |
| 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 |
arangelov | 2c1c37a | 2019-12-06 14:43:34 +0000 | [diff] [blame] | 70 | protected ResolverListController createListController(UserHandle userHandle) { |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 71 | 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 Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 101 | } |