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

Commit 834e5993 authored by Michael Chan's avatar Michael Chan
Browse files

b/2234854 Fixed Bluetooth API return codes for requesting permission to enable bluetooth

Also added test app.
parent 4d004e91
Loading
Loading
Loading
Loading
+16 −17
Original line number Diff line number Diff line
@@ -16,6 +16,11 @@

package com.android.settings.bluetooth;

import com.android.internal.app.AlertActivity;
import com.android.internal.app.AlertController;
import com.android.settings.R;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
@@ -29,10 +34,6 @@ import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.android.internal.app.AlertActivity;
import com.android.internal.app.AlertController;
import com.android.settings.R;

/**
 * RequestPermissionActivity asks the user whether to enable discovery. This is
 * usually started by an application wanted to start bluetooth and or discovery
@@ -47,12 +48,6 @@ public class RequestPermissionActivity extends AlertActivity implements

    private static final int MAX_DISCOVERABLE_TIMEOUT = 300;

    // Result code: Error
    public static final int RESULT_ERROR = -2;

    // Result code: User rejected the request
    public static final int RESULT_USER_DENIED = -1;

    // Non-error return code: BT is starting or has started successfully. Used
    // by this Activity and RequestPermissionHelperActivity
    /* package */ static final int RESULT_BT_STARTING_OR_STARTED = -1000;
@@ -180,7 +175,7 @@ public class RequestPermissionActivity extends AlertActivity implements
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode != REQUEST_CODE_START_BT) {
            Log.e(TAG, "Unexpected onActivityResult " + requestCode + " " + resultCode);
            setResult(RESULT_ERROR);
            setResult(Activity.RESULT_CANCELED);
            finish();
            return;
        }
@@ -208,7 +203,7 @@ public class RequestPermissionActivity extends AlertActivity implements
                break;

            case DialogInterface.BUTTON_NEGATIVE:
                setResult(RESULT_USER_DENIED);
                setResult(Activity.RESULT_CANCELED);
                break;
        }
    }
@@ -218,14 +213,18 @@ public class RequestPermissionActivity extends AlertActivity implements

        if (mEnableOnly) {
            // BT enabled. Done
            returnCode = 0;
            returnCode = Activity.RESULT_OK;
        } else if (mLocalManager.getBluetoothAdapter().setScanMode(
                BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, mTimeout)) {
            // If already in discoverable mode, this will extend the timeout.
            persistDiscoverableEndTimestamp(System.currentTimeMillis() + mTimeout * 1000);
            returnCode = mTimeout;
            // Activity.RESULT_FIRST_USER should be 1
            if (returnCode < Activity.RESULT_FIRST_USER) {
                returnCode = Activity.RESULT_FIRST_USER;
            }
        } else {
            returnCode = RESULT_ERROR;
            returnCode = Activity.RESULT_CANCELED;
        }

        setResult(returnCode);
@@ -254,14 +253,14 @@ public class RequestPermissionActivity extends AlertActivity implements
            Log.e(TAG, "Error: this activity may be started only with intent "
                    + BluetoothAdapter.ACTION_REQUEST_ENABLE + " or "
                    + BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            setResult(RESULT_ERROR);
            setResult(Activity.RESULT_CANCELED);
            return true;
        }

        mLocalManager = LocalBluetoothManager.getInstance(this);
        if (mLocalManager == null) {
            Log.e(TAG, "Error: there's a problem starting bluetooth");
            setResult(RESULT_ERROR);
            setResult(Activity.RESULT_CANCELED);
            return true;
        }

@@ -284,7 +283,7 @@ public class RequestPermissionActivity extends AlertActivity implements

    @Override
    public void onBackPressed() {
        setResult(RequestPermissionActivity.RESULT_USER_DENIED);
        setResult(Activity.RESULT_CANCELED);
        super.onBackPressed();
    }
}
+10 −9
Original line number Diff line number Diff line
@@ -16,6 +16,11 @@

package com.android.settings.bluetooth;

import com.android.internal.app.AlertActivity;
import com.android.internal.app.AlertController;
import com.android.settings.R;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.DialogInterface;
import android.content.Intent;
@@ -24,10 +29,6 @@ import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.android.internal.app.AlertActivity;
import com.android.internal.app.AlertController;
import com.android.settings.R;

/**
 * RequestPermissionHelperActivity asks the user whether to enable discovery.
 * This is usually started by RequestPermissionActivity.
@@ -107,12 +108,12 @@ public class RequestPermissionHelperActivity extends AlertActivity implements
                        || mLocalManager.getBluetoothAdapter().enable()) {
                    returnCode = RequestPermissionActivity.RESULT_BT_STARTING_OR_STARTED;
                } else {
                    returnCode = RequestPermissionActivity.RESULT_ERROR;
                    returnCode = Activity.RESULT_CANCELED;
                }
                break;

            case DialogInterface.BUTTON_NEGATIVE:
                returnCode = RequestPermissionActivity.RESULT_USER_DENIED;
                returnCode = Activity.RESULT_CANCELED;
                break;
            default:
                return;
@@ -131,14 +132,14 @@ public class RequestPermissionHelperActivity extends AlertActivity implements
            mTimeout = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
                    BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT);
        } else {
            setResult(RequestPermissionActivity.RESULT_ERROR);
            setResult(Activity.RESULT_CANCELED);
            return true;
        }

        mLocalManager = LocalBluetoothManager.getInstance(this);
        if (mLocalManager == null) {
            Log.e(TAG, "Error: there's a problem starting bluetooth");
            setResult(RequestPermissionActivity.RESULT_ERROR);
            setResult(Activity.RESULT_CANCELED);
            return true;
        }

@@ -147,7 +148,7 @@ public class RequestPermissionHelperActivity extends AlertActivity implements

    @Override
    public void onBackPressed() {
        setResult(RequestPermissionActivity.RESULT_USER_DENIED);
        setResult(Activity.RESULT_CANCELED);
        super.onBackPressed();
    }
}
+13 −3
Original line number Diff line number Diff line
@@ -17,8 +17,18 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.settings.tests">

    <uses-permission android:name="android.permission.BLUETOOTH" />

    <application>
        <uses-library android:name="android.test.runner" />
        <activity android:name="BluetoothRequestPermissionTest"
            android:label="Bluetooth Perm Test" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <instrumentation android:name="SettingsLaunchPerformance"
+46 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2009 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ListView android:id="@+id/msg_container"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:transcriptMode="normal"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="@android:style/ButtonBar">

        <Button android:id="@+id/enable"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/enable" />

        <Button android:id="@+id/discover"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/discoverable" />
    </LinearLayout>
</LinearLayout>
+23 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2009 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <!-- Test only. Do not translate. -->
    <!-- Test only. Do not translate. -->
    <!-- Test only. Do not translate. -->
    <string name="enable">Enable</string>
    <string name="discoverable">Discoverable</string>
</resources>
Loading