Home | History | Annotate | Download | only in opp
      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 
     33 package com.android.bluetooth.opp;
     34 
     35 import com.android.bluetooth.R;
     36 
     37 import java.io.File;
     38 import java.io.FileNotFoundException;
     39 import java.io.FileOutputStream;
     40 import java.io.IOException;
     41 import java.util.ArrayList;
     42 
     43 import android.app.Activity;
     44 import android.bluetooth.BluetoothDevice;
     45 import android.bluetooth.BluetoothDevicePicker;
     46 import android.content.Intent;
     47 import android.content.ContentResolver;
     48 import android.content.Context;
     49 import android.net.Uri;
     50 import android.os.Bundle;
     51 import android.util.Log;
     52 import android.provider.Settings;
     53 
     54 /**
     55  * This class is designed to act as the entry point of handling the share intent
     56  * via BT from other APPs. and also make "Bluetooth" available in sharing method
     57  * selection dialog.
     58  */
     59 public class BluetoothOppLauncherActivity extends Activity {
     60     private static final String TAG = "BluetoothLauncherActivity";
     61     private static final boolean D = Constants.DEBUG;
     62     private static final boolean V = Constants.VERBOSE;
     63 
     64     @Override
     65     public void onCreate(Bundle savedInstanceState) {
     66         super.onCreate(savedInstanceState);
     67 
     68         Intent intent = getIntent();
     69         String action = intent.getAction();
     70 
     71         if (action.equals(Intent.ACTION_SEND) || action.equals(Intent.ACTION_SEND_MULTIPLE)) {
     72             /*
     73              * Other application is trying to share a file via Bluetooth,
     74              * probably Pictures, videos, or vCards. The Intent should contain
     75              * an EXTRA_STREAM with the data to attach.
     76              */
     77             if (action.equals(Intent.ACTION_SEND)) {
     78                 // TODO: handle type == null case
     79                 String type = intent.getType();
     80                 Uri stream = (Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM);
     81                 CharSequence extra_text = intent.getCharSequenceExtra(Intent.EXTRA_TEXT);
     82                 // If we get ACTION_SEND intent with EXTRA_STREAM, we'll use the
     83                 // uri data;
     84                 // If we get ACTION_SEND intent without EXTRA_STREAM, but with
     85                 // EXTRA_TEXT, we will try send this TEXT out; Currently in
     86                 // Browser, share one link goes to this case;
     87                 if (stream != null && type != null) {
     88                     if (V) Log.v(TAG, "Get ACTION_SEND intent: Uri = " + stream + "; mimetype = "
     89                                 + type);
     90                     // Save type/stream, will be used when adding transfer
     91                     // session to DB.
     92                     BluetoothOppManager.getInstance(this).saveSendingFileInfo(type,
     93                             stream.toString(), false);
     94                 } else if (extra_text != null && type != null) {
     95                     if (V) Log.v(TAG, "Get ACTION_SEND intent with Extra_text = "
     96                                 + extra_text.toString() + "; mimetype = " + type);
     97                     Uri fileUri = creatFileForSharedContent(this, extra_text);
     98 
     99                     if (fileUri != null) {
    100                         BluetoothOppManager.getInstance(this).saveSendingFileInfo(type,
    101                                 fileUri.toString(), false);
    102                     }
    103                 } else {
    104                     Log.e(TAG, "type is null; or sending file URI is null");
    105                     finish();
    106                     return;
    107                 }
    108             } else if (action.equals(Intent.ACTION_SEND_MULTIPLE)) {
    109                 ArrayList<Uri> uris = new ArrayList<Uri>();
    110                 String mimeType = intent.getType();
    111                 uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
    112                 if (mimeType != null && uris != null) {
    113                     if (V) Log.v(TAG, "Get ACTION_SHARE_MULTIPLE intent: uris " + uris + "\n Type= "
    114                                 + mimeType);
    115                     BluetoothOppManager.getInstance(this).saveSendingFileInfo(mimeType,
    116                             uris, false);
    117                 } else {
    118                     Log.e(TAG, "type is null; or sending files URIs are null");
    119                     finish();
    120                     return;
    121                 }
    122             }
    123 
    124             if (!isBluetoothAllowed()) {
    125                 Intent in = new Intent(this, BluetoothOppBtErrorActivity.class);
    126                 in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    127                 in.putExtra("title", this.getString(R.string.airplane_error_title));
    128                 in.putExtra("content", this.getString(R.string.airplane_error_msg));
    129                 this.startActivity(in);
    130 
    131                 finish();
    132                 return;
    133             }
    134 
    135             // TODO: In the future, we may send intent to DevicePickerActivity
    136             // directly,
    137             // and let DevicePickerActivity to handle Bluetooth Enable.
    138             if (!BluetoothOppManager.getInstance(this).isEnabled()) {
    139                 if (V) Log.v(TAG, "Prepare Enable BT!! ");
    140                 Intent in = new Intent(this, BluetoothOppBtEnableActivity.class);
    141                 in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    142                 this.startActivity(in);
    143             } else {
    144                 if (V) Log.v(TAG, "BT already enabled!! ");
    145                 Intent in1 = new Intent(BluetoothDevicePicker.ACTION_LAUNCH);
    146                 in1.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
    147                 in1.putExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false);
    148                 in1.putExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE,
    149                         BluetoothDevicePicker.FILTER_TYPE_TRANSFER);
    150                 in1.putExtra(BluetoothDevicePicker.EXTRA_LAUNCH_PACKAGE,
    151                         Constants.THIS_PACKAGE_NAME);
    152                 in1.putExtra(BluetoothDevicePicker.EXTRA_LAUNCH_CLASS,
    153                         BluetoothOppReceiver.class.getName());
    154                 if (V) {Log.d(TAG,"Launching " +BluetoothDevicePicker.ACTION_LAUNCH );}
    155                 this.startActivity(in1);
    156             }
    157         } else if (action.equals(Constants.ACTION_OPEN)) {
    158             Uri uri = getIntent().getData();
    159             if (V) Log.v(TAG, "Get ACTION_OPEN intent: Uri = " + uri);
    160 
    161             Intent intent1 = new Intent();
    162             intent1.setAction(action);
    163             intent1.setClassName(Constants.THIS_PACKAGE_NAME, BluetoothOppReceiver.class.getName());
    164             intent1.setData(uri);
    165             this.sendBroadcast(intent1);
    166         }
    167         finish();
    168     }
    169 
    170     /* Returns true if Bluetooth is allowed given current airplane mode settings. */
    171     private final boolean isBluetoothAllowed() {
    172         final ContentResolver resolver = this.getContentResolver();
    173 
    174         // Check if airplane mode is on
    175         final boolean isAirplaneModeOn = Settings.System.getInt(resolver,
    176                 Settings.System.AIRPLANE_MODE_ON, 0) == 1;
    177         if (!isAirplaneModeOn) {
    178             return true;
    179         }
    180 
    181         // Check if airplane mode matters
    182         final String airplaneModeRadios = Settings.System.getString(resolver,
    183                 Settings.System.AIRPLANE_MODE_RADIOS);
    184         final boolean isAirplaneSensitive = airplaneModeRadios == null ? true :
    185                 airplaneModeRadios.contains(Settings.System.RADIO_BLUETOOTH);
    186         if (!isAirplaneSensitive) {
    187             return true;
    188         }
    189 
    190         // Check if Bluetooth may be enabled in airplane mode
    191         final String airplaneModeToggleableRadios = Settings.System.getString(resolver,
    192                 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
    193         final boolean isAirplaneToggleable = airplaneModeToggleableRadios == null ? false :
    194                 airplaneModeToggleableRadios.contains(Settings.System.RADIO_BLUETOOTH);
    195         if (isAirplaneToggleable) {
    196             return true;
    197         }
    198 
    199         // If we get here we're not allowed to use Bluetooth right now
    200         return false;
    201     }
    202 
    203     private Uri creatFileForSharedContent(Context context, CharSequence shareContent) {
    204         if (shareContent == null) {
    205             return null;
    206         }
    207 
    208         Uri fileUri = null;
    209         FileOutputStream outStream = null;
    210         try {
    211             String fileName = getString(R.string.bluetooth_share_file_name) + ".html";
    212             context.deleteFile(fileName);
    213 
    214             String uri = shareContent.toString();
    215             String content = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;"
    216                 + " charset=UTF-8\"/></head><body>" + "<a href=\"" + uri + "\">" + uri + "</a></p>"
    217                 + "</body></html>";
    218             byte[] byteBuff = content.getBytes();
    219 
    220             outStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);
    221             if (outStream != null) {
    222                 outStream.write(byteBuff, 0, byteBuff.length);
    223                 fileUri = Uri.fromFile(new File(context.getFilesDir(), fileName));
    224                 if (fileUri != null) {
    225                     if (D) Log.d(TAG, "Created one file for shared content: "
    226                             + fileUri.toString());
    227                 }
    228             }
    229         } catch (FileNotFoundException e) {
    230             Log.e(TAG, "FileNotFoundException: " + e.toString());
    231             e.printStackTrace();
    232         } catch (IOException e) {
    233             Log.e(TAG, "IOException: " + e.toString());
    234         } catch (Exception e) {
    235             Log.e(TAG, "Exception: " + e.toString());
    236         } finally {
    237             try {
    238                 if (outStream != null) {
    239                     outStream.close();
    240                 }
    241             } catch (IOException e) {
    242                 e.printStackTrace();
    243             }
    244         }
    245         return fileUri;
    246     }
    247 }
    248