• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.jobscheduler.cts.jobtestapp;
18 
19 import android.app.Activity;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.os.Bundle;
25 import android.os.Handler;
26 import android.os.Message;
27 import android.util.Log;
28 
29 /**
30  * Just a no-op activity to keep the test app process in the foreground state when desired.
31  */
32 public class TestActivity extends Activity {
33     private static final String TAG = TestActivity.class.getSimpleName();
34     private static final String PACKAGE_NAME = "android.jobscheduler.cts.jobtestapp";
35     private static final long DEFAULT_WAIT_DURATION = 30_000;
36 
37     static final int FINISH_ACTIVITY_MSG = 1;
38     public static final String ACTION_FINISH_ACTIVITY = PACKAGE_NAME + ".action.FINISH_ACTIVITY";
39 
40     Handler mFinishHandler = new Handler() {
41         @Override
42         public void handleMessage(Message msg) {
43             switch (msg.what) {
44                 case FINISH_ACTIVITY_MSG:
45                     Log.d(TAG, "Finishing test activity: " + TestActivity.class.getCanonicalName());
46                     unregisterReceiver(mFinishReceiver);
47                     finish();
48             }
49         }
50     };
51 
52     final BroadcastReceiver mFinishReceiver = new BroadcastReceiver() {
53         @Override
54         public void onReceive(Context context, Intent intent) {
55             mFinishHandler.removeMessages(FINISH_ACTIVITY_MSG);
56             mFinishHandler.sendEmptyMessage(FINISH_ACTIVITY_MSG);
57         }
58     };
59 
60     @Override
onCreate(Bundle savedInstance)61     public void onCreate(Bundle savedInstance) {
62         Log.d(TAG, "Started test activity: " + TestActivity.class.getCanonicalName());
63         super.onCreate(savedInstance);
64         // automatically finish after 30 seconds.
65         mFinishHandler.sendEmptyMessageDelayed(FINISH_ACTIVITY_MSG, DEFAULT_WAIT_DURATION);
66         registerReceiver(mFinishReceiver, new IntentFilter(ACTION_FINISH_ACTIVITY));
67     }
68 }
69