add discoverability timoeut when set by 3rd party app

Change-Id: Ibfd358121f8f9fbbf3b9bc06c5be7b9300e0ba53
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 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 @@
     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 @@
 
     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 @@
         mDiscoveryPreference = discoveryPreference;
         mSharedPreferences = discoveryPreference.getSharedPreferences();
         discoveryPreference.setPersistent(false);
-
-        mAlarmManager = (AlarmManager) mContext.getSystemService (Context.ALARM_SERVICE);
     }
 
     public void resume() {
@@ -147,12 +139,14 @@
             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 @@
     }
 
     void handleModeChanged(int mode) {
+        Log.d(TAG, "handleModeChanged(): mode = " + mode);
         if (mode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
             mDiscoverable = true;
             updateCountdownSummary();
@@ -294,34 +289,4 @@
             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.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 @@
         } 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) {