Home | History | Annotate | Download | only in bluetooth
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.settings.bluetooth;
     18 
     19 import android.bluetooth.BluetoothDevice;
     20 import android.content.BroadcastReceiver;
     21 import android.content.Context;
     22 import android.content.DialogInterface;
     23 import android.content.Intent;
     24 import android.content.IntentFilter;
     25 import android.os.Bundle;
     26 import android.preference.Preference;
     27 import android.util.Log;
     28 import android.view.View;
     29 import android.widget.CheckBox;
     30 import android.widget.CompoundButton;
     31 import android.widget.EditText;
     32 import android.widget.TextView;
     33 import android.widget.Button;
     34 import android.widget.CompoundButton.OnCheckedChangeListener;
     35 
     36 import com.android.internal.app.AlertActivity;
     37 import com.android.internal.app.AlertController;
     38 
     39 import com.android.settings.R;
     40 
     41 /**
     42  * BluetoothPermissionActivity shows a dialog for accepting incoming
     43  * profile connection request from untrusted devices.
     44  */
     45 public class BluetoothPermissionActivity extends AlertActivity implements
     46         DialogInterface.OnClickListener, Preference.OnPreferenceChangeListener {
     47     private static final String TAG = "BluetoothPermissionActivity";
     48     private static final boolean DEBUG = false;
     49 
     50     private static final String KEY_USER_TIMEOUT = "user_timeout";
     51 
     52     private View mView;
     53     private TextView messageView;
     54     private Button mOkButton;
     55     private BluetoothDevice mDevice;
     56 
     57     private BroadcastReceiver mReceiver = new BroadcastReceiver() {
     58         @Override
     59         public void onReceive(Context context, Intent intent) {
     60             String action = intent.getAction();
     61             if (action.equals(BluetoothDevice.ACTION_CONNECTION_ACCESS_CANCEL)) {
     62                 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
     63                 if (mDevice.equals(device)) dismissDialog();
     64             }
     65         }
     66     };
     67 
     68     private void dismissDialog() {
     69         this.dismiss();
     70     }
     71 
     72     @Override
     73     protected void onCreate(Bundle savedInstanceState) {
     74         super.onCreate(savedInstanceState);
     75 
     76         if (DEBUG) Log.d(TAG, "onCreate");
     77         Intent i = getIntent();
     78         String action = i.getAction();
     79         if (action.equals(BluetoothDevice.ACTION_CONNECTION_ACCESS_REQUEST)) {
     80             mDevice = i.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
     81             showConnectionDialog();
     82             registerReceiver(mReceiver,
     83                              new IntentFilter(BluetoothDevice.ACTION_CONNECTION_ACCESS_CANCEL));
     84         } else {
     85             Log.e(TAG, "Error: this activity may be started only with intent "
     86                     + "ACTION_CONNECTION_ACCESS_REQUEST");
     87             finish();
     88         }
     89     }
     90 
     91     private void showConnectionDialog() {
     92         final AlertController.AlertParams p = mAlertParams;
     93         p.mIconId = android.R.drawable.ic_dialog_info;
     94         p.mTitle = getString(R.string.bluetooth_connection_permission_request);
     95         p.mView = createView();
     96         p.mPositiveButtonText = getString(R.string.yes);
     97         p.mPositiveButtonListener = this;
     98         p.mNegativeButtonText = getString(R.string.no);
     99         p.mNegativeButtonListener = this;
    100         mOkButton = mAlert.getButton(DialogInterface.BUTTON_POSITIVE);
    101         setupAlert();
    102     }
    103 
    104     private String createDisplayText() {
    105         String mRemoteName = mDevice != null ? mDevice.getName() : null;
    106         if (mRemoteName == null) mRemoteName = getString(R.string.unknown);
    107         String mMessage1 = getString(R.string.bluetooth_connection_dialog_text,
    108                 mRemoteName);
    109         return mMessage1;
    110     }
    111 
    112     private View createView() {
    113         mView = getLayoutInflater().inflate(R.layout.bluetooth_connection_access, null);
    114         messageView = (TextView)mView.findViewById(R.id.message);
    115         messageView.setText(createDisplayText());
    116         return mView;
    117     }
    118 
    119     private void onPositive() {
    120         sendIntentToReceiver(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY,
    121                              BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT,
    122                              BluetoothDevice.CONNECTION_ACCESS_YES);
    123         finish();
    124     }
    125 
    126     private void onNegative() {
    127         sendIntentToReceiver(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY,
    128                              BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT,
    129                              BluetoothDevice.CONNECTION_ACCESS_NO);
    130         finish();
    131     }
    132 
    133     private void sendIntentToReceiver(final String intentName, final String extraName,
    134             final int extraValue) {
    135         Intent intent = new Intent(intentName);
    136         if (extraName != null) {
    137             intent.putExtra(extraName, extraValue);
    138         }
    139         intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
    140         sendBroadcast(intent, android.Manifest.permission.BLUETOOTH_ADMIN);
    141     }
    142 
    143     public void onClick(DialogInterface dialog, int which) {
    144         switch (which) {
    145             case DialogInterface.BUTTON_POSITIVE:
    146                 onPositive();
    147                 break;
    148 
    149             case DialogInterface.BUTTON_NEGATIVE:
    150                 onNegative();
    151                 break;
    152             default:
    153                 break;
    154         }
    155     }
    156 
    157     @Override
    158     protected void onDestroy() {
    159         super.onDestroy();
    160         unregisterReceiver(mReceiver);
    161     }
    162 
    163     public boolean onPreferenceChange(Preference preference, Object newValue) {
    164         return true;
    165     }
    166 }
    167