BluetoothOppLauncherActivity.java revision 151f41949621e082e127fe468bedd28289bc3a19
1/*
2 * Copyright (c) 2008-2009, Motorola, Inc.
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * - Neither the name of the Motorola, Inc. nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33package com.android.bluetooth.opp;
34
35import com.android.bluetooth.R;
36
37import java.io.File;
38import java.io.FileNotFoundException;
39import java.io.FileOutputStream;
40import java.io.IOException;
41import java.util.ArrayList;
42
43import android.app.Activity;
44import android.bluetooth.BluetoothDevicePicker;
45import android.content.Intent;
46import android.content.Context;
47import android.net.Uri;
48import android.os.Bundle;
49import android.util.Log;
50import android.provider.Settings;
51
52/**
53 * This class is designed to act as the entry point of handling the share intent
54 * via BT from other APPs. and also make "Bluetooth" available in sharing method
55 * selection dialog.
56 */
57public class BluetoothOppLauncherActivity extends Activity {
58    private static final String TAG = "BluetoothLauncherActivity";
59    private static final boolean D = Constants.DEBUG;
60    private static final boolean V = Constants.VERBOSE;
61
62    @Override
63    public void onCreate(Bundle savedInstanceState) {
64        super.onCreate(savedInstanceState);
65
66        Intent intent = getIntent();
67        String action = intent.getAction();
68
69        if (action.equals(Intent.ACTION_SEND) || action.equals(Intent.ACTION_SEND_MULTIPLE)) {
70            /*
71             * Other application is trying to share a file via Bluetooth,
72             * probably Pictures, videos, or vCards. The Intent should contain
73             * an EXTRA_STREAM with the data to attach.
74             */
75            if (action.equals(Intent.ACTION_SEND)) {
76                // TODO: handle type == null case
77                String type = intent.getType();
78                Uri stream = (Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM);
79                CharSequence extra_text = intent.getCharSequenceExtra(Intent.EXTRA_TEXT);
80                // If we get ACTION_SEND intent with EXTRA_STREAM, we'll use the
81                // uri data;
82                // If we get ACTION_SEND intent without EXTRA_STREAM, but with
83                // EXTRA_TEXT, we will try send this TEXT out; Currently in
84                // Browser, share one link goes to this case;
85                if (stream != null && type != null) {
86                    if (V) Log.v(TAG, "Get ACTION_SEND intent: Uri = " + stream + "; mimetype = "
87                                + type);
88                    // Save type/stream, will be used when adding transfer
89                    // session to DB.
90                    BluetoothOppManager.getInstance(this).saveSendingFileInfo(type,
91                            stream.toString());
92                } else if (extra_text != null && type != null) {
93                    if (V) Log.v(TAG, "Get ACTION_SEND intent with Extra_text = "
94                                + extra_text.toString() + "; mimetype = " + type);
95                    Uri fileUri = creatFileForSharedContent(this, extra_text);
96
97                    if (fileUri != null) {
98                        BluetoothOppManager.getInstance(this).saveSendingFileInfo(type,
99                                fileUri.toString());
100                    }
101                } else {
102                    Log.e(TAG, "type is null; or sending file URI is null");
103                    finish();
104                    return;
105                }
106            } else if (action.equals(Intent.ACTION_SEND_MULTIPLE)) {
107                ArrayList<Uri> uris = new ArrayList<Uri>();
108                String mimeType = intent.getType();
109                uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
110                if (mimeType != null && uris != null) {
111                    if (V) Log.v(TAG, "Get ACTION_SHARE_MULTIPLE intent: uris " + uris + "\n Type= "
112                                + mimeType);
113                    BluetoothOppManager.getInstance(this).saveSendingFileInfo(mimeType, uris);
114                } else {
115                    Log.e(TAG, "type is null; or sending files URIs are null");
116                    finish();
117                    return;
118                }
119            }
120
121            if (isAirplaneModeOn()) {
122                Intent in = new Intent(this, BluetoothOppBtErrorActivity.class);
123                in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
124                in.putExtra("title", this.getString(R.string.airplane_error_title));
125                in.putExtra("content", this.getString(R.string.airplane_error_msg));
126                this.startActivity(in);
127
128                finish();
129                return;
130            }
131
132            // TODO: In the future, we may send intent to DevicePickerActivity
133            // directly,
134            // and let DevicePickerActivity to handle Bluetooth Enable.
135            if (!BluetoothOppManager.getInstance(this).isEnabled()) {
136                if (V) Log.v(TAG, "Prepare Enable BT!! ");
137                Intent in = new Intent(this, BluetoothOppBtEnableActivity.class);
138                in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
139                this.startActivity(in);
140            } else {
141                if (V) Log.v(TAG, "BT already enabled!! ");
142                Intent in1 = new Intent(BluetoothDevicePicker.ACTION_LAUNCH);
143                in1.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
144                in1.putExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false);
145                in1.putExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE,
146                        BluetoothDevicePicker.FILTER_TYPE_TRANSFER);
147                in1.putExtra(BluetoothDevicePicker.EXTRA_LAUNCH_PACKAGE,
148                        Constants.THIS_PACKAGE_NAME);
149                in1.putExtra(BluetoothDevicePicker.EXTRA_LAUNCH_CLASS,
150                        BluetoothOppReceiver.class.getName());
151
152                this.startActivity(in1);
153            }
154        } else if (action.equals(Constants.ACTION_OPEN)) {
155            Uri uri = getIntent().getData();
156            if (V) Log.v(TAG, "Get ACTION_OPEN intent: Uri = " + uri);
157
158            Intent intent1 = new Intent();
159            intent1.setAction(action);
160            intent1.setClassName(Constants.THIS_PACKAGE_NAME, BluetoothOppReceiver.class.getName());
161            intent1.setData(uri);
162            this.sendBroadcast(intent1);
163        }
164        finish();
165    }
166
167    /* Returns true if airplane mode is currently on */
168    private final boolean isAirplaneModeOn() {
169        return Settings.System.getInt(this.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
170                0) == 1;
171    }
172    private Uri creatFileForSharedContent(Context context, CharSequence shareContent) {
173        if (shareContent == null) {
174            return null;
175        }
176
177        Uri fileUri = null;
178        FileOutputStream outStream = null;
179        try {
180            String fileName = getString(R.string.bluetooth_share_file_name) + ".html";
181            context.deleteFile(fileName);
182
183            String uri = shareContent.toString();
184            String content = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;"
185                + " charset=UTF-8\"/></head><body>" + "<a href=\"" + uri + "\">" + uri + "</a></p>"
186                + "</body></html>";
187            byte[] byteBuff = content.getBytes();
188
189            outStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);
190            if (outStream != null) {
191                outStream.write(byteBuff, 0, byteBuff.length);
192                fileUri = Uri.fromFile(new File(context.getFilesDir(), fileName));
193                if (fileUri != null) {
194                    if (D) Log.d(TAG, "Created one file for shared content: "
195                            + fileUri.toString());
196                }
197            }
198        } catch (FileNotFoundException e) {
199            Log.e(TAG, "FileNotFoundException: " + e.toString());
200            e.printStackTrace();
201        } catch (IOException e) {
202            Log.e(TAG, "IOException: " + e.toString());
203        } catch (Exception e) {
204            Log.e(TAG, "Exception: " + e.toString());
205        } finally {
206            try {
207                if (outStream != null) {
208                    outStream.close();
209                }
210            } catch (IOException e) {
211                e.printStackTrace();
212            }
213        }
214        return fileUri;
215    }
216}
217