BluetoothOppBtEnablingActivity.java revision dc3bacd6eb626a93f2c3c4c62b3ef6aa9e575d92
1/*
2 * Copyright (c) 2008-2009, Motorola, Inc.
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * - Neither the name of the Motorola, Inc. nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33package com.android.bluetooth.opp;
34
35import com.android.bluetooth.R;
36import com.android.internal.app.AlertActivity;
37import com.android.internal.app.AlertController;
38
39import android.bluetooth.BluetoothAdapter;
40import android.content.BroadcastReceiver;
41import android.content.Context;
42import android.content.Intent;
43import android.content.IntentFilter;
44import android.os.Bundle;
45import android.os.Handler;
46import android.os.Message;
47import android.util.Log;
48import android.view.KeyEvent;
49import android.view.View;
50import android.widget.TextView;
51
52/**
53 * This class is designed to show BT enabling progress.
54 */
55public class BluetoothOppBtEnablingActivity extends AlertActivity {
56    private static final String TAG = "BluetoothOppEnablingActivity";
57
58    private static final boolean D = Constants.DEBUG;
59
60    private static final boolean V = Constants.VERBOSE;
61
62    private static final int BT_ENABLING_TIMEOUT = 0;
63
64    private static final int BT_ENABLING_TIMEOUT_VALUE = 20000;
65
66    @Override
67    protected void onCreate(Bundle savedInstanceState) {
68        super.onCreate(savedInstanceState);
69
70        IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
71        registerReceiver(mBluetoothReceiver, filter);
72
73        // Set up the "dialog"
74        final AlertController.AlertParams p = mAlertParams;
75        p.mIconId = android.R.drawable.ic_dialog_info;
76        p.mTitle = getString(R.string.enabling_progress_title);
77        p.mView = createView();
78        setupAlert();
79
80        // Add timeout for enabling progress
81        mTimeoutHandler.sendMessageDelayed(mTimeoutHandler.obtainMessage(BT_ENABLING_TIMEOUT),
82                BT_ENABLING_TIMEOUT_VALUE);
83    }
84
85    private View createView() {
86        View view = getLayoutInflater().inflate(R.layout.bt_enabling_progress, null);
87        TextView contentView = (TextView)view.findViewById(R.id.progress_info);
88        contentView.setText(getString(R.string.enabling_progress_content));
89
90        return view;
91    }
92
93    @Override
94    public boolean onKeyDown(int keyCode, KeyEvent event) {
95        if (keyCode == KeyEvent.KEYCODE_BACK) {
96            if (D) Log.d(TAG, "onKeyDown() called; Key: back key");
97            mTimeoutHandler.removeMessages(BT_ENABLING_TIMEOUT);
98            cancelSendingProgress();
99        }
100        return true;
101    }
102
103    @Override
104    protected void onDestroy() {
105        super.onDestroy();
106        unregisterReceiver(mBluetoothReceiver);
107    }
108
109    private final Handler mTimeoutHandler = new Handler() {
110        @Override
111        public void handleMessage(Message msg) {
112            switch (msg.what) {
113                case BT_ENABLING_TIMEOUT:
114                    if (V) Log.v(TAG, "Received BT_ENABLING_TIMEOUT msg.");
115                    cancelSendingProgress();
116                    break;
117                default:
118                    break;
119            }
120        }
121    };
122
123    private final BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver() {
124        @Override
125        public void onReceive(Context context, Intent intent) {
126            String action = intent.getAction();
127            if (V) Log.v(TAG, "Received intent: " + action) ;
128            if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
129                switch (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
130                    case BluetoothAdapter.STATE_ON:
131                        mTimeoutHandler.removeMessages(BT_ENABLING_TIMEOUT);
132                        finish();
133                        break;
134                    default:
135                        break;
136                }
137            }
138        }
139    };
140
141    private void cancelSendingProgress() {
142        BluetoothOppManager mOppManager = BluetoothOppManager.getInstance(this);
143        if (mOppManager.mSendingFlag) {
144            mOppManager.mSendingFlag = false;
145        }
146        finish();
147    }
148}
149