1 /*
2  * Copyright (C) 2019 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.assist.common;
18 
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.os.Handler;
22 import android.os.Looper;
23 import android.os.RemoteCallback;
24 
25 public class BaseRemoteCallbackActivity extends Activity {
26 
27     private RemoteCallback mRemoteCallback;
28 
29     @Override
onCreate(Bundle savedInstanceState)30     protected void onCreate(Bundle savedInstanceState) {
31         super.onCreate(savedInstanceState);
32         registerReceivingCallback();
33     }
34 
notify(String action)35     protected void notify(String action) {
36         RemoteCallback callback = getIntent().getParcelableExtra(Utils.EXTRA_REMOTE_CALLBACK);
37 
38         if (callback == null) {
39             throw new IllegalStateException("Test ran expecting " + action
40                     + " but did not register callback");
41         } else {
42             Bundle bundle = new Bundle();
43             bundle.putString(Utils.EXTRA_REMOTE_CALLBACK_ACTION, action);
44             callback.sendResult(bundle);
45         }
46     }
47 
registerReceivingCallback()48     protected void registerReceivingCallback() {
49         RemoteCallback remoteCallback = getIntent().getParcelableExtra(Utils.EXTRA_REMOTE_CALLBACK_RECEIVING);
50         if (remoteCallback == null) {
51             return;
52 
53         }
54 
55         mRemoteCallback = new RemoteCallback((results) -> {
56             String action = results.getString(Utils.EXTRA_REMOTE_CALLBACK_ACTION);
57             if (action.equals(Utils.ACTION_END_OF_TEST)) {
58                 if (!isFinishing()) {
59                     finish();
60                 }
61             } else {
62                 onReceivedEventFromCaller(results, action);
63             }
64         }, new Handler(Looper.getMainLooper()));
65 
66         Bundle bundle = new Bundle();
67         bundle.putString(Utils.EXTRA_REMOTE_CALLBACK_ACTION, Utils.EXTRA_REMOTE_CALLBACK_RECEIVING_ACTION);
68         bundle.putParcelable(Utils.EXTRA_REMOTE_CALLBACK_RECEIVING, mRemoteCallback);
69         remoteCallback.sendResult(bundle);
70     }
71 
72     /**
73      * React to events from the calling test. Called from the main thread.
74      */
onReceivedEventFromCaller(Bundle results, String action)75     protected void onReceivedEventFromCaller(Bundle results, String action) {
76     }
77 }
78