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.BluetoothMethodProxy;
52 import com.android.bluetooth.R;
53 import com.android.internal.annotations.VisibleForTesting;
54 
55 /** This class is designed to show BT enabling progress. */
56 public class BluetoothOppBtEnablingActivity extends AlertActivity {
57     private static final String TAG = "BluetoothOppEnablingActivity";
58 
59     private static final int BT_ENABLING_TIMEOUT = 0;
60 
61     @VisibleForTesting static int sBtEnablingTimeoutMs = 20000;
62 
63     private boolean mRegistered = false;
64 
65     @Override
onCreate(Bundle savedInstanceState)66     protected void onCreate(Bundle savedInstanceState) {
67         super.onCreate(savedInstanceState);
68 
69         getWindow().addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
70         // If BT is already enabled jus return.
71         BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
72         if (BluetoothMethodProxy.getInstance().bluetoothAdapterIsEnabled(adapter)) {
73             finish();
74             return;
75         }
76 
77         IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
78         filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
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(
88                 mTimeoutHandler.obtainMessage(BT_ENABLING_TIMEOUT), sBtEnablingTimeoutMs);
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             Log.d(TAG, "onKeyDown() called; Key: back key");
103             mTimeoutHandler.removeMessages(BT_ENABLING_TIMEOUT);
104             cancelSendingProgress();
105         }
106         return true;
107     }
108 
109     @Override
onDestroy()110     protected void onDestroy() {
111         super.onDestroy();
112         if (mRegistered) {
113             unregisterReceiver(mBluetoothReceiver);
114         }
115     }
116 
117     @VisibleForTesting
118     final Handler mTimeoutHandler =
119             new Handler() {
120                 @Override
121                 public void handleMessage(Message msg) {
122                     switch (msg.what) {
123                         case BT_ENABLING_TIMEOUT:
124                             Log.v(TAG, "Received BT_ENABLING_TIMEOUT msg.");
125                             cancelSendingProgress();
126                             break;
127                         default:
128                             break;
129                     }
130                 }
131             };
132 
133     @VisibleForTesting
134     final BroadcastReceiver mBluetoothReceiver =
135             new BroadcastReceiver() {
136                 @Override
137                 public void onReceive(Context context, Intent intent) {
138                     String action = intent.getAction();
139                     Log.v(TAG, "Received intent: " + action);
140                     if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
141                         switch (intent.getIntExtra(
142                                 BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
143                             case BluetoothAdapter.STATE_ON:
144                                 mTimeoutHandler.removeMessages(BT_ENABLING_TIMEOUT);
145                                 finish();
146                                 break;
147                             default:
148                                 break;
149                         }
150                     }
151                 }
152             };
153 
cancelSendingProgress()154     private void cancelSendingProgress() {
155         BluetoothOppManager mOppManager = BluetoothOppManager.getInstance(this);
156         if (mOppManager.mSendingFlag) {
157             mOppManager.mSendingFlag = false;
158         }
159         finish();
160     }
161 }
162