Home | History | Annotate | Download | only in bluetooth
      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.BluetoothProfile;
     21 import android.content.Context;
     22 import android.content.DialogInterface;
     23 import android.widget.Toast;
     24 
     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;
     32 
     33 /**
     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.
     37  */
     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
     41 
     42     private Utils() {
     43     }
     44 
     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;
     55         default:
     56             return 0;
     57         }
     58     }
     59 
     60     // Create (or recycle existing) and show disconnect dialog.
     61     static AlertDialog showDisconnectDialog(Context context,
     62             AlertDialog dialog,
     63             DialogInterface.OnClickListener disconnectListener,
     64             CharSequence title, CharSequence message) {
     65         if (dialog == null) {
     66             dialog = new AlertDialog.Builder(context)
     67                     .setPositiveButton(android.R.string.ok, disconnectListener)
     68                     .setNegativeButton(android.R.string.cancel, null)
     69                     .create();
     70         } else {
     71             if (dialog.isShowing()) {
     72                 dialog.dismiss();
     73             }
     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);
     78         }
     79         dialog.setTitle(title);
     80         dialog.setMessage(message);
     81         dialog.show();
     82         return dialog;
     83     }
     84 
     85     // TODO: wire this up to show connection errors...
     86     static void showConnectingError(Context context, String name) {
     87         // if (!mIsConnectingErrorPossible) {
     88         //     return;
     89         // }
     90         // mIsConnectingErrorPossible = false;
     91 
     92         showError(context, name, R.string.bluetooth_connecting_error_message);
     93     }
     94 
     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)
    102                 .setMessage(message)
    103                 .setPositiveButton(android.R.string.ok, null)
    104                 .show();
    105         } else {
    106             Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    107         }
    108     }
    109 
    110     /**
    111      * Update the search Index for a specific class name and resources.
    112      */
    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;
    117         data.title = title;
    118         data.screenTitle = screenTitle;
    119         data.iconResId = iconResId;
    120         data.enabled = enabled;
    121 
    122         Index.getInstance(context).updateFromSearchIndexableData(data);
    123     }
    124 
    125     public static LocalBluetoothManager getLocalBtManager(Context context) {
    126         return LocalBluetoothManager.getInstance(context, mOnInitCallback);
    127     }
    128 
    129     private static final ErrorListener mErrorListener = new ErrorListener() {
    130         @Override
    131         public void onShowError(Context context, String name, int messageResId) {
    132             showError(context, name, messageResId);
    133         }
    134     };
    135 
    136     private static final BluetoothManagerCallback mOnInitCallback = new BluetoothManagerCallback() {
    137         @Override
    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);
    143         }
    144     };
    145 }
    146