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