Skip to content

Commit

Permalink
Resolves issue #1: restart monitoring updates when value is changed
Browse files Browse the repository at this point in the history
  • Loading branch information
jush committed Apr 9, 2011
1 parent ecaa564 commit 4cbeb9a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
10 changes: 7 additions & 3 deletions res/xml/mainpreferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference android:title="Update interval" android:numeric="integer" android:summary="How often the connection is checked in minutes" android:key="@string/updateIntervalID"/>
</PreferenceScreen>
xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:title="Update interval"
android:numeric="integer"
android:summary="How often the connection is checked in minutes"
android:key="@string/updateIntervalID" />
</PreferenceScreen>
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,54 @@
package org.jush.locationtimemonitoring.activities;

import org.jush.locationtimemonitoring.R;
import org.jush.locationtimemonitoring.monitoring.MonitoringManager;
import org.jush.locationtimemonitoring.util.LocalLog;

import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.widget.Toast;

public class MainPreferences extends PreferenceActivity implements OnPreferenceChangeListener{

public class MainPreferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.mainpreferences);
Preference intervalPreference = findPreference(getText(R.string.updateIntervalID));
intervalPreference.setOnPreferenceChangeListener(this);
}

/* (non-Javadoc)
* @see android.preference.Preference.OnPreferenceChangeListener#onPreferenceChange(android.preference.Preference, java.lang.Object)
*/
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
LocalLog.debug("Preference changed: " + preference.getKey() + " newValue type: " + newValue.getClass() + " value: " + newValue);
boolean result;
if (!(newValue instanceof String)) {
result = false;
} else {
try {
int newInterval = Integer.valueOf((String) newValue);
if (newInterval > 0) {
result = true;
MonitoringManager monitoringManager = MonitoringManager.getInstance(getApplicationContext());
if (monitoringManager.getUpdateInterval() != newInterval) {
monitoringManager.startMonitoring(newInterval);
}
} else {
result = false;
}
} catch (NumberFormatException e) {
result = false;
}
}
if (!result) {
LocalLog.debug("Invalid interval value: use positive numbers.");
Toast.makeText(getApplicationContext(), "Error: use positive minutes.", Toast.LENGTH_LONG).show();
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@

public class MonitoringManager {
/**
* How often the wifi is checked in minutes
* How often the wifi is checked in minutes by default
*/
public static final int DEFAULT_UPDATE_INTERVAL = 5;

/**
* Unique string used to broadcast intents to the monitoring update.
*/
public static String MONITORING_UPDATE = MonitoringUpdate.class.getPackage().getName()
+ ".MONITORING_UPDATE";

Expand All @@ -48,7 +51,10 @@ public class MonitoringManager {
private Context ctx;

/**
* This class is singleton so the constructor is hidden
*
* @param applicationContext the unique application context that will be
* used to broadcast intents and retrieve system services.
*/
private MonitoringManager(Context applicationContext) {
LocalLog.debug("New MonitoringManager instance created!");
Expand All @@ -63,6 +69,12 @@ public static MonitoringManager getInstance(Context applicationContext) {
}

public void startMonitoring() {
int updateInterval = getUpdateInterval();

startMonitoring(updateInterval);
}

public void startMonitoring(int updateInterval) {
// When the alarm goes off, we want to broadcast an Intent to our
// BroadcastReceiver. Here we make an Intent with an explicit class
// name to have our own receiver (which has been published in
Expand All @@ -75,8 +87,6 @@ public void startMonitoring() {
// We want the alarm to go off now.
long firstTime = SystemClock.elapsedRealtime();

int updateInterval = getUpdateInterval();

// Schedule the alarm!
AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,
Expand Down

0 comments on commit 4cbeb9a

Please sign in to comment.