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