Home | History | Annotate | Download | only in opp
      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 
     33 package com.android.bluetooth.opp;
     34 
     35 import com.android.bluetooth.R;
     36 
     37 import android.content.BroadcastReceiver;
     38 import android.content.ContentValues;
     39 import android.content.Context;
     40 import android.content.DialogInterface;
     41 import android.content.Intent;
     42 import android.content.IntentFilter;
     43 import android.net.Uri;
     44 import android.os.Bundle;
     45 import android.os.Handler;
     46 import android.os.Message;
     47 import android.util.Log;
     48 import android.view.KeyEvent;
     49 import android.view.View;
     50 import android.widget.TextView;
     51 import android.widget.Toast;
     52 import android.text.format.Formatter;
     53 
     54 import com.android.internal.app.AlertActivity;
     55 import com.android.internal.app.AlertController;
     56 
     57 /**
     58  * This class is designed to ask user to confirm if accept incoming file;
     59  */
     60 public 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         setTheme(R.style.Theme_Material_Settings_Floating);
     95         if (V) Log.d(TAG, "onCreate(): action = " + getIntent().getAction());
     96         super.onCreate(savedInstanceState);
     97 
     98         Intent intent = getIntent();
     99         mUri = intent.getData();
    100         mTransInfo = new BluetoothOppTransferInfo();
    101         mTransInfo = BluetoothOppUtility.queryRecord(this, mUri);
    102         if (mTransInfo == null) {
    103             if (V) Log.e(TAG, "Error: Can not get data from db");
    104             finish();
    105             return;
    106         }
    107 
    108         // Set up the "dialog"
    109         final AlertController.AlertParams p = mAlertParams;
    110         p.mTitle = getString(R.string.incoming_file_confirm_content);
    111         p.mView = createView();
    112         p.mPositiveButtonText = getString(R.string.incoming_file_confirm_ok);
    113         p.mPositiveButtonListener = this;
    114         p.mNegativeButtonText = getString(R.string.incoming_file_confirm_cancel);
    115         p.mNegativeButtonListener = this;
    116         setupAlert();
    117         if (V) Log.v(TAG, "mTimeout: " + mTimeout);
    118         if (mTimeout) {
    119             onTimeout();
    120         }
    121 
    122         if (V) Log.v(TAG, "BluetoothIncomingFileConfirmActivity: Got uri:" + mUri);
    123 
    124         registerReceiver(mReceiver, new IntentFilter(
    125                 BluetoothShare.USER_CONFIRMATION_TIMEOUT_ACTION));
    126     }
    127 
    128     private View createView() {
    129         View view = getLayoutInflater().inflate(R.layout.incoming_dialog, null);
    130 
    131         ((TextView)view.findViewById(R.id.from_content)).setText(mTransInfo.mDeviceName);
    132         ((TextView)view.findViewById(R.id.filename_content)).setText(mTransInfo.mFileName);
    133         ((TextView)view.findViewById(R.id.size_content)).setText(
    134                 Formatter.formatFileSize(this, mTransInfo.mTotalBytes));
    135 
    136         return view;
    137     }
    138 
    139     public void onClick(DialogInterface dialog, int which) {
    140         switch (which) {
    141             case DialogInterface.BUTTON_POSITIVE:
    142                 if (!mTimeout) {
    143                     // Update database
    144                     mUpdateValues = new ContentValues();
    145                     mUpdateValues.put(BluetoothShare.USER_CONFIRMATION,
    146                             BluetoothShare.USER_CONFIRMATION_CONFIRMED);
    147                     this.getContentResolver().update(mUri, mUpdateValues, null, null);
    148 
    149                     Toast.makeText(this, getString(R.string.bt_toast_1), Toast.LENGTH_SHORT).show();
    150                 }
    151                 break;
    152 
    153             case DialogInterface.BUTTON_NEGATIVE:
    154                 // Update database
    155                 mUpdateValues = new ContentValues();
    156                 mUpdateValues.put(BluetoothShare.USER_CONFIRMATION,
    157                         BluetoothShare.USER_CONFIRMATION_DENIED);
    158                 this.getContentResolver().update(mUri, mUpdateValues, null, null);
    159                 break;
    160         }
    161     }
    162 
    163     @Override
    164     public boolean onKeyDown(int keyCode, KeyEvent event) {
    165         if (keyCode == KeyEvent.KEYCODE_BACK) {
    166             if (D) Log.d(TAG, "onKeyDown() called; Key: back key");
    167             mUpdateValues = new ContentValues();
    168             mUpdateValues.put(BluetoothShare.VISIBILITY, BluetoothShare.VISIBILITY_HIDDEN);
    169             this.getContentResolver().update(mUri, mUpdateValues, null, null);
    170 
    171             Toast.makeText(this, getString(R.string.bt_toast_2), Toast.LENGTH_SHORT).show();
    172             finish();
    173             return true;
    174         }
    175         return false;
    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