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