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.app.PendingIntent; 21 import android.content.Intent; 22 import android.os.Bundle; 23 24 public class SubActivityScreen extends Activity { 25 static final int NO_RESULT_MODE = 0; 26 static final int RESULT_MODE = 1; 27 static final int PENDING_RESULT_MODE = 2; 28 static final int FINISH_SUB_MODE = 3; 29 30 static final int CHILD_OFFSET = 1000; 31 32 int mMode; 33 SubActivityScreen()34 public SubActivityScreen() { 35 } 36 37 @Override onCreate(Bundle icicle)38 public void onCreate(Bundle icicle) { 39 super.onCreate(icicle); 40 mMode = getIntent().getIntExtra("mode", mMode); 41 //Log.i("foo", "SubActivityScreen pid=" + Process.myPid() 42 // + " mode=" + mMode); 43 44 // Move on to the next thing that will generate a result... but only 45 // if we are being launched for the first time. 46 if (icicle == null) { 47 if (mMode == PENDING_RESULT_MODE) { 48 PendingIntent apr = createPendingResult(1, null, 49 Intent.FILL_IN_ACTION); 50 Intent res = new Intent(); 51 res.putExtra("tkey", "tval"); 52 res.setAction("test"); 53 try { 54 apr.send(this, RESULT_OK, res); 55 } catch (PendingIntent.CanceledException e) { 56 } 57 } else if (mMode < CHILD_OFFSET) { 58 Intent intent = new Intent(); 59 intent.setClass(this, SubActivityScreen.class); 60 intent.putExtra("mode", CHILD_OFFSET+mMode); 61 //System.out.println("*** Starting from onStart: " + intent); 62 startActivityForResult(intent, 1); 63 return; 64 } 65 } 66 } 67 68 @Override onRestoreInstanceState(Bundle state)69 protected void onRestoreInstanceState(Bundle state) { 70 super.onRestoreInstanceState(state); 71 } 72 73 @Override onResume()74 protected void onResume() { 75 super.onResume(); 76 77 //Log.i("foo", "SubActivityScreen pid=" + Process.myPid() + " onResume"); 78 79 if (mMode >= CHILD_OFFSET) { 80 // Wait a little bit, to give our parent time to kill itself 81 // if that is something it is into. 82 try { 83 Thread.sleep(500); 84 } catch (InterruptedException e) { 85 setResult(RESULT_CANCELED, (new Intent()).setAction("Interrupted!")); 86 finish(); 87 return; 88 } 89 //System.out.println("Resuming sub-activity: mode=" + mMode); 90 switch (mMode-CHILD_OFFSET) { 91 case NO_RESULT_MODE: 92 finish(); 93 break; 94 case RESULT_MODE: 95 Intent res = new Intent(); 96 res.putExtra("tkey", "tval"); 97 res.setAction("test"); 98 setResult(RESULT_OK, res); 99 finish(); 100 break; 101 case FINISH_SUB_MODE: 102 break; 103 } 104 } 105 } 106 107 @Override onActivityResult(int requestCode, int resultCode, Intent data)108 protected void onActivityResult(int requestCode, int resultCode, 109 Intent data) { 110 //Log.i("foo", "SubActivityScreen pid=" + Process.myPid() 111 // + " onActivityResult: req=" + requestCode 112 // + " res=" + resultCode); 113 114 // Assume success. 115 setResult(RESULT_OK); 116 117 if (requestCode == 1) { 118 switch (mMode) { 119 case NO_RESULT_MODE: 120 case FINISH_SUB_MODE: 121 if (resultCode != RESULT_CANCELED) { 122 setResult(RESULT_CANCELED, (new Intent()).setAction( 123 "Incorrect result code returned: " + resultCode)); 124 } 125 break; 126 case RESULT_MODE: 127 case PENDING_RESULT_MODE: 128 if (resultCode != RESULT_OK) { 129 setResult(RESULT_CANCELED, (new Intent()).setAction( 130 "Incorrect result code returned: " + resultCode)); 131 } else if (data == null) { 132 setResult(RESULT_CANCELED, (new Intent()).setAction( 133 "null data returned")); 134 } else if (!("test".equals(data.getAction()))) { 135 setResult(RESULT_CANCELED, (new Intent()).setAction( 136 "Incorrect action returned: " + data)); 137 } else if (!("tval".equals(data.getStringExtra("tkey")))) { 138 setResult(RESULT_CANCELED, (new Intent()).setAction( 139 "Incorrect extras returned: " + data.getExtras())); 140 } 141 break; 142 } 143 } else { 144 setResult(RESULT_CANCELED, (new Intent()).setAction( 145 "Incorrect request code returned: " + requestCode)); 146 } 147 148 finish(); 149 } 150 151 @Override onSaveInstanceState(Bundle outState)152 protected void onSaveInstanceState(Bundle outState) { 153 super.onSaveInstanceState(outState); 154 } 155 156 @Override onStop()157 protected void onStop() { 158 super.onStop(); 159 handleBeforeStopping(); 160 } 161 handleBeforeStopping()162 public void handleBeforeStopping() { 163 if (mMode == FINISH_SUB_MODE) { 164 finishActivity(1); 165 } 166 } 167 } 168 169