• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 package com.android.settings.bluetooth;
18 
19 import android.app.AlertDialog;
20 import android.bluetooth.BluetoothDevice;
21 import android.bluetooth.BluetoothProfile;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.provider.Settings;
25 import android.support.annotation.VisibleForTesting;
26 import android.widget.Toast;
27 
28 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
29 import com.android.settings.R;
30 import com.android.settings.overlay.FeatureFactory;
31 import com.android.settingslib.bluetooth.LocalBluetoothManager;
32 import com.android.settingslib.bluetooth.LocalBluetoothManager.BluetoothManagerCallback;
33 import com.android.settingslib.bluetooth.Utils.ErrorListener;
34 
35 /**
36  * Utils is a helper class that contains constants for various
37  * Android resource IDs, debug logging flags, and static methods
38  * for creating dialogs.
39  */
40 public final class Utils {
41     static final boolean V = com.android.settingslib.bluetooth.Utils.V; // verbose logging
42     static final boolean D =  com.android.settingslib.bluetooth.Utils.D;  // regular logging
43 
Utils()44     private Utils() {
45     }
46 
getConnectionStateSummary(int connectionState)47     public static int getConnectionStateSummary(int connectionState) {
48         switch (connectionState) {
49             case BluetoothProfile.STATE_CONNECTED:
50                 return R.string.bluetooth_connected;
51             case BluetoothProfile.STATE_CONNECTING:
52                 return R.string.bluetooth_connecting;
53             case BluetoothProfile.STATE_DISCONNECTED:
54                 return R.string.bluetooth_disconnected;
55             case BluetoothProfile.STATE_DISCONNECTING:
56                 return R.string.bluetooth_disconnecting;
57             default:
58                 return 0;
59         }
60     }
61 
62     // Create (or recycle existing) and show disconnect dialog.
showDisconnectDialog(Context context, AlertDialog dialog, DialogInterface.OnClickListener disconnectListener, CharSequence title, CharSequence message)63     static AlertDialog showDisconnectDialog(Context context,
64             AlertDialog dialog,
65             DialogInterface.OnClickListener disconnectListener,
66             CharSequence title, CharSequence message) {
67         if (dialog == null) {
68             dialog = new AlertDialog.Builder(context)
69                     .setPositiveButton(android.R.string.ok, disconnectListener)
70                     .setNegativeButton(android.R.string.cancel, null)
71                     .create();
72         } else {
73             if (dialog.isShowing()) {
74                 dialog.dismiss();
75             }
76             // use disconnectListener for the correct profile(s)
77             CharSequence okText = context.getText(android.R.string.ok);
78             dialog.setButton(DialogInterface.BUTTON_POSITIVE,
79                     okText, disconnectListener);
80         }
81         dialog.setTitle(title);
82         dialog.setMessage(message);
83         dialog.show();
84         return dialog;
85     }
86 
87     // TODO: wire this up to show connection errors...
showConnectingError(Context context, String name)88     static void showConnectingError(Context context, String name) {
89         showConnectingError(context, name, getLocalBtManager(context));
90     }
91 
92     @VisibleForTesting
showConnectingError(Context context, String name, LocalBluetoothManager manager)93     static void showConnectingError(Context context, String name, LocalBluetoothManager manager) {
94         FeatureFactory.getFactory(context).getMetricsFeatureProvider().visible(context,
95             MetricsEvent.VIEW_UNKNOWN, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_CONNECT_ERROR);
96         showError(context, name, R.string.bluetooth_connecting_error_message, manager);
97     }
98 
showError(Context context, String name, int messageResId)99     static void showError(Context context, String name, int messageResId) {
100         showError(context, name, messageResId, getLocalBtManager(context));
101     }
102 
showError(Context context, String name, int messageResId, LocalBluetoothManager manager)103     private static void showError(Context context, String name, int messageResId,
104             LocalBluetoothManager manager) {
105         String message = context.getString(messageResId, name);
106         Context activity = manager.getForegroundActivity();
107         if (manager.isForegroundActivity()) {
108             new AlertDialog.Builder(activity)
109                 .setTitle(R.string.bluetooth_error_title)
110                 .setMessage(message)
111                 .setPositiveButton(android.R.string.ok, null)
112                 .show();
113         } else {
114             Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
115         }
116     }
117 
getLocalBtManager(Context context)118     public static LocalBluetoothManager getLocalBtManager(Context context) {
119         return LocalBluetoothManager.getInstance(context, mOnInitCallback);
120     }
121 
createRemoteName(Context context, BluetoothDevice device)122     public static String createRemoteName(Context context, BluetoothDevice device) {
123         String mRemoteName = device != null ? device.getAliasName() : null;
124 
125         if (mRemoteName == null) {
126             mRemoteName = context.getString(R.string.unknown);
127         }
128         return mRemoteName;
129     }
130 
131     private static final ErrorListener mErrorListener = new ErrorListener() {
132         @Override
133         public void onShowError(Context context, String name, int messageResId) {
134             showError(context, name, messageResId);
135         }
136     };
137 
138     private static final BluetoothManagerCallback mOnInitCallback = new BluetoothManagerCallback() {
139         @Override
140         public void onBluetoothManagerInitialized(Context appContext,
141                 LocalBluetoothManager bluetoothManager) {
142             com.android.settingslib.bluetooth.Utils.setErrorListener(mErrorListener);
143         }
144     };
145 
isBluetoothScanningEnabled(Context context)146     public static boolean isBluetoothScanningEnabled(Context context) {
147         return Settings.Global.getInt(context.getContentResolver(),
148                 Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE, 0) == 1;
149     }
150 }
151