Skip to content

Commit

Permalink
Issue rtyley#100: Made a few changes that were found during review of…
Browse files Browse the repository at this point in the history
… PR rtyley#103.

The following items were changed:
  * Removed commented options to sync on a specific day of the week and sync on
    a specific day of the month.
  * Added additional possibilities to sync every: 5 minutes, 4 hours, daily (not
    at a specified time), and weekly.
  • Loading branch information
jwir3 committed Mar 17, 2014
1 parent ab7b25a commit 0165ae3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
26 changes: 18 additions & 8 deletions agit/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,25 +124,35 @@
<string name="setting_sync_frequency_daily_title">Sync daily at</string>
<string name="setting_sync_frequency_daily_hour_key">setting_sync_frequency_daily_hour</string>
<string name="setting_sync_frequency_daily_min_key">setting_sync_frequency_daily_min</string>
<string name="setting_sync_frequency_subtitle_key">setting_sync_frequency_subtitle</string>

<!--
These next two arrays need to be in sync (no pun intended). In other words, if you change
setting_sync_choices to have another value, you need to put a corresponding integer value
into the setting_sync_choices_values
-->
<string-array name="setting_sync_choices">
<item>Manually</item>
<item>Schedule daily at&#8230;</item>
<item>Every minute</item>
<item>Every 5 minutes</item>
<item>Every 15 minutes</item>
<item>Every hour</item>
<item>Every 3 hours</item>
<item>Every 6 hours</item>
<item>Every 12 hours</item>
<item>Every 4 hours</item>
<item>Every day</item>
<item>Every week</item>
</string-array>

<!-- These next values are in MINUTES (except for the special values for "Manually" and "Sync Daily" -->
<string-array name="setting_sync_choices_values">
<item>-1</item>
<item>@string/setting_sync_frequency_daily</item>
<item>1</item>
<item>5</item>
<item>15</item>
<item>60</item>
<item>180</item>
<item>360</item>
<item>720</item>
<!-- <item>Every week on&#8230;</item> -->
<!-- <item>Every month on&#8230;</item> -->
<item>240</item>
<item>1440</item>
<item>10080</item>
</string-array>
</resources>
34 changes: 31 additions & 3 deletions agit/src/main/java/com/madgag/agit/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ public void onCreate(Bundle aSavedInstanceState) {
super.onCreate(aSavedInstanceState);
addPreferencesFromResource(R.layout.settings_activity);
ListPreference syncFreq = (ListPreference) findPreference(getString(R.string.setting_sync_frequency_key));
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SettingsActivity.this);
setSummary();
syncFreq.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
String value = (String)o;
final SharedPreferences.Editor edtr = prefs.edit();

if (value.equals(getString(R.string.setting_sync_frequency_daily))) {
// Check if our previous value was the same thing, so we can set it in the time
// picker dialog.
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SettingsActivity.this);
int hourDefault = 12;
int minDefault = 0;
String prevVal = prefs.getString(getString(R.string.setting_sync_frequency_key), "-1");
Expand All @@ -56,13 +59,15 @@ public boolean onPreferenceChange(Preference preference, Object o) {
TimePickerDialog.OnTimeSetListener timeListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minOfHour) {
SharedPreferences.Editor edtr = prefs.edit();
String subTitle = "Sync daily at " + hourOfDay + ":" + (minOfHour < 10 ? "0" : "") + minOfHour;
edtr.putInt(getString(R.string.setting_sync_frequency_daily_hour_key), hourOfDay);
edtr.putInt(getString(R.string.setting_sync_frequency_daily_min_key), minOfHour);
edtr.putString(getString(R.string.setting_sync_frequency_subtitle_key), subTitle);
edtr.commit();

SyncRepoManager manager = new SyncRepoManager();
manager.setDailySync(SettingsActivity.this, hourOfDay, minOfHour);
SettingsActivity.this.setSummary();
}
};

Expand All @@ -71,10 +76,27 @@ public void onTimeSet(TimePicker timePicker, int hourOfDay, int minOfHour) {
timePicker.setTitle(getString(R.string.setting_sync_frequency_daily_title));
timePicker.show();
} else {
// Loop to find out what the index of the chosen value is in the array - it should be the same
// as the chosen value in the array of possible choices (see comment in strings.xml).
String[] indexArray = getResources().getStringArray(R.array.setting_sync_choices_values);
String[] subtitleArray = getResources().getStringArray(R.array.setting_sync_choices);
int subtitleIndex = 0;
for (String s : indexArray) {
if (s.equals(o)) {
break;
}
subtitleIndex++;
}

String subTitle = subtitleArray[subtitleIndex];
edtr.putString(getString(R.string.setting_sync_frequency_subtitle_key), subTitle);
edtr.commit();
SyncRepoManager manager = new SyncRepoManager();
manager.cancelDailySync(SettingsActivity.this);
}

SettingsActivity.this.setSummary();

return true;
}
});
Expand All @@ -90,6 +112,12 @@ public void onDestroy() {
}
}

private ListPreference mSyncPreferences;
private void setSummary() {
ListPreference syncFreq = (ListPreference) findPreference(getString(R.string.setting_sync_frequency_key));
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SettingsActivity.this);
syncFreq.setSummary(prefs.getString(getString(R.string.setting_sync_frequency_subtitle_key),
getString(R.string.setting_instruction_sync_frequency)));
}

private static final String TAG = "SettingsActivity";
}
3 changes: 0 additions & 3 deletions agit/src/main/java/com/madgag/agit/sync/SyncRepoManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public static void cancelDailySync(Context aContext) {
@Override
public void onReceive(Context aContext, Intent aIntent) {
Calendar rightNow = Calendar.getInstance();
long timeInMs = rightNow.getTimeInMillis();
long offset = rightNow.get(Calendar.ZONE_OFFSET) +
rightNow.get(Calendar.DST_OFFSET);
long sinceMidnight = (rightNow.getTimeInMillis() + offset) %
Expand All @@ -96,8 +95,6 @@ public void onReceive(Context aContext, Intent aIntent) {
int clockHour = hoursSinceMidnight % 12;
int minutesSinceMidnight = (int)((sinceMidnight % (hoursSinceMidnight * (60 * 60 * 1000))) / (60*1000));

long msAtMidnight = (rightNow.getTimeInMillis() + offset) - sinceMidnight;

Log.d(TAG, "onReceive called at " + clockHour + ":" + minutesSinceMidnight + " " + (isPM ? "pm" : "am"));
doSyncNow();
}
Expand Down

0 comments on commit 0165ae3

Please sign in to comment.