am e3e1e8d1: Fixed potention ANR issue with Bluetooth OPP share.

* commit 'e3e1e8d1fe2a75b38ee1dfe5a102685877a22f84':
  Fixed potention ANR issue with Bluetooth OPP share.
diff --git a/src/com/android/bluetooth/opp/BluetoothOppLauncherActivity.java b/src/com/android/bluetooth/opp/BluetoothOppLauncherActivity.java
index f9e96f5..bbe7899 100644
--- a/src/com/android/bluetooth/opp/BluetoothOppLauncherActivity.java
+++ b/src/com/android/bluetooth/opp/BluetoothOppLauncherActivity.java
@@ -69,6 +69,17 @@
         String action = intent.getAction();
 
         if (action.equals(Intent.ACTION_SEND) || action.equals(Intent.ACTION_SEND_MULTIPLE)) {
+            //Check if Bluetooth is available in the beginning instead of at the end
+            if (!isBluetoothAllowed()) {
+                Intent in = new Intent(this, BluetoothOppBtErrorActivity.class);
+                in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+                in.putExtra("title", this.getString(R.string.airplane_error_title));
+                in.putExtra("content", this.getString(R.string.airplane_error_msg));
+                startActivity(in);
+                finish();
+                return;
+            }
+
             /*
              * Other application is trying to share a file via Bluetooth,
              * probably Pictures, videos, or vCards. The Intent should contain
@@ -76,8 +87,8 @@
              */
             if (action.equals(Intent.ACTION_SEND)) {
                 // TODO: handle type == null case
-                String type = intent.getType();
-                Uri stream = (Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM);
+                final String type = intent.getType();
+                final Uri stream = (Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM);
                 CharSequence extra_text = intent.getCharSequenceExtra(Intent.EXTRA_TEXT);
                 // If we get ACTION_SEND intent with EXTRA_STREAM, we'll use the
                 // uri data;
@@ -89,16 +100,38 @@
                                 + type);
                     // Save type/stream, will be used when adding transfer
                     // session to DB.
-                    BluetoothOppManager.getInstance(this).saveSendingFileInfo(type,
-                            stream.toString(), false);
+                    Thread t = new Thread(new Runnable() {
+                        public void run() {
+                            BluetoothOppManager.getInstance(BluetoothOppLauncherActivity.this)
+                                .saveSendingFileInfo(type,stream.toString(), false);
+                            //Done getting file info..Launch device picker and finish this activity
+                            launchDevicePicker();
+                            finish();
+                        }
+                    });
+                    t.start();
+                    return;
                 } else if (extra_text != null && type != null) {
                     if (V) Log.v(TAG, "Get ACTION_SEND intent with Extra_text = "
                                 + extra_text.toString() + "; mimetype = " + type);
-                    Uri fileUri = creatFileForSharedContent(this, extra_text);
-
+                    final Uri fileUri = creatFileForSharedContent(this, extra_text);
                     if (fileUri != null) {
-                        BluetoothOppManager.getInstance(this).saveSendingFileInfo(type,
-                                fileUri.toString(), false);
+                        Thread t = new Thread(new Runnable() {
+                            public void run() {
+                                BluetoothOppManager.getInstance(BluetoothOppLauncherActivity.this)
+                                    .saveSendingFileInfo(type,fileUri.toString(), false);
+                                //Done getting file info..Launch device picker
+                                //and finish this activity
+                                launchDevicePicker();
+                                finish();
+                            }
+                        });
+                        t.start();
+                        return;
+                    } else {
+                        Log.w(TAG,"Error trying to do set text...File not created!");
+                        finish();
+                        return;
                     }
                 } else {
                     Log.e(TAG, "type is null; or sending file URI is null");
@@ -106,54 +139,29 @@
                     return;
                 }
             } else if (action.equals(Intent.ACTION_SEND_MULTIPLE)) {
-                ArrayList<Uri> uris = new ArrayList<Uri>();
-                String mimeType = intent.getType();
-                uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
+                final String mimeType = intent.getType();
+                final ArrayList<Uri> uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
                 if (mimeType != null && uris != null) {
                     if (V) Log.v(TAG, "Get ACTION_SHARE_MULTIPLE intent: uris " + uris + "\n Type= "
                                 + mimeType);
-                    BluetoothOppManager.getInstance(this).saveSendingFileInfo(mimeType,
-                            uris, false);
+                    Thread t = new Thread(new Runnable() {
+                        public void run() {
+                            BluetoothOppManager.getInstance(BluetoothOppLauncherActivity.this)
+                                .saveSendingFileInfo(mimeType,uris, false);
+                            //Done getting file info..Launch device picker
+                            //and finish this activity
+                            launchDevicePicker();
+                            finish();
+                        }
+                    });
+                    t.start();
+                    return;
                 } else {
                     Log.e(TAG, "type is null; or sending files URIs are null");
                     finish();
                     return;
                 }
             }
-
-            if (!isBluetoothAllowed()) {
-                Intent in = new Intent(this, BluetoothOppBtErrorActivity.class);
-                in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-                in.putExtra("title", this.getString(R.string.airplane_error_title));
-                in.putExtra("content", this.getString(R.string.airplane_error_msg));
-                this.startActivity(in);
-
-                finish();
-                return;
-            }
-
-            // TODO: In the future, we may send intent to DevicePickerActivity
-            // directly,
-            // and let DevicePickerActivity to handle Bluetooth Enable.
-            if (!BluetoothOppManager.getInstance(this).isEnabled()) {
-                if (V) Log.v(TAG, "Prepare Enable BT!! ");
-                Intent in = new Intent(this, BluetoothOppBtEnableActivity.class);
-                in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-                this.startActivity(in);
-            } else {
-                if (V) Log.v(TAG, "BT already enabled!! ");
-                Intent in1 = new Intent(BluetoothDevicePicker.ACTION_LAUNCH);
-                in1.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
-                in1.putExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false);
-                in1.putExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE,
-                        BluetoothDevicePicker.FILTER_TYPE_TRANSFER);
-                in1.putExtra(BluetoothDevicePicker.EXTRA_LAUNCH_PACKAGE,
-                        Constants.THIS_PACKAGE_NAME);
-                in1.putExtra(BluetoothDevicePicker.EXTRA_LAUNCH_CLASS,
-                        BluetoothOppReceiver.class.getName());
-                if (V) {Log.d(TAG,"Launching " +BluetoothDevicePicker.ACTION_LAUNCH );}
-                this.startActivity(in1);
-            }
         } else if (action.equals(Constants.ACTION_OPEN)) {
             Uri uri = getIntent().getData();
             if (V) Log.v(TAG, "Get ACTION_OPEN intent: Uri = " + uri);
@@ -163,10 +171,41 @@
             intent1.setClassName(Constants.THIS_PACKAGE_NAME, BluetoothOppReceiver.class.getName());
             intent1.setData(uri);
             this.sendBroadcast(intent1);
+            finish();
+        } else {
+            Log.w(TAG, "Unsupported action: " + action);
+            finish();
         }
-        finish();
     }
 
+    /**
+     * Turns on Bluetooth if not already on, or launches device picker if Bluetooth is on
+     * @return
+     */
+    private final void launchDevicePicker() {
+        // TODO: In the future, we may send intent to DevicePickerActivity
+        // directly,
+        // and let DevicePickerActivity to handle Bluetooth Enable.
+        if (!BluetoothOppManager.getInstance(this).isEnabled()) {
+            if (V) Log.v(TAG, "Prepare Enable BT!! ");
+            Intent in = new Intent(this, BluetoothOppBtEnableActivity.class);
+            in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+            startActivity(in);
+        } else {
+            if (V) Log.v(TAG, "BT already enabled!! ");
+            Intent in1 = new Intent(BluetoothDevicePicker.ACTION_LAUNCH);
+            in1.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
+            in1.putExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false);
+            in1.putExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE,
+                    BluetoothDevicePicker.FILTER_TYPE_TRANSFER);
+            in1.putExtra(BluetoothDevicePicker.EXTRA_LAUNCH_PACKAGE,
+                    Constants.THIS_PACKAGE_NAME);
+            in1.putExtra(BluetoothDevicePicker.EXTRA_LAUNCH_CLASS,
+                    BluetoothOppReceiver.class.getName());
+            if (V) {Log.d(TAG,"Launching " +BluetoothDevicePicker.ACTION_LAUNCH );}
+            startActivity(in1);
+        }
+    }
     /* Returns true if Bluetooth is allowed given current airplane mode settings. */
     private final boolean isBluetoothAllowed() {
         final ContentResolver resolver = this.getContentResolver();