BluetoothOppIncomingFileConfirmActivity.java revision 6769b59d715ea98bd72eafcfea9acd2714a887da
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.ContentValues;
38import android.content.DialogInterface;
39import android.content.Intent;
40import android.net.Uri;
41import android.os.Bundle;
42import android.util.Log;
43import android.view.View;
44import android.widget.TextView;
45import android.widget.Toast;
46import android.text.format.Formatter;
47
48import com.android.internal.app.AlertActivity;
49import com.android.internal.app.AlertController;
50
51/**
52 * This class is designed to ask user to confirm if accept incoming file;
53 */
54public class BluetoothOppIncomingFileConfirmActivity extends AlertActivity implements
55        DialogInterface.OnClickListener {
56    private static final String TAG = "BluetoothIncomingFileConfirmActivity";
57
58    private BluetoothOppTransferInfo mTransInfo;
59
60    private Uri mUri;
61
62    private ContentValues mUpdateValues;
63
64    private boolean mOkOrCancelClicked = false;
65
66    @Override
67    protected void onCreate(Bundle savedInstanceState) {
68        super.onCreate(savedInstanceState);
69
70        Intent intent = getIntent();
71        mUri = intent.getData();
72        mTransInfo = new BluetoothOppTransferInfo();
73        mTransInfo = BluetoothOppUtility.queryRecord(this, mUri);
74        if (mTransInfo == null) {
75            if (Constants.LOGVV) {
76                Log.e(TAG, "Error: Can not get data from db");
77            }
78            finish();
79            return;
80        }
81
82        // Set up the "dialog"
83        final AlertController.AlertParams p = mAlertParams;
84        p.mIconId = android.R.drawable.ic_dialog_alert;
85        p.mTitle = getString(R.string.incoming_file_confirm_title);
86        p.mView = createView();
87        p.mPositiveButtonText = getString(R.string.incoming_file_confirm_ok);
88        p.mPositiveButtonListener = this;
89        p.mNegativeButtonText = getString(R.string.incoming_file_confirm_cancel);
90        p.mNegativeButtonListener = this;
91        setupAlert();
92
93        if (Constants.LOGVV) {
94            Log.v(TAG, "BluetoothIncomingFileConfirmActivity: Got uri:" + mUri);
95        }
96    }
97
98    private View createView() {
99        View view = getLayoutInflater().inflate(R.layout.confirm_dialog, null);
100
101        TextView contentView = (TextView)view.findViewById(R.id.content);
102
103        String text = getString(R.string.incoming_file_confirm_content).replace("%s1",
104                mTransInfo.mDeviceName).replace("%s2", mTransInfo.mFileName).replace("%s3",
105                Formatter.formatFileSize(this, mTransInfo.mTotalBytes));
106
107        contentView.setText(text);
108
109        return view;
110    }
111
112    public void onClick(DialogInterface dialog, int which) {
113        switch (which) {
114            case DialogInterface.BUTTON_POSITIVE:
115                // Update database
116                mUpdateValues = new ContentValues();
117                mUpdateValues.put(BluetoothShare.USER_CONFIRMATION,
118                        BluetoothShare.USER_CONFIRMATION_CONFIRMED);
119                this.getContentResolver().update(mUri, mUpdateValues, null, null);
120
121                Toast.makeText(this, getString(R.string.bt_toast_1), Toast.LENGTH_SHORT).show();
122                break;
123
124            case DialogInterface.BUTTON_NEGATIVE:
125                // Update database
126                mUpdateValues = new ContentValues();
127                mUpdateValues.put(BluetoothShare.USER_CONFIRMATION,
128                        BluetoothShare.USER_CONFIRMATION_DENIED);
129                this.getContentResolver().update(mUri, mUpdateValues, null, null);
130
131                Toast.makeText(this, getString(R.string.bt_toast_2), Toast.LENGTH_SHORT).show();
132                break;
133        }
134        mOkOrCancelClicked = true;
135    }
136
137    @Override
138    protected void onDestroy() {
139        super.onDestroy();
140        if (mOkOrCancelClicked) {
141            return;
142        }
143        // for the "back" key press case
144        mUpdateValues = new ContentValues();
145        mUpdateValues.put(BluetoothShare.VISIBILITY, BluetoothShare.VISIBILITY_HIDDEN);
146        this.getContentResolver().update(mUri, mUpdateValues, null, null);
147        if (Constants.LOGVV) {
148            Log.v(TAG, "db updated: change to VISIBILITY_HIDDEN");
149        }
150        Toast.makeText(this, getString(R.string.bt_toast_2), Toast.LENGTH_SHORT).show();
151    }
152}
153