packages/apps/Settings
Revisão | cdd9d033b0f16c705433f009812e69bed977e1cf (tree) |
---|---|
Hora | 2011-05-28 02:27:14 |
Autor | Chris Wren <cwren@goog...> |
Commiter | Jaikumar Ganesh |
Allow users to change the length of BT disoverability. DO NOT MERGE
It can set to be two minues, five minutes, an hour, or forever discoverable.
The default is 120 secs.
This addresses the internal bug 2413429:
http://b/issue?id=2413429
and external issue 6348:
http://code.google.com/p/android/issues/detail?id=6348
Original Author: Chris Wren <crwen@google.com>
Modifications done by: Jaikumar Ganesh<jaikumar@google.com>
Change-Id: Ie12e56ac41aa01a161d263c7525b6e021d4eeb1f
@@ -255,6 +255,24 @@ | ||
255 | 255 | <item>TTLS</item> |
256 | 256 | </string-array> |
257 | 257 | |
258 | + <!-- Bluetooth Settings --> | |
259 | + | |
260 | + <!-- Discoverable mode timeout options --> | |
261 | + <string-array name="bluetooth_visibility_timeout_entries"> | |
262 | + <item>2 Minutes</item> | |
263 | + <item>5 Minutes</item> | |
264 | + <item>1 Hour</item> | |
265 | + <item>Never</item> | |
266 | + </string-array> | |
267 | + | |
268 | + <!-- Values for visibility_duration_entries matching constants in BluetoothSettings. Do not translate. --> | |
269 | + <string-array name="bluetooth_visibility_timeout_values" translatable="false"> | |
270 | + <item>twomin</item> | |
271 | + <item>fivemin</item> | |
272 | + <item>onehour</item> | |
273 | + <item>never</item> | |
274 | + </string-array> | |
275 | + | |
258 | 276 | <!-- Match this with drawable.wifi_signal. --> <skip /> |
259 | 277 | <!-- Wi-Fi settings. The signal strength a Wi-Fi network has. --> |
260 | 278 | <string-array name="wifi_signal"> |
@@ -184,9 +184,14 @@ | ||
184 | 184 | <string name="bluetooth_visibility">Discoverable</string> |
185 | 185 | <!-- Bluetooth settings screen, summary after selecting Discoverable check box --> |
186 | 186 | <string name="bluetooth_is_discoverable">Discoverable for <xliff:g id="discoverable_time_period">%1$s</xliff:g> seconds\u2026</string> |
187 | + <!-- Bluetooth settings screen, Discoverable checkbox summary text when Discoverable duration is set to "forever" --> | |
188 | + <string name="bluetooth_is_discoverable_always">Discoverable</string> | |
187 | 189 | <!-- Bluetooth settings screen, Discoverable checkbox summary text --> |
188 | 190 | <string name="bluetooth_not_discoverable">Make device discoverable</string> |
189 | - <!-- Bluetooth settings screen, heading above the list of nearby bluetooth devices --> | |
191 | + <!-- Bluetooth settings screen, option name to pick discoverability timeout duration (a list dialog comes up) --> | |
192 | + <string name="bluetooth_visibility_timeout">Discoverable timeout</string> | |
193 | + <!-- Bluetooth settings screen, Discoverable timout list dialog summary text --> | |
194 | + <string name="bluetooth_visibility_timeout_summary">Set how long device will be discoverable</string> | |
190 | 195 | <!-- Bluetooth settings screen, check box label whether or not to allow |
191 | 196 | bluetooth voice dialing when lock screen is up--> |
192 | 197 | <string name="bluetooth_lock_voice_dialing">Lock voice dialing</string> |
@@ -194,7 +199,7 @@ | ||
194 | 199 | <string name="bluetooth_lock_voice_dialing_summary"> |
195 | 200 | Prevent use of the bluetooth dialer when the screen is locked |
196 | 201 | </string> |
197 | - | |
202 | + <!-- Bluetooth settings screen, heading above the list of nearby bluetooth devices --> | |
198 | 203 | <string name="bluetooth_devices">Bluetooth devices</string> |
199 | 204 | <!-- Bluetooth settings screen, title for the current bluetooth name setting --> |
200 | 205 | <string name="bluetooth_device_name">Device name</string> |
@@ -243,9 +248,15 @@ | ||
243 | 248 | <!-- Strings for asking to the user whether to allow an app to enable discovery mode --> |
244 | 249 | <string name="bluetooth_ask_discovery">"An application on your phone is requesting permission to make your phone discoverable by other Bluetooth devices for <xliff:g id="timeout">%1$d</xliff:g> seconds. Do you want to do this?"</string> |
245 | 250 | |
251 | + <!-- Strings for asking to the user whether to allow an app to enable lasting discovery mode --> | |
252 | + <string name="bluetooth_ask_lasting_discovery">"An application on your phone is requesting permission to make your phone always discoverable by other Bluetooth devices. Do you want to do this?"</string> | |
253 | + | |
246 | 254 | <!-- Strings for asking to the user whether to allow an app to enable bluetooth and discovery mode --> |
247 | 255 | <string name="bluetooth_ask_enablement_and_discovery">"An application on your phone is requesting permission to turn on Bluetooth and to make your phone discoverable by other devices for <xliff:g id="timeout">%1$d</xliff:g> seconds. Do you want to do this?"</string> |
248 | 256 | |
257 | + <!-- Strings for asking to the user whether to allow an app to enable bluetooth and discovery mode --> | |
258 | + <string name="bluetooth_ask_enablement_and_lasting_discovery">"An application on your phone is requesting permission to turn on Bluetooth and to make your phone discoverable by other devices. Do you want to do this?"</string> | |
259 | + | |
249 | 260 | <!-- Strings for msg to display to user while bluetooth is turning on --> |
250 | 261 | <string name="bluetooth_turning_on">"Turning on Bluetooth\u2026"</string> |
251 | 262 |
@@ -40,6 +40,14 @@ | ||
40 | 40 | android:summaryOff="@string/bluetooth_not_discoverable" |
41 | 41 | android:persistent="false" /> |
42 | 42 | |
43 | + <ListPreference | |
44 | + android:key="bt_discoverable_timeout" | |
45 | + android:title="@string/bluetooth_visibility_timeout" | |
46 | + android:dependency="bt_discoverable" | |
47 | + android:summary="@string/bluetooth_visibility_timeout_summary" | |
48 | + android:entries="@array/bluetooth_visibility_timeout_entries" | |
49 | + android:entryValues="@array/bluetooth_visibility_timeout_values" /> | |
50 | + | |
43 | 51 | <Preference |
44 | 52 | android:key="bt_scan" |
45 | 53 | android:dependency="bt_checkbox" |
@@ -26,8 +26,11 @@ import android.content.IntentFilter; | ||
26 | 26 | import android.content.SharedPreferences; |
27 | 27 | import android.os.Handler; |
28 | 28 | import android.os.SystemProperties; |
29 | +import android.preference.ListPreference; | |
29 | 30 | import android.preference.Preference; |
30 | 31 | import android.preference.CheckBoxPreference; |
32 | +import android.provider.Settings; | |
33 | +import android.util.Log; | |
31 | 34 | |
32 | 35 | /** |
33 | 36 | * BluetoothDiscoverableEnabler is a helper to manage the "Discoverable" |
@@ -39,14 +42,28 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan | ||
39 | 42 | |
40 | 43 | private static final String SYSTEM_PROPERTY_DISCOVERABLE_TIMEOUT = |
41 | 44 | "debug.bt.discoverable_time"; |
42 | - /* package */ static final int DEFAULT_DISCOVERABLE_TIMEOUT = 120; | |
43 | 45 | |
44 | - /* package */ static final String SHARED_PREFERENCES_KEY_DISCOVERABLE_END_TIMESTAMP = | |
45 | - "discoverable_end_timestamp"; | |
46 | + static final int DISCOVERABLE_TIMEOUT_TWO_MINUTES = 120; | |
47 | + static final int DISCOVERABLE_TIMEOUT_FIVE_MINUTES = 300; | |
48 | + static final int DISCOVERABLE_TIMEOUT_ONE_HOUR = 3600; | |
49 | + static final int DISCOVERABLE_TIMEOUT_NEVER = 0; | |
50 | + | |
51 | + static final String SHARED_PREFERENCES_KEY_DISCOVERABLE_END_TIMESTAMP = | |
52 | + "discoverable_end_timestamp"; | |
53 | + | |
54 | + private static final String VALUE_DISCOVERABLE_TIMEOUT_TWO_MINUTES = "twomin"; | |
55 | + private static final String VALUE_DISCOVERABLE_TIMEOUT_FIVE_MINUTES = "fivemin"; | |
56 | + private static final String VALUE_DISCOVERABLE_TIMEOUT_ONE_HOUR = "onehour"; | |
57 | + private static final String VALUE_DISCOVERABLE_TIMEOUT_NEVER = "never"; | |
58 | + | |
59 | + // no need for this timeout anymore since we have the listPreference default value | |
60 | + // leaving now temporary until requestpermissionactivity is modified.. | |
61 | + static final int DEFAULT_DISCOVERABLE_TIMEOUT = DISCOVERABLE_TIMEOUT_TWO_MINUTES; | |
46 | 62 | |
47 | 63 | private final Context mContext; |
48 | 64 | private final Handler mUiHandler; |
49 | 65 | private final CheckBoxPreference mCheckBoxPreference; |
66 | + private final ListPreference mTimeoutListPreference; | |
50 | 67 | |
51 | 68 | private final LocalBluetoothManager mLocalManager; |
52 | 69 |
@@ -69,12 +86,16 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan | ||
69 | 86 | } |
70 | 87 | }; |
71 | 88 | |
72 | - public BluetoothDiscoverableEnabler(Context context, CheckBoxPreference checkBoxPreference) { | |
89 | + public BluetoothDiscoverableEnabler(Context context, | |
90 | + CheckBoxPreference checkBoxPreference, ListPreference timeoutListPreference) { | |
73 | 91 | mContext = context; |
74 | 92 | mUiHandler = new Handler(); |
75 | 93 | mCheckBoxPreference = checkBoxPreference; |
94 | + mTimeoutListPreference = timeoutListPreference; | |
76 | 95 | |
77 | 96 | checkBoxPreference.setPersistent(false); |
97 | + // we actually want to persist this since can't infer from BT device state | |
98 | + mTimeoutListPreference.setPersistent(true); | |
78 | 99 | |
79 | 100 | mLocalManager = LocalBluetoothManager.getInstance(context); |
80 | 101 | if (mLocalManager == null) { |
@@ -91,7 +112,7 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan | ||
91 | 112 | IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED); |
92 | 113 | mContext.registerReceiver(mReceiver, filter); |
93 | 114 | mCheckBoxPreference.setOnPreferenceChangeListener(this); |
94 | - | |
115 | + mTimeoutListPreference.setOnPreferenceChangeListener(this); | |
95 | 116 | handleModeChanged(mLocalManager.getBluetoothAdapter().getScanMode()); |
96 | 117 | } |
97 | 118 |
@@ -102,12 +123,18 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan | ||
102 | 123 | |
103 | 124 | mUiHandler.removeCallbacks(mUpdateCountdownSummaryRunnable); |
104 | 125 | mCheckBoxPreference.setOnPreferenceChangeListener(null); |
126 | + mTimeoutListPreference.setOnPreferenceChangeListener(null); | |
105 | 127 | mContext.unregisterReceiver(mReceiver); |
106 | 128 | } |
107 | 129 | |
108 | 130 | public boolean onPreferenceChange(Preference preference, Object value) { |
109 | - // Turn on/off BT discoverability | |
110 | - setEnabled((Boolean) value); | |
131 | + if (preference == mCheckBoxPreference) { | |
132 | + // Turn on/off BT discoverability | |
133 | + setEnabled((Boolean) value); | |
134 | + } else if (preference == mTimeoutListPreference) { | |
135 | + mTimeoutListPreference.setValue((String) value); | |
136 | + setEnabled(true); | |
137 | + } | |
111 | 138 | |
112 | 139 | return true; |
113 | 140 | } |
@@ -116,26 +143,52 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan | ||
116 | 143 | BluetoothAdapter manager = mLocalManager.getBluetoothAdapter(); |
117 | 144 | |
118 | 145 | if (enable) { |
119 | - | |
120 | 146 | int timeout = getDiscoverableTimeout(); |
121 | 147 | manager.setDiscoverableTimeout(timeout); |
122 | 148 | |
123 | - mCheckBoxPreference.setSummaryOn( | |
124 | - mContext.getResources().getString(R.string.bluetooth_is_discoverable, timeout)); | |
125 | - | |
126 | - long endTimestamp = System.currentTimeMillis() + timeout * 1000; | |
149 | + long endTimestamp = System.currentTimeMillis() + timeout * 1000L; | |
127 | 150 | persistDiscoverableEndTimestamp(endTimestamp); |
128 | 151 | |
129 | - manager.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE); | |
152 | + updateCountdownSummary(); | |
153 | + | |
154 | + manager.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, timeout); | |
130 | 155 | } else { |
131 | 156 | manager.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE); |
132 | 157 | } |
133 | 158 | } |
134 | 159 | |
160 | + private void updateTimerDisplay(int timeout) { | |
161 | + if (getDiscoverableTimeout() == DISCOVERABLE_TIMEOUT_NEVER) { | |
162 | + mCheckBoxPreference.setSummaryOn( | |
163 | + mContext.getResources() | |
164 | + .getString(R.string.bluetooth_is_discoverable_always)); | |
165 | + } else { | |
166 | + mCheckBoxPreference.setSummaryOn( | |
167 | + mContext.getResources() | |
168 | + .getString(R.string.bluetooth_is_discoverable, timeout)); | |
169 | + } | |
170 | + } | |
171 | + | |
135 | 172 | private int getDiscoverableTimeout() { |
136 | 173 | int timeout = SystemProperties.getInt(SYSTEM_PROPERTY_DISCOVERABLE_TIMEOUT, -1); |
137 | - if (timeout <= 0) { | |
138 | - timeout = DEFAULT_DISCOVERABLE_TIMEOUT; | |
174 | + if (timeout < 0) { | |
175 | + String timeoutValue = null; | |
176 | + if (mTimeoutListPreference != null && mTimeoutListPreference.getValue() != null) { | |
177 | + timeoutValue = mTimeoutListPreference.getValue().toString(); | |
178 | + } else { | |
179 | + mTimeoutListPreference.setValue(VALUE_DISCOVERABLE_TIMEOUT_TWO_MINUTES); | |
180 | + return DISCOVERABLE_TIMEOUT_TWO_MINUTES; | |
181 | + } | |
182 | + | |
183 | + if (timeoutValue.equals(VALUE_DISCOVERABLE_TIMEOUT_NEVER)) { | |
184 | + timeout = DISCOVERABLE_TIMEOUT_NEVER; | |
185 | + } else if (timeoutValue.equals(VALUE_DISCOVERABLE_TIMEOUT_ONE_HOUR)) { | |
186 | + timeout = DISCOVERABLE_TIMEOUT_ONE_HOUR; | |
187 | + } else if (timeoutValue.equals(VALUE_DISCOVERABLE_TIMEOUT_FIVE_MINUTES)) { | |
188 | + timeout = DISCOVERABLE_TIMEOUT_FIVE_MINUTES; | |
189 | + } else { | |
190 | + timeout = DISCOVERABLE_TIMEOUT_TWO_MINUTES; | |
191 | + } | |
139 | 192 | } |
140 | 193 | |
141 | 194 | return timeout; |
@@ -151,7 +204,6 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan | ||
151 | 204 | if (mode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { |
152 | 205 | mCheckBoxPreference.setChecked(true); |
153 | 206 | updateCountdownSummary(); |
154 | - | |
155 | 207 | } else { |
156 | 208 | mCheckBoxPreference.setChecked(false); |
157 | 209 | } |
@@ -173,11 +225,8 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan | ||
173 | 225 | return; |
174 | 226 | } |
175 | 227 | |
176 | - String formattedTimeLeft = String.valueOf((endTimestamp - currentTimestamp) / 1000); | |
177 | - | |
178 | - mCheckBoxPreference.setSummaryOn( | |
179 | - mContext.getResources().getString(R.string.bluetooth_is_discoverable, | |
180 | - formattedTimeLeft)); | |
228 | + int timeLeft = (int) ((endTimestamp - currentTimestamp) / 1000L); | |
229 | + updateTimerDisplay(timeLeft); | |
181 | 230 | |
182 | 231 | synchronized (this) { |
183 | 232 | mUiHandler.removeCallbacks(mUpdateCountdownSummaryRunnable); |
@@ -31,9 +31,13 @@ import android.content.IntentFilter; | ||
31 | 31 | import android.os.Bundle; |
32 | 32 | import android.os.ParcelUuid; |
33 | 33 | import android.preference.CheckBoxPreference; |
34 | +import android.preference.ListPreference; | |
34 | 35 | import android.preference.Preference; |
35 | 36 | import android.preference.PreferenceActivity; |
36 | 37 | import android.preference.PreferenceScreen; |
38 | +import android.preference.PreferenceCategory; | |
39 | +import android.preference.PreferenceScreen; | |
40 | +import android.text.TextUtils; | |
37 | 41 | import android.view.ContextMenu; |
38 | 42 | import android.view.MenuItem; |
39 | 43 | import android.view.View; |
@@ -55,6 +59,7 @@ public class BluetoothSettings extends PreferenceActivity | ||
55 | 59 | private static final String KEY_BT_CHECKBOX = "bt_checkbox"; |
56 | 60 | private static final String KEY_BT_DISCOVERABLE = "bt_discoverable"; |
57 | 61 | private static final String KEY_BT_DEVICE_LIST = "bt_device_list"; |
62 | + private static final String KEY_BT_DISCOVERABLE_TIMEOUT = "bt_discoverable_timeout"; | |
58 | 63 | private static final String KEY_BT_NAME = "bt_name"; |
59 | 64 | private static final String KEY_BT_SCAN = "bt_scan"; |
60 | 65 |
@@ -141,9 +146,9 @@ public class BluetoothSettings extends PreferenceActivity | ||
141 | 146 | this, |
142 | 147 | (CheckBoxPreference) findPreference(KEY_BT_CHECKBOX)); |
143 | 148 | |
144 | - mDiscoverableEnabler = new BluetoothDiscoverableEnabler( | |
145 | - this, | |
146 | - (CheckBoxPreference) findPreference(KEY_BT_DISCOVERABLE)); | |
149 | + mDiscoverableEnabler = new BluetoothDiscoverableEnabler(this, | |
150 | + (CheckBoxPreference) findPreference(KEY_BT_DISCOVERABLE), | |
151 | + (ListPreference) findPreference(KEY_BT_DISCOVERABLE_TIMEOUT)); | |
147 | 152 | |
148 | 153 | mNamePreference = (BluetoothNamePreference) findPreference(KEY_BT_NAME); |
149 | 154 |
@@ -157,7 +157,15 @@ public class RequestPermissionActivity extends Activity implements | ||
157 | 157 | builder.setCancelable(false); |
158 | 158 | } else { |
159 | 159 | // Ask the user whether to turn on discovery mode or not |
160 | - builder.setMessage(getString(R.string.bluetooth_ask_enablement_and_discovery, mTimeout)); | |
160 | + // For lasting discoverable mode there is a different message | |
161 | + // TODO(): Revisit this when public APIs for discoverable timeout are introduced. | |
162 | + if (mTimeout == BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER) { | |
163 | + builder.setMessage( | |
164 | + getString(R.string.bluetooth_ask_enablement_and_lasting_discovery)); | |
165 | + } else { | |
166 | + builder.setMessage( | |
167 | + getString(R.string.bluetooth_ask_enablement_and_discovery, mTimeout)); | |
168 | + } | |
161 | 169 | builder.setPositiveButton(getString(R.string.yes), this); |
162 | 170 | builder.setNegativeButton(getString(R.string.no), this); |
163 | 171 | } |
@@ -243,9 +251,14 @@ public class RequestPermissionActivity extends Activity implements | ||
243 | 251 | |
244 | 252 | Log.e(TAG, "Timeout = " + mTimeout); |
245 | 253 | |
246 | - if (mTimeout > MAX_DISCOVERABLE_TIMEOUT) { | |
247 | - mTimeout = MAX_DISCOVERABLE_TIMEOUT; | |
248 | - } else if (mTimeout <= 0) { | |
254 | + // Right now assuming for simplicity that an app can pick any int value, | |
255 | + // and if equal to BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER | |
256 | + // it will be treated as a request for lasting discoverability. | |
257 | + // Alternatively, a check can be added here for enforcing the specific allowed values | |
258 | + // as listed in BluetoothDiscoverableEnabler. | |
259 | + // We need to make all these value public. | |
260 | + | |
261 | + if (mTimeout <= 0) { | |
249 | 262 | mTimeout = BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT; |
250 | 263 | } |
251 | 264 | } else { |
@@ -75,7 +75,11 @@ public class RequestPermissionHelperActivity extends AlertActivity implements | ||
75 | 75 | if (mEnableOnly) { |
76 | 76 | tv.setText(getString(R.string.bluetooth_ask_enablement)); |
77 | 77 | } else { |
78 | - tv.setText(getString(R.string.bluetooth_ask_enablement_and_discovery, mTimeout)); | |
78 | + if (mTimeout == BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER) { | |
79 | + tv.setText(getString(R.string.bluetooth_ask_enablement_and_lasting_discovery)); | |
80 | + } else { | |
81 | + tv.setText(getString(R.string.bluetooth_ask_enablement_and_discovery, mTimeout)); | |
82 | + } | |
79 | 83 | } |
80 | 84 | |
81 | 85 | p.mPositiveButtonText = getString(R.string.yes); |