Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit beb645bb authored by Nick Pelly's avatar Nick Pelly
Browse files

First cut at Bluetooth Beam.

Add Bluetooth address white-listing to avoid receive confirmation from
Beam interactions.

Change-Id: I25720c6e902c494cbb9845a30a175edad4225b89
parent ad3cd982
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -43,6 +43,7 @@
                <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
                <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
                <!--action android:name="android.intent.action.BOOT_COMPLETED" /-->
                <!--action android:name="android.intent.action.BOOT_COMPLETED" /-->
                <action android:name="android.btopp.intent.action.OPEN_RECEIVED_FILES" />
                <action android:name="android.btopp.intent.action.OPEN_RECEIVED_FILES" />
                <action android:name="todo-whitelist" />
            </intent-filter>
            </intent-filter>
        </receiver>
        </receiver>
        <activity android:name=".opp.BluetoothOppLauncherActivity"
        <activity android:name=".opp.BluetoothOppLauncherActivity"
+9 −1
Original line number Original line Diff line number Diff line
@@ -41,6 +41,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.ArrayList;


import android.app.Activity;
import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevicePicker;
import android.bluetooth.BluetoothDevicePicker;
import android.content.Intent;
import android.content.Intent;
import android.content.ContentResolver;
import android.content.ContentResolver;
@@ -66,6 +67,7 @@ public class BluetoothOppLauncherActivity extends Activity {


        Intent intent = getIntent();
        Intent intent = getIntent();
        String action = intent.getAction();
        String action = intent.getAction();
        BluetoothDevice device = null;


        if (action.equals(Intent.ACTION_SEND) || action.equals(Intent.ACTION_SEND_MULTIPLE)) {
        if (action.equals(Intent.ACTION_SEND) || action.equals(Intent.ACTION_SEND_MULTIPLE)) {
            /*
            /*
@@ -78,6 +80,9 @@ public class BluetoothOppLauncherActivity extends Activity {
                String type = intent.getType();
                String type = intent.getType();
                Uri stream = (Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM);
                Uri stream = (Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM);
                CharSequence extra_text = intent.getCharSequenceExtra(Intent.EXTRA_TEXT);
                CharSequence extra_text = intent.getCharSequenceExtra(Intent.EXTRA_TEXT);
                //TODO: consider only checking EXTRA_DEVICE if it came from NFC
                device = (BluetoothDevice)intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                // If we get ACTION_SEND intent with EXTRA_STREAM, we'll use the
                // If we get ACTION_SEND intent with EXTRA_STREAM, we'll use the
                // uri data;
                // uri data;
                // If we get ACTION_SEND intent without EXTRA_STREAM, but with
                // If we get ACTION_SEND intent without EXTRA_STREAM, but with
@@ -138,7 +143,7 @@ public class BluetoothOppLauncherActivity extends Activity {
                Intent in = new Intent(this, BluetoothOppBtEnableActivity.class);
                Intent in = new Intent(this, BluetoothOppBtEnableActivity.class);
                in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                this.startActivity(in);
                this.startActivity(in);
            } else {
            } else if (device == null) {
                if (V) Log.v(TAG, "BT already enabled!! ");
                if (V) Log.v(TAG, "BT already enabled!! ");
                Intent in1 = new Intent(BluetoothDevicePicker.ACTION_LAUNCH);
                Intent in1 = new Intent(BluetoothDevicePicker.ACTION_LAUNCH);
                in1.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                in1.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
@@ -151,6 +156,9 @@ public class BluetoothOppLauncherActivity extends Activity {
                        BluetoothOppReceiver.class.getName());
                        BluetoothOppReceiver.class.getName());


                this.startActivity(in1);
                this.startActivity(in1);
            } else {
                // we already know where to send to
                BluetoothOppManager.getInstance(this).startTransfer(device);
            }
            }
        } else if (action.equals(Constants.ACTION_OPEN)) {
        } else if (action.equals(Constants.ACTION_OPEN)) {
            Uri uri = getIntent().getData();
            Uri uri = getIntent().getData();
+12 −0
Original line number Original line Diff line number Diff line
@@ -47,6 +47,7 @@ import android.text.TextUtils;
import android.util.Log;
import android.util.Log;


import java.util.ArrayList;
import java.util.ArrayList;
import java.util.List;


/**
/**
 * This class provides a simplified interface on top of other Bluetooth service
 * This class provides a simplified interface on top of other Bluetooth service
@@ -139,6 +140,17 @@ public class BluetoothOppManager {
        return true;
        return true;
    }
    }


    List<String> mWhitelist = new ArrayList<String>();

    public void addToWhitelist(String address) {
        //TODO: timeout whitelist
        mWhitelist.add(address);
    }

    public boolean isWhitelisted(String address) {
        return mWhitelist.contains(address);
    }

    /**
    /**
     * Restore data from preference
     * Restore data from preference
     */
     */
+9 −1
Original line number Original line Diff line number Diff line
@@ -46,7 +46,9 @@ import android.net.Uri;
import android.provider.LiveFolders;
import android.provider.LiveFolders;
import android.util.Log;
import android.util.Log;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashMap;
import java.util.List;


/**
/**
 * This provider allows application to interact with Bluetooth OPP manager
 * This provider allows application to interact with Bluetooth OPP manager
@@ -250,6 +252,7 @@ public final class BluetoothOppProvider extends ContentProvider {
        }
        }
        Integer dir = values.getAsInteger(BluetoothShare.DIRECTION);
        Integer dir = values.getAsInteger(BluetoothShare.DIRECTION);
        Integer con = values.getAsInteger(BluetoothShare.USER_CONFIRMATION);
        Integer con = values.getAsInteger(BluetoothShare.USER_CONFIRMATION);
        String address = values.getAsString(BluetoothShare.DESTINATION);


        if (values.getAsInteger(BluetoothShare.DIRECTION) == null) {
        if (values.getAsInteger(BluetoothShare.DIRECTION) == null) {
            dir = BluetoothShare.DIRECTION_OUTBOUND;
            dir = BluetoothShare.DIRECTION_OUTBOUND;
@@ -258,8 +261,13 @@ public final class BluetoothOppProvider extends ContentProvider {
            con = BluetoothShare.USER_CONFIRMATION_AUTO_CONFIRMED;
            con = BluetoothShare.USER_CONFIRMATION_AUTO_CONFIRMED;
        }
        }
        if (dir == BluetoothShare.DIRECTION_INBOUND && con == null) {
        if (dir == BluetoothShare.DIRECTION_INBOUND && con == null) {
            if (BluetoothOppManager.getInstance(getContext()).isWhitelisted(address)) {
                if (D) Log.d(TAG, address + " is in whitelist, auto confirming");
                con = BluetoothShare.USER_CONFIRMATION_AUTO_CONFIRMED;
            } else {
                con = BluetoothShare.USER_CONFIRMATION_PENDING;
                con = BluetoothShare.USER_CONFIRMATION_PENDING;
            }
            }
        }
        filteredValues.put(BluetoothShare.USER_CONFIRMATION, con);
        filteredValues.put(BluetoothShare.USER_CONFIRMATION, con);
        filteredValues.put(BluetoothShare.DIRECTION, dir);
        filteredValues.put(BluetoothShare.DIRECTION, dir);


+8 −0
Original line number Original line Diff line number Diff line
@@ -54,6 +54,7 @@ import android.widget.Toast;
 */
 */
public class BluetoothOppReceiver extends BroadcastReceiver {
public class BluetoothOppReceiver extends BroadcastReceiver {
    private static final String TAG = "BluetoothOppReceiver";
    private static final String TAG = "BluetoothOppReceiver";
    private static final boolean D = Constants.DEBUG;
    private static final boolean V = Constants.VERBOSE;
    private static final boolean V = Constants.VERBOSE;


    @Override
    @Override
@@ -248,6 +249,13 @@ public class BluetoothOppReceiver extends BroadcastReceiver {
            if (toastMsg != null) {
            if (toastMsg != null) {
                Toast.makeText(context, toastMsg, Toast.LENGTH_SHORT).show();
                Toast.makeText(context, toastMsg, Toast.LENGTH_SHORT).show();
            }
            }
        } else if (action.equals("todo-whitelist")) {
            //TODO: Verify sender of intent
            BluetoothDevice device =
                    (BluetoothDevice)intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (D) Log.d(TAG, "Adding " + device + " to whitelist");
            if (device == null) return;
            BluetoothOppManager.getInstance(context).addToWhitelist(device.getAddress());
        }
        }
    }
    }
}
}