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