1 /* 2 * Copyright (C) 2022 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 android.telephony.cts.util; 18 19 import static com.android.compatibility.common.util.SystemUtil.runWithShellPermissionIdentity; 20 21 import static org.junit.Assert.assertTrue; 22 23 import android.app.role.RoleManager; 24 import android.content.Context; 25 import android.content.pm.PackageManager; 26 import android.os.Process; 27 import android.os.UserHandle; 28 import android.telephony.TelephonyManager; 29 30 import androidx.test.core.app.ApplicationProvider; 31 32 import org.junit.Assume; 33 34 import java.util.concurrent.CountDownLatch; 35 import java.util.concurrent.Executor; 36 37 public class DefaultSmsAppHelper { ensureDefaultSmsApp()38 public static void ensureDefaultSmsApp() { 39 if (!hasTelephony() || !hasSms()) { 40 return; 41 } 42 43 Context context = ApplicationProvider.getApplicationContext(); 44 45 String packageName = context.getPackageName(); 46 RoleManager roleManager = context.getSystemService(RoleManager.class); 47 Executor executor = context.getMainExecutor(); 48 UserHandle user = Process.myUserHandle(); 49 50 CountDownLatch latch = new CountDownLatch(1); 51 boolean[] success = new boolean[1]; 52 53 runWithShellPermissionIdentity(() -> { 54 roleManager.addRoleHolderAsUser( 55 RoleManager.ROLE_SMS, 56 packageName, 57 RoleManager.MANAGE_HOLDERS_FLAG_DONT_KILL_APP, 58 user, 59 executor, 60 successful -> { 61 success[0] = successful; 62 latch.countDown(); 63 }); 64 }); 65 66 try { 67 latch.await(); 68 assertTrue(success[0]); 69 } catch (InterruptedException ex) { 70 throw new RuntimeException(ex.getMessage()); 71 } 72 } 73 stopBeingDefaultSmsApp()74 public static void stopBeingDefaultSmsApp() { 75 if (!hasSms()) { 76 return; 77 } 78 Context context = ApplicationProvider.getApplicationContext(); 79 80 String packageName = context.getPackageName(); 81 RoleManager roleManager = context.getSystemService(RoleManager.class); 82 Executor executor = context.getMainExecutor(); 83 UserHandle user = Process.myUserHandle(); 84 85 CountDownLatch latch = new CountDownLatch(1); 86 boolean[] success = new boolean[1]; 87 88 runWithShellPermissionIdentity(() -> { 89 roleManager.removeRoleHolderAsUser( 90 RoleManager.ROLE_SMS, 91 packageName, 92 RoleManager.MANAGE_HOLDERS_FLAG_DONT_KILL_APP, 93 user, 94 executor, 95 successful -> { 96 success[0] = successful; 97 latch.countDown(); 98 }); 99 }); 100 101 try { 102 latch.await(); 103 assertTrue(success[0]); 104 } catch (InterruptedException ex) { 105 throw new RuntimeException(ex.getMessage()); 106 } 107 } 108 assumeTelephony()109 public static void assumeTelephony() { 110 Assume.assumeTrue(hasTelephony()); 111 } 112 assumeMessaging()113 public static void assumeMessaging() { 114 Assume.assumeTrue(hasSms()); 115 } 116 hasTelephony()117 public static boolean hasTelephony() { 118 Context context = ApplicationProvider.getApplicationContext(); 119 return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY); 120 } 121 hasSms()122 public static boolean hasSms() { 123 TelephonyManager telephonyManager = (TelephonyManager) 124 ApplicationProvider.getApplicationContext().getSystemService( 125 Context.TELEPHONY_SERVICE); 126 return telephonyManager.isSmsCapable(); 127 } 128 } 129