BluetoothOppLauncherActivity.java revision 6769b59d715ea98bd72eafcfea9acd2714a887da
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.util.ArrayList;
38import android.app.Activity;
39import android.content.Intent;
40import android.net.Uri;
41import android.os.Bundle;
42import android.util.Log;
43import android.provider.Settings;
44
45/**
46 * This class is designed to act as the entry point of handling the share intent
47 * via BT from other APPs. and also make "Bluetooth" available in sharing method
48 * selection dialog.
49 */
50public class BluetoothOppLauncherActivity extends Activity {
51    private static final String TAG = "BluetoothLauncherActivity";
52
53    @Override
54    public void onCreate(Bundle savedInstanceState) {
55        super.onCreate(savedInstanceState);
56
57        Intent intent = getIntent();
58        String action = intent.getAction();
59
60        if (action.equals(Intent.ACTION_SEND)
61                || action.equals("android.intent.action.ACTION_SHARE_MULTIPLE")) {
62            /*
63             * Other application is trying to share a file via Bluetooth,
64             * probably Pictures, videos, or vCards. The Intent should contain
65             * an EXTRA_STREAM with the data to attach.
66             */
67            if (action.equals(Intent.ACTION_SEND)) {
68                // TODO(Moto): handle type == null case
69                // TODO(Moto): handle extra data is filename case
70                String type = intent.getType();
71                Uri stream = (Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM);
72                if (stream != null && type != null) {
73                    if (Constants.LOGVV) {
74                        Log.v(TAG, "Get ACTION_SEND intent: Uri = " + stream + "; mimetype = "
75                                + type);
76                    }
77                    // Save type/stream, will be used when adding transfer
78                    // session to DB.
79                    BluetoothOppManager.getInstance(this).saveSendingFileInfo(type,
80                            stream.toString());
81                } else {
82                    Log
83                            .e(TAG,
84                                    "type is null; or sending file URI - getParcelableExtra(Intent.EXTRA_STREAM) == null");
85                    finish();
86                    return;
87                }
88            } else if (action.equals("android.intent.action.ACTION_SHARE_MULTIPLE")) {
89                // TODO(Moto): how about send Uri list, not string list?
90                // TODO(Moto): Later will use Intent.ACTION_SEND_MULTIPLE
91                // Convince media team change
92                ArrayList<String> uris = new ArrayList<String>();
93                String mimeType = intent.getType();
94                uris = intent.getStringArrayListExtra(Intent.EXTRA_STREAM);
95                if (mimeType != null && uris != null) {
96                    if (Constants.LOGVV) {
97                        Log.v(TAG, "Get ACTION_SHARE_MULTIPLE intent: uris " + uris + "\n Type= "
98                                + mimeType);
99                    }
100                    BluetoothOppManager.getInstance(this).saveSendingFileInfo(mimeType, uris);
101                } else {
102                    Log
103                            .e(TAG,
104                                    "type is null; or sending files URIs - getStringArrayListExtra(Intent.EXTRA_STREAM) == null");
105                    finish();
106                    return;
107                }
108            }
109
110            if (isAirplaneModeOn()) {
111                Intent in = new Intent(this, BluetoothOppBtErrorActivity.class);
112                in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
113                in.putExtra("title", this.getString(R.string.airplane_error_title));
114                in.putExtra("content", this.getString(R.string.airplane_error_msg));
115                this.startActivity(in);
116
117                finish();
118                return;
119            }
120
121            // TODO(Moto): In the future, we may send intent to DevicePickerActivity
122            // directly,
123            // and let DevicePickerActivity to handle Bluetooth Enable.
124            if (!BluetoothOppManager.getInstance(this).isEnabled()) {
125                if (Constants.LOGVV) {
126                    Log.v(TAG, "Prepare Enable BT!! ");
127                }
128                Intent in = new Intent(this, BluetoothOppBtEnableActivity.class);
129                in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
130                this.startActivity(in);
131            } else {
132                if (Constants.LOGVV) {
133                    Log.v(TAG, "BT already enabled!! ");
134                }
135                Intent in1 = new Intent(this, BluetoothDevicePickerActivity.class);
136                in1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
137                this.startActivity(in1);
138            }
139        } else if (action.equals(Constants.ACTION_OPEN)) {
140            Uri uri = getIntent().getData();
141            if (Constants.LOGVV) {
142                Log.v(TAG, "Get ACTION_OPEN intent: Uri = " + uri);
143            }
144
145            Intent intent1 = new Intent();
146            intent1.setAction(action);
147            intent1.setClassName(Constants.THIS_PACKAGE_NAME, BluetoothOppReceiver.class.getName());
148            intent1.setData(uri);
149            this.sendBroadcast(intent1);
150        }
151        finish();
152        // setContentView(R.layout.main);
153    }
154
155    /* Returns true if airplane mode is currently on */
156    private final boolean isAirplaneModeOn() {
157        return Settings.System.getInt(this.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
158                0) == 1;
159    }
160}
161