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.BluetoothClass; 21 import android.bluetooth.BluetoothProfile; 22 import android.content.Context; 23 import android.content.DialogInterface; 24 import android.widget.Toast; 25 26 import com.android.settings.R; 27 28 /** 29 * Utils is a helper class that contains constants for various 30 * Android resource IDs, debug logging flags, and static methods 31 * for creating dialogs. 32 */ 33 final class Utils { 34 static final boolean V = false; // verbose logging 35 static final boolean D = true; // regular logging 36 37 private Utils() { 38 } 39 40 public static int getConnectionStateSummary(int connectionState) { 41 switch (connectionState) { 42 case BluetoothProfile.STATE_CONNECTED: 43 return R.string.bluetooth_connected; 44 case BluetoothProfile.STATE_CONNECTING: 45 return R.string.bluetooth_connecting; 46 case BluetoothProfile.STATE_DISCONNECTED: 47 return R.string.bluetooth_disconnected; 48 case BluetoothProfile.STATE_DISCONNECTING: 49 return R.string.bluetooth_disconnecting; 50 default: 51 return 0; 52 } 53 } 54 55 // Create (or recycle existing) and show disconnect dialog. 56 static AlertDialog showDisconnectDialog(Context context, 57 AlertDialog dialog, 58 DialogInterface.OnClickListener disconnectListener, 59 CharSequence title, CharSequence message) { 60 if (dialog == null) { 61 dialog = new AlertDialog.Builder(context) 62 .setPositiveButton(android.R.string.ok, disconnectListener) 63 .setNegativeButton(android.R.string.cancel, null) 64 .create(); 65 } else { 66 if (dialog.isShowing()) { 67 dialog.dismiss(); 68 } 69 // use disconnectListener for the correct profile(s) 70 CharSequence okText = context.getText(android.R.string.ok); 71 dialog.setButton(DialogInterface.BUTTON_POSITIVE, 72 okText, disconnectListener); 73 } 74 dialog.setTitle(title); 75 dialog.setMessage(message); 76 dialog.show(); 77 return dialog; 78 } 79 80 // TODO: wire this up to show connection errors... 81 static void showConnectingError(Context context, String name) { 82 // if (!mIsConnectingErrorPossible) { 83 // return; 84 // } 85 // mIsConnectingErrorPossible = false; 86 87 showError(context, name, R.string.bluetooth_connecting_error_message); 88 } 89 90 static void showError(Context context, String name, int messageResId) { 91 String message = context.getString(messageResId, name); 92 LocalBluetoothManager manager = LocalBluetoothManager.getInstance(context); 93 Context activity = manager.getForegroundActivity(); 94 if(manager.isForegroundActivity()) { 95 new AlertDialog.Builder(activity) 96 .setIconAttribute(android.R.attr.alertDialogIcon) 97 .setTitle(R.string.bluetooth_error_title) 98 .setMessage(message) 99 .setPositiveButton(android.R.string.ok, null) 100 .show(); 101 } else { 102 Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 103 } 104 } 105 } 106