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.app.cts;
18 
19 import android.app.Service;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.os.Bundle;
24 import android.os.IBinder;
25 import android.os.ResultReceiver;
26 
27 import java.util.concurrent.CountDownLatch;
28 
29 /**
30  * The helper class to start an instrumentation from a different process.
31  */
32 public class InstrumentationHelperService extends Service {
33     private static final String ACTION_START_INSTRUMENTATION =
34             "android.app.cts.ACTION_START_INSTRUMENTATION";
35     private static final String EXTRA_INSTRUMENTATIION_NAME = "instrumentation_name";
36 
37     @Override
onBind(Intent intent)38     public IBinder onBind(Intent intent) {
39         return null;
40     }
41 
42     @Override
onStartCommand(Intent intent, int flags, int startId)43     public int onStartCommand(Intent intent, int flags, int startId) {
44         final String action = intent.getAction();
45         if (ACTION_START_INSTRUMENTATION.equals(action)) {
46             final String instrumentationName = intent.getStringExtra(EXTRA_INSTRUMENTATIION_NAME);
47             final ResultReceiver r = intent.getParcelableExtra(Intent.EXTRA_RESULT_RECEIVER,
48                     ResultReceiver.class);
49 
50             boolean result = false;
51             try {
52                 startInstrumentation(
53                         ComponentName.unflattenFromString(instrumentationName), null, null);
54                 result = true;
55             } catch (SecurityException e) {
56             }
57             r.send(result ? 1 : 0, null);
58         }
59         return START_NOT_STICKY;
60     }
61 
62     /**
63      * Start the given instrumentation from this service and return result.
64      */
startInstrumentation(Context context, String instrumentationName)65     static boolean startInstrumentation(Context context, String instrumentationName)
66             throws InterruptedException {
67         final Intent intent = new Intent(ACTION_START_INSTRUMENTATION);
68         final boolean[] resultHolder = new boolean[1];
69         final CountDownLatch latch = new CountDownLatch(1);
70         final ResultReceiver r = new ResultReceiver(null) {
71             @Override
72             protected void onReceiveResult(int resultCode, Bundle resultData) {
73                 resultHolder[0] = resultCode == 1;
74                 latch.countDown();
75             }
76         };
77         intent.putExtra(EXTRA_INSTRUMENTATIION_NAME, instrumentationName);
78         intent.putExtra(Intent.EXTRA_RESULT_RECEIVER, r);
79         intent.setClassName("android.app.cts", "android.app.cts.InstrumentationHelperService");
80         context.startService(intent);
81         latch.await();
82         return resultHolder[0];
83     }
84 }
85