BluetoothOppIncomingFileConfirmActivity.java revision ce4d93666275df294cb073fe41de5b85932570a8
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    private static final boolean D = Constants.DEBUG;
58    private static final boolean V = Constants.VERBOSE;
59
60    private BluetoothOppTransferInfo mTransInfo;
61
62    private Uri mUri;
63
64    private ContentValues mUpdateValues;
65
66    private boolean mOkOrCancelClicked = false;
67
68    @Override
69    protected void onCreate(Bundle savedInstanceState) {
70        super.onCreate(savedInstanceState);
71
72        Intent intent = getIntent();
73        mUri = intent.getData();
74        mTransInfo = new BluetoothOppTransferInfo();
75        mTransInfo = BluetoothOppUtility.queryRecord(this, mUri);
76        if (mTransInfo == null) {
77            if (V) Log.e(TAG, "Error: Can not get data from db");
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_info;
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 (V) Log.v(TAG, "BluetoothIncomingFileConfirmActivity: Got uri:" + mUri);
94    }
95
96    private View createView() {
97        View view = getLayoutInflater().inflate(R.layout.confirm_dialog, null);
98
99        TextView contentView = (TextView)view.findViewById(R.id.content);
100
101        String text = getString(R.string.incoming_file_confirm_content, mTransInfo.mDeviceName,
102                mTransInfo.mFileName, Formatter.formatFileSize(this, mTransInfo.mTotalBytes));
103
104        contentView.setText(text);
105
106        return view;
107    }
108
109    public void onClick(DialogInterface dialog, int which) {
110        switch (which) {
111            case DialogInterface.BUTTON_POSITIVE:
112                // Update database
113                mUpdateValues = new ContentValues();
114                mUpdateValues.put(BluetoothShare.USER_CONFIRMATION,
115                        BluetoothShare.USER_CONFIRMATION_CONFIRMED);
116                this.getContentResolver().update(mUri, mUpdateValues, null, null);
117
118                Toast.makeText(this, getString(R.string.bt_toast_1), Toast.LENGTH_SHORT).show();
119                break;
120
121            case DialogInterface.BUTTON_NEGATIVE:
122                // Update database
123                mUpdateValues = new ContentValues();
124                mUpdateValues.put(BluetoothShare.USER_CONFIRMATION,
125                        BluetoothShare.USER_CONFIRMATION_DENIED);
126                this.getContentResolver().update(mUri, mUpdateValues, null, null);
127
128                Toast.makeText(this, getString(R.string.bt_toast_2), Toast.LENGTH_SHORT).show();
129                break;
130        }
131        mOkOrCancelClicked = true;
132    }
133
134    @Override
135    protected void onDestroy() {
136        super.onDestroy();
137        if (mOkOrCancelClicked) {
138            return;
139        }
140        // for the "back" key press case
141        mUpdateValues = new ContentValues();
142        mUpdateValues.put(BluetoothShare.VISIBILITY, BluetoothShare.VISIBILITY_HIDDEN);
143        this.getContentResolver().update(mUri, mUpdateValues, null, null);
144        if (V) Log.v(TAG, "db updated: change to VISIBILITY_HIDDEN");
145        Toast.makeText(this, getString(R.string.bt_toast_2), Toast.LENGTH_SHORT).show();
146    }
147}
148