1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.cts.managedprofile; 18 19 import static android.app.admin.DevicePolicyManager.FLAG_MANAGED_CAN_ACCESS_PARENT; 20 import static android.app.admin.DevicePolicyManager.FLAG_PARENT_CAN_ACCESS_MANAGED; 21 import static android.util.DisplayMetrics.DENSITY_DEFAULT; 22 23 import static com.android.cts.managedprofile.BaseManagedProfileTest.ADMIN_RECEIVER_COMPONENT; 24 25 import static junit.framework.Assert.assertTrue; 26 27 import android.app.admin.DevicePolicyManager; 28 import android.content.Context; 29 import android.content.Intent; 30 import android.content.IntentFilter; 31 import android.content.pm.LauncherApps; 32 import android.content.pm.ShortcutInfo; 33 import android.content.pm.ShortcutManager; 34 import android.graphics.Bitmap; 35 import android.graphics.BitmapFactory; 36 import android.graphics.drawable.Drawable; 37 import android.graphics.drawable.Icon; 38 import android.os.Bundle; 39 import android.os.UserHandle; 40 import android.util.DisplayMetrics; 41 42 import androidx.test.InstrumentationRegistry; 43 import androidx.test.runner.AndroidJUnit4; 44 import static com.google.common.truth.Truth.assertThat; 45 46 import org.junit.After; 47 import org.junit.Test; 48 import org.junit.runner.RunWith; 49 50 import java.util.Collections; 51 import java.util.List; 52 53 @RunWith(AndroidJUnit4.class) 54 public class LauncherAppsTest { 55 56 private static final String INTERACT_ACROSS_USERS_FULL_PERMISSION = 57 "android.permission.INTERACT_ACROSS_USERS_FULL"; 58 private static final String ACCESS_SHORTCUTS_PERMISSION = "android.permission.ACCESS_SHORTCUTS"; 59 60 private final Context mContext = InstrumentationRegistry.getContext(); 61 62 @After tearDown()63 public void tearDown() { 64 InstrumentationRegistry.getInstrumentation().getUiAutomation() 65 .dropShellPermissionIdentity(); 66 } 67 68 @Test addDynamicShortcuts()69 public void addDynamicShortcuts() { 70 Bitmap bitmap = BitmapFactory.decodeResource( 71 mContext.getResources(), R.drawable.black_32x32); 72 final Icon icon1 = Icon.createWithBitmap(bitmap); 73 final ShortcutInfo s1 = new ShortcutInfo.Builder(mContext, "s1") 74 .setShortLabel("shortlabel1") 75 .setIcon(icon1) 76 .setIntents(new Intent[]{new Intent(Intent.ACTION_VIEW)}) 77 .build(); 78 ShortcutManager shortcutManager = mContext.getSystemService(ShortcutManager.class); 79 shortcutManager.addDynamicShortcuts(Collections.singletonList(s1)); 80 shortcutManager.updateShortcuts(Collections.singletonList(s1)); 81 } 82 83 @Test removeAllDynamicShortcuts()84 public void removeAllDynamicShortcuts() { 85 ShortcutManager shortcutManager = mContext.getSystemService(ShortcutManager.class); 86 shortcutManager.removeAllDynamicShortcuts(); 87 } 88 89 @Test shortcutIconDrawable_currentToOtherProfile_withUsersFullPermission_isNotNull()90 public void shortcutIconDrawable_currentToOtherProfile_withUsersFullPermission_isNotNull() { 91 InstrumentationRegistry.getInstrumentation().getUiAutomation().adoptShellPermissionIdentity( 92 INTERACT_ACROSS_USERS_FULL_PERMISSION, 93 ACCESS_SHORTCUTS_PERMISSION); 94 UserHandle otherProfileUserHandle = 95 getOtherProfileUserId(InstrumentationRegistry.getArguments()); 96 Context contextAsUser = mContext.createContextAsUser(otherProfileUserHandle, /* flags */ 0); 97 List<ShortcutInfo> shortcuts = getShortcuts(otherProfileUserHandle, contextAsUser); 98 LauncherApps launcherApps = contextAsUser.getSystemService(LauncherApps.class); 99 100 Drawable drawable = launcherApps.getShortcutIconDrawable(shortcuts.get(0), DENSITY_DEFAULT); 101 102 assertThat(drawable).isNotNull(); 103 } 104 105 @Test shortcutIconDrawable_currentToOtherProfile_withoutUsersFullPermission_isNull()106 public void shortcutIconDrawable_currentToOtherProfile_withoutUsersFullPermission_isNull() { 107 InstrumentationRegistry.getInstrumentation().getUiAutomation().adoptShellPermissionIdentity( 108 INTERACT_ACROSS_USERS_FULL_PERMISSION, 109 ACCESS_SHORTCUTS_PERMISSION); 110 UserHandle otherProfileUserHandle = 111 getOtherProfileUserId(InstrumentationRegistry.getArguments()); 112 Context contextAsUser = mContext.createContextAsUser(otherProfileUserHandle, /* flags */ 0); 113 List<ShortcutInfo> shortcuts = getShortcuts(otherProfileUserHandle, contextAsUser); 114 LauncherApps launcherApps = contextAsUser.getSystemService(LauncherApps.class); 115 InstrumentationRegistry.getInstrumentation().getUiAutomation() 116 .dropShellPermissionIdentity(); 117 InstrumentationRegistry.getInstrumentation().getUiAutomation().adoptShellPermissionIdentity( 118 ACCESS_SHORTCUTS_PERMISSION); 119 120 Drawable drawable = launcherApps.getShortcutIconDrawable(shortcuts.get(0), DENSITY_DEFAULT); 121 122 assertThat(drawable).isNull(); 123 } 124 getShortcuts(UserHandle otherProfileUserHandle, Context contextAsUser)125 private List<ShortcutInfo> getShortcuts(UserHandle otherProfileUserHandle, 126 Context contextAsUser) { 127 LauncherApps.ShortcutQuery query = new LauncherApps.ShortcutQuery() 128 .setPackage(mContext.getPackageName()) 129 .setQueryFlags(LauncherApps.ShortcutQuery.FLAG_MATCH_DYNAMIC); 130 return contextAsUser.getSystemService(LauncherApps.class) 131 .getShortcuts(query, otherProfileUserHandle); 132 } 133 getOtherProfileUserId(Bundle bundle)134 private UserHandle getOtherProfileUserId(Bundle bundle) { 135 return UserHandle.of(Integer.parseInt(bundle.getString("otherProfileUserId"))); 136 } 137 } 138