Commit 4bac9bda authored by Ralph Morelli's avatar Ralph Morelli

Addresses issue #290 -- texting component receives messages when stopped and receivind disabled.

The texting component was modified to save its state (receivingEnabled and GoogleVoiceEnabled) in
shared preferences.

Change-Id: Ibf7e38f4992cad09d55f4cce5cc629f795bd4d1f
parent dba936db
...@@ -43,6 +43,7 @@ import android.content.BroadcastReceiver; ...@@ -43,6 +43,7 @@ import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.telephony.gsm.SmsManager; import android.telephony.gsm.SmsManager;
import android.telephony.gsm.SmsMessage; import android.telephony.gsm.SmsMessage;
...@@ -89,16 +90,16 @@ import android.widget.Toast; ...@@ -89,16 +90,16 @@ import android.widget.Toast;
"android.permission.ACCOUNT_MANAGER, android.permission.MANAGE_ACCOUNTS, " + "android.permission.ACCOUNT_MANAGER, android.permission.MANAGE_ACCOUNTS, " +
"android.permission.GET_ACCOUNTS, android.permission.USE_CREDENTIALS") "android.permission.GET_ACCOUNTS, android.permission.USE_CREDENTIALS")
@UsesLibraries(libraries = @UsesLibraries(libraries =
"google-api-client-beta.jar," + "google-api-client-beta.jar," +
"google-api-client-android2-beta.jar," + "google-api-client-android2-beta.jar," +
"google-http-client-beta.jar," + "google-http-client-beta.jar," +
"google-http-client-android2-beta.jar," + "google-http-client-android2-beta.jar," +
"google-http-client-android3-beta.jar," + "google-http-client-android3-beta.jar," +
"google-oauth-client-beta.jar," + "google-oauth-client-beta.jar," +
"guava-11.0.1.jar") "guava-11.0.1.jar")
public class Texting extends AndroidNonvisibleComponent public class Texting extends AndroidNonvisibleComponent
implements Component, OnResumeListener, OnPauseListener, OnInitializeListener { implements Component, OnResumeListener, OnPauseListener, OnInitializeListener, OnStopListener {
public static final String TAG = "Texting Component"; public static final String TAG = "Texting Component";
...@@ -112,6 +113,7 @@ import android.widget.Toast; ...@@ -112,6 +113,7 @@ import android.widget.Toast;
public static final String GV_SMS_SEND_URL = "https://www.google.com/voice/b/0/sms/send/"; public static final String GV_SMS_SEND_URL = "https://www.google.com/voice/b/0/sms/send/";
public static final String GV_URL = "https://www.google.com/voice/b/0"; public static final String GV_URL = "https://www.google.com/voice/b/0";
// Meta data key and value that identify an app for handling incoming Sms // Meta data key and value that identify an app for handling incoming Sms
// Used by Texting component // Used by Texting component
public static final String META_DATA_SMS_KEY = "sms_handler_component"; public static final String META_DATA_SMS_KEY = "sms_handler_component";
...@@ -124,6 +126,10 @@ import android.widget.Toast; ...@@ -124,6 +126,10 @@ import android.widget.Toast;
private static final String SENT = "SMS_SENT"; private static final String SENT = "SMS_SENT";
private static final String UTF8 = "UTF-8"; private static final String UTF8 = "UTF-8";
private static final String MESSAGE_DELIMITER = "\u0001"; private static final String MESSAGE_DELIMITER = "\u0001";
private static final String PREF_GVENABLED = "gvenabled"; // Boolean flag for GV is enabled
private static final String PREF_RCVENABLED = "receiving"; // Boolean flag for app is receiving
private static final String PREF_FILE = "TextingState"; // State of Texting component
// Google Voice oauth helper // Google Voice oauth helper
private GoogleVoiceUtil gvHelper; private GoogleVoiceUtil gvHelper;
...@@ -143,16 +149,16 @@ import android.widget.Toast; ...@@ -143,16 +149,16 @@ import android.widget.Toast;
// No messages can be received until Initialized // No messages can be received until Initialized
private boolean isInitialized; private boolean isInitialized;
// True when resumed and false when paused. // True when resumed and false when paused.
// Messages are cached when app is not running // Messages are cached when app is not running
private static boolean isRunning; private static boolean isRunning;
// Cache file for cached messages // Cache file for cached messages
private static final String CACHE_FILE = "textingmsgcache"; private static final String CACHE_FILE = "textingmsgcache";
private static int messagesCached; private static int messagesCached;
private static Object cacheLock = new Object(); private static Object cacheLock = new Object();
/** /**
* Creates a new TextMessage component. * Creates a new TextMessage component.
* *
...@@ -164,11 +170,19 @@ import android.widget.Toast; ...@@ -164,11 +170,19 @@ import android.widget.Toast;
Texting.component = (Texting)this; Texting.component = (Texting)this;
activity = container.$context(); activity = container.$context();
SharedPreferences prefs = activity.getSharedPreferences(PREF_FILE, Activity.MODE_PRIVATE);
if (prefs != null) {
receivingEnabled = prefs.getBoolean(PREF_RCVENABLED, true);
googleVoiceEnabled = prefs.getBoolean(PREF_GVENABLED, false);
Log.i(TAG, "Starting with receiving Enabled=" + receivingEnabled + " GV enabled=" + googleVoiceEnabled);
} else {
receivingEnabled = true;
googleVoiceEnabled = false;
}
smsManager = SmsManager.getDefault(); smsManager = SmsManager.getDefault();
PhoneNumber(""); PhoneNumber("");
receivingEnabled = true;
googleVoiceEnabled = false;
isInitialized = false; // Set true when the form is initialized and can dispatch isInitialized = false; // Set true when the form is initialized and can dispatch
isRunning = false; // This will be set true in onResume and false in onPause isRunning = false; // This will be set true in onResume and false in onPause
...@@ -176,6 +190,7 @@ import android.widget.Toast; ...@@ -176,6 +190,7 @@ import android.widget.Toast;
container.$form().registerForOnInitialize(this); container.$form().registerForOnInitialize(this);
container.$form().registerForOnResume(this); container.$form().registerForOnResume(this);
container.$form().registerForOnPause(this); container.$form().registerForOnPause(this);
container.$form().registerForOnStop(this);
} }
/** /**
...@@ -289,6 +304,10 @@ import android.widget.Toast; ...@@ -289,6 +304,10 @@ import android.widget.Toast;
public void GoogleVoiceEnabled(boolean enabled) { public void GoogleVoiceEnabled(boolean enabled) {
if (SdkLevel.getLevel() >= SdkLevel.LEVEL_ECLAIR) { if (SdkLevel.getLevel() >= SdkLevel.LEVEL_ECLAIR) {
this.googleVoiceEnabled = enabled; this.googleVoiceEnabled = enabled;
SharedPreferences prefs = activity.getSharedPreferences(PREF_FILE, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(PREF_GVENABLED, enabled);
editor.commit();
} else { } else {
Toast.makeText(activity, "Sorry, your phone's system does not support this option.", Toast.LENGTH_LONG).show(); Toast.makeText(activity, "Sorry, your phone's system does not support this option.", Toast.LENGTH_LONG).show();
} }
...@@ -319,6 +338,15 @@ import android.widget.Toast; ...@@ -319,6 +338,15 @@ import android.widget.Toast;
@SimpleProperty() @SimpleProperty()
public void ReceivingEnabled(boolean enabled) { public void ReceivingEnabled(boolean enabled) {
Texting.receivingEnabled = enabled; Texting.receivingEnabled = enabled;
SharedPreferences prefs = activity.getSharedPreferences(PREF_FILE, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(PREF_RCVENABLED, enabled);
editor.commit();
}
public static boolean isReceivingEnabled(Context context) {
SharedPreferences prefs = context.getSharedPreferences(PREF_FILE, Activity.MODE_PRIVATE);
return prefs.getBoolean(PREF_RCVENABLED, true);
} }
/** /**
...@@ -366,7 +394,7 @@ import android.widget.Toast; ...@@ -366,7 +394,7 @@ import android.widget.Toast;
Log.i(TAG, "Message + " + k + " " + phoneAndMessage); Log.i(TAG, "Message + " + k + " " + phoneAndMessage);
int delim = phoneAndMessage.indexOf(":"); int delim = phoneAndMessage.indexOf(":");
// If receiving is not enabled, messages are not dispatched // If receiving is not enabled, messages are not dispatched
if (receivingEnabled && delim != -1) { if (receivingEnabled && delim != -1) {
MessageReceived(phoneAndMessage.substring(0,delim), MessageReceived(phoneAndMessage.substring(0,delim),
...@@ -374,7 +402,7 @@ import android.widget.Toast; ...@@ -374,7 +402,7 @@ import android.widget.Toast;
} }
} }
} }
/** /**
* Retrieves cached messages from the cache file * Retrieves cached messages from the cache file
* and deletes the file. * and deletes the file.
...@@ -398,8 +426,7 @@ import android.widget.Toast; ...@@ -398,8 +426,7 @@ import android.widget.Toast;
messagesCached = 0; messagesCached = 0;
Log.i(TAG, "Retrieved cache " + cache); Log.i(TAG, "Retrieved cache " + cache);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
Log.e(TAG, "File not found error reading from cache file"); Log.e(TAG, "No Cache file found -- this is not (usually) an error");
e.printStackTrace();
return null; return null;
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "I/O Error reading from cache file"); Log.e(TAG, "I/O Error reading from cache file");
...@@ -409,7 +436,7 @@ import android.widget.Toast; ...@@ -409,7 +436,7 @@ import android.widget.Toast;
String messagelist[] = cache.split(MESSAGE_DELIMITER); String messagelist[] = cache.split(MESSAGE_DELIMITER);
return messagelist; return messagelist;
} }
/** /**
* Called by SmsBroadcastReceiver * Called by SmsBroadcastReceiver
* @return * @return
...@@ -417,7 +444,7 @@ import android.widget.Toast; ...@@ -417,7 +444,7 @@ import android.widget.Toast;
public static boolean isRunning() { public static boolean isRunning() {
return isRunning; return isRunning;
} }
/** /**
* Used to keep count in Notifications. * Used to keep count in Notifications.
* @return * @return
...@@ -425,7 +452,7 @@ import android.widget.Toast; ...@@ -425,7 +452,7 @@ import android.widget.Toast;
public static int getCachedMsgCount() { public static int getCachedMsgCount() {
return messagesCached; return messagesCached;
} }
/** /**
* Processes cached messages if the app is initialized * Processes cached messages if the app is initialized
*/ */
...@@ -439,7 +466,7 @@ import android.widget.Toast; ...@@ -439,7 +466,7 @@ import android.widget.Toast;
nm.cancel(SmsBroadcastReceiver.NOTIFICATION_ID); nm.cancel(SmsBroadcastReceiver.NOTIFICATION_ID);
} }
} }
/** /**
* Messages received while paused will be cached * Messages received while paused will be cached
*/ */
...@@ -448,7 +475,7 @@ import android.widget.Toast; ...@@ -448,7 +475,7 @@ import android.widget.Toast;
Log.i(TAG, "onPause()"); Log.i(TAG, "onPause()");
isRunning = false; isRunning = false;
} }
/** /**
* This method is called by SmsBroadcastRecieiver when a message is received. * This method is called by SmsBroadcastRecieiver when a message is received.
* @param phone * @param phone
...@@ -463,7 +490,7 @@ import android.widget.Toast; ...@@ -463,7 +490,7 @@ import android.widget.Toast;
} }
} }
} }
/** /**
* Messages a cached in a private file * Messages a cached in a private file
* @param context * @param context
...@@ -748,12 +775,12 @@ import android.widget.Toast; ...@@ -748,12 +775,12 @@ import android.widget.Toast;
if (googleVoiceEnabled) { if (googleVoiceEnabled) {
Log.i(TAG, "Sending via GV"); Log.i(TAG, "Sending via GV");
// Get the authtoken // Get the authtoken
OAuth2Helper oauthHelper = new OAuth2Helper(); OAuth2Helper oauthHelper = new OAuth2Helper();
String authToken = oauthHelper.getRefreshedAuthToken(activity, GV_SERVICE); String authToken = oauthHelper.getRefreshedAuthToken(activity, GV_SERVICE);
Log.i(TAG, "authToken = " + authToken); Log.i(TAG, "authToken = " + authToken);
if (gvHelper == null) { if (gvHelper == null) {
gvHelper = new GoogleVoiceUtil(authToken); gvHelper = new GoogleVoiceUtil(authToken);
} }
...@@ -805,5 +832,17 @@ import android.widget.Toast; ...@@ -805,5 +832,17 @@ import android.widget.Toast;
} }
} }
} }
/**
* Save the component's state in shared preference file before it is killed.
*/
@Override
public void onStop() {
SharedPreferences prefs = activity.getSharedPreferences(PREF_FILE, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(PREF_RCVENABLED, receivingEnabled);
editor.putBoolean(PREF_GVENABLED, googleVoiceEnabled);
editor.commit();
}
} }
...@@ -71,7 +71,7 @@ public class SmsBroadcastReceiver extends BroadcastReceiver { ...@@ -71,7 +71,7 @@ public class SmsBroadcastReceiver extends BroadcastReceiver {
// If activity is receiving messages, send the message; // If activity is receiving messages, send the message;
// It'll be cached if the app isn't running // It'll be cached if the app isn't running
if (Texting.receivingEnabled) { if (Texting.isReceivingEnabled(context)) {
if (isRepl(context)) { // If we are the Repl, we only handle texts if we are running if (isRepl(context)) { // If we are the Repl, we only handle texts if we are running
if (Texting.isRunning()) { if (Texting.isRunning()) {
Texting.handledReceivedMessage(context, phone, msg); Texting.handledReceivedMessage(context, phone, msg);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment