2 * Copyright (C) 2011 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.settings.bluetooth;
19 import android.app.AlertDialog;
20 import android.bluetooth.BluetoothProfile;
21 import android.content.Context;
22 import android.content.DialogInterface;
23 import android.widget.Toast;
25 import com.android.settings.R;
26 import com.android.settings.bluetooth.DockService.DockBluetoothCallback;
27 import com.android.settings.search.Index;
28 import com.android.settings.search.SearchIndexableRaw;
29 import com.android.settingslib.bluetooth.LocalBluetoothManager;
30 import com.android.settingslib.bluetooth.LocalBluetoothManager.BluetoothManagerCallback;
31 import com.android.settingslib.bluetooth.Utils.ErrorListener;
34 * Utils is a helper class that contains constants for various
35 * Android resource IDs, debug logging flags, and static methods
36 * for creating dialogs.
38 public final class Utils {
39 static final boolean V = com.android.settingslib.bluetooth.Utils.V; // verbose logging
40 static final boolean D = com.android.settingslib.bluetooth.Utils.D; // regular logging
45 public static int getConnectionStateSummary(int connectionState) {
46 switch (connectionState) {
47 case BluetoothProfile.STATE_CONNECTED:
48 return R.string.bluetooth_connected;
49 case BluetoothProfile.STATE_CONNECTING:
50 return R.string.bluetooth_connecting;
51 case BluetoothProfile.STATE_DISCONNECTED:
52 return R.string.bluetooth_disconnected;
53 case BluetoothProfile.STATE_DISCONNECTING:
54 return R.string.bluetooth_disconnecting;
60 // Create (or recycle existing) and show disconnect dialog.
61 static AlertDialog showDisconnectDialog(Context context,
63 DialogInterface.OnClickListener disconnectListener,
64 CharSequence title, CharSequence message) {
66 dialog = new AlertDialog.Builder(context)
67 .setPositiveButton(android.R.string.ok, disconnectListener)
68 .setNegativeButton(android.R.string.cancel, disconnectListener)
71 if (dialog.isShowing()) {
74 // use disconnectListener for the correct profile(s)
75 CharSequence okText = context.getText(android.R.string.ok);
76 dialog.setButton(DialogInterface.BUTTON_POSITIVE,
77 okText, disconnectListener);
79 dialog.setTitle(title);
80 dialog.setMessage(message);
85 // TODO: wire this up to show connection errors...
86 static void showConnectingError(Context context, String name) {
87 // if (!mIsConnectingErrorPossible) {
90 // mIsConnectingErrorPossible = false;
92 showError(context, name, R.string.bluetooth_connecting_error_message);
95 static void showError(Context context, String name, int messageResId) {
96 String message = context.getString(messageResId, name);
97 LocalBluetoothManager manager = getLocalBtManager(context);
98 Context activity = manager.getForegroundActivity();
99 if(manager.isForegroundActivity()) {
100 new AlertDialog.Builder(activity)
101 .setTitle(R.string.bluetooth_error_title)
103 .setPositiveButton(android.R.string.ok, null)
106 Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
111 * Update the search Index for a specific class name and resources.
113 public static void updateSearchIndex(Context context, String className, String title,
114 String screenTitle, int iconResId, boolean enabled) {
115 SearchIndexableRaw data = new SearchIndexableRaw(context);
116 data.className = className;
118 data.screenTitle = screenTitle;
119 data.iconResId = iconResId;
120 data.enabled = enabled;
122 Index.getInstance(context).updateFromSearchIndexableData(data);
125 public static LocalBluetoothManager getLocalBtManager(Context context) {
126 return LocalBluetoothManager.getInstance(context, mOnInitCallback);
129 private static final ErrorListener mErrorListener = new ErrorListener() {
131 public void onShowError(Context context, String name, int messageResId) {
132 showError(context, name, messageResId);
136 private static final BluetoothManagerCallback mOnInitCallback = new BluetoothManagerCallback() {
138 public void onBluetoothManagerInitialized(Context appContext,
139 LocalBluetoothManager bluetoothManager) {
140 bluetoothManager.getEventManager().registerCallback(
141 new DockBluetoothCallback(appContext));
142 com.android.settingslib.bluetooth.Utils.setErrorListener(mErrorListener);