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