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            public void onClick(View v) {
66                // Start the activity whose result we want to retrieve.  The
67                // result will come back with request code GET_CODE.
68                Intent intent = new Intent(getActivity(), SendResult.class);
69                startActivityForResult(intent, GET_CODE);
70            }
71        };
72
73        private OnClickListener mIntentSenderListener = new OnClickListener() {
74            public void onClick(View v) {
75                // Start the intent sender whose result we want to retrieve.  The
76                // result will come back with request code GET_INTENT_SENDER_CODE.
77                Intent intent = new Intent(getActivity(), SendResult.class);
78                PendingIntent pendingIntent = PendingIntent.getActivity(getContext(),
79                        GET_INTENT_SENDER_CODE, intent, 0);
80                try {
81                    startIntentSenderForResult(pendingIntent.getIntentSender(),
82                            GET_INTENT_SENDER_CODE, null, 0, 0, 0, null);
83                } catch (IntentSender.SendIntentException e) {
84                    // We will be adding to our text.
85                    Editable text = (Editable)mResults.getText();
86                    text.append(e.getMessage());
87                    text.append("\n");
88                }
89            }
90        };
91
92        @Override
93        public void onCreate(Bundle savedInstanceState) {
94            super.onCreate(savedInstanceState);
95        }
96
97        @Override
98        public void onSaveInstanceState(Bundle outState) {
99            super.onSaveInstanceState(outState);
100        }
101
102        @Override
103        public View onCreateView(LayoutInflater inflater, ViewGroup container,
104                Bundle savedInstanceState) {
105            View v = inflater.inflate(R.layout.receive_result, container, false);
106
107            // Retrieve the TextView widget that will display results.
108            mResults = (TextView)v.findViewById(R.id.results);
109
110            // This allows us to later extend the text buffer.
111            mResults.setText(mResults.getText(), TextView.BufferType.EDITABLE);
112
113            // Watch for button clicks.
114            Button getButton = (Button)v.findViewById(R.id.get);
115            getButton.setOnClickListener(mGetListener);
116            Button intentSenderButton = (Button) v.findViewById(R.id.get_intentsender);
117            intentSenderButton.setOnClickListener(mIntentSenderListener);
118
119            return v;
120        }
121
122        /**
123         * This method is called when the sending activity has finished, with the
124         * result it supplied.
125         */
126        @Override
127        public void onActivityResult(int requestCode, int resultCode, Intent data) {
128            // You can use the requestCode to select between multiple child
129            // activities you may have started.  Here there is only one thing
130            // we launch.
131            if (requestCode == GET_CODE || requestCode == GET_INTENT_SENDER_CODE) {
132
133                // We will be adding to our text.
134                Editable text = (Editable)mResults.getText();
135
136                text.append((requestCode == GET_CODE) ? "Activity " : "IntentSender ");
137
138                // This is a standard resultCode that is sent back if the
139                // activity doesn't supply an explicit result.  It will also
140                // be returned if the activity failed to launch.
141                if (resultCode == RESULT_CANCELED) {
142                    text.append("(cancelled)");
143
144                // Our protocol with the sending activity is that it will send
145                // text in 'data' as its result.
146                } else {
147                    text.append("(okay ");
148                    text.append(Integer.toString(resultCode));
149                    text.append(") ");
150                    if (data != null) {
151                        text.append(data.getAction());
152                    }
153                }
154
155                text.append("\n");
156            }
157        }
158    }
159}
160