Skip to content

Commit

Permalink
* Added ability to lookup contact name
Browse files Browse the repository at this point in the history
* Moved Morse binary tree lookup to strings.xml
* Morse code lookup methods are commented out for now
  • Loading branch information
JakeWharton committed Mar 27, 2009
1 parent fa40fd2 commit 3f31267
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 76 deletions.
63 changes: 32 additions & 31 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jakewharton.smsmorse"
android:versionCode="111"
android:versionName="1.1.1">

<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>

<application
android:icon="@drawable/icon"
android:label="@string/app_name">

<activity android:name=".ui.Preferences" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".ui.About" android:label="@string/app_about_name"/>

<receiver android:name=".transaction.EventReceiver" android:label="@string/app_receiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="com.jakewharton.smsmorse.VIBRATE_MORSE" />
</intent-filter>
</receiver>

</application>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jakewharton.smsmorse"
android:versionCode="112"
android:versionName="1.1.2">

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

<application
android:icon="@drawable/icon"
android:label="@string/app_name">

<activity android:name=".ui.Preferences" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".ui.About" android:label="@string/app_about_name"/>

<receiver android:name=".transaction.EventReceiver" android:label="@string/app_receiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="com.jakewharton.smsmorse.VIBRATE_MORSE" />
</intent-filter>
</receiver>

</application>

</manifest>
18 changes: 18 additions & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
<string name="preference_vibrate_counts">vibrate_counts</string>
<string name="preference_vibrate_counts_title">Vibrate Number Counts</string>
<string name="preference_vibrate_counts_summary">Use counts to vibrate the sender number rather than Morse code representation.</string>
<string name="preference_lookup_contact_name">lookup_contact_name</string>
<string name="preference_lookup_contact_name_title">Lookup Contact Name</string>
<string name="preference_lookup_contact_name_summary">Use the contact name rather than just the number for "sender".</string>
<string name="preference_vibrate_parts">vibrate_parts</string>
<string name="preference_vibrate_parts_title">Vibrate Parts</string>
<string name="preference_vibrate_parts_summary">Currently:</string>
Expand Down Expand Up @@ -54,4 +57,19 @@
<string name="preference_input_enabled_summary">Enable ability to reply to SMS message by tapping Morse code.</string>
<string name="preference_error_char">error_char</string>
<string name="preference_error_allowed">error_allowed</string>

<!--
* Morse code binary lookup tree. Left for every dot, right for every dash
*
* #
* E T
* I A N M
* S U R W D K G O
* H V F # L A P J B X C Y Z Q # #
* 5 4 # 3 # # # 2 & # + # # # # 1 6 = / # # # ( # 7 # # # 8 # 9 0
* # # # # # # ######?_####\"##.####@###'##-########;!#)#####,####:#######
* #########$
-->
<string name="morse_code_binary_lookup_tree">#ETIANMSURWDKGOHVF#LAPJBXCYZQ##54#3###2&amp;#+####16+/###(#7###8#90############?_####"##.####@###'##-########;!#)#####,####:################$</string>
<string name="morse_code_binary_lookup_tree_null_char">#</string>
</resources>
5 changes: 5 additions & 0 deletions res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
android:entries="@array/preference_vibrate_parts_entries"
android:entryValues="@array/preference_vibrate_parts_entry_values"
android:defaultValue="0"/>
<CheckBoxPreference
android:key="@string/preference_lookup_contact_name"
android:title="@string/preference_lookup_contact_name_title"
android:summary="@string/preference_lookup_contact_name_summary"
android:defaultValue="true"/>
<CheckBoxPreference
android:key="@string/preference_vibrate_counts"
android:title="@string/preference_vibrate_counts_title"
Expand Down
66 changes: 42 additions & 24 deletions src/com/jakewharton/smsmorse/R.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ public static final class string {
/** Menu Items
*/
public static final int menu_restore_defaults=0x7f060006;
/**
* Morse code binary lookup tree. Left for every dot, right for every dash
*
* #
* E T
* I A N M
* S U R W D K G O
* H V F # L A P J B X C Y Z Q # #
* 5 4 # 3 # # # 2 & # + # # # # 1 6 = / # # # ( # 7 # # # 8 # 9 0
* # # # # # # ######?_####\"##.####@###'##-########;!#)#####,####:#######
* #########$
*/
public static final int morse_code_binary_lookup_tree=0x7f06002f;
public static final int morse_code_binary_lookup_tree_null_char=0x7f060030;
public static final int preference_dot_length=0x7f06000e;
public static final int preference_dot_length_summary=0x7f060010;
public static final int preference_dot_length_title=0x7f06000f;
Expand All @@ -73,42 +88,45 @@ public static final class string {
public static final int preference_enabled=0x7f06000b;
public static final int preference_enabled_summary=0x7f06000d;
public static final int preference_enabled_title=0x7f06000c;
public static final int preference_error_allowed=0x7f06002b;
public static final int preference_error_char=0x7f06002a;
public static final int preference_error_allowed=0x7f06002e;
public static final int preference_error_char=0x7f06002d;
/** Preference Headers
*/
public static final int preference_header_general=0x7f060008;
public static final int preference_header_input=0x7f06000a;
public static final int preference_header_rules=0x7f060009;
/** Input Preferences
*/
public static final int preference_input_enabled=0x7f060027;
public static final int preference_input_enabled_summary=0x7f060029;
public static final int preference_input_enabled_title=0x7f060028;
public static final int preference_input_enabled=0x7f06002a;
public static final int preference_input_enabled_summary=0x7f06002c;
public static final int preference_input_enabled_title=0x7f06002b;
public static final int preference_lookup_contact_name=0x7f060014;
public static final int preference_lookup_contact_name_summary=0x7f060016;
public static final int preference_lookup_contact_name_title=0x7f060015;
/** Rules Preferences
*/
public static final int preference_screen_off_only=0x7f06001b;
public static final int preference_screen_off_only_summary=0x7f06001d;
public static final int preference_screen_off_only_title=0x7f06001c;
public static final int preference_test=0x7f060018;
public static final int preference_test_summary=0x7f06001a;
public static final int preference_test_title=0x7f060019;
public static final int preference_screen_off_only=0x7f06001e;
public static final int preference_screen_off_only_summary=0x7f060020;
public static final int preference_screen_off_only_title=0x7f06001f;
public static final int preference_test=0x7f06001b;
public static final int preference_test_summary=0x7f06001d;
public static final int preference_test_title=0x7f06001c;
public static final int preference_vibrate_counts=0x7f060011;
public static final int preference_vibrate_counts_summary=0x7f060013;
public static final int preference_vibrate_counts_title=0x7f060012;
public static final int preference_vibrate_normal=0x7f06001e;
public static final int preference_vibrate_normal_summary=0x7f060020;
public static final int preference_vibrate_normal_title=0x7f06001f;
public static final int preference_vibrate_parts=0x7f060014;
public static final int preference_vibrate_parts_dialog_title=0x7f060017;
public static final int preference_vibrate_parts_summary=0x7f060016;
public static final int preference_vibrate_parts_title=0x7f060015;
public static final int preference_vibrate_silent=0x7f060024;
public static final int preference_vibrate_silent_summary=0x7f060026;
public static final int preference_vibrate_silent_title=0x7f060025;
public static final int preference_vibrate_vibrate=0x7f060021;
public static final int preference_vibrate_vibrate_summary=0x7f060023;
public static final int preference_vibrate_vibrate_title=0x7f060022;
public static final int preference_vibrate_normal=0x7f060021;
public static final int preference_vibrate_normal_summary=0x7f060023;
public static final int preference_vibrate_normal_title=0x7f060022;
public static final int preference_vibrate_parts=0x7f060017;
public static final int preference_vibrate_parts_dialog_title=0x7f06001a;
public static final int preference_vibrate_parts_summary=0x7f060019;
public static final int preference_vibrate_parts_title=0x7f060018;
public static final int preference_vibrate_silent=0x7f060027;
public static final int preference_vibrate_silent_summary=0x7f060029;
public static final int preference_vibrate_silent_title=0x7f060028;
public static final int preference_vibrate_vibrate=0x7f060024;
public static final int preference_vibrate_vibrate_summary=0x7f060026;
public static final int preference_vibrate_vibrate_title=0x7f060025;
}
public static final class xml {
public static final int preferences=0x7f040000;
Expand Down
44 changes: 23 additions & 21 deletions src/com/jakewharton/smsmorse/transaction/EventReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
import java.util.ArrayList;

import com.jakewharton.smsmorse.R;

import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.Cursor;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.provider.Contacts;
import android.telephony.gsm.SmsMessage;
import android.util.Log;

Expand Down Expand Up @@ -44,7 +45,7 @@ public class EventReceiver extends BroadcastReceiver {
private static final boolean DEFAULT_VIBRATE_SILENT = false;
private static final long DEFAULT_INITIAL_PAUSE = 500L;
private static final String DEFAULT_ERROR_CHAR = "_";
private static final float DEFAULT_ERROR_ALLOWED = 0.2F;
private static final float DEFAULT_PERCENT_ERROR = 0.2F;

//Morse code
private final static int DOTS_IN_DASH = 3;
Expand Down Expand Up @@ -126,17 +127,6 @@ public class EventReceiver extends BroadcastReceiver {
{DOT, DOT, DOT, DOT, DOT, DOT, DOT, DOT, DOT} //9
};

//Binary lookup tree. Left for every dot, right for every dash
private final static String MORSE_BINARY_LOOKUP =
" # "+
" E T "+
" I A N M "+
" S U R W D K G O "+
" H V F # L A P J B X C Y Z Q # # "+
" 5 4 # 3 # # # 2 & # + # # # # 1 6 = / # # # ( # 7 # # # 8 # 9 0 "+
"# # # # # # ######?_####\"##.####@###'##-########;!#)#####,####:#######"+
"#########$".replace(" ", "").replace("\n", "");

//Instance variables
private SharedPreferences settings;
private Resources resources;
Expand All @@ -154,9 +144,9 @@ public void onReceive(Context context, Intent intent) {
if (action.equals(VIBRATE_IN_MORSE)) {
vibrateMorse(convertToVibrations(extras.getString(VIBRATE_IN_MORSE_KEY)));
}
else if (action.equals(PARSE_MORSE)) {
/*else if (action.equals(PARSE_MORSE)) {
Log.i(TAG, "Parsed to: " + parseMorse(extras.getLongArray(PARSE_MORSE_KEY)));
}
}*/
else if (action.equals(SMS_RECEIVED)) {
final boolean smsValid = extras != null;
final boolean enabled = settings.getBoolean(resources.getString(R.string.preference_enabled), DEFAULT_ENABLED);
Expand All @@ -182,17 +172,29 @@ else if (action.equals(SMS_RECEIVED)) {
final int vibrateParts = Integer.parseInt(settings.getString(resources.getString(R.string.preference_vibrate_parts), DEFAULT_VIBRATE_PARTS));
for (SmsMessage message : messages) {
if ((vibrateParts == VIBRATE_CONTENT_SENDER) || (vibrateParts == VIBRATE_CONTENT_SENDER_MESSAGE))
vibrations.addAll(convertToVibrations(message.getOriginatingAddress(), true));
vibrations.addAll(convertSenderToVibrations(context, message.getOriginatingAddress()));
if (vibrateParts != VIBRATE_CONTENT_SENDER)
vibrations.addAll(convertToVibrations(message.getMessageBody()));
if (vibrateParts == VIBRATE_CONTENT_MESSAGE_SENDER)
vibrations.addAll(convertToVibrations(message.getOriginatingAddress(), true));
vibrations.addAll(convertSenderToVibrations(context, message.getOriginatingAddress()));
}

vibrateMorse(vibrations);
}
}
}
private ArrayList<Long> convertSenderToVibrations(Context context, String sender) {
if (settings.getBoolean(context.getString(R.string.preference_lookup_contact_name), true)) {
final String[] projection = new String[] { Contacts.PeopleColumns.DISPLAY_NAME };
final String selection = Contacts.Phones.NUMBER + " = " + sender;
final Cursor results = context.getContentResolver().query(Contacts.Phones.CONTENT_URI, projection, selection, null, Contacts.ContactMethods.PERSON_ID);

if (results.moveToFirst()) {
return convertToVibrations(results.getString(results.getColumnIndex(Contacts.PeopleColumns.DISPLAY_NAME)));
}
}
return convertToVibrations(sender, true);
}
private void vibrateMorse(final ArrayList<Long> vibrationLongs) {
final long[] vibrations = new long[vibrationLongs.size()];
final StringBuffer morseVibrations = new StringBuffer("Vibrating Morse: ");
Expand All @@ -207,7 +209,7 @@ private void vibrateMorse(final ArrayList<Long> vibrationLongs) {
vibrator.vibrate(vibrations, -1);
Log.i(TAG, morseVibrations.toString());
}
private String parseMorse(final long[] buttonPresses) {
/*private String parseMorse(final long[] buttonPresses) {
final char errorChar = settings.getString(resources.getString(R.string.preference_error_char), DEFAULT_ERROR_CHAR).charAt(0);
final float errorAllowed = settings.getFloat(resources.getString(R.string.preference_error_allowed), DEFAULT_ERROR_ALLOWED);
final float errorAllowedAbove = 1 + errorAllowed;
Expand Down Expand Up @@ -246,10 +248,10 @@ private String parseMorse(final long[] buttonPresses) {
pauses.add(pause);
}
else if ((pause >= pauseAverage * DOTS_IN_LETTER_GAP * errorAllowedBelow) && (pause <= pauseAverage * DOTS_IN_LETTER_GAP * errorAllowedAbove)) {
if ((lookupPointer >= MORSE_BINARY_LOOKUP.length()) || (MORSE_BINARY_LOOKUP.charAt(lookupPointer) == '#'))
if ((lookupPointer >= MORSE_BINARY_TREE.length()) || (MORSE_BINARY_TREE.charAt(lookupPointer) == MORSE_BINARY_NULL))
message.append(errorChar);
else
message.append(MORSE_BINARY_LOOKUP.charAt(lookupPointer));
message.append(MORSE_BINARY_TREE.charAt(lookupPointer));
lookupPointer = 0;
pauses.add(pause / DOTS_IN_LETTER_GAP);
Expand All @@ -273,7 +275,7 @@ private int average(final ArrayList<Long> numberList) {
for (Long number : numberList)
sum += (Long)number;
return (int)(sum / numberList.size());
}
}*/
private ArrayList<Long> convertToVibrations(final String message) {
return convertToVibrations(message, false);
}
Expand Down

0 comments on commit 3f31267

Please sign in to comment.