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