FragmentReceiveResultSupport.java revision e2104f4b5c8e3ad63570306a25e61502dfe4c418
1/*
2 * Copyright (C) 2011 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
17package com.example.android.supportv4.app;
18
19import android.app.PendingIntent;
20import android.content.Intent;
21import android.content.IntentSender;
22import android.os.Bundle;
23import android.support.v4.app.Fragment;
24import android.support.v4.app.FragmentActivity;
25import android.support.v4.app.FragmentTransaction;
26import android.text.Editable;
27import android.view.LayoutInflater;
28import android.view.View;
29import android.view.View.OnClickListener;
30import android.view.ViewGroup;
31import android.widget.Button;
32import android.widget.FrameLayout;
33import android.widget.TextView;
34
35import com.example.android.supportv4.R;
36
37public class FragmentReceiveResultSupport extends FragmentActivity {
38
39    @Override
40    protected void onCreate(Bundle savedInstanceState) {
41        super.onCreate(savedInstanceState);
42        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
43                ViewGroup.LayoutParams.MATCH_PARENT,
44                ViewGroup.LayoutParams.MATCH_PARENT);
45        FrameLayout frame = new FrameLayout(this);
46        frame.setId(R.id.simple_fragment);
47        setContentView(frame, lp);
48
49        if (savedInstanceState == null) {
50            // Do first time initialization -- add fragment.
51            Fragment newFragment = new ReceiveResultFragment();
52            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
53            ft.add(R.id.simple_fragment, newFragment).commit();
54        }
55    }
56
57    public static class ReceiveResultFragment extends Fragment {
58        // Definition of the one requestCode we use for receiving results.
59        static final private int GET_CODE = 0;
60        static final private int GET_INTENT_SENDER_CODE = 1;
61
62        private TextView mResults;
63
64        private OnClickListener mGetListener = new OnClickListener() {
65            @Override
66            public void onClick(View v) {
67                // Start the activity whose result we want to retrieve.  The
68                // result will come back with request code GET_CODE.
69                Intent intent = new Intent(getActivity(), SendResult.class);
70                startActivityForResult(intent, GET_CODE);
71            }
72        };
73
74        private OnClickListener mIntentSenderListener = new OnClickListener() {
75            @Override
76            public void onClick(View v) {
77                // Start the intent sender whose result we want to retrieve.  The
78                // result will come back with request code GET_INTENT_SENDER_CODE.
79                Intent intent = new Intent(getActivity(), SendResult.class);
80                PendingIntent pendingIntent = PendingIntent.getActivity(getContext(),
81                        GET_INTENT_SENDER_CODE, intent, 0);
82                try {
83                    startIntentSenderForResult(pendingIntent.getIntentSender(),
84                            GET_INTENT_SENDER_CODE, null, 0, 0, 0, null);
85                } catch (IntentSender.SendIntentException e) {
86                    // We will be adding to our text.
87                    Editable text = (Editable)mResults.getText();
88                    text.append(e.getMessage());
89                    text.append("\n");
90                }
91            }
92        };
93
94        @Override
95        public void onCreate(Bundle savedInstanceState) {
96            super.onCreate(savedInstanceState);
97        }
98
99        @Override
100        public void onSaveInstanceState(Bundle outState) {
101            super.onSaveInstanceState(outState);
102        }
103
104        @Override
105        public View onCreateView(LayoutInflater inflater, ViewGroup container,
106                Bundle savedInstanceState) {
107            View v = inflater.inflate(R.layout.receive_result, container, false);
108
109            // Retrieve the TextView widget that will display results.
110            mResults = (TextView)v.findViewById(R.id.results);
111
112            // This allows us to later extend the text buffer.
113            mResults.setText(mResults.getText(), TextView.BufferType.EDITABLE);
114
115            // Watch for button clicks.
116            Button getButton = (Button)v.findViewById(R.id.get);
117            getButton.setOnClickListener(mGetListener);
118            Button intentSenderButton = (Button) v.findViewById(R.id.get_intentsender);
119            intentSenderButton.setOnClickListener(mIntentSenderListener);
120
121            return v;
122        }
123
124        /**
125         * This method is called when the sending activity has finished, with the
126         * result it supplied.
127         */
128        @Override
129        public void onActivityResult(int requestCode, int resultCode, Intent data) {
130            // You can use the requestCode to select between multiple child
131            // activities you may have started.  Here there is only one thing
132            // we launch.
133            if (requestCode == GET_CODE || requestCode == GET_INTENT_SENDER_CODE) {
134
135                // We will be adding to our text.
136                Editable text = (Editable)mResults.getText();
137
138                text.append((requestCode == GET_CODE) ? "Activity " : "IntentSender ");
139
140                // This is a standard resultCode that is sent back if the
141                // activity doesn't supply an explicit result.  It will also
142                // be returned if the activity failed to launch.
143                if (resultCode == RESULT_CANCELED) {
144                    text.append("(cancelled)");
145
146                // Our protocol with the sending activity is that it will send
147                // text in 'data' as its result.
148                } else {
149                    text.append("(okay ");
150                    text.append(Integer.toString(resultCode));
151                    text.append(") ");
152                    if (data != null) {
153                        text.append(data.getAction());
154                    }
155                }
156
157                text.append("\n");
158            }
159        }
160    }
161}
162