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