Fix discoverability timeout issues. (a) implement timeout logic (b) persist 'never timeout' after reboot (c) code cleanup
Change-Id: Ia7a8611d7212b9201994034d17da1d18e106107b
This commit is contained in:
committed by
Matthew Xie
parent
4e1876a40a
commit
59a97f7a73
@@ -1479,5 +1479,11 @@
|
|||||||
<meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget_info" />
|
<meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget_info" />
|
||||||
</receiver>
|
</receiver>
|
||||||
|
|
||||||
|
<receiver android:name=".bluetooth.BluetoothDiscoverableTimeoutReceiver">
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.bluetooth.intent.DISCOVERABLE_TIMEOUT" />
|
||||||
|
</intent-filter>
|
||||||
|
</receiver>
|
||||||
|
|
||||||
</application>
|
</application>
|
||||||
</manifest>
|
</manifest>
|
||||||
|
@@ -29,6 +29,12 @@ import android.text.format.DateUtils;
|
|||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
|
|
||||||
|
/* Required to handle timeout notification when phone is suspended */
|
||||||
|
import android.app.AlarmManager;
|
||||||
|
import android.app.PendingIntent;
|
||||||
|
import android.text.format.Time;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* BluetoothDiscoverableEnabler is a helper to manage the "Discoverable"
|
* BluetoothDiscoverableEnabler is a helper to manage the "Discoverable"
|
||||||
* checkbox. It sets/unsets discoverability and keeps track of how much time
|
* checkbox. It sets/unsets discoverability and keeps track of how much time
|
||||||
@@ -36,6 +42,8 @@ import com.android.settings.R;
|
|||||||
*/
|
*/
|
||||||
final class BluetoothDiscoverableEnabler implements Preference.OnPreferenceClickListener {
|
final class BluetoothDiscoverableEnabler implements Preference.OnPreferenceClickListener {
|
||||||
|
|
||||||
|
private static final String TAG = "BluetoothDiscoverableEnabler";
|
||||||
|
|
||||||
private static final String SYSTEM_PROPERTY_DISCOVERABLE_TIMEOUT =
|
private static final String SYSTEM_PROPERTY_DISCOVERABLE_TIMEOUT =
|
||||||
"debug.bt.discoverable_time";
|
"debug.bt.discoverable_time";
|
||||||
|
|
||||||
@@ -43,6 +51,7 @@ final class BluetoothDiscoverableEnabler implements Preference.OnPreferenceClick
|
|||||||
private static final int DISCOVERABLE_TIMEOUT_FIVE_MINUTES = 300;
|
private static final int DISCOVERABLE_TIMEOUT_FIVE_MINUTES = 300;
|
||||||
private static final int DISCOVERABLE_TIMEOUT_ONE_HOUR = 3600;
|
private static final int DISCOVERABLE_TIMEOUT_ONE_HOUR = 3600;
|
||||||
static final int DISCOVERABLE_TIMEOUT_NEVER = 0;
|
static final int DISCOVERABLE_TIMEOUT_NEVER = 0;
|
||||||
|
private static final String INTENT_DISCOVERABLE_TIMEOUT = "android.bluetooth.intent.DISCOVERABLE_TIMEOUT";
|
||||||
|
|
||||||
// Bluetooth advanced settings screen was replaced with action bar items.
|
// Bluetooth advanced settings screen was replaced with action bar items.
|
||||||
// Use the same preference key for discoverable timeout as the old ListPreference.
|
// Use the same preference key for discoverable timeout as the old ListPreference.
|
||||||
@@ -68,6 +77,8 @@ final class BluetoothDiscoverableEnabler implements Preference.OnPreferenceClick
|
|||||||
|
|
||||||
private int mTimeoutSecs = -1;
|
private int mTimeoutSecs = -1;
|
||||||
|
|
||||||
|
private AlarmManager mAlarmManager = null;
|
||||||
|
|
||||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
@@ -95,6 +106,8 @@ final class BluetoothDiscoverableEnabler implements Preference.OnPreferenceClick
|
|||||||
mDiscoveryPreference = discoveryPreference;
|
mDiscoveryPreference = discoveryPreference;
|
||||||
mSharedPreferences = discoveryPreference.getSharedPreferences();
|
mSharedPreferences = discoveryPreference.getSharedPreferences();
|
||||||
discoveryPreference.setPersistent(false);
|
discoveryPreference.setPersistent(false);
|
||||||
|
|
||||||
|
mAlarmManager = (AlarmManager) mContext.getSystemService (Context.ALARM_SERVICE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resume() {
|
public void resume() {
|
||||||
@@ -128,15 +141,18 @@ final class BluetoothDiscoverableEnabler implements Preference.OnPreferenceClick
|
|||||||
private void setEnabled(boolean enable) {
|
private void setEnabled(boolean enable) {
|
||||||
if (enable) {
|
if (enable) {
|
||||||
int timeout = getDiscoverableTimeout();
|
int timeout = getDiscoverableTimeout();
|
||||||
mLocalAdapter.setDiscoverableTimeout(timeout);
|
|
||||||
|
|
||||||
long endTimestamp = System.currentTimeMillis() + timeout * 1000L;
|
long endTimestamp = System.currentTimeMillis() + timeout * 1000L;
|
||||||
LocalBluetoothPreferences.persistDiscoverableEndTimestamp(mContext, endTimestamp);
|
LocalBluetoothPreferences.persistDiscoverableEndTimestamp(mContext, endTimestamp);
|
||||||
|
|
||||||
mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, timeout);
|
mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, timeout);
|
||||||
updateCountdownSummary();
|
updateCountdownSummary();
|
||||||
|
|
||||||
|
if (0 < timeout) {
|
||||||
|
setDiscoverableAlarm(endTimestamp);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
|
mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
|
||||||
|
cancelDiscoverableAlarm();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -278,4 +294,34 @@ final class BluetoothDiscoverableEnabler implements Preference.OnPreferenceClick
|
|||||||
mUiHandler.postDelayed(mUpdateCountdownSummaryRunnable, 1000);
|
mUiHandler.postDelayed(mUpdateCountdownSummaryRunnable, 1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setDiscoverableAlarm(long alarmTime) {
|
||||||
|
Log.d(TAG, "setDiscoverableAlarm(): alarmTime = " + alarmTime);
|
||||||
|
|
||||||
|
Intent intent = new Intent(INTENT_DISCOVERABLE_TIMEOUT);
|
||||||
|
intent.setClass(mContext, BluetoothDiscoverableTimeoutReceiver.class);
|
||||||
|
PendingIntent pending = PendingIntent.getBroadcast(
|
||||||
|
mContext, 0, intent, 0);
|
||||||
|
if (pending != null) {
|
||||||
|
// Cancel any previous alarms that do the same thing.
|
||||||
|
mAlarmManager.cancel(pending);
|
||||||
|
}
|
||||||
|
pending = PendingIntent.getBroadcast(
|
||||||
|
mContext, 0, intent, 0);
|
||||||
|
|
||||||
|
mAlarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pending);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void cancelDiscoverableAlarm() {
|
||||||
|
Log.d(TAG, "cancelDiscoverableAlarm(): Enter");
|
||||||
|
|
||||||
|
Intent intent = new Intent(INTENT_DISCOVERABLE_TIMEOUT);
|
||||||
|
intent.setClass(mContext, BluetoothDiscoverableTimeoutReceiver.class);
|
||||||
|
PendingIntent pending = PendingIntent.getBroadcast(
|
||||||
|
mContext, 0, intent, PendingIntent.FLAG_NO_CREATE);
|
||||||
|
if (pending != null) {
|
||||||
|
// Cancel any previous alarms that do the same thing.
|
||||||
|
mAlarmManager.cancel(pending);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2008 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.android.settings.bluetooth;
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.ContentResolver;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.bluetooth.BluetoothAdapter;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
public class BluetoothDiscoverableTimeoutReceiver extends BroadcastReceiver {
|
||||||
|
private static final String TAG = "BluetoothDiscoverableTimeoutReceiver";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
LocalBluetoothAdapter localBluetoothAdapter = LocalBluetoothAdapter.getInstance();
|
||||||
|
|
||||||
|
if(null != localBluetoothAdapter && localBluetoothAdapter.getState() == BluetoothAdapter.STATE_ON) {
|
||||||
|
Log.d(TAG, "Disable discoverable...");
|
||||||
|
|
||||||
|
localBluetoothAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
|
||||||
|
} else {
|
||||||
|
Log.e(TAG, "localBluetoothAdapter is NULL!!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
Reference in New Issue
Block a user