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