BluetoothOppIncomingFileConfirmActivity.java revision c4fbd756e2645147470c486ae96f2253f5e13a52
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
33package com.android.bluetooth.opp;
34
35import android.content.BroadcastReceiver;
36import android.content.ContentValues;
37import android.content.Context;
38import android.content.DialogInterface;
39import android.content.Intent;
40import android.content.IntentFilter;
41import android.net.Uri;
42import android.os.Bundle;
43import android.os.Handler;
44import android.os.Message;
45import android.text.format.Formatter;
46import android.util.Log;
47import android.view.KeyEvent;
48import android.view.View;
49import android.widget.TextView;
50import android.widget.Toast;
51
52import com.android.bluetooth.R;
53import com.android.internal.app.AlertActivity;
54import com.android.internal.app.AlertController;
55
56/**
57 * This class is designed to ask user to confirm if accept incoming file;
58 */
59public class BluetoothOppIncomingFileConfirmActivity extends AlertActivity
60        implements DialogInterface.OnClickListener {
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
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        Intent intent = getIntent();
98        mUri = intent.getData();
99        mTransInfo = new BluetoothOppTransferInfo();
100        mTransInfo = BluetoothOppUtility.queryRecord(this, mUri);
101        if (mTransInfo == null) {
102            if (V) {
103                Log.e(TAG, "Error: Can not get data from db");
104            }
105            finish();
106            return;
107        }
108
109        // Set up the "dialog"
110        final AlertController.AlertParams p = mAlertParams;
111        p.mTitle = getString(R.string.incoming_file_confirm_content);
112        p.mView = createView();
113        p.mPositiveButtonText = getString(R.string.incoming_file_confirm_ok);
114        p.mPositiveButtonListener = this;
115        p.mNegativeButtonText = getString(R.string.incoming_file_confirm_cancel);
116        p.mNegativeButtonListener = this;
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
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
144    @Override
145    public void onClick(DialogInterface dialog, int which) {
146        switch (which) {
147            case DialogInterface.BUTTON_POSITIVE:
148                if (!mTimeout) {
149                    // Update database
150                    mUpdateValues = new ContentValues();
151                    mUpdateValues.put(BluetoothShare.USER_CONFIRMATION,
152                            BluetoothShare.USER_CONFIRMATION_CONFIRMED);
153                    this.getContentResolver().update(mUri, mUpdateValues, null, null);
154
155                    Toast.makeText(this, getString(R.string.bt_toast_1), Toast.LENGTH_SHORT).show();
156                }
157                break;
158
159            case DialogInterface.BUTTON_NEGATIVE:
160                // Update database
161                mUpdateValues = new ContentValues();
162                mUpdateValues.put(BluetoothShare.USER_CONFIRMATION,
163                        BluetoothShare.USER_CONFIRMATION_DENIED);
164                this.getContentResolver().update(mUri, mUpdateValues, null, null);
165                break;
166        }
167    }
168
169    @Override
170    public boolean onKeyDown(int keyCode, KeyEvent event) {
171        if (keyCode == KeyEvent.KEYCODE_BACK) {
172            if (D) {
173                Log.d(TAG, "onKeyDown() called; Key: back key");
174            }
175            mUpdateValues = new ContentValues();
176            mUpdateValues.put(BluetoothShare.VISIBILITY, BluetoothShare.VISIBILITY_HIDDEN);
177            this.getContentResolver().update(mUri, mUpdateValues, null, null);
178
179            Toast.makeText(this, getString(R.string.bt_toast_2), Toast.LENGTH_SHORT).show();
180            finish();
181            return true;
182        }
183        return false;
184    }
185
186    @Override
187    protected void onDestroy() {
188        super.onDestroy();
189        unregisterReceiver(mReceiver);
190    }
191
192    @Override
193    protected void onRestoreInstanceState(Bundle savedInstanceState) {
194        super.onRestoreInstanceState(savedInstanceState);
195        mTimeout = savedInstanceState.getBoolean(PREFERENCE_USER_TIMEOUT);
196        if (V) {
197            Log.v(TAG, "onRestoreInstanceState() mTimeout: " + mTimeout);
198        }
199        if (mTimeout) {
200            onTimeout();
201        }
202    }
203
204    @Override
205    protected void onSaveInstanceState(Bundle outState) {
206        super.onSaveInstanceState(outState);
207        if (V) {
208            Log.v(TAG, "onSaveInstanceState() mTimeout: " + mTimeout);
209        }
210        outState.putBoolean(PREFERENCE_USER_TIMEOUT, mTimeout);
211    }
212
213    private void onTimeout() {
214        mTimeout = true;
215        mAlert.setTitle(
216                getString(R.string.incoming_file_confirm_timeout_content, mTransInfo.mDeviceName));
217        mAlert.getButton(DialogInterface.BUTTON_NEGATIVE).setVisibility(View.GONE);
218        mAlert.getButton(DialogInterface.BUTTON_POSITIVE)
219                .setText(getString(R.string.incoming_file_confirm_timeout_ok));
220
221        mTimeoutHandler.sendMessageDelayed(mTimeoutHandler.obtainMessage(DISMISS_TIMEOUT_DIALOG),
222                DISMISS_TIMEOUT_DIALOG_VALUE);
223    }
224
225    private final Handler mTimeoutHandler = new Handler() {
226        @Override
227        public void handleMessage(Message msg) {
228            switch (msg.what) {
229                case DISMISS_TIMEOUT_DIALOG:
230                    if (V) {
231                        Log.v(TAG, "Received DISMISS_TIMEOUT_DIALOG msg.");
232                    }
233                    finish();
234                    break;
235                default:
236                    break;
237            }
238        }
239    };
240}
241