BluetoothOppIncomingFileConfirmActivity.java revision 453050fdaf7dcd488f399d3549ce257e7b9e7e79
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 com.android.bluetooth.R;
36
37import android.content.BroadcastReceiver;
38import android.content.ContentValues;
39import android.content.Context;
40import android.content.DialogInterface;
41import android.content.Intent;
42import android.content.IntentFilter;
43import android.net.Uri;
44import android.os.Bundle;
45import android.os.Handler;
46import android.os.Message;
47import android.util.Log;
48import android.view.KeyEvent;
49import android.view.View;
50import android.widget.TextView;
51import android.widget.Toast;
52import android.text.format.Formatter;
53
54import com.android.internal.app.AlertActivity;
55import com.android.internal.app.AlertController;
56
57/**
58 * This class is designed to ask user to confirm if accept incoming file;
59 */
60public class BluetoothOppIncomingFileConfirmActivity extends AlertActivity implements
61        DialogInterface.OnClickListener {
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 TextView mContentView;
79
80    private boolean mTimeout = false;
81
82    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
83        @Override
84        public void onReceive(Context context, Intent intent) {
85            if (!BluetoothShare.USER_CONFIRMATION_TIMEOUT_ACTION.equals(intent.getAction())) {
86                return;
87            }
88            onTimeout();
89        }
90    };
91
92    @Override
93    protected void onCreate(Bundle savedInstanceState) {
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) Log.e(TAG, "Error: Can not get data from db");
102            finish();
103            return;
104        }
105
106        // Set up the "dialog"
107        final AlertController.AlertParams p = mAlertParams;
108        p.mIconId = android.R.drawable.ic_dialog_info;
109        p.mTitle = getString(R.string.incoming_file_confirm_title);
110        p.mView = createView();
111        p.mPositiveButtonText = getString(R.string.incoming_file_confirm_ok);
112        p.mPositiveButtonListener = this;
113        p.mNegativeButtonText = getString(R.string.incoming_file_confirm_cancel);
114        p.mNegativeButtonListener = this;
115        setupAlert();
116        if (V) Log.v(TAG, "mTimeout: " + mTimeout);
117        if (mTimeout) {
118            onTimeout();
119        }
120
121        if (V) Log.v(TAG, "BluetoothIncomingFileConfirmActivity: Got uri:" + mUri);
122
123        registerReceiver(mReceiver, new IntentFilter(
124                BluetoothShare.USER_CONFIRMATION_TIMEOUT_ACTION));
125    }
126
127    private View createView() {
128        View view = getLayoutInflater().inflate(R.layout.confirm_dialog, null);
129
130        mContentView = (TextView)view.findViewById(R.id.content);
131
132        String text = getString(R.string.incoming_file_confirm_content, mTransInfo.mDeviceName,
133                mTransInfo.mFileName, Formatter.formatFileSize(this, mTransInfo.mTotalBytes));
134
135        mContentView.setText(text);
136
137        return view;
138    }
139
140    public void onClick(DialogInterface dialog, int which) {
141        switch (which) {
142            case DialogInterface.BUTTON_POSITIVE:
143                if (!mTimeout) {
144                    // Update database
145                    mUpdateValues = new ContentValues();
146                    mUpdateValues.put(BluetoothShare.USER_CONFIRMATION,
147                            BluetoothShare.USER_CONFIRMATION_CONFIRMED);
148                    this.getContentResolver().update(mUri, mUpdateValues, null, null);
149
150                    Toast.makeText(this, getString(R.string.bt_toast_1), Toast.LENGTH_SHORT).show();
151                }
152                break;
153
154            case DialogInterface.BUTTON_NEGATIVE:
155                // Update database
156                mUpdateValues = new ContentValues();
157                mUpdateValues.put(BluetoothShare.USER_CONFIRMATION,
158                        BluetoothShare.USER_CONFIRMATION_DENIED);
159                this.getContentResolver().update(mUri, mUpdateValues, null, null);
160                break;
161        }
162    }
163
164    @Override
165    public boolean onKeyDown(int keyCode, KeyEvent event) {
166        if (keyCode == KeyEvent.KEYCODE_BACK) {
167            if (D) Log.d(TAG, "onKeyDown() called; Key: back key");
168            mUpdateValues = new ContentValues();
169            mUpdateValues.put(BluetoothShare.VISIBILITY, BluetoothShare.VISIBILITY_HIDDEN);
170            this.getContentResolver().update(mUri, mUpdateValues, null, null);
171
172            Toast.makeText(this, getString(R.string.bt_toast_2), Toast.LENGTH_SHORT).show();
173            finish();
174            return true;
175        }
176        return false;
177    }
178
179    @Override
180    protected void onDestroy() {
181        super.onDestroy();
182        unregisterReceiver(mReceiver);
183    }
184
185    @Override
186    protected void onRestoreInstanceState(Bundle savedInstanceState) {
187        super.onRestoreInstanceState(savedInstanceState);
188        mTimeout = savedInstanceState.getBoolean(PREFERENCE_USER_TIMEOUT);
189        if (V) Log.v(TAG, "onRestoreInstanceState() mTimeout: " + mTimeout);
190        if (mTimeout) {
191            onTimeout();
192        }
193    }
194
195    @Override
196    protected void onSaveInstanceState(Bundle outState) {
197        super.onSaveInstanceState(outState);
198        if (V) Log.v(TAG, "onSaveInstanceState() mTimeout: " + mTimeout);
199        outState.putBoolean(PREFERENCE_USER_TIMEOUT, mTimeout);
200    }
201
202    private void onTimeout() {
203        mTimeout = true;
204        mContentView.setText(getString(R.string.incoming_file_confirm_timeout_content,
205                mTransInfo.mDeviceName));
206        mAlert.getButton(DialogInterface.BUTTON_NEGATIVE).setVisibility(View.GONE);
207        mAlert.getButton(DialogInterface.BUTTON_POSITIVE).setText(
208                getString(R.string.incoming_file_confirm_timeout_ok));
209
210        mTimeoutHandler.sendMessageDelayed(mTimeoutHandler.obtainMessage(DISMISS_TIMEOUT_DIALOG),
211                DISMISS_TIMEOUT_DIALOG_VALUE);
212    }
213
214    private final Handler mTimeoutHandler = new Handler() {
215        @Override
216        public void handleMessage(Message msg) {
217            switch (msg.what) {
218                case DISMISS_TIMEOUT_DIALOG:
219                    if (V) Log.v(TAG, "Received DISMISS_TIMEOUT_DIALOG msg.");
220                    finish();
221                    break;
222                default:
223                    break;
224            }
225        }
226    };
227}
228