summaryrefslogtreecommitdiffstats
path: root/core/tests/coretests/src/android/app/activity/SubActivityScreen.java
blob: 919c59150bb41471835e8549b0486d01c1dbfed6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.app.activity;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;

public class SubActivityScreen extends Activity {
    static final int NO_RESULT_MODE = 0;
    static final int RESULT_MODE = 1;
    static final int PENDING_RESULT_MODE = 2;
    static final int FINISH_SUB_MODE = 3;

    static final int CHILD_OFFSET = 1000;

    int mMode;

    public SubActivityScreen() {
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        mMode = getIntent().getIntExtra("mode", mMode);
        //Log.i("foo", "SubActivityScreen pid=" + Process.myPid()
        //        + " mode=" + mMode);
        
        // Move on to the next thing that will generate a result...  but only
        // if we are being launched for the first time.
        if (icicle == null) {
	        if (mMode == PENDING_RESULT_MODE) {
	            PendingIntent apr = createPendingResult(1, null,
	                    Intent.FILL_IN_ACTION);
	            Intent res = new Intent();
                res.putExtra("tkey", "tval");
                res.setAction("test");
	            try {
    	            apr.send(this, RESULT_OK, res);
	            } catch (PendingIntent.CanceledException e) {
	            }
	        } else if (mMode < CHILD_OFFSET) {
	            Intent intent = new Intent();
	        	intent.setClass(this, SubActivityScreen.class);
	            intent.putExtra("mode", CHILD_OFFSET+mMode);
	            //System.out.println("*** Starting from onStart: " + intent);
	            startActivityForResult(intent, 1);
	            return;
	        }
        }
    }

    @Override
    protected void onRestoreInstanceState(Bundle state) {
        super.onRestoreInstanceState(state);
    }

    @Override
    protected void onResume() {
        super.onResume();

        //Log.i("foo", "SubActivityScreen pid=" + Process.myPid() + " onResume");
        
        if (mMode >= CHILD_OFFSET) {
        	// Wait a little bit, to give our parent time to kill itself
        	// if that is something it is into.
        	try {
	        	Thread.sleep(500);
        	} catch (InterruptedException e) {
        		setResult(RESULT_CANCELED, (new Intent()).setAction("Interrupted!"));
        		finish();
        		return;
        	}
            //System.out.println("Resuming sub-activity: mode=" + mMode);
            switch (mMode-CHILD_OFFSET) {
            case NO_RESULT_MODE:
                finish();
                break;
            case RESULT_MODE:
                Intent res = new Intent();
                res.putExtra("tkey", "tval");
                res.setAction("test");
                setResult(RESULT_OK, res);
                finish();
                break;
            case FINISH_SUB_MODE:
                break;
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
            Intent data) {
        //Log.i("foo", "SubActivityScreen pid=" + Process.myPid()
        //        + " onActivityResult: req=" + requestCode
        //        + " res=" + resultCode);

        // Assume success.
        setResult(RESULT_OK);

        if (requestCode == 1) {
            switch (mMode) {
            case NO_RESULT_MODE:
            case FINISH_SUB_MODE:
                if (resultCode != RESULT_CANCELED) {
                    setResult(RESULT_CANCELED, (new Intent()).setAction(
                            "Incorrect result code returned: " + resultCode));
                }
                break;
            case RESULT_MODE:
            case PENDING_RESULT_MODE:
                if (resultCode != RESULT_OK) {
                    setResult(RESULT_CANCELED, (new Intent()).setAction(
                            "Incorrect result code returned: " + resultCode));
                } else if (data == null) {
                    setResult(RESULT_CANCELED, (new Intent()).setAction(
                            "null data returned"));
                } else if (!("test".equals(data.getAction()))) {
                    setResult(RESULT_CANCELED, (new Intent()).setAction(
                            "Incorrect action returned: " + data));
                } else if (!("tval".equals(data.getStringExtra("tkey")))) {
                    setResult(RESULT_CANCELED, (new Intent()).setAction(
                            "Incorrect extras returned: " + data.getExtras()));
                }
                break;
            }
        } else {
            setResult(RESULT_CANCELED, (new Intent()).setAction(
                    "Incorrect request code returned: " + requestCode));
        }

        finish();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onStop() {
        super.onStop();
        handleBeforeStopping();
    }
    
    public void handleBeforeStopping() {
        if (mMode == FINISH_SUB_MODE) {
            finishActivity(1);
        }
    }
}