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