blob: 77f842aa503f1aecc42a1df360d967bbc383062f [file] [log] [blame]
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001/*
2 * Copyright (C) 2016 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 */
16package com.android.server.pm;
17
Makoto Onukid0010c52017-03-30 14:17:35 -070018import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.anyOrNull;
19import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.anyStringOrNull;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070020import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.cloneShortcutList;
21import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.hashSet;
22import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list;
23import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.makeBundle;
24import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.set;
25
Sunny Goyal145c8f82018-02-15 14:27:09 -080026import static org.mockito.ArgumentMatchers.anyBoolean;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070027import static org.mockito.Matchers.any;
Makoto Onuki157b1622016-06-02 16:13:10 -070028import static org.mockito.Matchers.anyInt;
29import static org.mockito.Matchers.anyString;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070030import static org.mockito.Matchers.eq;
31import static org.mockito.Mockito.doAnswer;
32import static org.mockito.Mockito.mock;
33import static org.mockito.Mockito.reset;
34import static org.mockito.Mockito.spy;
Makoto Onukiff14f732016-06-30 17:07:25 -070035import static org.mockito.Mockito.times;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070036import static org.mockito.Mockito.verify;
37import static org.mockito.Mockito.when;
38
39import android.annotation.NonNull;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070040import android.annotation.UserIdInt;
41import android.app.Activity;
Makoto Onuki33525d22016-08-03 15:45:24 -070042import android.app.ActivityManager;
Makoto Onukiea11db12016-06-24 15:17:44 -070043import android.app.ActivityManagerInternal;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070044import android.app.IUidObserver;
Mehdi Alizadeh14242af2018-12-20 20:11:35 -080045import android.app.Person;
Varun Shah2546cef2019-01-11 15:50:54 -080046import android.app.admin.DevicePolicyManager;
Makoto Onukiac042502016-05-20 16:39:42 -070047import android.app.usage.UsageStatsManagerInternal;
Makoto Onuki440a1ea2016-07-20 14:21:18 -070048import android.content.ActivityNotFoundException;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070049import android.content.BroadcastReceiver;
50import android.content.ComponentName;
51import android.content.Context;
52import android.content.Intent;
53import android.content.IntentFilter;
Makoto Onuki2d895c32016-12-02 15:48:40 -080054import android.content.IntentSender;
Mehdi Alizadehae808ff2020-01-21 13:39:53 -080055import android.content.LocusId;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070056import android.content.pm.ActivityInfo;
57import android.content.pm.ApplicationInfo;
58import android.content.pm.ILauncherApps;
59import android.content.pm.LauncherApps;
60import android.content.pm.LauncherApps.ShortcutQuery;
61import android.content.pm.PackageInfo;
62import android.content.pm.PackageManager;
63import android.content.pm.PackageManagerInternal;
Dan Cashman5c9f527e2018-04-03 16:42:23 -070064import android.content.pm.PackageParser;
Makoto Onukib08790c2016-06-23 14:05:46 -070065import android.content.pm.ResolveInfo;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070066import android.content.pm.ShortcutInfo;
67import android.content.pm.ShortcutManager;
68import android.content.pm.ShortcutServiceInternal;
69import android.content.pm.Signature;
Dan Cashman5c9f527e2018-04-03 16:42:23 -070070import android.content.pm.SigningInfo;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070071import android.content.pm.UserInfo;
72import android.content.res.Resources;
73import android.content.res.XmlResourceParser;
74import android.graphics.drawable.Icon;
75import android.net.Uri;
76import android.os.Bundle;
77import android.os.FileUtils;
78import android.os.Handler;
79import android.os.Looper;
Makoto Onukib5a012f2016-06-21 11:13:53 -070080import android.os.PersistableBundle;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070081import android.os.Process;
Makoto Onukia01f4f02016-12-15 15:58:41 -080082import android.os.RemoteException;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070083import android.os.UserHandle;
84import android.os.UserManager;
Sunny Goyal145c8f82018-02-15 14:27:09 -080085import android.os.UserManagerInternal;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070086import android.test.InstrumentationTestCase;
87import android.test.mock.MockContext;
Makoto Onuki2d895c32016-12-02 15:48:40 -080088import android.util.ArrayMap;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070089import android.util.Log;
90import android.util.Pair;
91
Makoto Onuki51ab2b32016-06-02 11:03:51 -070092import com.android.server.LocalServices;
93import com.android.server.SystemService;
94import com.android.server.pm.LauncherAppsService.LauncherAppsImpl;
95import com.android.server.pm.ShortcutUser.PackageWithUser;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070096import com.android.server.wm.ActivityTaskManagerInternal;
Mehdi Alizadeh32774622018-11-05 17:32:01 -080097
Makoto Onuki51ab2b32016-06-02 11:03:51 -070098import org.junit.Assert;
99import org.mockito.ArgumentCaptor;
Makoto Onukie70b29e2018-04-03 16:44:39 -0700100import org.mockito.invocation.InvocationOnMock;
101import org.mockito.stubbing.Answer;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700102
103import java.io.BufferedReader;
104import java.io.ByteArrayOutputStream;
105import java.io.File;
106import java.io.FileReader;
Makoto Onuki76269922016-07-15 14:58:54 -0700107import java.io.IOException;
108import java.io.InputStreamReader;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700109import java.io.PrintWriter;
110import java.util.ArrayList;
111import java.util.HashMap;
112import java.util.HashSet;
113import java.util.LinkedHashMap;
114import java.util.List;
Makoto Onuki157b1622016-06-02 16:13:10 -0700115import java.util.Locale;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700116import java.util.Map;
Daulet Zhanguzinfdb98192019-12-20 18:11:26 +0000117import java.util.Objects;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700118import java.util.Set;
Makoto Onukib08790c2016-06-23 14:05:46 -0700119import java.util.function.BiFunction;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700120import java.util.function.BiPredicate;
121import java.util.function.Consumer;
Makoto Onukie70b29e2018-04-03 16:44:39 -0700122import java.util.function.Function;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700123
124public abstract class BaseShortcutManagerTest extends InstrumentationTestCase {
125 protected static final String TAG = "ShortcutManagerTest";
126
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700127 protected static final boolean DUMP_IN_TEARDOWN = false; // DO NOT SUBMIT WITH true
128
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700129 /**
130 * Whether to enable dump or not. Should be only true when debugging to avoid bugs where
131 * dump affecting the behavior.
132 */
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700133 protected static final boolean ENABLE_DUMP = false // DO NOT SUBMIT WITH true
134 || DUMP_IN_TEARDOWN || ShortcutService.DEBUG;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700135
136 protected static final String[] EMPTY_STRINGS = new String[0]; // Just for readability.
137
Makoto Onukib08790c2016-06-23 14:05:46 -0700138 protected static final String MAIN_ACTIVITY_CLASS = "MainActivity";
Makoto Onuki2d895c32016-12-02 15:48:40 -0800139 protected static final String PIN_CONFIRM_ACTIVITY_CLASS = "PinConfirmActivity";
Makoto Onukib08790c2016-06-23 14:05:46 -0700140
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700141 // public for mockito
142 public class BaseContext extends MockContext {
143 @Override
144 public Object getSystemService(String name) {
145 switch (name) {
146 case Context.USER_SERVICE:
147 return mMockUserManager;
Varun Shah2546cef2019-01-11 15:50:54 -0800148 case Context.DEVICE_POLICY_SERVICE:
149 return mMockDevicePolicyManager;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700150 }
Varun Shah2546cef2019-01-11 15:50:54 -0800151 throw new UnsupportedOperationException("Couldn't find system service: " + name);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700152 }
153
154 @Override
155 public String getSystemServiceName(Class<?> serviceClass) {
156 return getTestContext().getSystemServiceName(serviceClass);
157 }
158
159 @Override
160 public PackageManager getPackageManager() {
161 return mMockPackageManager;
162 }
163
164 @Override
165 public Resources getResources() {
166 return getTestContext().getResources();
167 }
168
169 @Override
170 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
171 IntentFilter filter, String broadcastPermission, Handler scheduler) {
172 // ignore.
173 return null;
174 }
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700175
176 @Override
177 public void unregisterReceiver(BroadcastReceiver receiver) {
178 // ignore.
179 }
Makoto Onuki2d895c32016-12-02 15:48:40 -0800180
181 @Override
182 public void startActivityAsUser(Intent intent, UserHandle user) {
183 // ignore, use spy to intercept it.
184 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700185 }
186
187 /** Context used in the client side */
188 public class ClientContext extends BaseContext {
189 @Override
190 public String getPackageName() {
191 return mInjectedClientPackage;
192 }
193
194 @Override
195 public int getUserId() {
196 return getCallingUserId();
197 }
198 }
199
200 /** Context used in the service side */
201 public class ServiceContext extends BaseContext {
202 long injectClearCallingIdentity() {
203 final int prevCallingUid = mInjectedCallingUid;
204 mInjectedCallingUid = Process.SYSTEM_UID;
205 return prevCallingUid;
206 }
207
208 void injectRestoreCallingIdentity(long token) {
209 mInjectedCallingUid = (int) token;
210 }
211
212 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700213 public int getUserId() {
214 return UserHandle.USER_SYSTEM;
215 }
216
217 public PackageInfo injectGetActivitiesWithMetadata(
218 String packageName, @UserIdInt int userId) {
219 return BaseShortcutManagerTest.this.injectGetActivitiesWithMetadata(packageName, userId);
220 }
221
222 public XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
223 return BaseShortcutManagerTest.this.injectXmlMetaData(activityInfo, key);
224 }
Makoto Onuki2d895c32016-12-02 15:48:40 -0800225
226 public void sendIntentSender(IntentSender intent) {
227 // Placeholder for spying.
228 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700229 }
230
231 /** ShortcutService with injection override methods. */
232 protected final class ShortcutServiceTestable extends ShortcutService {
233 final ServiceContext mContext;
234 IUidObserver mUidObserver;
235
236 public ShortcutServiceTestable(ServiceContext context, Looper looper) {
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700237 super(context, looper, /* onyForPackageManagerApis */ false);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700238 mContext = context;
239 }
240
241 @Override
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700242 public String injectGetLocaleTagsForUser(@UserIdInt int userId) {
243 return mInjectedLocale.toLanguageTag();
244 }
245
246 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700247 boolean injectShouldPerformVerification() {
248 return true; // Always verify during unit tests.
249 }
250
251 @Override
252 String injectShortcutManagerConstants() {
253 return ConfigConstants.KEY_RESET_INTERVAL_SEC + "=" + (INTERVAL / 1000) + ","
254 + ConfigConstants.KEY_MAX_SHORTCUTS + "=" + MAX_SHORTCUTS + ","
255 + ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "="
256 + MAX_UPDATES_PER_INTERVAL + ","
257 + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP + "=" + MAX_ICON_DIMENSION + ","
258 + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM + "="
259 + MAX_ICON_DIMENSION_LOWRAM + ","
260 + ConfigConstants.KEY_ICON_FORMAT + "=PNG,"
261 + ConfigConstants.KEY_ICON_QUALITY + "=100";
262 }
263
264 @Override
265 long injectClearCallingIdentity() {
266 return mContext.injectClearCallingIdentity();
267 }
268
269 @Override
270 void injectRestoreCallingIdentity(long token) {
271 mContext.injectRestoreCallingIdentity(token);
272 }
273
274 @Override
275 int injectDipToPixel(int dip) {
276 return dip;
277 }
278
279 @Override
280 long injectCurrentTimeMillis() {
281 return mInjectedCurrentTimeMillis;
282 }
283
284 @Override
285 long injectElapsedRealtime() {
286 // TODO This should be kept separately from mInjectedCurrentTimeMillis, since
287 // this should increase even if we rewind mInjectedCurrentTimeMillis in some tests.
288 return mInjectedCurrentTimeMillis - START_TIME;
289 }
290
291 @Override
Makoto Onuki475c3652017-05-08 14:29:03 -0700292 long injectUptimeMillis() {
293 return mInjectedCurrentTimeMillis - START_TIME - mDeepSleepTime;
294 }
295
296 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700297 int injectBinderCallingUid() {
298 return mInjectedCallingUid;
299 }
300
301 @Override
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800302 int injectBinderCallingPid() {
303 // Note it's not used in tests, so just return a "random" value.
304 return mInjectedCallingUid * 123;
305 }
306
307 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700308 int injectGetPackageUid(String packageName, int userId) {
309 return getInjectedPackageInfo(packageName, userId, false).applicationInfo.uid;
310 }
311
312 @Override
313 File injectSystemDataPath() {
314 return new File(mInjectedFilePathRoot, "system");
315 }
316
317 @Override
318 File injectUserDataPath(@UserIdInt int userId) {
319 return new File(mInjectedFilePathRoot, "user-" + userId);
320 }
321
322 @Override
323 void injectValidateIconResPackage(ShortcutInfo shortcut, Icon icon) {
324 // Can't check
325 }
326
327 @Override
328 boolean injectIsLowRamDevice() {
329 return mInjectedIsLowRamDevice;
330 }
331
332 @Override
333 void injectRegisterUidObserver(IUidObserver observer, int which) {
334 mUidObserver = observer;
335 }
336
337 @Override
Makoto Onuki634cecb2017-10-13 17:10:48 -0700338 boolean hasShortcutHostPermission(@NonNull String callingPackage, int userId,
339 int callingPid, int callingUid) {
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700340 return mDefaultLauncherChecker.test(callingPackage, userId);
341 }
342
343 @Override
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800344 boolean injectHasUnlimitedShortcutsApiCallsPermission(int callingPid, int callingUid) {
345 return mInjectHasUnlimitedShortcutsApiCallsPermission;
346 }
347
348 @Override
Makoto Onuki2d895c32016-12-02 15:48:40 -0800349 ComponentName getDefaultLauncher(@UserIdInt int userId) {
350 final ComponentName activity = mDefaultLauncher.get(userId);
351 if (activity != null) {
352 return activity;
353 }
354 return super.getDefaultLauncher(userId);
355 }
356
357 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700358 PackageInfo injectPackageInfoWithUninstalled(String packageName, @UserIdInt int userId,
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700359 boolean getSignatures) {
360 return getInjectedPackageInfo(packageName, userId, getSignatures);
361 }
362
363 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700364 ApplicationInfo injectApplicationInfoWithUninstalled(
365 String packageName, @UserIdInt int userId) {
366 PackageInfo pi = injectPackageInfoWithUninstalled(
367 packageName, userId, /* getSignatures= */ false);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700368 return pi != null ? pi.applicationInfo : null;
369 }
370
371 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700372 List<PackageInfo> injectGetPackagesWithUninstalled(@UserIdInt int userId) {
373 return BaseShortcutManagerTest.this.getInstalledPackagesWithUninstalled(userId);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700374 }
375
376 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700377 ActivityInfo injectGetActivityInfoWithMetadataWithUninstalled(ComponentName activity,
Makoto Onukib08790c2016-06-23 14:05:46 -0700378 @UserIdInt int userId) {
379 final PackageInfo pi = mContext.injectGetActivitiesWithMetadata(
380 activity.getPackageName(), userId);
381 if (pi == null || pi.activities == null) {
382 return null;
383 }
384 for (ActivityInfo ai : pi.activities) {
385 if (!mEnabledActivityChecker.test(ai.getComponentName(), userId)) {
386 continue;
387 }
388 if (activity.equals(ai.getComponentName())) {
389 return ai;
390 }
391 }
392 return null;
393 }
394
395 @Override
396 boolean injectIsMainActivity(@NonNull ComponentName activity, int userId) {
397 if (!mEnabledActivityChecker.test(activity, userId)) {
398 return false;
399 }
400 return mMainActivityChecker.test(activity, userId);
401 }
402
403 @Override
404 List<ResolveInfo> injectGetMainActivities(@NonNull String packageName, int userId) {
405 final PackageInfo pi = mContext.injectGetActivitiesWithMetadata(
406 packageName, userId);
407 if (pi == null || pi.activities == null) {
408 return null;
409 }
410 final ArrayList<ResolveInfo> ret = new ArrayList<>(pi.activities.length);
411 for (int i = 0; i < pi.activities.length; i++) {
412 if (!mEnabledActivityChecker.test(pi.activities[i].getComponentName(), userId)) {
413 continue;
414 }
415 final ResolveInfo ri = new ResolveInfo();
416 ri.activityInfo = pi.activities[i];
417 ret.add(ri);
418 }
419
420 return ret;
421 }
422
423 @Override
424 ComponentName injectGetDefaultMainActivity(@NonNull String packageName, int userId) {
425 return mMainActivityFetcher.apply(packageName, userId);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700426 }
427
428 @Override
Makoto Onuki2d895c32016-12-02 15:48:40 -0800429 ComponentName injectGetPinConfirmationActivity(@NonNull String launcherPackageName,
Sunny Goyal7f7372a2017-01-24 11:53:54 -0800430 int launcherUserId, int requestType) {
Makoto Onuki2d895c32016-12-02 15:48:40 -0800431 return mPinConfirmActivityFetcher.apply(launcherPackageName, launcherUserId);
432 }
433
434 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700435 boolean injectIsActivityEnabledAndExported(ComponentName activity, @UserIdInt int userId) {
436 return mEnabledActivityChecker.test(activity, userId);
437 }
438
439 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700440 XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
441 return mContext.injectXmlMetaData(activityInfo, key);
442 }
443
444 @Override
Makoto Onuki157b1622016-06-02 16:13:10 -0700445 void injectPostToHandler(Runnable r) {
Makoto Onukia2241832016-07-06 13:28:37 -0700446 runOnHandler(r);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700447 }
448
449 @Override
Makoto Onuki085a05c2016-08-19 11:39:29 -0700450 void injectRunOnNewThread(Runnable r) {
451 runOnHandler(r);
452 }
453
454 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700455 void injectEnforceCallingPermission(String permission, String message) {
456 if (!mCallerPermissions.contains(permission)) {
457 throw new SecurityException("Missing permission: " + permission);
458 }
459 }
460
461 @Override
Makoto Onukib08790c2016-06-23 14:05:46 -0700462 boolean injectIsSafeModeEnabled() {
463 return mSafeMode;
464 }
465
466 @Override
Makoto Onuki33663282016-08-22 16:19:04 -0700467 String injectBuildFingerprint() {
468 return mInjectedBuildFingerprint;
469 }
470
471 @Override
Sunny Goyal87a563e2017-01-01 19:42:45 -0800472 void injectSendIntentSender(IntentSender intent, Intent extras) {
Makoto Onuki2d895c32016-12-02 15:48:40 -0800473 mContext.sendIntentSender(intent);
474 }
475
476 @Override
Makoto Onuki35559d62017-11-06 16:26:32 -0800477 boolean injectHasAccessShortcutsPermission(int callingPid, int callingUid) {
Makoto Onuki634cecb2017-10-13 17:10:48 -0700478 return mInjectCheckAccessShortcutsPermission;
479 }
480
481 @Override
Makoto Onukia2241832016-07-06 13:28:37 -0700482 void wtf(String message, Throwable th) {
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700483 // During tests, WTF is fatal.
Makoto Onukia2241832016-07-06 13:28:37 -0700484 fail(message + " exception: " + th + "\n" + Log.getStackTraceString(th));
485 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700486 }
487
488 /** ShortcutManager with injection override methods. */
489 protected class ShortcutManagerTestable extends ShortcutManager {
490 public ShortcutManagerTestable(Context context, ShortcutServiceTestable service) {
491 super(context, service);
492 }
493
494 @Override
495 protected int injectMyUserId() {
496 return UserHandle.getUserId(mInjectedCallingUid);
497 }
498
499 @Override
500 public boolean setDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
501 // Note to simulate the binder RPC, we need to clone the incoming arguments.
502 // Otherwise bad things will happen because they're mutable.
503 return super.setDynamicShortcuts(cloneShortcutList(shortcutInfoList));
504 }
505
506 @Override
507 public boolean addDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
508 // Note to simulate the binder RPC, we need to clone the incoming arguments.
509 return super.addDynamicShortcuts(cloneShortcutList(shortcutInfoList));
510 }
511
512 @Override
513 public boolean updateShortcuts(List<ShortcutInfo> shortcutInfoList) {
514 // Note to simulate the binder RPC, we need to clone the incoming arguments.
515 return super.updateShortcuts(cloneShortcutList(shortcutInfoList));
516 }
517 }
518
519 protected class LauncherAppImplTestable extends LauncherAppsImpl {
520 final ServiceContext mContext;
521
522 public LauncherAppImplTestable(ServiceContext context) {
523 super(context);
524 mContext = context;
525 }
526
527 @Override
528 public void verifyCallingPackage(String callingPackage) {
529 // SKIP
530 }
531
532 @Override
533 void postToPackageMonitorHandler(Runnable r) {
Makoto Onukia2241832016-07-06 13:28:37 -0700534 runOnHandler(r);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700535 }
536
537 @Override
538 int injectBinderCallingUid() {
539 return mInjectedCallingUid;
540 }
541
542 @Override
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800543 int injectBinderCallingPid() {
544 // Note it's not used in tests, so just return a "random" value.
545 return mInjectedCallingUid * 123;
546 }
547
548 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700549 long injectClearCallingIdentity() {
550 final int prevCallingUid = mInjectedCallingUid;
551 mInjectedCallingUid = Process.SYSTEM_UID;
552 return prevCallingUid;
553 }
554
555 @Override
556 void injectRestoreCallingIdentity(long token) {
557 mInjectedCallingUid = (int) token;
558 }
Mehdi Alizadeh0de8c292020-01-21 17:27:26 -0800559
560 @Override
561 boolean injectHasAccessShortcutsPermission(int callingPid, int callingUid) {
562 return true;
563 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700564 }
565
566 protected class LauncherAppsTestable extends LauncherApps {
567 public LauncherAppsTestable(Context context, ILauncherApps service) {
568 super(context, service);
569 }
570 }
571
572 public static class ShortcutActivity extends Activity {
573 }
574
575 public static class ShortcutActivity2 extends Activity {
576 }
577
578 public static class ShortcutActivity3 extends Activity {
579 }
580
Makoto Onukia2241832016-07-06 13:28:37 -0700581 protected Looper mLooper;
582 protected Handler mHandler;
583
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700584 protected ServiceContext mServiceContext;
585 protected ClientContext mClientContext;
586
587 protected ShortcutServiceTestable mService;
588 protected ShortcutManagerTestable mManager;
589 protected ShortcutServiceInternal mInternal;
590
591 protected LauncherAppImplTestable mLauncherAppImpl;
592
593 // LauncherApps has per-instace state, so we need a differnt instance for each launcher.
594 protected final Map<Pair<Integer, String>, LauncherAppsTestable>
595 mLauncherAppsMap = new HashMap<>();
596 protected LauncherAppsTestable mLauncherApps; // Current one
597
598 protected File mInjectedFilePathRoot;
599
Makoto Onukib08790c2016-06-23 14:05:46 -0700600 protected boolean mSafeMode;
601
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700602 protected long mInjectedCurrentTimeMillis;
Makoto Onuki475c3652017-05-08 14:29:03 -0700603 protected long mDeepSleepTime; // Used to calculate "uptimeMillis".
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700604
605 protected boolean mInjectedIsLowRamDevice;
606
Makoto Onuki157b1622016-06-02 16:13:10 -0700607 protected Locale mInjectedLocale = Locale.ENGLISH;
608
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700609 protected int mInjectedCallingUid;
610 protected String mInjectedClientPackage;
611
612 protected Map<String, PackageInfo> mInjectedPackages;
613
614 protected Set<PackageWithUser> mUninstalledPackages;
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700615 protected Set<PackageWithUser> mDisabledPackages;
Makoto Onuki66e4a2b2017-01-23 11:37:45 -0800616 protected Set<PackageWithUser> mEphemeralPackages;
Makoto Onuki33663282016-08-22 16:19:04 -0700617 protected Set<String> mSystemPackages;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700618
619 protected PackageManager mMockPackageManager;
620 protected PackageManagerInternal mMockPackageManagerInternal;
621 protected UserManager mMockUserManager;
Varun Shah2546cef2019-01-11 15:50:54 -0800622 protected DevicePolicyManager mMockDevicePolicyManager;
Sunny Goyal145c8f82018-02-15 14:27:09 -0800623 protected UserManagerInternal mMockUserManagerInternal;
Makoto Onukiac042502016-05-20 16:39:42 -0700624 protected UsageStatsManagerInternal mMockUsageStatsManagerInternal;
Makoto Onukiea11db12016-06-24 15:17:44 -0700625 protected ActivityManagerInternal mMockActivityManagerInternal;
Wale Ogunwale6767eae2018-05-03 15:52:51 -0700626 protected ActivityTaskManagerInternal mMockActivityTaskManagerInternal;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700627
628 protected static final String CALLING_PACKAGE_1 = "com.android.test.1";
629 protected static final int CALLING_UID_1 = 10001;
630
631 protected static final String CALLING_PACKAGE_2 = "com.android.test.2";
632 protected static final int CALLING_UID_2 = 10002;
633
634 protected static final String CALLING_PACKAGE_3 = "com.android.test.3";
635 protected static final int CALLING_UID_3 = 10003;
636
637 protected static final String CALLING_PACKAGE_4 = "com.android.test.4";
638 protected static final int CALLING_UID_4 = 10004;
639
640 protected static final String LAUNCHER_1 = "com.android.launcher.1";
641 protected static final int LAUNCHER_UID_1 = 10011;
642
643 protected static final String LAUNCHER_2 = "com.android.launcher.2";
644 protected static final int LAUNCHER_UID_2 = 10012;
645
646 protected static final String LAUNCHER_3 = "com.android.launcher.3";
647 protected static final int LAUNCHER_UID_3 = 10013;
648
649 protected static final String LAUNCHER_4 = "com.android.launcher.4";
650 protected static final int LAUNCHER_UID_4 = 10014;
651
652 protected static final int USER_0 = UserHandle.USER_SYSTEM;
653 protected static final int USER_10 = 10;
654 protected static final int USER_11 = 11;
Makoto Onukide3c16c2017-01-26 11:39:31 -0800655 protected static final int USER_P0 = 20; // profile of user 0 (MANAGED_PROFILE *not* set)
656 protected static final int USER_P1 = 21; // another profile of user 0 (MANAGED_PROFILE set)
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700657
658 protected static final UserHandle HANDLE_USER_0 = UserHandle.of(USER_0);
659 protected static final UserHandle HANDLE_USER_10 = UserHandle.of(USER_10);
660 protected static final UserHandle HANDLE_USER_11 = UserHandle.of(USER_11);
661 protected static final UserHandle HANDLE_USER_P0 = UserHandle.of(USER_P0);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800662 protected static final UserHandle HANDLE_USER_P1 = UserHandle.of(USER_P1);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700663
664 protected static final UserInfo USER_INFO_0 = withProfileGroupId(
665 new UserInfo(USER_0, "user0",
Makoto Onuki2d895c32016-12-02 15:48:40 -0800666 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED), 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700667
668 protected static final UserInfo USER_INFO_10 =
669 new UserInfo(USER_10, "user10", UserInfo.FLAG_INITIALIZED);
670
671 protected static final UserInfo USER_INFO_11 =
672 new UserInfo(USER_11, "user11", UserInfo.FLAG_INITIALIZED);
673
Makoto Onukiaecbd032017-01-19 12:11:11 -0800674 /*
675 * Cheat: USER_P0 is a sub profile of USER_0, but it doesn't have the MANAGED_PROFILE flag set.
676 * Due to a change made to LauncherApps (b/34340531), work profile apps a no longer able
677 * to see the main profile, which would break tons of unit tests. We avoid it by not setting
678 * MANAGED_PROFILE for P0.
679 * We cover this negative case in CTS. (i.e. CTS has tests to make sure maanged profile
680 * can't access main profile's shortcuts.)
681 */
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700682 protected static final UserInfo USER_INFO_P0 = withProfileGroupId(
Makoto Onukiaecbd032017-01-19 12:11:11 -0800683 new UserInfo(USER_P0, "userP0", UserInfo.FLAG_INITIALIZED), 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700684
Makoto Onukide3c16c2017-01-26 11:39:31 -0800685 protected static final UserInfo USER_INFO_P1 = withProfileGroupId(
686 new UserInfo(USER_P1, "userP1",
687 UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_MANAGED_PROFILE), 0);
688
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700689 protected BiPredicate<String, Integer> mDefaultLauncherChecker =
690 (callingPackage, userId) ->
691 LAUNCHER_1.equals(callingPackage) || LAUNCHER_2.equals(callingPackage)
692 || LAUNCHER_3.equals(callingPackage) || LAUNCHER_4.equals(callingPackage);
693
Makoto Onuki2d895c32016-12-02 15:48:40 -0800694 private final Map<Integer, ComponentName> mDefaultLauncher = new ArrayMap<>();
695
Makoto Onukib08790c2016-06-23 14:05:46 -0700696 protected BiPredicate<ComponentName, Integer> mMainActivityChecker =
697 (activity, userId) -> true;
698
699 protected BiFunction<String, Integer, ComponentName> mMainActivityFetcher =
700 (packageName, userId) -> new ComponentName(packageName, MAIN_ACTIVITY_CLASS);
701
Makoto Onuki2d895c32016-12-02 15:48:40 -0800702 protected BiFunction<String, Integer, ComponentName> mPinConfirmActivityFetcher =
703 (packageName, userId) -> new ComponentName(packageName, PIN_CONFIRM_ACTIVITY_CLASS);
704
Makoto Onukib08790c2016-06-23 14:05:46 -0700705 protected BiPredicate<ComponentName, Integer> mEnabledActivityChecker
706 = (activity, userId) -> true; // all activities are enabled.
707
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700708 protected static final long START_TIME = 1440000000101L;
709
710 protected static final long INTERVAL = 10000;
711
712 protected static final int MAX_SHORTCUTS = 10;
713
714 protected static final int MAX_UPDATES_PER_INTERVAL = 3;
715
716 protected static final int MAX_ICON_DIMENSION = 128;
717
718 protected static final int MAX_ICON_DIMENSION_LOWRAM = 32;
719
720 protected static final ShortcutQuery QUERY_ALL = new ShortcutQuery();
721
722 protected final ArrayList<String> mCallerPermissions = new ArrayList<>();
723
724 protected final HashMap<String, LinkedHashMap<ComponentName, Integer>> mActivityMetadataResId
725 = new HashMap<>();
726
Makoto Onukia2241832016-07-06 13:28:37 -0700727 protected final Map<Integer, UserInfo> mUserInfos = new HashMap<>();
728 protected final Map<Integer, Boolean> mRunningUsers = new HashMap<>();
729 protected final Map<Integer, Boolean> mUnlockedUsers = new HashMap<>();
730
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700731 protected static final String PACKAGE_SYSTEM_LAUNCHER = "com.android.systemlauncher";
732 protected static final String PACKAGE_SYSTEM_LAUNCHER_NAME = "systemlauncher_name";
733 protected static final int PACKAGE_SYSTEM_LAUNCHER_PRIORITY = 0;
734
735 protected static final String PACKAGE_FALLBACK_LAUNCHER = "com.android.settings";
736 protected static final String PACKAGE_FALLBACK_LAUNCHER_NAME = "fallback";
737 protected static final int PACKAGE_FALLBACK_LAUNCHER_PRIORITY = -999;
738
Makoto Onuki33663282016-08-22 16:19:04 -0700739 protected String mInjectedBuildFingerprint = "build1";
740
Makoto Onuki634cecb2017-10-13 17:10:48 -0700741 protected boolean mInjectCheckAccessShortcutsPermission = false;
742
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800743 protected boolean mInjectHasUnlimitedShortcutsApiCallsPermission = false;
744
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700745 static {
746 QUERY_ALL.setQueryFlags(
Makoto Onuki4d6b87f2016-06-17 13:47:40 -0700747 ShortcutQuery.FLAG_GET_ALL_KINDS);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700748 }
749
750 @Override
751 protected void setUp() throws Exception {
752 super.setUp();
753
Makoto Onukia2241832016-07-06 13:28:37 -0700754 mLooper = Looper.getMainLooper();
755 mHandler = new Handler(mLooper);
756
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700757 mServiceContext = spy(new ServiceContext());
758 mClientContext = new ClientContext();
759
760 mMockPackageManager = mock(PackageManager.class);
761 mMockPackageManagerInternal = mock(PackageManagerInternal.class);
762 mMockUserManager = mock(UserManager.class);
Varun Shah2546cef2019-01-11 15:50:54 -0800763 mMockDevicePolicyManager = mock(DevicePolicyManager.class);
Sunny Goyal145c8f82018-02-15 14:27:09 -0800764 mMockUserManagerInternal = mock(UserManagerInternal.class);
Makoto Onukiac042502016-05-20 16:39:42 -0700765 mMockUsageStatsManagerInternal = mock(UsageStatsManagerInternal.class);
Makoto Onukiea11db12016-06-24 15:17:44 -0700766 mMockActivityManagerInternal = mock(ActivityManagerInternal.class);
Wale Ogunwale6767eae2018-05-03 15:52:51 -0700767 mMockActivityTaskManagerInternal = mock(ActivityTaskManagerInternal.class);
Makoto Onukiac042502016-05-20 16:39:42 -0700768
769 LocalServices.removeServiceForTest(PackageManagerInternal.class);
770 LocalServices.addService(PackageManagerInternal.class, mMockPackageManagerInternal);
771 LocalServices.removeServiceForTest(UsageStatsManagerInternal.class);
772 LocalServices.addService(UsageStatsManagerInternal.class, mMockUsageStatsManagerInternal);
Makoto Onukiea11db12016-06-24 15:17:44 -0700773 LocalServices.removeServiceForTest(ActivityManagerInternal.class);
774 LocalServices.addService(ActivityManagerInternal.class, mMockActivityManagerInternal);
Mehdi Alizadeh32774622018-11-05 17:32:01 -0800775 LocalServices.removeServiceForTest(ActivityTaskManagerInternal.class);
Wale Ogunwale6767eae2018-05-03 15:52:51 -0700776 LocalServices.addService(ActivityTaskManagerInternal.class, mMockActivityTaskManagerInternal);
Sunny Goyal145c8f82018-02-15 14:27:09 -0800777 LocalServices.removeServiceForTest(UserManagerInternal.class);
778 LocalServices.addService(UserManagerInternal.class, mMockUserManagerInternal);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700779
780 // Prepare injection values.
781
782 mInjectedCurrentTimeMillis = START_TIME;
783
784 mInjectedPackages = new HashMap<>();
785 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 1);
786 addPackage(CALLING_PACKAGE_2, CALLING_UID_2, 2);
787 addPackage(CALLING_PACKAGE_3, CALLING_UID_3, 3);
788 addPackage(CALLING_PACKAGE_4, CALLING_UID_4, 10);
789 addPackage(LAUNCHER_1, LAUNCHER_UID_1, 4);
790 addPackage(LAUNCHER_2, LAUNCHER_UID_2, 5);
791 addPackage(LAUNCHER_3, LAUNCHER_UID_3, 6);
792 addPackage(LAUNCHER_4, LAUNCHER_UID_4, 10);
793
794 // CALLING_PACKAGE_3 / LAUNCHER_3 are not backup target.
795 updatePackageInfo(CALLING_PACKAGE_3,
796 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
797 updatePackageInfo(LAUNCHER_3,
798 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
799
800 mUninstalledPackages = new HashSet<>();
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700801 mDisabledPackages = new HashSet<>();
Makoto Onuki33663282016-08-22 16:19:04 -0700802 mSystemPackages = new HashSet<>();
Makoto Onuki66e4a2b2017-01-23 11:37:45 -0800803 mEphemeralPackages = new HashSet<>();
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700804
805 mInjectedFilePathRoot = new File(getTestContext().getCacheDir(), "test-files");
806
807 deleteAllSavedFiles();
808
809 // Set up users.
Makoto Onukia2241832016-07-06 13:28:37 -0700810 mUserInfos.put(USER_0, USER_INFO_0);
811 mUserInfos.put(USER_10, USER_INFO_10);
812 mUserInfos.put(USER_11, USER_INFO_11);
813 mUserInfos.put(USER_P0, USER_INFO_P0);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800814 mUserInfos.put(USER_P1, USER_INFO_P1);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700815
Sunny Goyal145c8f82018-02-15 14:27:09 -0800816 when(mMockUserManagerInternal.isUserUnlockingOrUnlocked(anyInt()))
817 .thenAnswer(inv -> {
Makoto Onuki9c850012016-07-26 15:50:50 -0700818 final int userId = (Integer) inv.getArguments()[0];
819 return b(mRunningUsers.get(userId)) && b(mUnlockedUsers.get(userId));
Sunny Goyal145c8f82018-02-15 14:27:09 -0800820 });
821 when(mMockUserManagerInternal.getProfileParentId(anyInt()))
822 .thenAnswer(inv -> {
Makoto Onuki2d895c32016-12-02 15:48:40 -0800823 final int userId = (Integer) inv.getArguments()[0];
824 final UserInfo ui = mUserInfos.get(userId);
825 assertNotNull(ui);
826 if (ui.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) {
Sunny Goyal145c8f82018-02-15 14:27:09 -0800827 return userId;
Makoto Onuki2d895c32016-12-02 15:48:40 -0800828 }
829 final UserInfo parent = mUserInfos.get(ui.profileGroupId);
830 assertNotNull(parent);
Sunny Goyal145c8f82018-02-15 14:27:09 -0800831 return parent.id;
832 });
833
834 when(mMockUserManagerInternal.isProfileAccessible(anyInt(), anyInt(), anyString(),
835 anyBoolean())).thenAnswer(inv -> {
836 final int callingUserId = (Integer) inv.getArguments()[0];
837 final int targetUserId = (Integer) inv.getArguments()[1];
838 if (targetUserId == callingUserId) {
839 return true;
840 }
841 final UserInfo callingUserInfo = mUserInfos.get(callingUserId);
842 final UserInfo targetUserInfo = mUserInfos.get(targetUserId);
843 if (callingUserInfo == null || callingUserInfo.isManagedProfile()
844 || targetUserInfo == null || !targetUserInfo.isEnabled()) {
845 return false;
846 }
847 if (targetUserInfo.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
848 && targetUserInfo.profileGroupId == callingUserInfo.profileGroupId) {
849 return true;
850 }
851 final boolean isExternal = (Boolean) inv.getArguments()[3];
852 if (!isExternal) {
853 return false;
854 }
855 throw new SecurityException(inv.getArguments()[2] + " for unrelated profile "
856 + targetUserId);
857 });
Makoto Onuki2d895c32016-12-02 15:48:40 -0800858
Makoto Onukie70b29e2018-04-03 16:44:39 -0700859 when(mMockUserManager.getUserInfo(anyInt())).thenAnswer(new AnswerWithSystemCheck<>(
860 inv -> mUserInfos.get((Integer) inv.getArguments()[0])));
Makoto Onuki33525d22016-08-03 15:45:24 -0700861 when(mMockActivityManagerInternal.getUidProcessState(anyInt())).thenReturn(
862 ActivityManager.PROCESS_STATE_CACHED_EMPTY);
863
Makoto Onuki9c850012016-07-26 15:50:50 -0700864 // User 0 and P0 are always running
Makoto Onukia2241832016-07-06 13:28:37 -0700865 mRunningUsers.put(USER_0, true);
866 mRunningUsers.put(USER_10, false);
867 mRunningUsers.put(USER_11, false);
Makoto Onuki9c850012016-07-26 15:50:50 -0700868 mRunningUsers.put(USER_P0, true);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800869 mRunningUsers.put(USER_P1, true);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700870
Makoto Onukia2241832016-07-06 13:28:37 -0700871 // Unlock all users by default.
872 mUnlockedUsers.put(USER_0, true);
873 mUnlockedUsers.put(USER_10, true);
874 mUnlockedUsers.put(USER_11, true);
875 mUnlockedUsers.put(USER_P0, true);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800876 mUnlockedUsers.put(USER_P1, true);
Makoto Onukia2241832016-07-06 13:28:37 -0700877
878 // Set up resources
Makoto Onuki157b1622016-06-02 16:13:10 -0700879 setUpAppResources();
880
881 // Start the service.
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700882 initService();
883 setCaller(CALLING_PACKAGE_1);
Makoto Onuki248a0ef2016-11-03 15:59:01 -0700884
885 if (ENABLE_DUMP) {
886 Log.d(TAG, "setUp done");
887 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700888 }
889
Makoto Onukie70b29e2018-04-03 16:44:39 -0700890 /**
891 * Returns a boolean but also checks if the current UID is SYSTEM_UID.
892 */
893 protected class AnswerWithSystemCheck<T> implements Answer<T> {
894 private final Function<InvocationOnMock, T> mChecker;
895
896 public AnswerWithSystemCheck(Function<InvocationOnMock, T> checker) {
897 mChecker = checker;
898 }
899
900 @Override
901 public T answer(InvocationOnMock invocation) throws Throwable {
902 assertEquals("Must be called on SYSTEM UID.",
903 Process.SYSTEM_UID, mInjectedCallingUid);
904 return mChecker.apply(invocation);
905 }
906 }
907
Makoto Onuki9c850012016-07-26 15:50:50 -0700908 private static boolean b(Boolean value) {
909 return (value != null && value);
910 }
911
Makoto Onuki157b1622016-06-02 16:13:10 -0700912 protected void setUpAppResources() throws Exception {
913 setUpAppResources(/* offset = */ 0);
914 }
915
916 protected void setUpAppResources(int ressIdOffset) throws Exception {
917 // ressIdOffset is used to adjust resource IDs to emulate the case where an updated app
918 // has resource IDs changed.
919
920 doAnswer(pmInvocation -> {
921 assertEquals(Process.SYSTEM_UID, mInjectedCallingUid);
922
923 final String packageName = (String) pmInvocation.getArguments()[0];
924 final int userId = (Integer) pmInvocation.getArguments()[1];
925
926 final Resources res = mock(Resources.class);
927
928 doAnswer(resInvocation -> {
929 final int argResId = (Integer) resInvocation.getArguments()[0];
930
931 return "string-" + packageName + "-user:" + userId + "-res:" + argResId
932 + "/" + mInjectedLocale;
933 }).when(res).getString(anyInt());
934
935 doAnswer(resInvocation -> {
936 final int resId = (Integer) resInvocation.getArguments()[0];
937
938 // Always use the "string" resource type. The type doesn't matter during the test.
939 return packageName + ":string/r" + resId;
940 }).when(res).getResourceName(anyInt());
941
942 doAnswer(resInvocation -> {
943 final String argResName = (String) resInvocation.getArguments()[0];
944 final String argType = (String) resInvocation.getArguments()[1];
945 final String argPackageName = (String) resInvocation.getArguments()[2];
946
947 // See the above code. getResourceName() will just use "r" + res ID as the entry
948 // name.
949 String entryName = argResName;
950 if (entryName.contains("/")) {
951 entryName = ShortcutInfo.getResourceEntryName(entryName);
952 }
953 return Integer.parseInt(entryName.substring(1)) + ressIdOffset;
Makoto Onukid0010c52017-03-30 14:17:35 -0700954 }).when(res).getIdentifier(anyStringOrNull(), anyStringOrNull(), anyStringOrNull());
Makoto Onuki157b1622016-06-02 16:13:10 -0700955 return res;
956 }).when(mMockPackageManager).getResourcesForApplicationAsUser(anyString(), anyInt());
957 }
958
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700959 protected static UserInfo withProfileGroupId(UserInfo in, int groupId) {
960 in.profileGroupId = groupId;
961 return in;
962 }
963
964 @Override
965 protected void tearDown() throws Exception {
966 if (DUMP_IN_TEARDOWN) dumpsysOnLogcat("Teardown");
967
968 shutdownServices();
969
970 super.tearDown();
971 }
972
973 protected Context getTestContext() {
974 return getInstrumentation().getContext();
975 }
976
Mehdi Alizadeh39ed1b02018-11-26 19:24:11 -0800977 protected Context getClientContext() {
978 return mClientContext;
979 }
980
Makoto Onukia1d38b32016-06-10 15:32:26 -0700981 protected ShortcutManager getManager() {
982 return mManager;
983 }
984
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700985 protected void deleteAllSavedFiles() {
986 // Empty the data directory.
987 if (mInjectedFilePathRoot.exists()) {
988 Assert.assertTrue("failed to delete dir",
989 FileUtils.deleteContents(mInjectedFilePathRoot));
990 }
991 mInjectedFilePathRoot.mkdirs();
992 }
993
994 /** (Re-) init the manager and the service. */
995 protected void initService() {
996 shutdownServices();
997
998 LocalServices.removeServiceForTest(ShortcutServiceInternal.class);
999
1000 // Instantiate targets.
Makoto Onukia2241832016-07-06 13:28:37 -07001001 mService = new ShortcutServiceTestable(mServiceContext, mLooper);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001002 mManager = new ShortcutManagerTestable(mClientContext, mService);
1003
1004 mInternal = LocalServices.getService(ShortcutServiceInternal.class);
1005
1006 mLauncherAppImpl = new LauncherAppImplTestable(mServiceContext);
1007 mLauncherApps = null;
1008 mLauncherAppsMap.clear();
1009
Makoto Onuki157b1622016-06-02 16:13:10 -07001010 // Send boot sequence events.
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001011 mService.onBootPhase(SystemService.PHASE_LOCK_SETTINGS_READY);
Makoto Onuki157b1622016-06-02 16:13:10 -07001012
Makoto Onuki157b1622016-06-02 16:13:10 -07001013 mService.onBootPhase(SystemService.PHASE_BOOT_COMPLETED);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001014 }
1015
1016 protected void shutdownServices() {
1017 if (mService != null) {
1018 // Flush all the unsaved data from the previous instance.
1019 mService.saveDirtyInfo();
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001020
1021 // Make sure everything is consistent.
1022 mService.verifyStates();
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001023 }
1024 LocalServices.removeServiceForTest(ShortcutServiceInternal.class);
1025
1026 mService = null;
1027 mManager = null;
1028 mInternal = null;
1029 mLauncherAppImpl = null;
1030 mLauncherApps = null;
1031 mLauncherAppsMap.clear();
1032 }
1033
Makoto Onukia2241832016-07-06 13:28:37 -07001034 protected void runOnHandler(Runnable r) {
1035 final long token = mServiceContext.injectClearCallingIdentity();
1036 try {
1037 r.run();
1038 } finally {
1039 mServiceContext.injectRestoreCallingIdentity(token);
1040 }
1041 }
1042
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001043 protected void addPackage(String packageName, int uid, int version) {
1044 addPackage(packageName, uid, version, packageName);
1045 }
1046
1047 protected Signature[] genSignatures(String... signatures) {
1048 final Signature[] sigs = new Signature[signatures.length];
1049 for (int i = 0; i < signatures.length; i++){
1050 sigs[i] = new Signature(signatures[i].getBytes());
1051 }
1052 return sigs;
1053 }
1054
1055 protected PackageInfo genPackage(String packageName, int uid, int version, String... signatures) {
1056 final PackageInfo pi = new PackageInfo();
1057 pi.packageName = packageName;
1058 pi.applicationInfo = new ApplicationInfo();
1059 pi.applicationInfo.uid = uid;
1060 pi.applicationInfo.flags = ApplicationInfo.FLAG_INSTALLED
1061 | ApplicationInfo.FLAG_ALLOW_BACKUP;
1062 pi.versionCode = version;
Patrick Baumannc2def582018-04-04 12:14:15 -07001063 pi.applicationInfo.setVersionCode(version);
Michal Karpinski528c3e52018-02-07 17:47:10 +00001064 pi.signatures = null;
Dan Cashman5c9f527e2018-04-03 16:42:23 -07001065 pi.signingInfo = new SigningInfo(
1066 new PackageParser.SigningDetails(
1067 genSignatures(signatures),
1068 PackageParser.SigningDetails.SignatureSchemeVersion.SIGNING_BLOCK_V3,
1069 null,
Dan Cashman5c9f527e2018-04-03 16:42:23 -07001070 null));
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001071 return pi;
1072 }
1073
1074 protected void addPackage(String packageName, int uid, int version, String... signatures) {
1075 mInjectedPackages.put(packageName, genPackage(packageName, uid, version, signatures));
1076 }
1077
1078 protected void updatePackageInfo(String packageName, Consumer<PackageInfo> c) {
1079 c.accept(mInjectedPackages.get(packageName));
1080 }
1081
1082 protected void updatePackageVersion(String packageName, int increment) {
1083 updatePackageInfo(packageName, pi -> {
1084 pi.versionCode += increment;
Patrick Baumannc2def582018-04-04 12:14:15 -07001085 pi.applicationInfo.setVersionCode(pi.applicationInfo.longVersionCode + increment);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001086 });
1087 }
1088
1089 protected void updatePackageLastUpdateTime(String packageName, long increment) {
1090 updatePackageInfo(packageName, pi -> {
1091 pi.lastUpdateTime += increment;
1092 });
1093 }
1094
Makoto Onuki085a05c2016-08-19 11:39:29 -07001095 protected void setPackageLastUpdateTime(String packageName, long value) {
1096 updatePackageInfo(packageName, pi -> {
1097 pi.lastUpdateTime = value;
1098 });
1099 }
1100
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001101 protected void uninstallPackage(int userId, String packageName) {
1102 if (ENABLE_DUMP) {
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07001103 Log.v(TAG, "Uninstall package " + packageName + " / " + userId);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001104 }
1105 mUninstalledPackages.add(PackageWithUser.of(userId, packageName));
1106 }
1107
1108 protected void installPackage(int userId, String packageName) {
1109 if (ENABLE_DUMP) {
1110 Log.v(TAG, "Install package " + packageName + " / " + userId);
1111 }
1112 mUninstalledPackages.remove(PackageWithUser.of(userId, packageName));
1113 }
1114
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07001115 protected void disablePackage(int userId, String packageName) {
1116 if (ENABLE_DUMP) {
1117 Log.v(TAG, "Disable package " + packageName + " / " + userId);
1118 }
1119 mDisabledPackages.add(PackageWithUser.of(userId, packageName));
1120 }
1121
1122 protected void enablePackage(int userId, String packageName) {
1123 if (ENABLE_DUMP) {
1124 Log.v(TAG, "Enable package " + packageName + " / " + userId);
1125 }
1126 mDisabledPackages.remove(PackageWithUser.of(userId, packageName));
1127 }
1128
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001129 PackageInfo getInjectedPackageInfo(String packageName, @UserIdInt int userId,
1130 boolean getSignatures) {
1131 final PackageInfo pi = mInjectedPackages.get(packageName);
1132 if (pi == null) return null;
1133
1134 final PackageInfo ret = new PackageInfo();
1135 ret.packageName = pi.packageName;
1136 ret.versionCode = pi.versionCode;
Dianne Hackborn3accca02013-09-20 09:32:11 -07001137 ret.versionCodeMajor = pi.versionCodeMajor;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001138 ret.lastUpdateTime = pi.lastUpdateTime;
1139
1140 ret.applicationInfo = new ApplicationInfo(pi.applicationInfo);
1141 ret.applicationInfo.uid = UserHandle.getUid(userId, pi.applicationInfo.uid);
1142 ret.applicationInfo.packageName = pi.packageName;
1143
1144 if (mUninstalledPackages.contains(PackageWithUser.of(userId, packageName))) {
1145 ret.applicationInfo.flags &= ~ApplicationInfo.FLAG_INSTALLED;
1146 }
Makoto Onuki66e4a2b2017-01-23 11:37:45 -08001147 if (mEphemeralPackages.contains(PackageWithUser.of(userId, packageName))) {
Todd Kennedybe0b8892017-02-15 14:13:52 -08001148 ret.applicationInfo.privateFlags |= ApplicationInfo.PRIVATE_FLAG_INSTANT;
Makoto Onuki66e4a2b2017-01-23 11:37:45 -08001149 }
Makoto Onuki33663282016-08-22 16:19:04 -07001150 if (mSystemPackages.contains(packageName)) {
1151 ret.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
1152 }
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07001153 ret.applicationInfo.enabled =
1154 !mDisabledPackages.contains(PackageWithUser.of(userId, packageName));
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001155
1156 if (getSignatures) {
Michal Karpinski528c3e52018-02-07 17:47:10 +00001157 ret.signatures = null;
Dan Cashman5c9f527e2018-04-03 16:42:23 -07001158 ret.signingInfo = pi.signingInfo;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001159 }
1160
1161 return ret;
1162 }
1163
1164 protected void addApplicationInfo(PackageInfo pi, List<ApplicationInfo> list) {
1165 if (pi != null && pi.applicationInfo != null) {
1166 list.add(pi.applicationInfo);
1167 }
1168 }
1169
1170 protected List<ApplicationInfo> getInstalledApplications(int userId) {
1171 final ArrayList<ApplicationInfo> ret = new ArrayList<>();
1172
1173 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_1, userId, false), ret);
1174 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_2, userId, false), ret);
1175 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_3, userId, false), ret);
1176 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_4, userId, false), ret);
1177 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_1, userId, false), ret);
1178 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_2, userId, false), ret);
1179 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_3, userId, false), ret);
1180 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_4, userId, false), ret);
1181
1182 return ret;
1183 }
1184
Makoto Onuki6dd9fb72016-06-01 13:55:54 -07001185 private void addPackageInfo(PackageInfo pi, List<PackageInfo> list) {
1186 if (pi != null) {
1187 list.add(pi);
1188 }
1189 }
1190
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001191 private List<PackageInfo> getInstalledPackagesWithUninstalled(int userId) {
Makoto Onuki6dd9fb72016-06-01 13:55:54 -07001192 final ArrayList<PackageInfo> ret = new ArrayList<>();
1193
1194 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_1, userId, false), ret);
1195 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_2, userId, false), ret);
1196 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_3, userId, false), ret);
1197 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_4, userId, false), ret);
1198 addPackageInfo(getInjectedPackageInfo(LAUNCHER_1, userId, false), ret);
1199 addPackageInfo(getInjectedPackageInfo(LAUNCHER_2, userId, false), ret);
1200 addPackageInfo(getInjectedPackageInfo(LAUNCHER_3, userId, false), ret);
1201 addPackageInfo(getInjectedPackageInfo(LAUNCHER_4, userId, false), ret);
1202
1203 return ret;
1204 }
1205
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001206 protected void addManifestShortcutResource(ComponentName activity, int resId) {
1207 final String packageName = activity.getPackageName();
1208 LinkedHashMap<ComponentName, Integer> map = mActivityMetadataResId.get(packageName);
1209 if (map == null) {
1210 map = new LinkedHashMap<>();
1211 mActivityMetadataResId.put(packageName, map);
1212 }
1213 map.put(activity, resId);
1214 }
1215
1216 protected PackageInfo injectGetActivitiesWithMetadata(String packageName, @UserIdInt int userId) {
1217 final PackageInfo ret = getInjectedPackageInfo(packageName, userId,
1218 /* getSignatures=*/ false);
1219
1220 final HashMap<ComponentName, Integer> activities = mActivityMetadataResId.get(packageName);
1221 if (activities != null) {
1222 final ArrayList<ActivityInfo> list = new ArrayList<>();
1223
1224 for (ComponentName cn : activities.keySet()) {
1225 ActivityInfo ai = new ActivityInfo();
1226 ai.packageName = cn.getPackageName();
1227 ai.name = cn.getClassName();
1228 ai.metaData = new Bundle();
1229 ai.metaData.putInt(ShortcutParser.METADATA_KEY, activities.get(cn));
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001230 ai.applicationInfo = ret.applicationInfo;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001231 list.add(ai);
1232 }
1233 ret.activities = list.toArray(new ActivityInfo[list.size()]);
1234 }
1235 return ret;
1236 }
1237
1238 protected XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
1239 if (!ShortcutParser.METADATA_KEY.equals(key) || activityInfo.metaData == null) {
1240 return null;
1241 }
1242 final int resId = activityInfo.metaData.getInt(key);
1243 return getTestContext().getResources().getXml(resId);
1244 }
1245
1246 /** Replace the current calling package */
1247 protected void setCaller(String packageName, int userId) {
1248 mInjectedClientPackage = packageName;
1249 mInjectedCallingUid =
Daulet Zhanguzinfdb98192019-12-20 18:11:26 +00001250 Objects.requireNonNull(getInjectedPackageInfo(packageName, userId, false),
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001251 "Unknown package").applicationInfo.uid;
1252
1253 // Set up LauncherApps for this caller.
1254 final Pair<Integer, String> key = Pair.create(userId, packageName);
1255 if (!mLauncherAppsMap.containsKey(key)) {
1256 mLauncherAppsMap.put(key, new LauncherAppsTestable(mClientContext, mLauncherAppImpl));
1257 }
1258 mLauncherApps = mLauncherAppsMap.get(key);
1259 }
1260
1261 protected void setCaller(String packageName) {
1262 setCaller(packageName, UserHandle.USER_SYSTEM);
1263 }
1264
1265 protected String getCallingPackage() {
1266 return mInjectedClientPackage;
1267 }
1268
Makoto Onuki2d895c32016-12-02 15:48:40 -08001269 /**
Makoto Onuki7d0fa812018-02-21 11:24:43 -08001270 * This controls {@link ShortcutService#hasShortcutHostPermission}, but
Makoto Onuki2d895c32016-12-02 15:48:40 -08001271 * not {@link ShortcutService#getDefaultLauncher(int)}. To control the later, use
1272 * {@link #setDefaultLauncher(int, ComponentName)}.
1273 */
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001274 protected void setDefaultLauncherChecker(BiPredicate<String, Integer> p) {
1275 mDefaultLauncherChecker = p;
1276 }
1277
Makoto Onuki2d895c32016-12-02 15:48:40 -08001278 /**
1279 * Set the default launcher. This will update {@link #mDefaultLauncherChecker} set by
1280 * {@link #setDefaultLauncherChecker} too.
1281 */
1282 protected void setDefaultLauncher(int userId, ComponentName launcherActivity) {
1283 mDefaultLauncher.put(userId, launcherActivity);
1284
1285 final BiPredicate<String, Integer> oldChecker = mDefaultLauncherChecker;
1286 mDefaultLauncherChecker = (checkPackageName, checkUserId) -> {
1287 if ((checkUserId == userId) && (launcherActivity != null)) {
1288 return launcherActivity.getPackageName().equals(checkPackageName);
1289 }
1290 return oldChecker.test(checkPackageName, checkUserId);
1291 };
1292 }
1293
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001294 protected void runWithCaller(String packageName, int userId, Runnable r) {
1295 final String previousPackage = mInjectedClientPackage;
1296 final int previousUserId = UserHandle.getUserId(mInjectedCallingUid);
1297
1298 setCaller(packageName, userId);
1299
1300 r.run();
1301
1302 setCaller(previousPackage, previousUserId);
1303 }
1304
Makoto Onuki157b1622016-06-02 16:13:10 -07001305 protected void runWithSystemUid(Runnable r) {
1306 final int origUid = mInjectedCallingUid;
1307 mInjectedCallingUid = Process.SYSTEM_UID;
1308 r.run();
1309 mInjectedCallingUid = origUid;
1310 }
1311
1312 protected void lookupAndFillInResourceNames(ShortcutInfo si) {
1313 runWithSystemUid(() -> si.lookupAndFillInResourceNames(
1314 mService.injectGetResourcesForApplicationAsUser(si.getPackage(), si.getUserId())));
1315 }
1316
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001317 protected int getCallingUserId() {
1318 return UserHandle.getUserId(mInjectedCallingUid);
1319 }
1320
1321 protected UserHandle getCallingUser() {
1322 return UserHandle.of(getCallingUserId());
1323 }
1324
1325 /** For debugging */
1326 protected void dumpsysOnLogcat() {
1327 dumpsysOnLogcat("");
1328 }
1329
1330 protected void dumpsysOnLogcat(String message) {
1331 dumpsysOnLogcat(message, false);
1332 }
1333
1334 protected void dumpsysOnLogcat(String message, boolean force) {
1335 if (force || !ENABLE_DUMP) return;
1336
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001337 Log.v(TAG, "Dumping ShortcutService: " + message);
Makoto Onuki50a320e2017-05-31 14:38:42 -07001338 for (String line : dumpsys("-u").split("\n")) {
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001339 Log.v(TAG, line);
1340 }
1341 }
1342
Makoto Onuki76269922016-07-15 14:58:54 -07001343 protected String dumpCheckin() {
Makoto Onuki50a320e2017-05-31 14:38:42 -07001344 return dumpsys("--checkin");
Makoto Onuki76269922016-07-15 14:58:54 -07001345 }
1346
Makoto Onuki50a320e2017-05-31 14:38:42 -07001347 protected String dumpsys(String... args) {
Makoto Onuki76269922016-07-15 14:58:54 -07001348 final ArrayList<String> origPermissions = new ArrayList<>(mCallerPermissions);
1349 mCallerPermissions.add(android.Manifest.permission.DUMP);
1350 try {
1351 final ByteArrayOutputStream out = new ByteArrayOutputStream();
1352 final PrintWriter pw = new PrintWriter(out);
Makoto Onukic4361e32017-04-03 11:24:25 -07001353 mService.dumpNoCheck(/* fd */ null, pw, args);
Makoto Onuki76269922016-07-15 14:58:54 -07001354 pw.close();
1355
1356 return out.toString();
1357 } finally {
1358 mCallerPermissions.clear();
1359 mCallerPermissions.addAll(origPermissions);
1360 }
1361 }
1362
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001363 /**
1364 * For debugging, dump arbitrary file on logcat.
1365 */
1366 protected void dumpFileOnLogcat(String path) {
1367 dumpFileOnLogcat(path, "");
1368 }
1369
1370 protected void dumpFileOnLogcat(String path, String message) {
1371 if (!ENABLE_DUMP) return;
1372
1373 Log.v(TAG, "Dumping file: " + path + " " + message);
1374 final StringBuilder sb = new StringBuilder();
1375 try (BufferedReader br = new BufferedReader(new FileReader(path))) {
1376 String line;
1377 while ((line = br.readLine()) != null) {
1378 Log.v(TAG, line);
1379 }
1380 } catch (Exception e) {
1381 Log.e(TAG, "Couldn't read file", e);
1382 fail("Exception " + e);
1383 }
1384 }
1385
1386 /**
1387 * For debugging, dump the main state file on logcat.
1388 */
1389 protected void dumpBaseStateFile() {
1390 mService.saveDirtyInfo();
1391 dumpFileOnLogcat(mInjectedFilePathRoot.getAbsolutePath()
1392 + "/system/" + ShortcutService.FILENAME_BASE_STATE);
1393 }
1394
1395 /**
1396 * For debugging, dump per-user state file on logcat.
1397 */
1398 protected void dumpUserFile(int userId) {
1399 dumpUserFile(userId, "");
1400 }
1401
1402 protected void dumpUserFile(int userId, String message) {
1403 mService.saveDirtyInfo();
1404 dumpFileOnLogcat(mInjectedFilePathRoot.getAbsolutePath()
1405 + "/user-" + userId
1406 + "/" + ShortcutService.FILENAME_USER_PACKAGES, message);
1407 }
1408
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001409 /**
Makoto Onukia01f4f02016-12-15 15:58:41 -08001410 * Make a shortcut with an ID only.
1411 */
1412 protected ShortcutInfo makeShortcutIdOnly(String id) {
1413 return new ShortcutInfo.Builder(mClientContext, id).build();
1414 }
1415
1416 /**
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001417 * Make a shortcut with an ID.
1418 */
1419 protected ShortcutInfo makeShortcut(String id) {
1420 return makeShortcut(
1421 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001422 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001423 }
1424
Makoto Onukia01f4f02016-12-15 15:58:41 -08001425 @Deprecated // Title was renamed to short label.
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001426 protected ShortcutInfo makeShortcutWithTitle(String id, String title) {
1427 return makeShortcut(
1428 id, title, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001429 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001430 }
1431
Makoto Onukia01f4f02016-12-15 15:58:41 -08001432 protected ShortcutInfo makeShortcutWithShortLabel(String id, String shortLabel) {
1433 return makeShortcut(
1434 id, shortLabel, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001435 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onukia01f4f02016-12-15 15:58:41 -08001436 }
1437
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001438 /**
1439 * Make a shortcut with an ID and timestamp.
1440 */
1441 protected ShortcutInfo makeShortcutWithTimestamp(String id, long timestamp) {
1442 final ShortcutInfo s = makeShortcut(
1443 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001444 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001445 s.setTimestamp(timestamp);
1446 return s;
1447 }
1448
1449 /**
1450 * Make a shortcut with an ID, a timestamp and an activity component
1451 */
1452 protected ShortcutInfo makeShortcutWithTimestampWithActivity(String id, long timestamp,
1453 ComponentName activity) {
1454 final ShortcutInfo s = makeShortcut(
1455 id, "Title-" + id, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001456 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001457 s.setTimestamp(timestamp);
1458 return s;
1459 }
1460
1461 /**
1462 * Make a shortcut with an ID and icon.
1463 */
1464 protected ShortcutInfo makeShortcutWithIcon(String id, Icon icon) {
1465 return makeShortcut(
1466 id, "Title-" + id, /* activity =*/ null, icon,
Makoto Onuki99302b52017-03-29 12:42:26 -07001467 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001468 }
1469
1470 protected ShortcutInfo makePackageShortcut(String packageName, String id) {
1471 String origCaller = getCallingPackage();
1472
1473 setCaller(packageName);
1474 ShortcutInfo s = makeShortcut(
1475 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001476 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001477 setCaller(origCaller); // restore the caller
1478
1479 return s;
1480 }
1481
1482 /**
1483 * Make multiple shortcuts with IDs.
1484 */
1485 protected List<ShortcutInfo> makeShortcuts(String... ids) {
1486 final ArrayList<ShortcutInfo> ret = new ArrayList();
1487 for (String id : ids) {
1488 ret.add(makeShortcut(id));
1489 }
1490 return ret;
1491 }
1492
1493 protected ShortcutInfo.Builder makeShortcutBuilder() {
1494 return new ShortcutInfo.Builder(mClientContext);
1495 }
1496
1497 protected ShortcutInfo makeShortcutWithActivity(String id, ComponentName activity) {
1498 return makeShortcut(
1499 id, "Title-" + id, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001500 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001501 }
1502
Makoto Onukib5a012f2016-06-21 11:13:53 -07001503 protected ShortcutInfo makeShortcutWithIntent(String id, Intent intent) {
1504 return makeShortcut(
1505 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001506 intent, /* rank =*/ 0);
Makoto Onukib5a012f2016-06-21 11:13:53 -07001507 }
1508
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001509 protected ShortcutInfo makeShortcutWithActivityAndTitle(String id, ComponentName activity,
1510 String title) {
1511 return makeShortcut(
1512 id, title, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001513 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001514 }
1515
1516 protected ShortcutInfo makeShortcutWithActivityAndRank(String id, ComponentName activity,
1517 int rank) {
1518 return makeShortcut(
1519 id, "Title-" + id, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001520 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), rank);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001521 }
1522
1523 /**
1524 * Make a shortcut with details.
1525 */
1526 protected ShortcutInfo makeShortcut(String id, String title, ComponentName activity,
Makoto Onuki99302b52017-03-29 12:42:26 -07001527 Icon icon, Intent intent, int rank) {
Makoto Onukib5a012f2016-06-21 11:13:53 -07001528 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
Makoto Onuki255461f2017-01-10 11:47:25 -08001529 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
Makoto Onukib5a012f2016-06-21 11:13:53 -07001530 .setShortLabel(title)
Makoto Onuki99302b52017-03-29 12:42:26 -07001531 .setRank(rank)
1532 .setIntent(intent);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001533 if (icon != null) {
1534 b.setIcon(icon);
1535 }
1536 if (activity != null) {
1537 b.setActivity(activity);
1538 }
1539 final ShortcutInfo s = b.build();
1540
1541 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1542
1543 return s;
1544 }
1545
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001546 protected ShortcutInfo makeShortcutWithIntents(String id, Intent... intents) {
1547 return makeShortcut(
1548 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
1549 intents, /* rank =*/ 0);
1550 }
1551
1552 /**
1553 * Make a shortcut with details.
1554 */
1555 protected ShortcutInfo makeShortcut(String id, String title, ComponentName activity,
1556 Icon icon, Intent[] intents, int rank) {
1557 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
Makoto Onuki255461f2017-01-10 11:47:25 -08001558 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001559 .setShortLabel(title)
1560 .setRank(rank)
1561 .setIntents(intents);
1562 if (icon != null) {
1563 b.setIcon(icon);
1564 }
1565 if (activity != null) {
1566 b.setActivity(activity);
1567 }
1568 final ShortcutInfo s = b.build();
1569
1570 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1571
1572 return s;
1573 }
1574
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001575 /**
Makoto Onukib5a012f2016-06-21 11:13:53 -07001576 * Make a shortcut with details.
1577 */
1578 protected ShortcutInfo makeShortcutWithExtras(String id, Intent intent,
1579 PersistableBundle extras) {
1580 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
Makoto Onuki255461f2017-01-10 11:47:25 -08001581 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
Makoto Onukib5a012f2016-06-21 11:13:53 -07001582 .setShortLabel("title-" + id)
1583 .setExtras(extras)
1584 .setIntent(intent);
1585 final ShortcutInfo s = b.build();
1586
1587 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1588
1589 return s;
1590 }
1591
1592 /**
Mehdi Alizadeha20781c2019-04-25 14:52:02 -07001593 * Make a shortcut with an ID and Category.
1594 */
1595 protected ShortcutInfo makeShortcutWithCategory(String id, Set<String> categories) {
1596 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
1597 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
1598 .setShortLabel("title-" + id)
1599 .setIntent(makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class))
1600 .setCategories(categories);
1601 final ShortcutInfo s = b.build();
1602
1603 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1604
1605 return s;
1606 }
1607
1608 /**
Mehdi Alizadehae808ff2020-01-21 13:39:53 -08001609 * Make a shortcut with an ID and a locus ID.
1610 */
1611 protected ShortcutInfo makeShortcutWithLocusId(String id, LocusId locusId) {
1612 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
1613 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
1614 .setShortLabel("title-" + id)
1615 .setIntent(makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class))
1616 .setLocusId(locusId);
1617 final ShortcutInfo s = b.build();
1618
1619 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1620
1621 return s;
1622 }
1623
1624 /**
Mehdi Alizadeh0de8c292020-01-21 17:27:26 -08001625 * Make a long lived shortcut with an ID.
1626 */
1627 protected ShortcutInfo makeLongLivedShortcut(String id) {
1628 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
1629 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
1630 .setShortLabel("title-" + id)
1631 .setIntent(makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class))
1632 .setLongLived(true);
1633 final ShortcutInfo s = b.build();
1634
1635 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1636
1637 return s;
1638 }
1639
1640 /**
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001641 * Make an intent.
1642 */
1643 protected Intent makeIntent(String action, Class<?> clazz, Object... bundleKeysAndValues) {
1644 final Intent intent = new Intent(action);
1645 intent.setComponent(makeComponent(clazz));
1646 intent.replaceExtras(makeBundle(bundleKeysAndValues));
1647 return intent;
1648 }
1649
1650 /**
Mehdi Alizadeh14242af2018-12-20 20:11:35 -08001651 * Make a Person.
1652 */
1653 protected Person makePerson(CharSequence name, String key, String uri) {
1654 final Person.Builder builder = new Person.Builder();
1655 return builder.setName(name).setKey(key).setUri(uri).build();
1656 }
1657
1658 /**
Mehdi Alizadehae808ff2020-01-21 13:39:53 -08001659 * Make a LocusId.
1660 */
1661 protected LocusId makeLocusId(String id) {
1662 return new LocusId(id);
1663 }
1664
1665 /**
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001666 * Make an component name, with the client context.
1667 */
1668 @NonNull
1669 protected ComponentName makeComponent(Class<?> clazz) {
1670 return new ComponentName(mClientContext, clazz);
1671 }
1672
1673 @NonNull
1674 protected ShortcutInfo findById(List<ShortcutInfo> list, String id) {
1675 for (ShortcutInfo s : list) {
1676 if (s.getId().equals(id)) {
1677 return s;
1678 }
1679 }
1680 fail("Shortcut with id " + id + " not found");
1681 return null;
1682 }
1683
1684 protected void assertSystem() {
1685 assertEquals("Caller must be system", Process.SYSTEM_UID, mInjectedCallingUid);
1686 }
1687
1688 protected void assertResetTimes(long expectedLastResetTime, long expectedNextResetTime) {
1689 assertEquals(expectedLastResetTime, mService.getLastResetTimeLocked());
1690 assertEquals(expectedNextResetTime, mService.getNextResetTimeLocked());
1691 }
1692
1693 public static List<ShortcutInfo> assertAllNotHaveIcon(
1694 List<ShortcutInfo> actualShortcuts) {
1695 for (ShortcutInfo s : actualShortcuts) {
1696 assertNull("ID " + s.getId(), s.getIcon());
1697 }
1698 return actualShortcuts;
1699 }
1700
1701 @NonNull
1702 protected List<ShortcutInfo> assertAllHaveFlags(@NonNull List<ShortcutInfo> actualShortcuts,
1703 int shortcutFlags) {
1704 for (ShortcutInfo s : actualShortcuts) {
1705 assertTrue("ID " + s.getId() + " doesn't have flags " + shortcutFlags,
1706 s.hasFlags(shortcutFlags));
1707 }
1708 return actualShortcuts;
1709 }
1710
1711 protected ShortcutInfo getPackageShortcut(String packageName, String shortcutId, int userId) {
1712 return mService.getPackageShortcutForTest(packageName, shortcutId, userId);
1713 }
1714
1715 protected void assertShortcutExists(String packageName, String shortcutId, int userId) {
1716 assertTrue(getPackageShortcut(packageName, shortcutId, userId) != null);
1717 }
1718
1719 protected void assertShortcutNotExists(String packageName, String shortcutId, int userId) {
1720 assertTrue(getPackageShortcut(packageName, shortcutId, userId) == null);
1721 }
1722
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001723 protected Intent[] launchShortcutAndGetIntentsInner(Runnable shortcutStarter,
1724 @NonNull String packageName, @NonNull String shortcutId, int userId) {
Wale Ogunwale6767eae2018-05-03 15:52:51 -07001725 reset(mMockActivityTaskManagerInternal);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001726 shortcutStarter.run();
1727
1728 final ArgumentCaptor<Intent[]> intentsCaptor = ArgumentCaptor.forClass(Intent[].class);
Wale Ogunwale6767eae2018-05-03 15:52:51 -07001729 verify(mMockActivityTaskManagerInternal).startActivitiesAsPackage(
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001730 eq(packageName),
1731 eq(userId),
1732 intentsCaptor.capture(),
Makoto Onukid0010c52017-03-30 14:17:35 -07001733 anyOrNull(Bundle.class));
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001734 return intentsCaptor.getValue();
1735 }
1736
1737 protected Intent[] launchShortcutAndGetIntents(
1738 @NonNull String packageName, @NonNull String shortcutId, int userId) {
1739 return launchShortcutAndGetIntentsInner(
1740 () -> {
1741 mLauncherApps.startShortcut(packageName, shortcutId, null, null,
1742 UserHandle.of(userId));
1743 }, packageName, shortcutId, userId
1744 );
1745 }
1746
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001747 protected Intent launchShortcutAndGetIntent(
1748 @NonNull String packageName, @NonNull String shortcutId, int userId) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001749 final Intent[] intents = launchShortcutAndGetIntents(packageName, shortcutId, userId);
1750 assertEquals(1, intents.length);
1751 return intents[0];
1752 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001753
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001754 protected Intent[] launchShortcutAndGetIntents_withShortcutInfo(
1755 @NonNull String packageName, @NonNull String shortcutId, int userId) {
1756 return launchShortcutAndGetIntentsInner(
1757 () -> {
1758 mLauncherApps.startShortcut(
1759 getShortcutInfoAsLauncher(packageName, shortcutId, userId), null, null);
1760 }, packageName, shortcutId, userId
1761 );
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001762 }
1763
1764 protected Intent launchShortcutAndGetIntent_withShortcutInfo(
1765 @NonNull String packageName, @NonNull String shortcutId, int userId) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001766 final Intent[] intents = launchShortcutAndGetIntents_withShortcutInfo(
1767 packageName, shortcutId, userId);
1768 assertEquals(1, intents.length);
1769 return intents[0];
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001770 }
1771
1772 protected void assertShortcutLaunchable(@NonNull String packageName, @NonNull String shortcutId,
1773 int userId) {
1774 assertNotNull(launchShortcutAndGetIntent(packageName, shortcutId, userId));
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001775 }
1776
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001777 protected void assertShortcutNotLaunched(@NonNull String packageName,
1778 @NonNull String shortcutId, int userId) {
Wale Ogunwale6767eae2018-05-03 15:52:51 -07001779 reset(mMockActivityTaskManagerInternal);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001780 try {
1781 mLauncherApps.startShortcut(packageName, shortcutId, null, null,
1782 UserHandle.of(userId));
1783 fail("ActivityNotFoundException was not thrown");
1784 } catch (ActivityNotFoundException expected) {
1785 }
1786 // This shouldn't have been called.
Wale Ogunwale6767eae2018-05-03 15:52:51 -07001787 verify(mMockActivityTaskManagerInternal, times(0)).startActivitiesAsPackage(
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001788 anyString(),
1789 anyInt(),
1790 any(Intent[].class),
Makoto Onukid0010c52017-03-30 14:17:35 -07001791 anyOrNull(Bundle.class));
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001792 }
1793
1794 protected void assertStartShortcutThrowsException(@NonNull String packageName,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001795 @NonNull String shortcutId, int userId, Class<?> expectedException) {
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001796 Exception thrown = null;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001797 try {
Makoto Onukid6880792016-06-29 13:37:43 -07001798 mLauncherApps.startShortcut(packageName, shortcutId, null, null,
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001799 UserHandle.of(userId));
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001800 } catch (Exception e) {
1801 thrown = e;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001802 }
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001803 assertNotNull("Exception was not thrown", thrown);
1804 assertEquals("Exception type different", expectedException, thrown.getClass());
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001805 }
1806
1807 protected void assertBitmapDirectories(int userId, String... expectedDirectories) {
1808 final Set<String> expected = hashSet(set(expectedDirectories));
1809
1810 final Set<String> actual = new HashSet<>();
1811
1812 final File[] files = mService.getUserBitmapFilePath(userId).listFiles();
1813 if (files != null) {
1814 for (File child : files) {
1815 if (child.isDirectory()) {
1816 actual.add(child.getName());
1817 }
1818 }
1819 }
1820
1821 assertEquals(expected, actual);
1822 }
1823
1824 protected void assertBitmapFiles(int userId, String packageName, String... expectedFiles) {
1825 final Set<String> expected = hashSet(set(expectedFiles));
1826
1827 final Set<String> actual = new HashSet<>();
1828
1829 final File[] files = new File(mService.getUserBitmapFilePath(userId), packageName)
1830 .listFiles();
1831 if (files != null) {
1832 for (File child : files) {
1833 if (child.isFile()) {
1834 actual.add(child.getName());
1835 }
1836 }
1837 }
1838
1839 assertEquals(expected, actual);
1840 }
1841
1842 protected String getBitmapFilename(int userId, String packageName, String shortcutId) {
1843 final ShortcutInfo si = mService.getPackageShortcutForTest(packageName, shortcutId, userId);
1844 if (si == null) {
1845 return null;
1846 }
Makoto Onuki475c3652017-05-08 14:29:03 -07001847 mService.waitForBitmapSavesForTest();
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001848 return new File(si.getBitmapPath()).getName();
1849 }
1850
Makoto Onuki475c3652017-05-08 14:29:03 -07001851 protected String getBitmapAbsPath(int userId, String packageName, String shortcutId) {
1852 final ShortcutInfo si = mService.getPackageShortcutForTest(packageName, shortcutId, userId);
1853 if (si == null) {
1854 return null;
1855 }
1856 mService.waitForBitmapSavesForTest();
1857 return new File(si.getBitmapPath()).getAbsolutePath();
1858 }
1859
Makoto Onukif3ba2e02016-07-12 09:18:50 -07001860 /**
1861 * @return all shortcuts stored internally for the caller. This reflects the *internal* view
1862 * of shortcuts, which may be different from what {@link #getCallerVisibleShortcuts} would
1863 * return, because getCallerVisibleShortcuts() will get shortcuts from the proper "front door"
1864 * which performs some extra checks, like {@link ShortcutPackage#onRestored}.
1865 */
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001866 protected List<ShortcutInfo> getCallerShortcuts() {
1867 final ShortcutPackage p = mService.getPackageShortcutForTest(
1868 getCallingPackage(), getCallingUserId());
1869 return p == null ? null : p.getAllShortcutsForTest();
1870 }
1871
Makoto Onukif3ba2e02016-07-12 09:18:50 -07001872 /**
Mehdi Alizadeh32774622018-11-05 17:32:01 -08001873 * @return all share targets stored internally for the caller.
1874 */
1875 protected List<ShareTargetInfo> getCallerShareTargets() {
1876 final ShortcutPackage p = mService.getPackageShortcutForTest(
1877 getCallingPackage(), getCallingUserId());
1878 return p == null ? null : p.getAllShareTargetsForTest();
1879 }
1880
1881 /**
Mehdi Alizadeha20781c2019-04-25 14:52:02 -07001882 * @return the number of shortcuts stored internally for the caller that can be used as a share
1883 * target in the ShareSheet. Such shortcuts have a matching category with at least one of the
1884 * defined ShareTargets from the app's Xml resource.
1885 */
1886 protected int getCallerSharingShortcutCount() {
1887 final ShortcutPackage p = mService.getPackageShortcutForTest(
1888 getCallingPackage(), getCallingUserId());
1889 return p == null ? 0 : p.getSharingShortcutCount();
1890 }
1891
1892 /**
Makoto Onukif3ba2e02016-07-12 09:18:50 -07001893 * @return all shortcuts owned by caller that are actually visible via ShortcutManager.
1894 * See also {@link #getCallerShortcuts}.
1895 */
1896 protected List<ShortcutInfo> getCallerVisibleShortcuts() {
1897 final ArrayList<ShortcutInfo> ret = new ArrayList<>();
1898 ret.addAll(mManager.getDynamicShortcuts());
1899 ret.addAll(mManager.getPinnedShortcuts());
1900 ret.addAll(mManager.getManifestShortcuts());
1901 return ret;
1902 }
1903
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001904 protected ShortcutInfo getCallerShortcut(String shortcutId) {
1905 return getPackageShortcut(getCallingPackage(), shortcutId, getCallingUserId());
1906 }
1907
1908 protected List<ShortcutInfo> getLauncherShortcuts(String launcher, int userId, int queryFlags) {
1909 final List<ShortcutInfo>[] ret = new List[1];
1910 runWithCaller(launcher, userId, () -> {
1911 final ShortcutQuery q = new ShortcutQuery();
1912 q.setQueryFlags(queryFlags);
1913 ret[0] = mLauncherApps.getShortcuts(q, UserHandle.of(userId));
1914 });
1915 return ret[0];
1916 }
1917
1918 protected List<ShortcutInfo> getLauncherPinnedShortcuts(String launcher, int userId) {
1919 return getLauncherShortcuts(launcher, userId, ShortcutQuery.FLAG_GET_PINNED);
1920 }
1921
Makoto Onukia01f4f02016-12-15 15:58:41 -08001922 protected List<ShortcutInfo> getShortcutAsLauncher(int targetUserId) {
1923 final ShortcutQuery q = new ShortcutQuery();
1924 q.setQueryFlags(ShortcutQuery.FLAG_MATCH_DYNAMIC | ShortcutQuery.FLAG_MATCH_DYNAMIC
1925 | ShortcutQuery.FLAG_MATCH_PINNED);
1926 return mLauncherApps.getShortcuts(q, UserHandle.of(targetUserId));
1927 }
1928
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001929 protected ShortcutInfo getShortcutInfoAsLauncher(String packageName, String shortcutId,
1930 int userId) {
1931 final List<ShortcutInfo> infoList =
1932 mLauncherApps.getShortcutInfo(packageName, list(shortcutId),
1933 UserHandle.of(userId));
1934 assertEquals("No shortcutInfo found (or too many of them)", 1, infoList.size());
1935 return infoList.get(0);
1936 }
1937
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001938 protected Intent genPackageAddIntent(String packageName, int userId) {
1939 installPackage(userId, packageName);
1940
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001941 Intent i = new Intent(Intent.ACTION_PACKAGE_ADDED);
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001942 i.setData(Uri.parse("package:" + packageName));
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001943 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1944 return i;
1945 }
1946
1947 protected Intent genPackageDeleteIntent(String pakcageName, int userId) {
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001948 uninstallPackage(userId, pakcageName);
1949
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001950 Intent i = new Intent(Intent.ACTION_PACKAGE_REMOVED);
1951 i.setData(Uri.parse("package:" + pakcageName));
1952 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1953 return i;
1954 }
1955
1956 protected Intent genPackageUpdateIntent(String pakcageName, int userId) {
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001957 installPackage(userId, pakcageName);
1958
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001959 Intent i = new Intent(Intent.ACTION_PACKAGE_ADDED);
1960 i.setData(Uri.parse("package:" + pakcageName));
1961 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1962 i.putExtra(Intent.EXTRA_REPLACING, true);
1963 return i;
1964 }
1965
Makoto Onukib08790c2016-06-23 14:05:46 -07001966 protected Intent genPackageChangedIntent(String pakcageName, int userId) {
1967 Intent i = new Intent(Intent.ACTION_PACKAGE_CHANGED);
1968 i.setData(Uri.parse("package:" + pakcageName));
1969 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1970 return i;
1971 }
1972
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001973 protected Intent genPackageDataClear(String packageName, int userId) {
1974 Intent i = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED);
1975 i.setData(Uri.parse("package:" + packageName));
1976 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1977 return i;
1978 }
1979
1980 protected void assertExistsAndShadow(ShortcutPackageItem spi) {
1981 assertNotNull(spi);
1982 assertTrue(spi.getPackageInfo().isShadow());
1983 }
1984
1985 protected File makeFile(File baseDirectory, String... paths) {
1986 File ret = baseDirectory;
1987
1988 for (String path : paths) {
1989 ret = new File(ret, path);
1990 }
1991
1992 return ret;
1993 }
1994
1995 protected boolean bitmapDirectoryExists(String packageName, int userId) {
Makoto Onuki475c3652017-05-08 14:29:03 -07001996 mService.waitForBitmapSavesForTest();
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001997 final File path = new File(mService.getUserBitmapFilePath(userId), packageName);
1998 return path.isDirectory();
1999 }
2000 protected static ShortcutQuery buildQuery(long changedSince,
2001 String packageName, ComponentName componentName,
2002 /* @ShortcutQuery.QueryFlags */ int flags) {
Mehdi Alizadehae808ff2020-01-21 13:39:53 -08002003 return buildQuery(changedSince, packageName, null, null, componentName, flags);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002004 }
2005
2006 protected static ShortcutQuery buildQuery(long changedSince,
Mehdi Alizadehae808ff2020-01-21 13:39:53 -08002007 String packageName, List<String> shortcutIds, List<LocusId> locusIds,
2008 ComponentName componentName, /* @ShortcutQuery.QueryFlags */ int flags) {
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002009 final ShortcutQuery q = new ShortcutQuery();
2010 q.setChangedSince(changedSince);
2011 q.setPackage(packageName);
2012 q.setShortcutIds(shortcutIds);
Mehdi Alizadehae808ff2020-01-21 13:39:53 -08002013 q.setLocusIds(locusIds);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002014 q.setActivity(componentName);
2015 q.setQueryFlags(flags);
2016 return q;
2017 }
2018
2019 protected static ShortcutQuery buildAllQuery(String packageName) {
2020 final ShortcutQuery q = new ShortcutQuery();
2021 q.setPackage(packageName);
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07002022 q.setQueryFlags(ShortcutQuery.FLAG_GET_ALL_KINDS);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002023 return q;
2024 }
2025
2026 protected static ShortcutQuery buildPinnedQuery(String packageName) {
2027 final ShortcutQuery q = new ShortcutQuery();
2028 q.setPackage(packageName);
2029 q.setQueryFlags(ShortcutQuery.FLAG_GET_PINNED);
2030 return q;
2031 }
2032
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07002033 protected static ShortcutQuery buildQueryWithFlags(int queryFlags) {
2034 final ShortcutQuery q = new ShortcutQuery();
2035 q.setQueryFlags(queryFlags);
2036 return q;
2037 }
2038
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002039 protected void backupAndRestore() {
2040 int prevUid = mInjectedCallingUid;
2041
2042 mInjectedCallingUid = Process.SYSTEM_UID; // Only system can call it.
2043
2044 dumpsysOnLogcat("Before backup");
2045
2046 final byte[] payload = mService.getBackupPayload(USER_0);
2047 if (ENABLE_DUMP) {
2048 final String xml = new String(payload);
2049 Log.v(TAG, "Backup payload:");
2050 for (String line : xml.split("\n")) {
2051 Log.v(TAG, line);
2052 }
2053 }
2054
2055 // Before doing anything else, uninstall all packages.
2056 for (int userId : list(USER_0, USER_P0)) {
2057 for (String pkg : list(CALLING_PACKAGE_1, CALLING_PACKAGE_2, CALLING_PACKAGE_3,
2058 LAUNCHER_1, LAUNCHER_2, LAUNCHER_3)) {
2059 uninstallPackage(userId, pkg);
2060 }
2061 }
2062
2063 shutdownServices();
2064
2065 deleteAllSavedFiles();
2066
2067 initService();
2068 mService.applyRestore(payload, USER_0);
2069
2070 // handleUnlockUser will perform the gone package check, but it shouldn't remove
2071 // shadow information.
2072 mService.handleUnlockUser(USER_0);
2073
2074 dumpsysOnLogcat("After restore");
2075
2076 mInjectedCallingUid = prevUid;
2077 }
2078
2079 protected void prepareCrossProfileDataSet() {
Makoto Onuki9c850012016-07-26 15:50:50 -07002080 mRunningUsers.put(USER_10, true); // this test needs user 10.
2081
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002082 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2083 assertTrue(mManager.setDynamicShortcuts(list(
2084 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
2085 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
2086 });
2087 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2088 assertTrue(mManager.setDynamicShortcuts(list(
2089 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
2090 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
2091 });
2092 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
2093 assertTrue(mManager.setDynamicShortcuts(list(
2094 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
2095 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
2096 });
2097 runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
2098 assertTrue(mManager.setDynamicShortcuts(list()));
2099 });
2100 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
2101 assertTrue(mManager.setDynamicShortcuts(list(
2102 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
2103 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
2104 });
2105 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
2106 assertTrue(mManager.setDynamicShortcuts(list(
2107 makeShortcut("x1"), makeShortcut("x2"), makeShortcut("x3"),
2108 makeShortcut("x4"), makeShortcut("x5"), makeShortcut("x6"))));
2109 });
2110
2111 runWithCaller(LAUNCHER_1, USER_0, () -> {
2112 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1"), HANDLE_USER_0);
2113 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s1", "s2"), HANDLE_USER_0);
2114 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s1", "s2", "s3"), HANDLE_USER_0);
2115
2116 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1", "s4"), HANDLE_USER_P0);
2117 });
2118 runWithCaller(LAUNCHER_2, USER_0, () -> {
2119 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2"), HANDLE_USER_0);
2120 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s2", "s3"), HANDLE_USER_0);
2121 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s2", "s3", "s4"), HANDLE_USER_0);
2122
2123 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2", "s5"), HANDLE_USER_P0);
2124 });
Makoto Onukif3ba2e02016-07-12 09:18:50 -07002125
2126 // Note LAUNCHER_3 has allowBackup=false.
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002127 runWithCaller(LAUNCHER_3, USER_0, () -> {
2128 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3"), HANDLE_USER_0);
2129 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s3", "s4"), HANDLE_USER_0);
2130 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s3", "s4", "s5"), HANDLE_USER_0);
2131
2132 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3", "s6"), HANDLE_USER_P0);
2133 });
2134 runWithCaller(LAUNCHER_4, USER_0, () -> {
2135 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list(), HANDLE_USER_0);
2136 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list(), HANDLE_USER_0);
2137 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list(), HANDLE_USER_0);
2138 mLauncherApps.pinShortcuts(CALLING_PACKAGE_4, list(), HANDLE_USER_0);
2139 });
2140
2141 // Launcher on a managed profile is referring ot user 0!
2142 runWithCaller(LAUNCHER_1, USER_P0, () -> {
2143 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3", "s4"), HANDLE_USER_0);
2144 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s3", "s4", "s5"), HANDLE_USER_0);
2145 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s3", "s4", "s5", "s6"),
2146 HANDLE_USER_0);
2147
2148 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s4", "s1"), HANDLE_USER_P0);
2149 });
2150 runWithCaller(LAUNCHER_1, USER_10, () -> {
2151 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("x4", "x5"), HANDLE_USER_10);
2152 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("x4", "x5", "x6"), HANDLE_USER_10);
2153 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("x4", "x5", "x6", "x1"),
2154 HANDLE_USER_10);
2155 });
2156
2157 // Then remove some dynamic shortcuts.
2158 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2159 assertTrue(mManager.setDynamicShortcuts(list(
2160 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2161 });
2162 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2163 assertTrue(mManager.setDynamicShortcuts(list(
2164 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2165 });
2166 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
2167 assertTrue(mManager.setDynamicShortcuts(list(
2168 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2169 });
2170 runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
2171 assertTrue(mManager.setDynamicShortcuts(list()));
2172 });
2173 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
2174 assertTrue(mManager.setDynamicShortcuts(list(
2175 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2176 });
2177 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
2178 assertTrue(mManager.setDynamicShortcuts(list(
2179 makeShortcut("x1"), makeShortcut("x2"), makeShortcut("x3"))));
2180 });
2181 }
Makoto Onukib5a012f2016-06-21 11:13:53 -07002182
2183 public static List<ShortcutInfo> assertAllHaveIconResId(
2184 List<ShortcutInfo> actualShortcuts) {
2185 for (ShortcutInfo s : actualShortcuts) {
2186 assertTrue("ID " + s.getId() + " not have icon res ID", s.hasIconResource());
2187 assertFalse("ID " + s.getId() + " shouldn't have icon FD", s.hasIconFile());
2188 }
2189 return actualShortcuts;
2190 }
2191
2192 public static List<ShortcutInfo> assertAllHaveIconFile(
2193 List<ShortcutInfo> actualShortcuts) {
2194 for (ShortcutInfo s : actualShortcuts) {
2195 assertFalse("ID " + s.getId() + " shouldn't have icon res ID", s.hasIconResource());
2196 assertTrue("ID " + s.getId() + " not have icon FD", s.hasIconFile());
2197 }
2198 return actualShortcuts;
2199 }
2200
2201 public static List<ShortcutInfo> assertAllHaveIcon(
2202 List<ShortcutInfo> actualShortcuts) {
2203 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukia01f4f02016-12-15 15:58:41 -08002204 assertTrue("ID " + s.getId() + " has no icon ",
2205 s.hasIconFile() || s.hasIconResource() || s.getIcon() != null);
Makoto Onukib5a012f2016-06-21 11:13:53 -07002206 }
2207 return actualShortcuts;
2208 }
2209
2210 public static List<ShortcutInfo> assertAllStringsResolved(
2211 List<ShortcutInfo> actualShortcuts) {
2212 for (ShortcutInfo s : actualShortcuts) {
2213 assertTrue("ID " + s.getId(), s.hasStringResourcesResolved());
2214 }
2215 return actualShortcuts;
2216 }
Makoto Onuki76269922016-07-15 14:58:54 -07002217
2218 public String readTestAsset(String assetPath) throws IOException {
2219 final StringBuilder sb = new StringBuilder();
2220 try (BufferedReader br = new BufferedReader(
2221 new InputStreamReader(
2222 getTestContext().getResources().getAssets().open(assetPath)))) {
2223 String line;
2224 while ((line = br.readLine()) != null) {
2225 sb.append(line);
2226 sb.append(System.lineSeparator());
2227 }
2228 }
2229 return sb.toString();
2230 }
Makoto Onuki0b9d1db2016-07-18 14:16:41 -07002231
2232 protected void prepareGetHomeActivitiesAsUser(ComponentName preferred,
2233 List<ResolveInfo> candidates, int userId) {
2234 doAnswer(inv -> {
2235 ((List) inv.getArguments()[0]).addAll(candidates);
2236 return preferred;
2237 }).when(mMockPackageManagerInternal).getHomeActivitiesAsUser(any(List.class), eq(userId));
2238 }
2239
2240 protected static ComponentName cn(String packageName, String name) {
2241 return new ComponentName(packageName, name);
2242 }
2243
2244 protected static ResolveInfo ri(String packageName, String name, boolean isSystem, int priority) {
2245 final ResolveInfo ri = new ResolveInfo();
2246 ri.activityInfo = new ActivityInfo();
2247 ri.activityInfo.applicationInfo = new ApplicationInfo();
2248
2249 ri.activityInfo.packageName = packageName;
2250 ri.activityInfo.name = name;
2251 if (isSystem) {
2252 ri.activityInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
2253 }
2254 ri.priority = priority;
2255 return ri;
2256 }
2257
2258 protected static ResolveInfo getSystemLauncher() {
2259 return ri(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME, true,
2260 PACKAGE_SYSTEM_LAUNCHER_PRIORITY);
2261 }
2262
2263 protected static ResolveInfo getFallbackLauncher() {
2264 return ri(PACKAGE_FALLBACK_LAUNCHER, PACKAGE_FALLBACK_LAUNCHER_NAME, true,
2265 PACKAGE_FALLBACK_LAUNCHER_PRIORITY);
2266 }
Makoto Onukia01f4f02016-12-15 15:58:41 -08002267
Tony Maked6ef622017-12-07 16:36:16 +00002268 protected void makeUidForeground(int uid) {
Makoto Onukia01f4f02016-12-15 15:58:41 -08002269 try {
2270 mService.mUidObserver.onUidStateChanged(
Hui Yu26969322019-08-21 14:56:35 -07002271 uid, ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE, 0,
2272 ActivityManager.PROCESS_CAPABILITY_NONE);
Tony Maked6ef622017-12-07 16:36:16 +00002273 } catch (RemoteException e) {
2274 e.rethrowAsRuntimeException();
2275 }
2276 }
2277
2278 protected void makeCallerForeground() {
2279 makeUidForeground(mInjectedCallingUid);
2280 }
2281
2282 protected void makeUidBackground(int uid) {
2283 try {
2284 mService.mUidObserver.onUidStateChanged(
Hui Yu26969322019-08-21 14:56:35 -07002285 uid, ActivityManager.PROCESS_STATE_TOP_SLEEPING, 0,
2286 ActivityManager.PROCESS_CAPABILITY_NONE);
Makoto Onukia01f4f02016-12-15 15:58:41 -08002287 } catch (RemoteException e) {
2288 e.rethrowAsRuntimeException();
2289 }
2290 }
2291
2292 protected void makeCallerBackground() {
Tony Maked6ef622017-12-07 16:36:16 +00002293 makeUidBackground(mInjectedCallingUid);
Makoto Onukia01f4f02016-12-15 15:58:41 -08002294 }
2295
2296 protected void publishManifestShortcutsAsCaller(int resId) {
2297 addManifestShortcutResource(
2298 new ComponentName(getCallingPackage(), ShortcutActivity.class.getName()),
2299 resId);
2300 updatePackageVersion(getCallingPackage(), 1);
2301 mService.mPackageMonitor.onReceive(getTestContext(),
2302 genPackageAddIntent(getCallingPackage(), getCallingUserId()));
2303 }
Makoto Onuki50a320e2017-05-31 14:38:42 -07002304
2305 protected void assertFileNotExists(String path) {
2306 final File f = new File(mInjectedFilePathRoot, path);
2307 assertFalse("File shouldn't exist: " + f.getAbsolutePath(), f.exists());
2308 }
2309
2310 protected void assertFileExistsWithContent(String path) {
2311 final File f = new File(mInjectedFilePathRoot, path);
2312 assertTrue("File should exist: " + f.getAbsolutePath(), f.exists());
2313 assertTrue("File should be larger than 0b: " + f.getAbsolutePath(), f.length() > 0);
2314 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002315}