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 
33 package com.android.bluetooth.opp;
34 
35 import android.bluetooth.AlertActivity;
36 import android.bluetooth.BluetoothAdapter;
37 import android.content.BroadcastReceiver;
38 import android.content.Context;
39 import android.content.Intent;
40 import android.content.IntentFilter;
41 import android.os.Bundle;
42 import android.os.Handler;
43 import android.os.Message;
44 import android.util.Log;
45 import android.view.KeyEvent;
46 import android.view.View;
47 import android.widget.TextView;
48 
49 import com.android.bluetooth.R;
50 
51 /**
52  * This class is designed to show BT enabling progress.
53  */
54 public class BluetoothOppBtEnablingActivity extends AlertActivity {
55     private static final String TAG = "BluetoothOppEnablingActivity";
56 
57     private static final boolean D = Constants.DEBUG;
58 
59     private static final boolean V = Constants.VERBOSE;
60 
61     private static final int BT_ENABLING_TIMEOUT = 0;
62 
63     private static final int BT_ENABLING_TIMEOUT_VALUE = 20000;
64 
65     private boolean mRegistered = false;
66 
67     @Override
onCreate(Bundle savedInstanceState)68     protected void onCreate(Bundle savedInstanceState) {
69         super.onCreate(savedInstanceState);
70 
71         // If BT is already enabled jus return.
72         BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
73         if (adapter.isEnabled()) {
74             finish();
75             return;
76         }
77 
78         IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
79         registerReceiver(mBluetoothReceiver, filter);
80         mRegistered = true;
81 
82         mAlertBuilder.setTitle(R.string.enabling_progress_title);
83         mAlertBuilder.setView(createView());
84         setupAlert();
85 
86         // Add timeout for enabling progress
87         mTimeoutHandler.sendMessageDelayed(mTimeoutHandler.obtainMessage(BT_ENABLING_TIMEOUT),
88                 BT_ENABLING_TIMEOUT_VALUE);
89     }
90 
createView()91     private View createView() {
92         View view = getLayoutInflater().inflate(R.layout.bt_enabling_progress, null);
93         TextView contentView = (TextView) view.findViewById(R.id.progress_info);
94         contentView.setText(getString(R.string.enabling_progress_content));
95 
96         return view;
97     }
98 
99     @Override
onKeyDown(int keyCode, KeyEvent event)100     public boolean onKeyDown(int keyCode, KeyEvent event) {
101         if (keyCode == KeyEvent.KEYCODE_BACK) {
102             if (D) {
103                 Log.d(TAG, "onKeyDown() called; Key: back key");
104             }
105             mTimeoutHandler.removeMessages(BT_ENABLING_TIMEOUT);
106             cancelSendingProgress();
107         }
108         return true;
109     }
110 
111     @Override
onDestroy()112     protected void onDestroy() {
113         super.onDestroy();
114         if (mRegistered) {
115             unregisterReceiver(mBluetoothReceiver);
116         }
117     }
118 
119     private final Handler mTimeoutHandler = new Handler() {
120         @Override
121         public void handleMessage(Message msg) {
122             switch (msg.what) {
123                 case BT_ENABLING_TIMEOUT:
124                     if (V) {
125                         Log.v(TAG, "Received BT_ENABLING_TIMEOUT msg.");
126                     }
127                     cancelSendingProgress();
128                     break;
129                 default:
130                     break;
131             }
132         }
133     };
134 
135     private final BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver() {
136         @Override
137         public void onReceive(Context context, Intent intent) {
138             String action = intent.getAction();
139             if (V) {
140                 Log.v(TAG, "Received intent: " + action);
141             }
142             if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
143                 switch (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
144                     case BluetoothAdapter.STATE_ON:
145                         mTimeoutHandler.removeMessages(BT_ENABLING_TIMEOUT);
146                         finish();
147                         break;
148                     default:
149                         break;
150                 }
151             }
152         }
153     };
154 
cancelSendingProgress()155     private void cancelSendingProgress() {
156         BluetoothOppManager mOppManager = BluetoothOppManager.getInstance(this);
157         if (mOppManager.mSendingFlag) {
158             mOppManager.mSendingFlag = false;
159         }
160         finish();
161     }
162 }
163