add discoverability timoeut when set by 3rd party app
Change-Id: Ibfd358121f8f9fbbf3b9bc06c5be7b9300e0ba53
This commit is contained in:
committed by
Matthew Xie
parent
0134761426
commit
4bb010a67f
@@ -29,9 +29,6 @@ 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.text.format.Time;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@@ -51,7 +48,6 @@ 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.
|
||||||
@@ -77,8 +73,6 @@ 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) {
|
||||||
@@ -106,8 +100,6 @@ 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() {
|
||||||
@@ -147,12 +139,14 @@ final class BluetoothDiscoverableEnabler implements Preference.OnPreferenceClick
|
|||||||
mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, timeout);
|
mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, timeout);
|
||||||
updateCountdownSummary();
|
updateCountdownSummary();
|
||||||
|
|
||||||
|
Log.d(TAG, "setEnabled(): enabled = " + enable + "timeout = " + timeout);
|
||||||
|
|
||||||
if (0 < timeout) {
|
if (0 < timeout) {
|
||||||
setDiscoverableAlarm(endTimestamp);
|
BluetoothDiscoverableTimeoutReceiver.setDiscoverableAlarm(mContext, endTimestamp);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
|
mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
|
||||||
cancelDiscoverableAlarm();
|
BluetoothDiscoverableTimeoutReceiver.cancelDiscoverableAlarm(mContext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -254,6 +248,7 @@ final class BluetoothDiscoverableEnabler implements Preference.OnPreferenceClick
|
|||||||
}
|
}
|
||||||
|
|
||||||
void handleModeChanged(int mode) {
|
void handleModeChanged(int mode) {
|
||||||
|
Log.d(TAG, "handleModeChanged(): mode = " + mode);
|
||||||
if (mode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
|
if (mode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
|
||||||
mDiscoverable = true;
|
mDiscoverable = true;
|
||||||
updateCountdownSummary();
|
updateCountdownSummary();
|
||||||
@@ -294,34 +289,4 @@ 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -23,9 +23,52 @@ import android.content.Intent;
|
|||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
/* Required to handle timeout notification when phone is suspended */
|
||||||
|
import android.app.AlarmManager;
|
||||||
|
import android.app.PendingIntent;
|
||||||
|
|
||||||
public class BluetoothDiscoverableTimeoutReceiver extends BroadcastReceiver {
|
public class BluetoothDiscoverableTimeoutReceiver extends BroadcastReceiver {
|
||||||
private static final String TAG = "BluetoothDiscoverableTimeoutReceiver";
|
private static final String TAG = "BluetoothDiscoverableTimeoutReceiver";
|
||||||
|
|
||||||
|
private static final String INTENT_DISCOVERABLE_TIMEOUT = "android.bluetooth.intent.DISCOVERABLE_TIMEOUT";
|
||||||
|
|
||||||
|
static void setDiscoverableAlarm(Context context, long alarmTime) {
|
||||||
|
Log.d(TAG, "setDiscoverableAlarm(): alarmTime = " + alarmTime);
|
||||||
|
|
||||||
|
Intent intent = new Intent(INTENT_DISCOVERABLE_TIMEOUT);
|
||||||
|
intent.setClass(context, BluetoothDiscoverableTimeoutReceiver.class);
|
||||||
|
PendingIntent pending = PendingIntent.getBroadcast(
|
||||||
|
context, 0, intent, 0);
|
||||||
|
AlarmManager alarmManager =
|
||||||
|
(AlarmManager) context.getSystemService (Context.ALARM_SERVICE);
|
||||||
|
|
||||||
|
if (pending != null) {
|
||||||
|
// Cancel any previous alarms that do the same thing.
|
||||||
|
alarmManager.cancel(pending);
|
||||||
|
Log.d(TAG, "setDiscoverableAlarm(): cancel prev alarm");
|
||||||
|
}
|
||||||
|
pending = PendingIntent.getBroadcast(
|
||||||
|
context, 0, intent, 0);
|
||||||
|
|
||||||
|
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pending);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void cancelDiscoverableAlarm(Context context) {
|
||||||
|
Log.d(TAG, "cancelDiscoverableAlarm(): Enter");
|
||||||
|
|
||||||
|
Intent intent = new Intent(INTENT_DISCOVERABLE_TIMEOUT);
|
||||||
|
intent.setClass(context, BluetoothDiscoverableTimeoutReceiver.class);
|
||||||
|
PendingIntent pending = PendingIntent.getBroadcast(
|
||||||
|
context, 0, intent, PendingIntent.FLAG_NO_CREATE);
|
||||||
|
if (pending != null) {
|
||||||
|
// Cancel any previous alarms that do the same thing.
|
||||||
|
AlarmManager alarmManager =
|
||||||
|
(AlarmManager) context.getSystemService (Context.ALARM_SERVICE);
|
||||||
|
|
||||||
|
alarmManager.cancel(pending);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
LocalBluetoothAdapter localBluetoothAdapter = LocalBluetoothAdapter.getInstance();
|
LocalBluetoothAdapter localBluetoothAdapter = LocalBluetoothAdapter.getInstance();
|
||||||
|
@@ -228,8 +228,12 @@ public class RequestPermissionActivity extends Activity implements
|
|||||||
} else if (mLocalAdapter.setScanMode(
|
} else if (mLocalAdapter.setScanMode(
|
||||||
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, mTimeout)) {
|
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, mTimeout)) {
|
||||||
// If already in discoverable mode, this will extend the timeout.
|
// If already in discoverable mode, this will extend the timeout.
|
||||||
|
long endTime = System.currentTimeMillis() + (long) mTimeout * 1000;
|
||||||
LocalBluetoothPreferences.persistDiscoverableEndTimestamp(
|
LocalBluetoothPreferences.persistDiscoverableEndTimestamp(
|
||||||
this, System.currentTimeMillis() + (long) mTimeout * 1000);
|
this, endTime);
|
||||||
|
if (0 < mTimeout) {
|
||||||
|
BluetoothDiscoverableTimeoutReceiver.setDiscoverableAlarm(this, endTime);
|
||||||
|
}
|
||||||
returnCode = mTimeout;
|
returnCode = mTimeout;
|
||||||
// Activity.RESULT_FIRST_USER should be 1
|
// Activity.RESULT_FIRST_USER should be 1
|
||||||
if (returnCode < RESULT_FIRST_USER) {
|
if (returnCode < RESULT_FIRST_USER) {
|
||||||
|
Reference in New Issue
Block a user