diff options
author | Srikanth Uppala <suppala@broadcom.com> | 2012-04-13 04:10:09 -0700 |
---|---|---|
committer | Matthew Xie <mattx@google.com> | 2012-07-16 15:38:37 -0700 |
commit | 4bb010a67f80f5224b0777b48abdb08403b87616 (patch) | |
tree | 251574c4115beab1d31ab48ca1a2b2c23c982724 /src/com/android/settings/bluetooth | |
parent | 0134761426f25c8ad352c294893fa88bf633b365 (diff) | |
download | packages_apps_settings-4bb010a67f80f5224b0777b48abdb08403b87616.zip packages_apps_settings-4bb010a67f80f5224b0777b48abdb08403b87616.tar.gz packages_apps_settings-4bb010a67f80f5224b0777b48abdb08403b87616.tar.bz2 |
add discoverability timoeut when set by 3rd party app
Change-Id: Ibfd358121f8f9fbbf3b9bc06c5be7b9300e0ba53
Diffstat (limited to 'src/com/android/settings/bluetooth')
3 files changed, 53 insertions, 41 deletions
diff --git a/src/com/android/settings/bluetooth/BluetoothDiscoverableEnabler.java b/src/com/android/settings/bluetooth/BluetoothDiscoverableEnabler.java index 4e43d98..80ce8e2 100755 --- a/src/com/android/settings/bluetooth/BluetoothDiscoverableEnabler.java +++ b/src/com/android/settings/bluetooth/BluetoothDiscoverableEnabler.java @@ -29,9 +29,6 @@ import android.text.format.DateUtils; 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; @@ -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_ONE_HOUR = 3600; 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. // 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 AlarmManager mAlarmManager = null; - private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { @@ -106,8 +100,6 @@ final class BluetoothDiscoverableEnabler implements Preference.OnPreferenceClick mDiscoveryPreference = discoveryPreference; mSharedPreferences = discoveryPreference.getSharedPreferences(); discoveryPreference.setPersistent(false); - - mAlarmManager = (AlarmManager) mContext.getSystemService (Context.ALARM_SERVICE); } public void resume() { @@ -147,12 +139,14 @@ final class BluetoothDiscoverableEnabler implements Preference.OnPreferenceClick mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, timeout); updateCountdownSummary(); + Log.d(TAG, "setEnabled(): enabled = " + enable + "timeout = " + timeout); + if (0 < timeout) { - setDiscoverableAlarm(endTimestamp); + BluetoothDiscoverableTimeoutReceiver.setDiscoverableAlarm(mContext, endTimestamp); } } else { mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE); - cancelDiscoverableAlarm(); + BluetoothDiscoverableTimeoutReceiver.cancelDiscoverableAlarm(mContext); } } @@ -254,6 +248,7 @@ final class BluetoothDiscoverableEnabler implements Preference.OnPreferenceClick } void handleModeChanged(int mode) { + Log.d(TAG, "handleModeChanged(): mode = " + mode); if (mode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { mDiscoverable = true; updateCountdownSummary(); @@ -294,34 +289,4 @@ final class BluetoothDiscoverableEnabler implements Preference.OnPreferenceClick 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); - } - } } diff --git a/src/com/android/settings/bluetooth/BluetoothDiscoverableTimeoutReceiver.java b/src/com/android/settings/bluetooth/BluetoothDiscoverableTimeoutReceiver.java index 729498d..3ea9d9e 100644 --- a/src/com/android/settings/bluetooth/BluetoothDiscoverableTimeoutReceiver.java +++ b/src/com/android/settings/bluetooth/BluetoothDiscoverableTimeoutReceiver.java @@ -23,9 +23,52 @@ import android.content.Intent; import android.bluetooth.BluetoothAdapter; 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 { 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 public void onReceive(Context context, Intent intent) { LocalBluetoothAdapter localBluetoothAdapter = LocalBluetoothAdapter.getInstance(); diff --git a/src/com/android/settings/bluetooth/RequestPermissionActivity.java b/src/com/android/settings/bluetooth/RequestPermissionActivity.java index 529312d..08c10fb 100644 --- a/src/com/android/settings/bluetooth/RequestPermissionActivity.java +++ b/src/com/android/settings/bluetooth/RequestPermissionActivity.java @@ -228,8 +228,12 @@ public class RequestPermissionActivity extends Activity implements } else if (mLocalAdapter.setScanMode( BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, mTimeout)) { // If already in discoverable mode, this will extend the timeout. + long endTime = System.currentTimeMillis() + (long) mTimeout * 1000; LocalBluetoothPreferences.persistDiscoverableEndTimestamp( - this, System.currentTimeMillis() + (long) mTimeout * 1000); + this, endTime); + if (0 < mTimeout) { + BluetoothDiscoverableTimeoutReceiver.setDiscoverableAlarm(this, endTime); + } returnCode = mTimeout; // Activity.RESULT_FIRST_USER should be 1 if (returnCode < RESULT_FIRST_USER) { |