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;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.telephony.gsm.SmsManager;
import android.telephony.gsm.SmsMessage;
......@@ -98,7 +99,7 @@ import android.widget.Toast;
"guava-11.0.1.jar")
public class Texting extends AndroidNonvisibleComponent
implements Component, OnResumeListener, OnPauseListener, OnInitializeListener {
implements Component, OnResumeListener, OnPauseListener, OnInitializeListener, OnStopListener {
public static final String TAG = "Texting Component";
......@@ -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_URL = "https://www.google.com/voice/b/0";
// Meta data key and value that identify an app for handling incoming Sms
// Used by Texting component
public static final String META_DATA_SMS_KEY = "sms_handler_component";
......@@ -124,6 +126,10 @@ import android.widget.Toast;
private static final String SENT = "SMS_SENT";
private static final String UTF8 = "UTF-8";
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
private GoogleVoiceUtil gvHelper;
......@@ -164,10 +170,18 @@ import android.widget.Toast;
Texting.component = (Texting)this;
activity = container.$context();
smsManager = SmsManager.getDefault();
PhoneNumber("");
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();
PhoneNumber("");
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
......@@ -176,6 +190,7 @@ import android.widget.Toast;
container.$form().registerForOnInitialize(this);
container.$form().registerForOnResume(this);
container.$form().registerForOnPause(this);
container.$form().registerForOnStop(this);
}
/**
......@@ -289,6 +304,10 @@ import android.widget.Toast;
public void GoogleVoiceEnabled(boolean enabled) {
if (SdkLevel.getLevel() >= SdkLevel.LEVEL_ECLAIR) {
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 {
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;
@SimpleProperty()
public void ReceivingEnabled(boolean 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);
}
/**
......@@ -398,8 +426,7 @@ import android.widget.Toast;
messagesCached = 0;
Log.i(TAG, "Retrieved cache " + cache);
} catch (FileNotFoundException e) {
Log.e(TAG, "File not found error reading from cache file");
e.printStackTrace();
Log.e(TAG, "No Cache file found -- this is not (usually) an error");
return null;
} catch (IOException e) {
Log.e(TAG, "I/O Error reading from cache file");
......@@ -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 {
// If activity is receiving messages, send the message;
// 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 (Texting.isRunning()) {
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