From 5ee69f6f2c35a6e68bb0428bfb853b873313cbb4 Mon Sep 17 00:00:00 2001 From: Ying Wang Date: Tue, 28 Sep 2010 18:26:18 -0700 Subject: [PATCH 1/4] Don't send non-existent system keys. Send only if the system key physically exists. Change-Id: I8fa102db679c55e5bfba1c9a68ecf2bc196021cd --- .../commands/monkey/MonkeySourceRandom.java | 80 ++++++++++++++----- 1 file changed, 60 insertions(+), 20 deletions(-) diff --git a/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandom.java b/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandom.java index 92acc7e20..f1edae11c 100644 --- a/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandom.java +++ b/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandom.java @@ -19,6 +19,7 @@ package com.android.commands.monkey; import android.content.ComponentName; import android.os.SystemClock; import android.view.Display; +import android.view.KeyCharacterMap; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.WindowManagerImpl; @@ -50,6 +51,18 @@ public class MonkeySourceRandom implements MonkeyEventSource { KeyEvent.KEYCODE_VOLUME_UP, KeyEvent.KEYCODE_VOLUME_DOWN, KeyEvent.KEYCODE_MUTE, }; + /** If a physical key exists? */ + private static final boolean[] PHYSICAL_KEY_EXISTS = new boolean[KeyEvent.getMaxKeyCode() + 1]; + static { + for (int i = 0; i < PHYSICAL_KEY_EXISTS.length; ++i) { + PHYSICAL_KEY_EXISTS[i] = true; + } + // Only examine SYS_KEYS + for (int i = 0; i < SYS_KEYS.length; ++i) { + PHYSICAL_KEY_EXISTS[SYS_KEYS[i]] = KeyCharacterMap.deviceHasKey(SYS_KEYS[i]); + } + } + /** Nice names for all key events. */ private static final String[] KEY_NAMES = { "KEYCODE_UNKNOWN", @@ -282,7 +295,6 @@ public class MonkeySourceRandom implements MonkeyEventSource { } // if verbose, show factors - if (mVerbose > 0) { System.out.println("// Event percentages:"); for (int i = 0; i < FACTORZ_COUNT; ++i) { @@ -290,6 +302,10 @@ public class MonkeySourceRandom implements MonkeyEventSource { } } + if (!validateKeys()) { + return false; + } + // finally, normalize and convert to running sum float sum = 0.0f; for (int i = 0; i < FACTORZ_COUNT; ++i) { @@ -299,6 +315,28 @@ public class MonkeySourceRandom implements MonkeyEventSource { return true; } + private static boolean validateKeyCategory(String catName, int[] keys, float factor) { + if (factor < 0.1f) { + return true; + } + for (int i = 0; i < keys.length; ++i) { + if (PHYSICAL_KEY_EXISTS[keys[i]]) { + return true; + } + } + System.err.println("** " + catName + " has no physical keys but with factor " + factor + "%."); + return false; + } + + /** + * See if any key exists for non-zero factors. + */ + private boolean validateKeys() { + return validateKeyCategory("NAV_KEYS", NAV_KEYS, mFactors[FACTOR_NAV]) + && validateKeyCategory("MAJOR_NAV_KEYS", MAJOR_NAV_KEYS, mFactors[FACTOR_MAJORNAV]) + && validateKeyCategory("SYS_KEYS", SYS_KEYS, mFactors[FACTOR_SYSOPS]); + } + /** * set the factors * @@ -441,25 +479,27 @@ public class MonkeySourceRandom implements MonkeyEventSource { } // The remaining event categories are injected as key events - if (cls < mFactors[FACTOR_NAV]) { - lastKey = NAV_KEYS[mRandom.nextInt(NAV_KEYS.length)]; - } else if (cls < mFactors[FACTOR_MAJORNAV]) { - lastKey = MAJOR_NAV_KEYS[mRandom.nextInt(MAJOR_NAV_KEYS.length)]; - } else if (cls < mFactors[FACTOR_SYSOPS]) { - lastKey = SYS_KEYS[mRandom.nextInt(SYS_KEYS.length)]; - } else if (cls < mFactors[FACTOR_APPSWITCH]) { - MonkeyActivityEvent e = new MonkeyActivityEvent(mMainApps.get( - mRandom.nextInt(mMainApps.size()))); - mQ.addLast(e); - return; - } else if (cls < mFactors[FACTOR_FLIP]) { - MonkeyFlipEvent e = new MonkeyFlipEvent(mKeyboardOpen); - mKeyboardOpen = !mKeyboardOpen; - mQ.addLast(e); - return; - } else { - lastKey = 1 + mRandom.nextInt(KeyEvent.getMaxKeyCode() - 1); - } + do { + if (cls < mFactors[FACTOR_NAV]) { + lastKey = NAV_KEYS[mRandom.nextInt(NAV_KEYS.length)]; + } else if (cls < mFactors[FACTOR_MAJORNAV]) { + lastKey = MAJOR_NAV_KEYS[mRandom.nextInt(MAJOR_NAV_KEYS.length)]; + } else if (cls < mFactors[FACTOR_SYSOPS]) { + lastKey = SYS_KEYS[mRandom.nextInt(SYS_KEYS.length)]; + } else if (cls < mFactors[FACTOR_APPSWITCH]) { + MonkeyActivityEvent e = new MonkeyActivityEvent(mMainApps.get( + mRandom.nextInt(mMainApps.size()))); + mQ.addLast(e); + return; + } else if (cls < mFactors[FACTOR_FLIP]) { + MonkeyFlipEvent e = new MonkeyFlipEvent(mKeyboardOpen); + mKeyboardOpen = !mKeyboardOpen; + mQ.addLast(e); + return; + } else { + lastKey = 1 + mRandom.nextInt(KeyEvent.getMaxKeyCode() - 1); + } + } while (!PHYSICAL_KEY_EXISTS[lastKey]); MonkeyKeyEvent e = new MonkeyKeyEvent(KeyEvent.ACTION_DOWN, lastKey); mQ.addLast(e); From 4d68d883a530730f95125ec9e2bd514bdfda3c04 Mon Sep 17 00:00:00 2001 From: Nick Kralevich Date: Fri, 1 Oct 2010 09:30:20 -0700 Subject: [PATCH 2/4] update required permissions. Change-Id: I49fb855c62aa1a11c8cbf7645aa4dbe2fa18faf3 --- apps/Tag/AndroidManifest.xml | 2 ++ apps/Tag/src/com/android/apps/tag/TagList.java | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/Tag/AndroidManifest.xml b/apps/Tag/AndroidManifest.xml index 44c96c205..ae198a339 100644 --- a/apps/Tag/AndroidManifest.xml +++ b/apps/Tag/AndroidManifest.xml @@ -32,4 +32,6 @@ + + diff --git a/apps/Tag/src/com/android/apps/tag/TagList.java b/apps/Tag/src/com/android/apps/tag/TagList.java index 5abd35bf9..aa9d8d36d 100644 --- a/apps/Tag/src/com/android/apps/tag/TagList.java +++ b/apps/Tag/src/com/android/apps/tag/TagList.java @@ -23,7 +23,6 @@ import android.content.DialogInterface; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; -import android.provider.Contacts; import android.view.Menu; import android.view.View; import android.widget.ListView; From 9c1a73ba8a6b6cee05f235ef71975c427a9f3a3c Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 1 Oct 2010 10:46:17 -0700 Subject: [PATCH 3/4] Update Monkey to new StrictMode API. Change-Id: I0f2e90496c4dece8ee3766170d8a1fe2d22a7ff4 --- cmds/monkey/src/com/android/commands/monkey/Monkey.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/cmds/monkey/src/com/android/commands/monkey/Monkey.java b/cmds/monkey/src/com/android/commands/monkey/Monkey.java index 71fde1345..676043aa7 100644 --- a/cmds/monkey/src/com/android/commands/monkey/Monkey.java +++ b/cmds/monkey/src/com/android/commands/monkey/Monkey.java @@ -243,7 +243,7 @@ public class Monkey { // redirected to a file?) So we allow disk writes // around this region for the monkey to minimize // harmless dropbox uploads from monkeys. - int savedPolicy = StrictMode.allowThreadDiskWrites(); + StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites(); System.out.println(" // " + (allow ? "Allowing" : "Rejecting") + " start of " + intent + " in package " + pkg); StrictMode.setThreadPolicy(savedPolicy); @@ -254,7 +254,7 @@ public class Monkey { } public boolean activityResuming(String pkg) { - int savedPolicy = StrictMode.allowThreadDiskWrites(); + StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites(); System.out.println(" // activityResuming(" + pkg + ")"); boolean allow = checkEnteringPackage(pkg) || (DEBUG_ALLOW_ANY_RESTARTS != 0); if (!allow) { @@ -271,7 +271,7 @@ public class Monkey { public boolean appCrashed(String processName, int pid, String shortMsg, String longMsg, long timeMillis, String stackTrace) { - int savedPolicy = StrictMode.allowThreadDiskWrites(); + StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites(); System.err.println("// CRASH: " + processName + " (pid " + pid + ")"); System.err.println("// Short Msg: " + shortMsg); System.err.println("// Long Msg: " + longMsg); @@ -296,7 +296,7 @@ public class Monkey { } public int appNotResponding(String processName, int pid, String processStats) { - int savedPolicy = StrictMode.allowThreadDiskWrites(); + StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites(); System.err.println("// NOT RESPONDING: " + processName + " (pid " + pid + ")"); System.err.println(processStats); StrictMode.setThreadPolicy(savedPolicy); @@ -1178,4 +1178,3 @@ public class Monkey { System.err.println(usage.toString()); } } - From e97e78a0600e08b0e84edee96aea81145c0df44c Mon Sep 17 00:00:00 2001 From: Nick Kralevich Date: Fri, 1 Oct 2010 15:43:07 -0700 Subject: [PATCH 4/4] Make unittests work, new TagBroadcastReceiver Change-Id: I5a3a1938a7fb91c8114447d24427f2c263bda5a3 --- apps/Tag/AndroidManifest.xml | 6 +++ .../apps/tag/TagBroadcastReceiver.java | 52 +++++++++++++++++++ .../Tag/src/com/android/apps/tag/TagList.java | 8 +-- apps/Tag/tests/AndroidManifest.xml | 6 +-- .../apps/tag/TagBroadcastReceiverTest.java | 44 ++++++++++++++++ 5 files changed, 110 insertions(+), 6 deletions(-) create mode 100644 apps/Tag/src/com/android/apps/tag/TagBroadcastReceiver.java create mode 100644 apps/Tag/tests/src/com/android/apps/tag/TagBroadcastReceiverTest.java diff --git a/apps/Tag/AndroidManifest.xml b/apps/Tag/AndroidManifest.xml index ae198a339..3388a3031 100644 --- a/apps/Tag/AndroidManifest.xml +++ b/apps/Tag/AndroidManifest.xml @@ -31,6 +31,12 @@ + + + + + + diff --git a/apps/Tag/src/com/android/apps/tag/TagBroadcastReceiver.java b/apps/Tag/src/com/android/apps/tag/TagBroadcastReceiver.java new file mode 100644 index 000000000..015e897ad --- /dev/null +++ b/apps/Tag/src/com/android/apps/tag/TagBroadcastReceiver.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.apps.tag; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.widget.Toast; +import com.trustedlogic.trustednfc.android.NdefMessage; +import com.trustedlogic.trustednfc.android.NdefRecord; +import com.trustedlogic.trustednfc.android.NfcManager; + +/** + * This class doesn't work. Sorry. Think of this class as pseudo + * code for now. + */ +public class TagBroadcastReceiver extends BroadcastReceiver { + + @Override + public void onReceive(Context context, Intent intent) { + if (intent.getAction().equals(NfcManager.NDEF_TAG_DISCOVERED_ACTION)) { + NdefMessage msg = intent.getParcelableExtra(NfcManager.NDEF_MESSAGE_EXTRA); + Toast.makeText(context, "got a new message", Toast.LENGTH_SHORT).show(); + insertIntoDb(msg); + } + } + + private void insertIntoDb(NdefMessage msg) { + for (NdefRecord record : msg.getRecords()) { + insertIntoRecordDb(record.getType(), record.getPayload()); + } + } + + private void insertIntoRecordDb(byte[] type, byte[] payload) { + // do something... + } + +} diff --git a/apps/Tag/src/com/android/apps/tag/TagList.java b/apps/Tag/src/com/android/apps/tag/TagList.java index aa9d8d36d..f8a93e692 100644 --- a/apps/Tag/src/com/android/apps/tag/TagList.java +++ b/apps/Tag/src/com/android/apps/tag/TagList.java @@ -27,18 +27,17 @@ import android.view.Menu; import android.view.View; import android.widget.ListView; import android.widget.SimpleCursorAdapter; -import com.trustedlogic.trustednfc.android.NfcManager; +import android.widget.Toast; /** * @author nnk@google.com (Nick Kralevich) */ public class TagList extends ListActivity implements DialogInterface.OnClickListener { - private NfcManager mManager; - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + Toast.makeText(getBaseContext(), "entered method", Toast.LENGTH_SHORT).show(); SQLiteDatabase db = new TagDBHelper(this.getBaseContext()).getReadableDatabase(); Cursor c = db.query("Tags", new String[] { "_id", "description" }, null, null, null, null, null); @@ -51,6 +50,9 @@ public class TagList extends ListActivity implements DialogInterface.OnClickList setListAdapter(sca); registerForContextMenu(getListView()); + c.close(); + db.close(); + Toast.makeText(getBaseContext(), "exit method", Toast.LENGTH_SHORT).show(); } @Override diff --git a/apps/Tag/tests/AndroidManifest.xml b/apps/Tag/tests/AndroidManifest.xml index f4e79ed34..63ef9e3f4 100644 --- a/apps/Tag/tests/AndroidManifest.xml +++ b/apps/Tag/tests/AndroidManifest.xml @@ -15,7 +15,7 @@ --> + package="com.android.apps.tag.tests">