1 /* 2 * Copyright (C) 2023 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 package android.app.stubs; 17 18 import static android.app.stubs.shared.Shared_getBindingUidImportance.ACTION_TEST_PROVIDER; 19 import static android.app.stubs.shared.Shared_getBindingUidImportance.ACTION_TEST_SERVICE_BINDING; 20 21 import static org.junit.Assert.assertNotNull; 22 import static org.junit.Assert.assertTrue; 23 24 import android.content.BroadcastReceiver; 25 import android.content.ComponentName; 26 import android.content.Context; 27 import android.content.Intent; 28 import android.content.ServiceConnection; 29 import android.net.Uri; 30 import android.os.IBinder; 31 import android.os.RemoteException; 32 import android.util.Log; 33 34 import java.util.concurrent.CountDownLatch; 35 import java.util.concurrent.TimeUnit; 36 37 public class Receiver_getBindingUidImportance extends BroadcastReceiver { 38 private static final String TAG = "Receiver_getBindingUidImportance"; 39 40 private static final String MAIN_CTS_PACKAGE = "android.app.cts.getbindinguidimportance"; 41 42 private static final ComponentName SERVICE_NAME = new ComponentName(MAIN_CTS_PACKAGE, 43 "android.app.cts.getbindinguidimportance." 44 + "ActivityManager_getBindingUidImportanceTest$MyService"); 45 46 private static final String AUTHORITY = "android.app.cts.getbindinguidimportance" 47 + ".ActivityManager_getBindingUidImportanceTest.MyProvider"; 48 49 @Override onReceive(Context context, Intent intent)50 public void onReceive(Context context, Intent intent) { 51 Log.d(TAG, "onReceive: intent=" + intent + " my-package: " + context.getPackageName()); 52 53 PendingResult pr = goAsync(); 54 55 new Thread(() -> { 56 try { 57 switch (intent.getAction()) { 58 case ACTION_TEST_SERVICE_BINDING: 59 doTestServiceBinding(context.getApplicationContext()); 60 break; 61 case ACTION_TEST_PROVIDER: 62 doTestProvider(context.getApplicationContext()); 63 break; 64 default: 65 Log.e(TAG, "Unknown intent received: " + intent); 66 break; 67 } 68 } catch (Throwable e) { 69 Log.e(TAG, "Exception caught in receiver", e); 70 } finally { 71 pr.finish(); 72 } 73 }).start(); 74 } 75 doTestServiceBinding(Context context)76 private void doTestServiceBinding(Context context) throws InterruptedException { 77 final CountDownLatch latch = new CountDownLatch(1); 78 79 final var sc = new ServiceConnection() { 80 @Override 81 public void onServiceConnected(ComponentName name, IBinder service) { 82 Log.d(TAG, "Service connected"); 83 latch.countDown(); 84 } 85 86 @Override 87 public void onServiceDisconnected(ComponentName name) { 88 latch.countDown(); 89 } 90 }; 91 92 assertTrue(context.bindService(new Intent().setComponent(SERVICE_NAME), 93 sc, Context.BIND_AUTO_CREATE)); 94 95 assertTrue("Service didn't connect in time", 96 latch.await(60, TimeUnit.SECONDS)); 97 98 context.unbindService(sc); 99 } 100 doTestProvider(Context context)101 private void doTestProvider(Context context) throws RemoteException { 102 try (var client = context.getContentResolver().acquireContentProviderClient(AUTHORITY)) { 103 try (var c = client.query(Uri.parse("content://" + AUTHORITY + "/"), 104 null, null, null)) { 105 assertNotNull(c); 106 } 107 } 108 } 109 } 110