BluetoothOppIncomingFileConfirmActivity.java revision b0fa09caef6a0748a568dc2a89f5d94b85423bb2
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        }
175        return true;
176    }
177
178    @Override
179    protected void onDestroy() {
180        super.onDestroy();
181        unregisterReceiver(mReceiver);
182    }
183
184    @Override
185    protected void onRestoreInstanceState(Bundle savedInstanceState) {
186        super.onRestoreInstanceState(savedInstanceState);
187        mTimeout = savedInstanceState.getBoolean(PREFERENCE_USER_TIMEOUT);
188        if (V) Log.v(TAG, "onRestoreInstanceState() mTimeout: " + mTimeout);
189        if (mTimeout) {
190            onTimeout();
191        }
192    }
193
194    @Override
195    protected void onSaveInstanceState(Bundle outState) {
196        super.onSaveInstanceState(outState);
197        if (V) Log.v(TAG, "onSaveInstanceState() mTimeout: " + mTimeout);
198        outState.putBoolean(PREFERENCE_USER_TIMEOUT, mTimeout);
199    }
200
201    private void onTimeout() {
202        mTimeout = true;
203        mContentView.setText(getString(R.string.incoming_file_confirm_timeout_content,
204                mTransInfo.mDeviceName));
205        mAlert.getButton(DialogInterface.BUTTON_NEGATIVE).setVisibility(View.GONE);
206        mAlert.getButton(DialogInterface.BUTTON_POSITIVE).setText(
207                getString(R.string.incoming_file_confirm_timeout_ok));
208
209        mTimeoutHandler.sendMessageDelayed(mTimeoutHandler.obtainMessage(DISMISS_TIMEOUT_DIALOG),
210                DISMISS_TIMEOUT_DIALOG_VALUE);
211    }
212
213    private final Handler mTimeoutHandler = new Handler() {
214        @Override
215        public void handleMessage(Message msg) {
216            switch (msg.what) {
217                case DISMISS_TIMEOUT_DIALOG:
218                    if (V) Log.v(TAG, "Received DISMISS_TIMEOUT_DIALOG msg.");
219                    finish();
220                    break;
221                default:
222                    break;
223            }
224        }
225    };
226}
227