TestActivity.java revision af9af3c451fde7712e86c9430169c306b91836a4
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.app.Activity; 38import android.content.ContentValues; 39import android.content.Context; 40import android.content.Intent; 41import android.database.Cursor; 42import android.net.Uri; 43import android.os.Bundle; 44import android.util.Log; 45import android.widget.Button; 46import android.widget.EditText; 47import android.view.View; 48import android.view.View.OnClickListener; 49 50public class TestActivity extends Activity { 51 52 public String currentInsert; 53 54 public int mCurrentByte = 0; 55 56 EditText mUpdateView; 57 58 EditText mAckView; 59 60 EditText mDeleteView; 61 62 EditText mInsertView; 63 64 EditText mAddressView; 65 66 EditText mMediaView; 67 68 TestTcpServer server; 69 70 /** Called when the activity is first created. */ 71 @Override 72 public void onCreate(Bundle savedInstanceState) { 73 super.onCreate(savedInstanceState); 74 75 Intent intent = getIntent(); 76 77 String action = intent.getAction(); 78 79 Context c = getBaseContext(); 80 81 if (Intent.ACTION_SEND.equals(action)) { 82 /* 83 * Other application is trying to share a file via Bluetooth, 84 * probably Pictures, or vCard. The Intent should contain an 85 * EXTRA_STREAM with the data to attach. 86 */ 87 88 String type = intent.getType(); 89 Uri stream = (Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM); 90 91 if (stream != null && type != null) { 92 /* 93 * if (MimeUtility.mimeTypeMatches(type, 94 * Email.ACCEPTABLE_ATTACHMENT_SEND_TYPES)) { 95 * addAttachment(stream); 96 */ 97 Log.v(Constants.TAG, " Get share intent with Uri " + stream + " mimetype is " 98 + type); 99 // Log.v(Constants.TAG, " trying Uri function " + 100 // stream.getAuthority() + " " + Uri.parse(stream)); 101 Cursor cursor = c.getContentResolver().query(stream, null, null, null, null); 102 cursor.close(); 103 104 } 105 /* start insert a record */ 106 /* 107 * ContentValues values = new ContentValues(); 108 * values.put(BluetoothShare.URI, stream.toString()); 109 * values.put(BluetoothShare.DESTINATION, "FF:FF:FF:00:00:00"); 110 * values.put(BluetoothShare.DIRECTION, 111 * BluetoothShare.DIRECTION_OUTBOUND); final Uri contentUri = 112 * getContentResolver().insert(BluetoothShare.CONTENT_URI, values); 113 * Log.v(Constants.TAG, "insert contentUri: " + contentUri); 114 */ 115 } 116 /* 117 * Context c = getBaseContext(); c.startService(new Intent(c, 118 * BluetoothOppService.class)); 119 */ 120 121 setContentView(R.layout.testactivity_main); 122 123 Button mInsertRecord = (Button)findViewById(R.id.insert_record); 124 Button mDeleteRecord = (Button)findViewById(R.id.delete_record); 125 Button mUpdateRecord = (Button)findViewById(R.id.update_record); 126 127 Button mAckRecord = (Button)findViewById(R.id.ack_record); 128 129 Button mDeleteAllRecord = (Button)findViewById(R.id.deleteAll_record); 130 mUpdateView = (EditText)findViewById(R.id.update_text); 131 mAckView = (EditText)findViewById(R.id.ack_text); 132 mDeleteView = (EditText)findViewById(R.id.delete_text); 133 mInsertView = (EditText)findViewById(R.id.insert_text); 134 135 mAddressView = (EditText)findViewById(R.id.address_text); 136 mMediaView = (EditText)findViewById(R.id.media_text); 137 138 mInsertRecord.setOnClickListener(insertRecordListener); 139 mDeleteRecord.setOnClickListener(deleteRecordListener); 140 mUpdateRecord.setOnClickListener(updateRecordListener); 141 mAckRecord.setOnClickListener(ackRecordListener); 142 mDeleteAllRecord.setOnClickListener(deleteAllRecordListener); 143 144 Button mStartTcpServer = (Button)findViewById(R.id.start_server); 145 mStartTcpServer.setOnClickListener(startTcpServerListener); 146 147 Button mNotifyTcpServer = (Button)findViewById(R.id.notify_server); 148 mNotifyTcpServer.setOnClickListener(notifyTcpServerListener); 149 /* parse insert result Uri */ 150 /* 151 * String id = contentUri.getPathSegments().get(1); Log.v(Constants.TAG, 152 * "insert record id is " + id); Uri contentUri1 = 153 * Uri.parse(BluetoothShare.CONTENT_URI + "/" + id); 154 */ 155 /* update a single column of a record */ 156 /* 157 * ContentValues updateValues = new ContentValues(); 158 * updateValues.put(BluetoothShare.TOTAL_BYTES, 120000); 159 * getContentResolver().update(contentUri1,updateValues,null,null); 160 */ 161 /* query a single column of a record */ 162 /* 163 * Cursor queryC = getContentResolver().query(contentUri1, null, null, 164 * null, null); if (queryC != null) { if (queryC.moveToFirst()) { int 165 * currentByteColumn = 166 * queryC.getColumnIndexOrThrow(BluetoothShare.CURRENT_BYTES); int 167 * currentByte = queryC.getInt(currentByteColumn); 168 */ 169 /* update a column of a record */ 170 /* 171 * for(int i =0;i<100;i++){ currentByte ++; 172 * updateValues.put(BluetoothShare.CURRENT_BYTES, currentByte); 173 * getContentResolver().update(contentUri1,updateValues,null,null); } } 174 * } 175 */ 176 /* query whole data base */ 177 /* 178 * Cursor c = managedQuery(contentUri1, new String [] {"_id", 179 * BluetoothShare.URI, BluetoothShare.STATUS, 180 * BluetoothShare.TOTAL_BYTES, BluetoothShare.CURRENT_BYTES, 181 * BluetoothShare._DATA, BluetoothShare.DIRECTION, 182 * BluetoothShare.MIMETYPE, BluetoothShare.DESTINATION, 183 * BluetoothShare.VISIBILITY, BluetoothShare.USER_CONFIRMATION, 184 * BluetoothShare.TIMESTAMP}, null, null, null); Log.v(Constants.TAG, 185 * "query " + contentUri1 +" get " + c.getCount()+" records"); 186 */ 187 /* delete a record */ 188 /* 189 * Uri contentUri2 = Uri.parse(BluetoothShare.CONTENT_URI + "/" + 1); 190 * getContentResolver().delete(contentUri2, null, null); 191 */ 192 193 } 194 195 public OnClickListener insertRecordListener = new OnClickListener() { 196 public void onClick(View view) { 197 198 String address = null; 199 if (mAddressView.getText().length() != 0) { 200 address = mAddressView.getText().toString(); 201 Log.v(Constants.TAG, "Send to address " + address); 202 } 203 if (address == null) { 204 address = "00:17:83:58:5D:CC"; 205 } 206 207 Integer media = null; 208 if (mMediaView.getText().length() != 0) { 209 media = Integer.parseInt(mMediaView.getText().toString().trim()); 210 Log.v(Constants.TAG, "Send media no. " + media); 211 } 212 if (media == null) { 213 media = 1; 214 } 215 ContentValues values = new ContentValues(); 216 values.put(BluetoothShare.URI, "content://media/external/images/media/" + media); 217 // values.put(BluetoothShare.DESTINATION, "FF:FF:FF:00:00:00"); 218 // baibai Q9 test 219 // values.put(BluetoothShare.DESTINATION, "12:34:56:78:9A:BC"); 220 // java's nokia 221 // values.put(BluetoothShare.DESTINATION, "00:1B:33:F0:58:FB"); 222 // Assis phone 223 // values.put(BluetoothShare.DESTINATION, "00:17:E5:5D:74:F3"); 224 // Jackson E6 225 // values.put(BluetoothShare.DESTINATION, "00:1A:1B:7F:1E:F0"); 226 // Baibai V950 227 // values.put(BluetoothShare.DESTINATION, "00:17:83:58:5D:CC"); 228 // Baibai NSC1173 229 // values.put(BluetoothShare.DESTINATION, "00:16:41:49:5B:F3"); 230 231 values.put(BluetoothShare.DESTINATION, address); 232 233 values.put(BluetoothShare.DIRECTION, BluetoothShare.DIRECTION_OUTBOUND); 234 235 Long ts = System.currentTimeMillis(); 236 values.put(BluetoothShare.TIMESTAMP, ts); 237 238 Integer records = null; 239 if (mInsertView.getText().length() != 0) { 240 records = Integer.parseInt(mInsertView.getText().toString().trim()); 241 Log.v(Constants.TAG, "parseInt " + records); 242 } 243 if (records == null) { 244 records = 1; 245 } 246 for (int i = 0; i < records; i++) { 247 Uri contentUri = getContentResolver().insert(BluetoothShare.CONTENT_URI, values); 248 Log.v(Constants.TAG, "insert contentUri: " + contentUri); 249 currentInsert = contentUri.getPathSegments().get(1); 250 Log.v(Constants.TAG, "currentInsert = " + currentInsert); 251 } 252 253 } 254 }; 255 256 public OnClickListener deleteRecordListener = new OnClickListener() { 257 public void onClick(View view) { 258 Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" 259 + mDeleteView.getText().toString()); 260 getContentResolver().delete(contentUri, null, null); 261 } 262 }; 263 264 public OnClickListener updateRecordListener = new OnClickListener() { 265 public void onClick(View view) { 266 Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" 267 + mUpdateView.getText().toString()); 268 ContentValues updateValues = new ContentValues(); 269 // mCurrentByte ++; 270 // updateValues.put(BluetoothShare.TOTAL_BYTES, "120000"); 271 // updateValues.put(BluetoothShare.CURRENT_BYTES, mCurrentByte); 272 // updateValues.put(BluetoothShare.VISIBILITY, 273 // BluetoothShare.VISIBILITY_HIDDEN); 274 updateValues.put(BluetoothShare.USER_CONFIRMATION, 275 BluetoothShare.USER_CONFIRMATION_CONFIRMED); 276 getContentResolver().update(contentUri, updateValues, null, null); 277 } 278 }; 279 280 public OnClickListener ackRecordListener = new OnClickListener() { 281 public void onClick(View view) { 282 Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" 283 + mAckView.getText().toString()); 284 ContentValues updateValues = new ContentValues(); 285 // mCurrentByte ++; 286 // updateValues.put(BluetoothShare.TOTAL_BYTES, "120000"); 287 // updateValues.put(BluetoothShare.CURRENT_BYTES, mCurrentByte); 288 updateValues.put(BluetoothShare.VISIBILITY, BluetoothShare.VISIBILITY_HIDDEN); 289 // updateValues.put(BluetoothShare.USER_CONFIRMATION, 290 // BluetoothShare.USER_CONFIRMATION_CONFIRMED); 291 getContentResolver().update(contentUri, updateValues, null, null); 292 } 293 }; 294 295 public OnClickListener deleteAllRecordListener = new OnClickListener() { 296 public void onClick(View view) { 297 Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + ""); 298 getContentResolver().delete(contentUri, null, null); 299 } 300 }; 301 302 public OnClickListener startTcpServerListener = new OnClickListener() { 303 public void onClick(View view) { 304 server = new TestTcpServer(); 305 Thread server_thread = new Thread(server); 306 server_thread.start(); 307 308 } 309 }; 310 311 public OnClickListener notifyTcpServerListener = new OnClickListener() { 312 public void onClick(View view) { 313 final Thread notifyThread = new Thread() { 314 public void run() { 315 synchronized (server) { 316 server.a = true; 317 server.notify(); 318 } 319 } 320 321 }; 322 notifyThread.start(); 323 } 324 325 }; 326} 327