• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.pbap;
34 
35 import android.bluetooth.BluetoothDevice;
36 import android.content.BroadcastReceiver;
37 import android.content.Context;
38 import android.content.DialogInterface;
39 import android.content.Intent;
40 import android.content.IntentFilter;
41 import android.os.Bundle;
42 import android.os.Handler;
43 import android.os.Message;
44 import android.preference.Preference;
45 import android.text.InputFilter;
46 import android.text.InputFilter.LengthFilter;
47 import android.text.TextWatcher;
48 import android.util.Log;
49 import android.view.View;
50 import android.widget.Button;
51 import android.widget.EditText;
52 import android.widget.TextView;
53 
54 import com.android.bluetooth.R;
55 import com.android.internal.app.AlertActivity;
56 import com.android.internal.app.AlertController;
57 
58 /**
59  * PbapActivity shows two dialogues: One for accepting incoming pbap request and
60  * the other prompts the user to enter a session key for authentication with a
61  * remote Bluetooth device.
62  */
63 public class BluetoothPbapActivity extends AlertActivity
64         implements DialogInterface.OnClickListener, Preference.OnPreferenceChangeListener,
65         TextWatcher {
66     private static final String TAG = "BluetoothPbapActivity";
67 
68     private static final boolean V = BluetoothPbapService.VERBOSE;
69 
70     private static final int BLUETOOTH_OBEX_AUTHKEY_MAX_LENGTH = 16;
71 
72     private static final int DIALOG_YES_NO_AUTH = 1;
73 
74     private static final String KEY_USER_TIMEOUT = "user_timeout";
75 
76     private View mView;
77 
78     private EditText mKeyView;
79 
80     private TextView mMessageView;
81 
82     private String mSessionKey = "";
83 
84     private int mCurrentDialog;
85 
86     private Button mOkButton;
87 
88     private boolean mTimeout = false;
89 
90     private static final int DISMISS_TIMEOUT_DIALOG = 0;
91 
92     private static final int DISMISS_TIMEOUT_DIALOG_VALUE = 2000;
93 
94     private BluetoothDevice mDevice;
95 
96     private BroadcastReceiver mReceiver = new BroadcastReceiver() {
97         @Override
98         public void onReceive(Context context, Intent intent) {
99             if (!BluetoothPbapService.USER_CONFIRM_TIMEOUT_ACTION.equals(intent.getAction())) {
100                 return;
101             }
102             onTimeout();
103         }
104     };
105 
106     @Override
onCreate(Bundle savedInstanceState)107     protected void onCreate(Bundle savedInstanceState) {
108         super.onCreate(savedInstanceState);
109         Intent i = getIntent();
110         String action = i.getAction();
111         mDevice = i.getParcelableExtra(BluetoothPbapService.EXTRA_DEVICE);
112         if (action != null && action.equals(BluetoothPbapService.AUTH_CHALL_ACTION)) {
113             showPbapDialog(DIALOG_YES_NO_AUTH);
114             mCurrentDialog = DIALOG_YES_NO_AUTH;
115         } else {
116             Log.e(TAG, "Error: this activity may be started only with intent "
117                     + "PBAP_ACCESS_REQUEST or PBAP_AUTH_CHALL ");
118             finish();
119         }
120         registerReceiver(mReceiver,
121                 new IntentFilter(BluetoothPbapService.USER_CONFIRM_TIMEOUT_ACTION));
122     }
123 
showPbapDialog(int id)124     private void showPbapDialog(int id) {
125         final AlertController.AlertParams p = mAlertParams;
126         switch (id) {
127             case DIALOG_YES_NO_AUTH:
128                 p.mTitle = getString(R.string.pbap_session_key_dialog_header);
129                 p.mView = createView(DIALOG_YES_NO_AUTH);
130                 p.mPositiveButtonText = getString(android.R.string.ok);
131                 p.mPositiveButtonListener = this;
132                 p.mNegativeButtonText = getString(android.R.string.cancel);
133                 p.mNegativeButtonListener = this;
134                 setupAlert();
135                 mOkButton = mAlert.getButton(DialogInterface.BUTTON_POSITIVE);
136                 mOkButton.setEnabled(false);
137                 break;
138             default:
139                 break;
140         }
141     }
142 
createDisplayText(final int id)143     private String createDisplayText(final int id) {
144         switch (id) {
145             case DIALOG_YES_NO_AUTH:
146                 String mMessage2 = getString(R.string.pbap_session_key_dialog_title, mDevice);
147                 return mMessage2;
148             default:
149                 return null;
150         }
151     }
152 
createView(final int id)153     private View createView(final int id) {
154         switch (id) {
155             case DIALOG_YES_NO_AUTH:
156                 mView = getLayoutInflater().inflate(R.layout.auth, null);
157                 mMessageView = (TextView) mView.findViewById(R.id.message);
158                 mMessageView.setText(createDisplayText(id));
159                 mKeyView = (EditText) mView.findViewById(R.id.text);
160                 mKeyView.addTextChangedListener(this);
161                 mKeyView.setFilters(new InputFilter[]{
162                         new LengthFilter(BLUETOOTH_OBEX_AUTHKEY_MAX_LENGTH)
163                 });
164                 return mView;
165             default:
166                 return null;
167         }
168     }
169 
onPositive()170     private void onPositive() {
171         if (!mTimeout) {
172             if (mCurrentDialog == DIALOG_YES_NO_AUTH) {
173                 sendIntentToReceiver(BluetoothPbapService.AUTH_RESPONSE_ACTION,
174                         BluetoothPbapService.EXTRA_SESSION_KEY, mSessionKey);
175                 mKeyView.removeTextChangedListener(this);
176             }
177         }
178         mTimeout = false;
179         finish();
180     }
181 
onNegative()182     private void onNegative() {
183         if (mCurrentDialog == DIALOG_YES_NO_AUTH) {
184             sendIntentToReceiver(BluetoothPbapService.AUTH_CANCELLED_ACTION, null, null);
185             mKeyView.removeTextChangedListener(this);
186         }
187         finish();
188     }
189 
sendIntentToReceiver(final String intentName, final String extraName, final String extraValue)190     private void sendIntentToReceiver(final String intentName, final String extraName,
191             final String extraValue) {
192         Intent intent = new Intent(intentName);
193         intent.setPackage(BluetoothPbapService.THIS_PACKAGE_NAME);
194         intent.putExtra(BluetoothPbapService.EXTRA_DEVICE, mDevice);
195         if (extraName != null) {
196             intent.putExtra(extraName, extraValue);
197         }
198         sendBroadcast(intent);
199     }
200 
201     @Override
onClick(DialogInterface dialog, int which)202     public void onClick(DialogInterface dialog, int which) {
203         switch (which) {
204             case DialogInterface.BUTTON_POSITIVE:
205                 if (mCurrentDialog == DIALOG_YES_NO_AUTH) {
206                     mSessionKey = mKeyView.getText().toString();
207                 }
208                 onPositive();
209                 break;
210 
211             case DialogInterface.BUTTON_NEGATIVE:
212                 onNegative();
213                 break;
214             default:
215                 break;
216         }
217     }
218 
onTimeout()219     private void onTimeout() {
220         mTimeout = true;
221         if (mCurrentDialog == DIALOG_YES_NO_AUTH) {
222             mMessageView.setText(getString(R.string.pbap_authentication_timeout_message, mDevice));
223             mKeyView.setVisibility(View.GONE);
224             mKeyView.clearFocus();
225             mKeyView.removeTextChangedListener(this);
226             mOkButton.setEnabled(true);
227             mAlert.getButton(DialogInterface.BUTTON_NEGATIVE).setVisibility(View.GONE);
228         }
229 
230         mTimeoutHandler.sendMessageDelayed(mTimeoutHandler.obtainMessage(DISMISS_TIMEOUT_DIALOG),
231                 DISMISS_TIMEOUT_DIALOG_VALUE);
232     }
233 
234     @Override
onRestoreInstanceState(Bundle savedInstanceState)235     protected void onRestoreInstanceState(Bundle savedInstanceState) {
236         super.onRestoreInstanceState(savedInstanceState);
237         mTimeout = savedInstanceState.getBoolean(KEY_USER_TIMEOUT);
238         if (V) {
239             Log.v(TAG, "onRestoreInstanceState() mTimeout: " + mTimeout);
240         }
241         if (mTimeout) {
242             onTimeout();
243         }
244     }
245 
246     @Override
onSaveInstanceState(Bundle outState)247     protected void onSaveInstanceState(Bundle outState) {
248         super.onSaveInstanceState(outState);
249         outState.putBoolean(KEY_USER_TIMEOUT, mTimeout);
250     }
251 
252     @Override
onDestroy()253     protected void onDestroy() {
254         super.onDestroy();
255         unregisterReceiver(mReceiver);
256     }
257 
258     @Override
onPreferenceChange(Preference preference, Object newValue)259     public boolean onPreferenceChange(Preference preference, Object newValue) {
260         return true;
261     }
262 
263     @Override
beforeTextChanged(CharSequence s, int start, int before, int after)264     public void beforeTextChanged(CharSequence s, int start, int before, int after) {
265     }
266 
267     @Override
onTextChanged(CharSequence s, int start, int before, int count)268     public void onTextChanged(CharSequence s, int start, int before, int count) {
269     }
270 
271     @Override
afterTextChanged(android.text.Editable s)272     public void afterTextChanged(android.text.Editable s) {
273         if (s.length() > 0) {
274             mOkButton.setEnabled(true);
275         }
276     }
277 
278     private final Handler mTimeoutHandler = new Handler() {
279         @Override
280         public void handleMessage(Message msg) {
281             switch (msg.what) {
282                 case DISMISS_TIMEOUT_DIALOG:
283                     if (V) {
284                         Log.v(TAG, "Received DISMISS_TIMEOUT_DIALOG msg.");
285                     }
286                     finish();
287                     break;
288                 default:
289                     break;
290             }
291         }
292     };
293 }
294