Skip to content

Commit

Permalink
record and display last trigger times
Browse files Browse the repository at this point in the history
  • Loading branch information
sbungartz committed Sep 11, 2015
1 parent 5795a46 commit 712aa2c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,10 @@ public static void triggerHeartbeat(Context context) {
context.sendBroadcast(new Intent("com.google.android.intent.action.GTALK_HEARTBEAT"));
context.sendBroadcast(new Intent("com.google.android.intent.action.MCS_HEARTBEAT"));
Log.i("heartbeater", "beating");

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putLong("gcm.heartbeat.lasttime", System.currentTimeMillis());
editor.commit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
import android.widget.Toast;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import de.sbungartz.whatsappfixer.R;

Expand Down Expand Up @@ -48,10 +53,18 @@ public boolean onPreferenceClick(Preference preference) {
return true;
}
});
}

SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
for(String key : POSITIVE_LONG_EDIT_TEXT_PREFERENCES) {
updatePositiveLongEditTextSummary(prefs, key);
private void updateLastTriggerTime(SharedPreferences prefs, String keyPrefix) {
long lastTimeMillis = prefs.getLong(keyPrefix + ".lasttime", 0);

Preference pref = findPreference(keyPrefix + ".now");
if(lastTimeMillis == 0) {
pref.setSummary("Never triggered");
} else {
Calendar c = new GregorianCalendar();
c.setTimeInMillis(lastTimeMillis);
pref.setSummary(String.format("Last triggered on %1$tF at %1$tT", c));
}
}

Expand All @@ -69,24 +82,40 @@ private void updatePositiveLongEditTextSummary(SharedPreferences prefs, String k
}
}

private static final Pattern LASTTIME_KEY = Pattern.compile("(.*)\\.lasttime");

@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if(POSITIVE_LONG_EDIT_TEXT_PREFERENCES.contains(key)) {
updatePositiveLongEditTextSummary(prefs, key);
}
Matcher matcher = LASTTIME_KEY.matcher(key);
if(matcher.matches()) {
String keyPrefix = matcher.group(1);
updateLastTriggerTime(prefs, keyPrefix);
} else {
if (POSITIVE_LONG_EDIT_TEXT_PREFERENCES.contains(key)) {
updatePositiveLongEditTextSummary(prefs, key);
}

Context context = getActivity();
Context context = getActivity();

Heartbeats.registerNextAlarm(context);
WhatsappRestarting.registerNextAlarm(context);
Heartbeats.registerNextAlarm(context);
WhatsappRestarting.registerNextAlarm(context);

Toast.makeText(context, "Updated background services.", Toast.LENGTH_SHORT).show();
Toast.makeText(context, "Updated background services.", Toast.LENGTH_SHORT).show();
}
}

@Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);

SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
for(String key : POSITIVE_LONG_EDIT_TEXT_PREFERENCES) {
updatePositiveLongEditTextSummary(prefs, key);
}

updateLastTriggerTime(prefs, "gcm.heartbeat");
updateLastTriggerTime(prefs, "whatsapp.restarting");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void registerNextAlarm(Context context) {
Log.i("restarting", "cancelled");
}
}

private static String getConnectionType(Context context) {
ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMan.getActiveNetworkInfo();
Expand Down Expand Up @@ -81,5 +81,10 @@ public static void restartNow(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
am.killBackgroundProcesses("com.whatsapp");
Log.i("restarting", "restarted");

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putLong("whatsapp.restarting.lasttime", System.currentTimeMillis());
editor.commit();
}
}

0 comments on commit 712aa2c

Please sign in to comment.