1afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project/*
2afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
3afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project *
4afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * you may not use this file except in compliance with the License.
6afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * You may obtain a copy of the License at
7afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project *
8afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project *
10afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * See the License for the specific language governing permissions and
14afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * limitations under the License.
15afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project */
16afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
17afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectpackage com.android.settings.bluetooth;
18afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
19d63c0112251ab4e4e977545368dd703d875012a4Nick Pellyimport android.bluetooth.BluetoothAdapter;
20afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.content.BroadcastReceiver;
21afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.content.Context;
22afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.content.Intent;
23afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.content.IntentFilter;
24ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hambyimport android.content.SharedPreferences;
25afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.os.Handler;
26afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.os.SystemProperties;
2739b467482d1bf256a111c757e9b7621c6f523271Jason Monkimport android.support.v7.preference.Preference;
28f2982a9ba19340b474bafb6b0bf5832673984e0dJason Monkimport android.util.Log;
29436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby
30436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hambyimport com.android.settings.R;
31f2982a9ba19340b474bafb6b0bf5832673984e0dJason Monkimport com.android.settingslib.bluetooth.BluetoothDiscoverableTimeoutReceiver;
32f2982a9ba19340b474bafb6b0bf5832673984e0dJason Monkimport com.android.settingslib.bluetooth.LocalBluetoothAdapter;
3317d2124c1ccb1bd46393f70252d06d866646e97dWei Wang
34afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project/**
35afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * BluetoothDiscoverableEnabler is a helper to manage the "Discoverable"
36afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * checkbox. It sets/unsets discoverability and keeps track of how much time
37afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * until the the discoverability is automatically turned off.
38afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project */
39ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hambyfinal class BluetoothDiscoverableEnabler implements Preference.OnPreferenceClickListener {
4091b33f14620519e15507e62192701abed29179a1Jaikumar Ganesh
4159a97f7a735fd2e47df8358cdd9276ada9de5386Srikanth Uppala    private static final String TAG = "BluetoothDiscoverableEnabler";
4259a97f7a735fd2e47df8358cdd9276ada9de5386Srikanth Uppala
43afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private static final String SYSTEM_PROPERTY_DISCOVERABLE_TIMEOUT =
44afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            "debug.bt.discoverable_time";
45afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
46436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby    private static final int DISCOVERABLE_TIMEOUT_TWO_MINUTES = 120;
47436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby    private static final int DISCOVERABLE_TIMEOUT_FIVE_MINUTES = 300;
48436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby    private static final int DISCOVERABLE_TIMEOUT_ONE_HOUR = 3600;
491b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren    static final int DISCOVERABLE_TIMEOUT_NEVER = 0;
501b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren
51ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby    // Bluetooth advanced settings screen was replaced with action bar items.
52ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby    // Use the same preference key for discoverable timeout as the old ListPreference.
53ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby    private static final String KEY_DISCOVERABLE_TIMEOUT = "bt_discoverable_timeout";
54ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby
551b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren    private static final String VALUE_DISCOVERABLE_TIMEOUT_TWO_MINUTES = "twomin";
561b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren    private static final String VALUE_DISCOVERABLE_TIMEOUT_FIVE_MINUTES = "fivemin";
571b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren    private static final String VALUE_DISCOVERABLE_TIMEOUT_ONE_HOUR = "onehour";
581b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren    private static final String VALUE_DISCOVERABLE_TIMEOUT_NEVER = "never";
591b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren
601b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren    static final int DEFAULT_DISCOVERABLE_TIMEOUT = DISCOVERABLE_TIMEOUT_TWO_MINUTES;
6191b33f14620519e15507e62192701abed29179a1Jaikumar Ganesh
62ce930c15aec5fcc54ca0e31a0c01680bc1e3e010Fabrice Di Meglio    private Context mContext;
63afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private final Handler mUiHandler;
64ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby    private final Preference mDiscoveryPreference;
6591b33f14620519e15507e62192701abed29179a1Jaikumar Ganesh
66436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby    private final LocalBluetoothAdapter mLocalAdapter;
6791b33f14620519e15507e62192701abed29179a1Jaikumar Ganesh
68ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby    private final SharedPreferences mSharedPreferences;
69ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby
70ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby    private boolean mDiscoverable;
71ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby    private int mNumberOfPairedDevices;
72ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby
73ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby    private int mTimeoutSecs = -1;
74ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby
75afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
76afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        @Override
77afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        public void onReceive(Context context, Intent intent) {
78ca564d6bdaa56fba60f499e092e1024fff5d1abaNick Pelly            if (BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(intent.getAction())) {
79f4779354b39353a48d0bec6cc5c71fd73e3f369aNick Pelly                int mode = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE,
80f4779354b39353a48d0bec6cc5c71fd73e3f369aNick Pelly                        BluetoothAdapter.ERROR);
81f4779354b39353a48d0bec6cc5c71fd73e3f369aNick Pelly                if (mode != BluetoothAdapter.ERROR) {
82afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                    handleModeChanged(mode);
83afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                }
84afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            }
85afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
86afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    };
87afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
88afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private final Runnable mUpdateCountdownSummaryRunnable = new Runnable() {
89afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        public void run() {
9091b33f14620519e15507e62192701abed29179a1Jaikumar Ganesh            updateCountdownSummary();
9191b33f14620519e15507e62192701abed29179a1Jaikumar Ganesh        }
92afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    };
93afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
94ce930c15aec5fcc54ca0e31a0c01680bc1e3e010Fabrice Di Meglio    BluetoothDiscoverableEnabler(LocalBluetoothAdapter adapter,
95ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby            Preference discoveryPreference) {
96afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        mUiHandler = new Handler();
97436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby        mLocalAdapter = adapter;
98ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        mDiscoveryPreference = discoveryPreference;
99ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        mSharedPreferences = discoveryPreference.getSharedPreferences();
100ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        discoveryPreference.setPersistent(false);
101afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
102afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
103ce930c15aec5fcc54ca0e31a0c01680bc1e3e010Fabrice Di Meglio    public void resume(Context context) {
104436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby        if (mLocalAdapter == null) {
105afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            return;
106afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
107afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
108ce930c15aec5fcc54ca0e31a0c01680bc1e3e010Fabrice Di Meglio        if (mContext != context) {
109ce930c15aec5fcc54ca0e31a0c01680bc1e3e010Fabrice Di Meglio            mContext = context;
110ce930c15aec5fcc54ca0e31a0c01680bc1e3e010Fabrice Di Meglio        }
111ce930c15aec5fcc54ca0e31a0c01680bc1e3e010Fabrice Di Meglio
112ca564d6bdaa56fba60f499e092e1024fff5d1abaNick Pelly        IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
113afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        mContext.registerReceiver(mReceiver, filter);
114ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        mDiscoveryPreference.setOnPreferenceClickListener(this);
115436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby        handleModeChanged(mLocalAdapter.getScanMode());
116afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
11791b33f14620519e15507e62192701abed29179a1Jaikumar Ganesh
118afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    public void pause() {
119436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby        if (mLocalAdapter == null) {
120afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            return;
121afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
12291b33f14620519e15507e62192701abed29179a1Jaikumar Ganesh
123afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        mUiHandler.removeCallbacks(mUpdateCountdownSummaryRunnable);
124afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        mContext.unregisterReceiver(mReceiver);
125ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        mDiscoveryPreference.setOnPreferenceClickListener(null);
126afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
12791b33f14620519e15507e62192701abed29179a1Jaikumar Ganesh
128ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby    public boolean onPreferenceClick(Preference preference) {
129ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        // toggle discoverability
130ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        mDiscoverable = !mDiscoverable;
131ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        setEnabled(mDiscoverable);
132afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        return true;
133afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
13491b33f14620519e15507e62192701abed29179a1Jaikumar Ganesh
135436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby    private void setEnabled(boolean enable) {
136afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        if (enable) {
137afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            int timeout = getDiscoverableTimeout();
1381b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren            long endTimestamp = System.currentTimeMillis() + timeout * 1000L;
139436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby            LocalBluetoothPreferences.persistDiscoverableEndTimestamp(mContext, endTimestamp);
14091b33f14620519e15507e62192701abed29179a1Jaikumar Ganesh
141436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby            mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, timeout);
142ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby            updateCountdownSummary();
14359a97f7a735fd2e47df8358cdd9276ada9de5386Srikanth Uppala
1444bb010a67f80f5224b0777b48abdb08403b87616Srikanth Uppala            Log.d(TAG, "setEnabled(): enabled = " + enable + "timeout = " + timeout);
1454bb010a67f80f5224b0777b48abdb08403b87616Srikanth Uppala
146b5f144af8ecc1c2f2e45f8a7d936f4ab368fdd97Matthew Xie            if (timeout > 0) {
1474bb010a67f80f5224b0777b48abdb08403b87616Srikanth Uppala                BluetoothDiscoverableTimeoutReceiver.setDiscoverableAlarm(mContext, endTimestamp);
14854313d7d664d81a6d7aa736413670d3827c1e56dKim Schulz            } else {
14954313d7d664d81a6d7aa736413670d3827c1e56dKim Schulz                BluetoothDiscoverableTimeoutReceiver.cancelDiscoverableAlarm(mContext);
15059a97f7a735fd2e47df8358cdd9276ada9de5386Srikanth Uppala            }
15154313d7d664d81a6d7aa736413670d3827c1e56dKim Schulz
152afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        } else {
153436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby            mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
1544bb010a67f80f5224b0777b48abdb08403b87616Srikanth Uppala            BluetoothDiscoverableTimeoutReceiver.cancelDiscoverableAlarm(mContext);
155afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
156afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
157afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
1581b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren    private void updateTimerDisplay(int timeout) {
1591b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren        if (getDiscoverableTimeout() == DISCOVERABLE_TIMEOUT_NEVER) {
160ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby            mDiscoveryPreference.setSummary(R.string.bluetooth_is_discoverable_always);
1611b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren        } else {
16249cfe8a3c8a15c5ec747cbeecacb50908da8e09bJake Hamby            String textTimeout = formatTimeRemaining(timeout);
163ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby            mDiscoveryPreference.setSummary(mContext.getString(R.string.bluetooth_is_discoverable,
164ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby                    textTimeout));
1651b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren        }
1661b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren    }
1671b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren
16849cfe8a3c8a15c5ec747cbeecacb50908da8e09bJake Hamby    private static String formatTimeRemaining(int timeout) {
16949cfe8a3c8a15c5ec747cbeecacb50908da8e09bJake Hamby        StringBuilder sb = new StringBuilder(6);    // "mmm:ss"
17049cfe8a3c8a15c5ec747cbeecacb50908da8e09bJake Hamby        int min = timeout / 60;
17149cfe8a3c8a15c5ec747cbeecacb50908da8e09bJake Hamby        sb.append(min).append(':');
17249cfe8a3c8a15c5ec747cbeecacb50908da8e09bJake Hamby        int sec = timeout - (min * 60);
17349cfe8a3c8a15c5ec747cbeecacb50908da8e09bJake Hamby        if (sec < 10) {
17449cfe8a3c8a15c5ec747cbeecacb50908da8e09bJake Hamby            sb.append('0');
17549cfe8a3c8a15c5ec747cbeecacb50908da8e09bJake Hamby        }
17649cfe8a3c8a15c5ec747cbeecacb50908da8e09bJake Hamby        sb.append(sec);
17749cfe8a3c8a15c5ec747cbeecacb50908da8e09bJake Hamby        return sb.toString();
17849cfe8a3c8a15c5ec747cbeecacb50908da8e09bJake Hamby    }
17949cfe8a3c8a15c5ec747cbeecacb50908da8e09bJake Hamby
180ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby    void setDiscoverableTimeout(int index) {
181ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        String timeoutValue;
182ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        switch (index) {
183ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby            case 0:
184ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby            default:
185ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby                mTimeoutSecs = DISCOVERABLE_TIMEOUT_TWO_MINUTES;
186ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby                timeoutValue = VALUE_DISCOVERABLE_TIMEOUT_TWO_MINUTES;
187ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby                break;
188ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby
189ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby            case 1:
190ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby                mTimeoutSecs = DISCOVERABLE_TIMEOUT_FIVE_MINUTES;
191ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby                timeoutValue = VALUE_DISCOVERABLE_TIMEOUT_FIVE_MINUTES;
192ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby                break;
193ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby
194ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby            case 2:
195ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby                mTimeoutSecs = DISCOVERABLE_TIMEOUT_ONE_HOUR;
196ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby                timeoutValue = VALUE_DISCOVERABLE_TIMEOUT_ONE_HOUR;
197ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby                break;
198ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby
199ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby            case 3:
200ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby                mTimeoutSecs = DISCOVERABLE_TIMEOUT_NEVER;
201ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby                timeoutValue = VALUE_DISCOVERABLE_TIMEOUT_NEVER;
202ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby                break;
203ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        }
204ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        mSharedPreferences.edit().putString(KEY_DISCOVERABLE_TIMEOUT, timeoutValue).apply();
205ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        setEnabled(true);   // enable discovery and reset timer
206ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby    }
207ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby
208afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private int getDiscoverableTimeout() {
209ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        if (mTimeoutSecs != -1) {
210ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby            return mTimeoutSecs;
211ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        }
212ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby
213afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        int timeout = SystemProperties.getInt(SYSTEM_PROPERTY_DISCOVERABLE_TIMEOUT, -1);
2141b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren        if (timeout < 0) {
215ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby            String timeoutValue = mSharedPreferences.getString(KEY_DISCOVERABLE_TIMEOUT,
216ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby                    VALUE_DISCOVERABLE_TIMEOUT_TWO_MINUTES);
2171b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren
2181b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren            if (timeoutValue.equals(VALUE_DISCOVERABLE_TIMEOUT_NEVER)) {
2191b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren                timeout = DISCOVERABLE_TIMEOUT_NEVER;
2201b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren            } else if (timeoutValue.equals(VALUE_DISCOVERABLE_TIMEOUT_ONE_HOUR)) {
2211b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren                timeout = DISCOVERABLE_TIMEOUT_ONE_HOUR;
2221b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren            } else if (timeoutValue.equals(VALUE_DISCOVERABLE_TIMEOUT_FIVE_MINUTES)) {
2231b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren                timeout = DISCOVERABLE_TIMEOUT_FIVE_MINUTES;
2241b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren            } else {
2251b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren                timeout = DISCOVERABLE_TIMEOUT_TWO_MINUTES;
2261b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren            }
227afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
228ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        mTimeoutSecs = timeout;
229afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        return timeout;
230afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
231afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
232ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby    int getDiscoverableTimeoutIndex() {
233ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        int timeout = getDiscoverableTimeout();
234ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        switch (timeout) {
235ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby            case DISCOVERABLE_TIMEOUT_TWO_MINUTES:
236ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby            default:
237ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby                return 0;
238ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby
239ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby            case DISCOVERABLE_TIMEOUT_FIVE_MINUTES:
240ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby                return 1;
241ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby
242ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby            case DISCOVERABLE_TIMEOUT_ONE_HOUR:
243ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby                return 2;
244ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby
245ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby            case DISCOVERABLE_TIMEOUT_NEVER:
246ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby                return 3;
247ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        }
248ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby    }
249ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby
250ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby    void setNumberOfPairedDevices(int pairedDevices) {
251ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        mNumberOfPairedDevices = pairedDevices;
2521ecee6c1001880dec4c8f076cc99377a4d082454jhtop.kim        handleModeChanged(mLocalAdapter.getScanMode());
253ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby    }
254ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby
255ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby    void handleModeChanged(int mode) {
2564bb010a67f80f5224b0777b48abdb08403b87616Srikanth Uppala        Log.d(TAG, "handleModeChanged(): mode = " + mode);
257d63c0112251ab4e4e977545368dd703d875012a4Nick Pelly        if (mode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
258ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby            mDiscoverable = true;
259afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            updateCountdownSummary();
260afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        } else {
261ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby            mDiscoverable = false;
262ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby            setSummaryNotDiscoverable();
263ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        }
264ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby    }
265ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby
266ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby    private void setSummaryNotDiscoverable() {
267ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        if (mNumberOfPairedDevices != 0) {
268ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby            mDiscoveryPreference.setSummary(R.string.bluetooth_only_visible_to_paired_devices);
269ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby        } else {
270ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby            mDiscoveryPreference.setSummary(R.string.bluetooth_not_visible_to_other_devices);
271afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
272afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
27391b33f14620519e15507e62192701abed29179a1Jaikumar Ganesh
274afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private void updateCountdownSummary() {
275436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby        int mode = mLocalAdapter.getScanMode();
276d63c0112251ab4e4e977545368dd703d875012a4Nick Pelly        if (mode != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
277afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            return;
278afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
27991b33f14620519e15507e62192701abed29179a1Jaikumar Ganesh
280afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        long currentTimestamp = System.currentTimeMillis();
281436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby        long endTimestamp = LocalBluetoothPreferences.getDiscoverableEndTimestamp(mContext);
28291b33f14620519e15507e62192701abed29179a1Jaikumar Ganesh
283afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        if (currentTimestamp > endTimestamp) {
284afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            // We're still in discoverable mode, but maybe there isn't a timeout.
285ca9812a8521fcc483e821fd5a88ec421de0b8f66Jake Hamby            updateTimerDisplay(0);
286afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            return;
287afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
28891b33f14620519e15507e62192701abed29179a1Jaikumar Ganesh
2891b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren        int timeLeft = (int) ((endTimestamp - currentTimestamp) / 1000L);
2901b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren        updateTimerDisplay(timeLeft);
29191b33f14620519e15507e62192701abed29179a1Jaikumar Ganesh
292afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        synchronized (this) {
293afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            mUiHandler.removeCallbacks(mUpdateCountdownSummaryRunnable);
294afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            mUiHandler.postDelayed(mUpdateCountdownSummaryRunnable, 1000);
295afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
296afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
297afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project}
298