• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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.Activity;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.os.Handler;
23 import android.os.Looper;
24 import android.os.Message;
25 import android.os.MessageQueue;
26 import android.os.SystemClock;
27 
28 public class TestedScreen extends Activity {
29     public static final String WAIT_BEFORE_FINISH = "TestedScreen.WAIT_BEFORE_FINISH";
30     public static final String DELIVER_RESULT = "TestedScreen.DELIVER_RESULT";
31     public static final String CLEAR_TASK = "TestedScreen.CLEAR_TASK";
32 
TestedScreen()33     public TestedScreen() {
34     }
35 
36     @Override
onCreate(Bundle icicle)37     public void onCreate(Bundle icicle) {
38         super.onCreate(icicle);
39         if (LaunchpadActivity.FORWARD_RESULT.equals(getIntent().getAction())) {
40             final Intent intent = new Intent(getIntent());
41             intent.setAction(DELIVER_RESULT);
42             intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
43             startActivity(intent);
44             finish();
45         } else if (DELIVER_RESULT.equals(getIntent().getAction())) {
46             setResult(RESULT_OK, new Intent().setAction(LaunchpadActivity.RETURNED_RESULT));
47             finish();
48         } else if (CLEAR_TASK.equals(getIntent().getAction())) {
49             if (!getIntent().getBooleanExtra(ClearTop.WAIT_CLEAR_TASK, false)) {
50                 launchClearTask();
51             }
52         }
53     }
54 
55     @Override
onRestoreInstanceState(Bundle state)56     protected void onRestoreInstanceState(Bundle state) {
57         super.onRestoreInstanceState(state);
58     }
59 
60     @Override
onResume()61     protected void onResume() {
62         super.onResume();
63         if (CLEAR_TASK.equals(getIntent().getAction())) {
64             if (getIntent().getBooleanExtra(ClearTop.WAIT_CLEAR_TASK, false)) {
65                 Looper.myLooper();
66                 Looper.myQueue().addIdleHandler(new Idler());
67             }
68         } else {
69             Looper.myLooper();
70             Looper.myQueue().addIdleHandler(new Idler());
71         }
72     }
73 
74     @Override
onSaveInstanceState(Bundle outState)75     protected void onSaveInstanceState(Bundle outState) {
76         super.onSaveInstanceState(outState);
77     }
78 
79     @Override
onStop()80     protected void onStop() {
81         super.onStop();
82     }
83 
launchClearTask()84     private void launchClearTask() {
85         final Intent intent = new Intent(getIntent()).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
86                 .setClass(this, ClearTop.class);
87         startActivity(intent);
88     }
89 
90     private final Handler mHandler = new Handler() {
91         @Override
92         public void handleMessage(Message msg) {
93             if (CLEAR_TASK.equals(getIntent().getAction())) {
94                 launchClearTask();
95             } else {
96                 setResult(RESULT_OK);
97                 finish();
98             }
99         }
100     };
101 
102     private class Idler implements MessageQueue.IdleHandler {
queueIdle()103         public final boolean queueIdle() {
104             if (WAIT_BEFORE_FINISH.equals(getIntent().getAction())) {
105                 final Message m = Message.obtain();
106                 mHandler.sendMessageAtTime(m, SystemClock.uptimeMillis() + 1000);
107             } else if (CLEAR_TASK.equals(getIntent().getAction())) {
108                 final Message m = Message.obtain();
109                 mHandler.sendMessageAtTime(m, SystemClock.uptimeMillis() + 1000);
110             } else {
111                 setResult(RESULT_OK);
112                 finish();
113             }
114             return false;
115         }
116     }
117 }
118