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.content.BroadcastReceiver; 39 import android.content.ContentValues; 40 import android.content.Context; 41 import android.content.DialogInterface; 42 import android.content.Intent; 43 import android.content.IntentFilter; 44 import android.net.Uri; 45 import android.os.Bundle; 46 import android.os.Handler; 47 import android.os.Message; 48 import android.text.format.Formatter; 49 import android.util.Log; 50 import android.view.KeyEvent; 51 import android.view.View; 52 import android.widget.TextView; 53 import android.widget.Toast; 54 55 import com.android.bluetooth.BluetoothMethodProxy; 56 import com.android.bluetooth.R; 57 58 /** 59 * This class is designed to ask user to confirm if accept incoming file; 60 */ 61 public class BluetoothOppIncomingFileConfirmActivity extends AlertActivity { 62 private static final String TAG = "BluetoothIncomingFileConfirmActivity"; 63 private static final boolean D = Constants.DEBUG; 64 private static final boolean V = Constants.VERBOSE; 65 66 private static final int DISMISS_TIMEOUT_DIALOG = 0; 67 68 private static final int DISMISS_TIMEOUT_DIALOG_VALUE = 2000; 69 70 private static final String PREFERENCE_USER_TIMEOUT = "user_timeout"; 71 72 private BluetoothOppTransferInfo mTransInfo; 73 74 private Uri mUri; 75 76 private ContentValues mUpdateValues; 77 78 private boolean mTimeout = false; 79 80 private BroadcastReceiver mReceiver = null; 81 82 @Override onCreate(Bundle savedInstanceState)83 protected void onCreate(Bundle savedInstanceState) { 84 setTheme(R.style.Theme_Material_Settings_Floating); 85 if (V) { 86 Log.d(TAG, "onCreate(): action = " + getIntent().getAction()); 87 } 88 super.onCreate(savedInstanceState); 89 90 getWindow().addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS); 91 Intent intent = getIntent(); 92 mUri = intent.getData(); 93 mTransInfo = new BluetoothOppTransferInfo(); 94 mTransInfo = BluetoothOppUtility.queryRecord(this, mUri); 95 if (mTransInfo == null) { 96 if (V) { 97 Log.e(TAG, "Error: Can not get data from db"); 98 } 99 finish(); 100 return; 101 } 102 103 mAlertBuilder.setTitle(getString(R.string.incoming_file_confirm_content)); 104 mAlertBuilder.setView(createView()); 105 mAlertBuilder.setPositiveButton(R.string.incoming_file_confirm_ok, 106 (dialog, which) -> onIncomingFileConfirmOk()); 107 mAlertBuilder.setNegativeButton(R.string.incoming_file_confirm_cancel, 108 (dialog, which) -> onIncomingFileConfirmCancel()); 109 110 setupAlert(); 111 if (V) { 112 Log.v(TAG, "mTimeout: " + mTimeout); 113 } 114 if (mTimeout) { 115 onTimeout(); 116 } 117 118 if (V) { 119 Log.v(TAG, "BluetoothIncomingFileConfirmActivity: Got uri:" + mUri); 120 } 121 122 mReceiver = new BroadcastReceiver() { 123 @Override 124 public void onReceive(Context context, Intent intent) { 125 if (!BluetoothShare.USER_CONFIRMATION_TIMEOUT_ACTION.equals(intent.getAction())) { 126 return; 127 } 128 onTimeout(); 129 } 130 }; 131 registerReceiver(mReceiver, 132 new IntentFilter(BluetoothShare.USER_CONFIRMATION_TIMEOUT_ACTION)); 133 } 134 createView()135 private View createView() { 136 View view = getLayoutInflater().inflate(R.layout.incoming_dialog, null); 137 138 ((TextView) view.findViewById(R.id.from_content)).setText(mTransInfo.mDeviceName); 139 String fileName = mTransInfo.mFileName; 140 if (fileName != null) { 141 fileName = fileName 142 .replace('\t', ' ') 143 .replace('\n', ' ') 144 .replace('\r', ' '); 145 } 146 ((TextView) view.findViewById(R.id.filename_content)).setText(fileName); 147 ((TextView) view.findViewById(R.id.size_content)).setText( 148 Formatter.formatFileSize(this, mTransInfo.mTotalBytes)); 149 150 return view; 151 } 152 onIncomingFileConfirmOk()153 private void onIncomingFileConfirmOk() { 154 if (!mTimeout) { 155 // Update database 156 mUpdateValues = new ContentValues(); 157 mUpdateValues.put(BluetoothShare.USER_CONFIRMATION, 158 BluetoothShare.USER_CONFIRMATION_CONFIRMED); 159 BluetoothMethodProxy.getInstance().contentResolverUpdate(this.getContentResolver(), 160 mUri, mUpdateValues, null, null); 161 162 Toast.makeText(this, getString(R.string.bt_toast_1), Toast.LENGTH_SHORT).show(); 163 } 164 } 165 onIncomingFileConfirmCancel()166 private void onIncomingFileConfirmCancel() { 167 // Update database 168 mUpdateValues = new ContentValues(); 169 mUpdateValues.put(BluetoothShare.USER_CONFIRMATION, 170 BluetoothShare.USER_CONFIRMATION_DENIED); 171 BluetoothMethodProxy.getInstance().contentResolverUpdate(this.getContentResolver(), 172 mUri, mUpdateValues, null, null); 173 } 174 175 @Override onKeyDown(int keyCode, KeyEvent event)176 public boolean onKeyDown(int keyCode, KeyEvent event) { 177 if (keyCode == KeyEvent.KEYCODE_BACK) { 178 if (D) { 179 Log.d(TAG, "onKeyDown() called; Key: back key"); 180 } 181 finish(); 182 return true; 183 } 184 return false; 185 } 186 187 @Override onDestroy()188 protected void onDestroy() { 189 super.onDestroy(); 190 if (mReceiver != null) { 191 unregisterReceiver(mReceiver); 192 } 193 } 194 195 @Override onRestoreInstanceState(Bundle savedInstanceState)196 protected void onRestoreInstanceState(Bundle savedInstanceState) { 197 super.onRestoreInstanceState(savedInstanceState); 198 mTimeout = savedInstanceState.getBoolean(PREFERENCE_USER_TIMEOUT); 199 if (V) { 200 Log.v(TAG, "onRestoreInstanceState() mTimeout: " + mTimeout); 201 } 202 if (mTimeout) { 203 onTimeout(); 204 } 205 } 206 207 @Override onSaveInstanceState(Bundle outState)208 protected void onSaveInstanceState(Bundle outState) { 209 super.onSaveInstanceState(outState); 210 if (V) { 211 Log.v(TAG, "onSaveInstanceState() mTimeout: " + mTimeout); 212 } 213 outState.putBoolean(PREFERENCE_USER_TIMEOUT, mTimeout); 214 } 215 onTimeout()216 private void onTimeout() { 217 mTimeout = true; 218 219 changeTitle(getString( 220 R.string.incoming_file_confirm_timeout_content, 221 mTransInfo.mDeviceName)); 222 changeButtonVisibility(DialogInterface.BUTTON_NEGATIVE, View.GONE); 223 changeButtonText( 224 DialogInterface.BUTTON_POSITIVE, 225 getString(R.string.incoming_file_confirm_timeout_ok)); 226 227 mTimeoutHandler.sendMessageDelayed(mTimeoutHandler.obtainMessage(DISMISS_TIMEOUT_DIALOG), 228 DISMISS_TIMEOUT_DIALOG_VALUE); 229 } 230 231 private final Handler mTimeoutHandler = new Handler() { 232 @Override 233 public void handleMessage(Message msg) { 234 switch (msg.what) { 235 case DISMISS_TIMEOUT_DIALOG: 236 if (V) { 237 Log.v(TAG, "Received DISMISS_TIMEOUT_DIALOG msg."); 238 } 239 finish(); 240 break; 241 default: 242 break; 243 } 244 } 245 }; 246 } 247