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.internal.app;
18 
19 import static org.mockito.Mockito.mock;
20 
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.pm.PackageManager;
26 import android.content.pm.ResolveInfo;
27 import android.content.res.Resources;
28 import android.database.Cursor;
29 import android.graphics.Bitmap;
30 import android.net.Uri;
31 import android.util.Size;
32 
33 import com.android.internal.logging.MetricsLogger;
34 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
35 
36 import java.util.function.Function;
37 
38 public class ChooserWrapperActivity extends ChooserActivity {
39     /*
40      * Simple wrapper around chooser activity to be able to initiate it under test
41      */
42     static final OverrideData sOverrides = new OverrideData();
43     private UsageStatsManager mUsm;
44 
getAdapter()45     ChooserListAdapter getAdapter() {
46         return (ChooserListAdapter) mAdapter;
47     }
48 
getIsSelected()49     boolean getIsSelected() { return mIsSuccessfullySelected; }
50 
getUsageStatsManager()51     UsageStatsManager getUsageStatsManager() {
52         if (mUsm == null) {
53             mUsm = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
54         }
55         return mUsm;
56     }
57 
58     @Override
isVoiceInteraction()59     public boolean isVoiceInteraction() {
60         if (sOverrides.isVoiceInteraction != null) {
61             return sOverrides.isVoiceInteraction;
62         }
63         return super.isVoiceInteraction();
64     }
65 
66     @Override
safelyStartActivity(TargetInfo cti)67     public void safelyStartActivity(TargetInfo cti) {
68         if (sOverrides.onSafelyStartCallback != null &&
69                 sOverrides.onSafelyStartCallback.apply(cti)) {
70             return;
71         }
72         super.safelyStartActivity(cti);
73     }
74 
75     @Override
createListController()76     protected ResolverListController createListController() {
77         return sOverrides.resolverListController;
78     }
79 
80     @Override
getPackageManager()81     public PackageManager getPackageManager() {
82         if (sOverrides.createPackageManager != null) {
83             return sOverrides.createPackageManager.apply(super.getPackageManager());
84         }
85         return super.getPackageManager();
86     }
87 
88     @Override
getResources()89     public Resources getResources() {
90         if (sOverrides.resources != null) {
91             return sOverrides.resources;
92         }
93         return super.getResources();
94     }
95 
96     @Override
loadThumbnail(Uri uri, Size size)97     protected Bitmap loadThumbnail(Uri uri, Size size) {
98         if (sOverrides.previewThumbnail != null) {
99             return sOverrides.previewThumbnail;
100         }
101         return super.loadThumbnail(uri, size);
102     }
103 
104     @Override
isImageType(String mimeType)105     protected boolean isImageType(String mimeType) {
106         return sOverrides.isImageType;
107     }
108 
109     @Override
getMetricsLogger()110     protected MetricsLogger getMetricsLogger() {
111         return sOverrides.metricsLogger;
112     }
113 
114     @Override
queryResolver(ContentResolver resolver, Uri uri)115     public Cursor queryResolver(ContentResolver resolver, Uri uri) {
116         if (sOverrides.resolverCursor != null) {
117             return sOverrides.resolverCursor;
118         }
119 
120         if (sOverrides.resolverForceException) {
121             throw new SecurityException("Test exception handling");
122         }
123 
124         return super.queryResolver(resolver, uri);
125     }
126 
127     @Override
isWorkProfile()128     protected boolean isWorkProfile() {
129         if (sOverrides.alternateProfileSetting != 0) {
130             return sOverrides.alternateProfileSetting == MetricsEvent.MANAGED_PROFILE;
131         }
132         return super.isWorkProfile();
133     }
134 
createTestDisplayResolveInfo(Intent originalIntent, ResolveInfo pri, CharSequence pLabel, CharSequence pInfo, Intent pOrigIntent)135     public DisplayResolveInfo createTestDisplayResolveInfo(Intent originalIntent, ResolveInfo pri,
136             CharSequence pLabel, CharSequence pInfo, Intent pOrigIntent) {
137         return new DisplayResolveInfo(originalIntent, pri, pLabel, pInfo, pOrigIntent);
138     }
139 
140     /**
141      * We cannot directly mock the activity created since instrumentation creates it.
142      * <p>
143      * Instead, we use static instances of this object to modify behavior.
144      */
145     static class OverrideData {
146         @SuppressWarnings("Since15")
147         public Function<PackageManager, PackageManager> createPackageManager;
148         public Function<TargetInfo, Boolean> onSafelyStartCallback;
149         public ResolverListController resolverListController;
150         public Boolean isVoiceInteraction;
151         public boolean isImageType;
152         public Cursor resolverCursor;
153         public boolean resolverForceException;
154         public Bitmap previewThumbnail;
155         public MetricsLogger metricsLogger;
156         public int alternateProfileSetting;
157         public Resources resources;
158 
reset()159         public void reset() {
160             onSafelyStartCallback = null;
161             isVoiceInteraction = null;
162             createPackageManager = null;
163             previewThumbnail = null;
164             isImageType = false;
165             resolverCursor = null;
166             resolverForceException = false;
167             resolverListController = mock(ResolverListController.class);
168             metricsLogger = mock(MetricsLogger.class);
169             alternateProfileSetting = 0;
170             resources = null;
171         }
172     }
173 }
174