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