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 com.android.stubs.am;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.os.Message;
23 import android.os.Messenger;
24 import android.os.RemoteException;
25 import android.util.Log;
26 
27 import com.android.frameworks.perftests.am.util.Constants;
28 
29 public class TestActivity extends Activity {
30     private static final String TAG = "TestActivity";
31     private static final boolean VERBOSE = InitService.VERBOSE;
32 
33     private Messenger mResultTo;
34 
35     @Override
onCreate(Bundle icicle)36     public void onCreate(Bundle icicle) {
37         super.onCreate(icicle);
38         if (VERBOSE) {
39             Log.i(TAG, getPackageName() + " onCreate()");
40         }
41         setContentView(R.layout.activity_content);
42         mResultTo = getIntent().getParcelableExtra(Constants.EXTRA_RECEIVER_CALLBACK);
43     }
44 
45     @Override
onNewIntent(Intent intent)46     public void onNewIntent(Intent intent) {
47         super.onNewIntent(intent);
48         setIntent(intent);
49         mResultTo = intent.getParcelableExtra(Constants.EXTRA_RECEIVER_CALLBACK);
50         if (intent.getBooleanExtra(Constants.EXTRA_REQ_FINISH_ACTIVITY, false)) {
51             if (VERBOSE) {
52                 Log.i(TAG, getPackageName() + " finishing activity");
53             }
54             finish();
55         }
56     }
57 
58     @Override
onResume()59     protected void onResume() {
60         super.onResume();
61         if (VERBOSE) {
62             Log.i(TAG, getPackageName() + " onResume()");
63         }
64         sendResult(Constants.RESULT_NO_ERROR);
65     }
66 
67     @Override
onDestroy()68     protected void onDestroy() {
69         super.onDestroy();
70         if (VERBOSE) {
71             Log.i(TAG, getPackageName() + " onDestroy()");
72         }
73         sendResult(Constants.RESULT_NO_ERROR);
74     }
75 
sendResult(int result)76     private void sendResult(int result) {
77         Message msg = Message.obtain();
78         msg.arg1 = getIntent().getIntExtra(Constants.EXTRA_ARG1, -1);
79         msg.arg2 = result;
80         try {
81             mResultTo.send(msg);
82         } catch (RemoteException e) {
83             Log.e(TAG, "Error in sending result back", e);
84         }
85     }
86 }
87