-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
53 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,10 +11,9 @@ | |
import org.commcare.activities.StandardHomeActivity; | ||
import org.commcare.adapters.HomeCardDisplayData; | ||
import org.commcare.adapters.SquareButtonViewHolder; | ||
import org.commcare.android.logging.ReportingUtils; | ||
import org.commcare.android.database.user.models.FormRecord; | ||
import org.commcare.dalvik.R; | ||
import org.commcare.models.database.SqlStorage; | ||
import org.commcare.android.database.user.models.FormRecord; | ||
import org.commcare.modern.util.Pair; | ||
import org.commcare.preferences.HiddenPreferences; | ||
import org.commcare.util.LogTypes; | ||
|
@@ -32,19 +31,23 @@ | |
* @author Phillip Mates ([email protected]). | ||
*/ | ||
public class SyncDetailCalculations { | ||
private final static String UNSENT_FORM_NUMBER_KEY = "unsent-number-limit"; | ||
private final static String UNSENT_FORM_TIME_KEY = "unsent-time-limit"; | ||
private final static String LAST_SYNC_KEY_BASE = "last-succesful-sync-"; | ||
private static final String UNSENT_FORM_NUMBER_KEY = "unsent-number-limit"; | ||
private static final String UNSENT_FORM_TIME_KEY = "unsent-time-limit"; | ||
private static final String LAST_SYNC_KEY_BASE = "last-succesful-sync-"; | ||
|
||
public static void updateSubText(final StandardHomeActivity activity, | ||
SquareButtonViewHolder squareButtonViewHolder, | ||
HomeCardDisplayData cardDisplayData, String notificationText) { | ||
public static void updateSubText( | ||
final StandardHomeActivity activity, | ||
SquareButtonViewHolder squareButtonViewHolder, | ||
HomeCardDisplayData cardDisplayData, | ||
String notificationText) { | ||
|
||
int numUnsentForms = getNumUnsentForms(); | ||
Pair<Long, String> lastSyncTimeAndMessage = getLastSyncTimeAndMessage(); | ||
|
||
Spannable syncIndicator = (activity.localize("home.unsent.forms.indicator", | ||
new String[]{String.valueOf(numUnsentForms)})); | ||
Spannable syncIndicator = | ||
(activity.localize( | ||
"home.unsent.forms.indicator", | ||
new String[] {String.valueOf(numUnsentForms)})); | ||
|
||
String syncStatus = ""; | ||
|
||
|
@@ -54,7 +57,6 @@ public static void updateSubText(final StandardHomeActivity activity, | |
syncStatus = lastSyncTimeAndMessage.second; | ||
} | ||
|
||
|
||
if (numUnsentForms != 0 || HiddenPreferences.shouldShowUnsentFormsWhenZero()) { | ||
if (!TextUtils.isEmpty(syncStatus)) { | ||
syncStatus += "\n\n"; | ||
|
@@ -65,13 +67,16 @@ public static void updateSubText(final StandardHomeActivity activity, | |
squareButtonViewHolder.subTextView.setText(syncStatus); | ||
|
||
setSyncSubtextColor( | ||
squareButtonViewHolder.subTextView, numUnsentForms, lastSyncTimeAndMessage.first, | ||
squareButtonViewHolder.subTextView, | ||
numUnsentForms, | ||
lastSyncTimeAndMessage.first, | ||
activity.getResources().getColor(cardDisplayData.subTextColor), | ||
activity.getResources().getColor(R.color.cc_dark_warm_accent_color)); | ||
} | ||
|
||
public static int getNumUnsentForms() { | ||
SqlStorage<FormRecord> formsStorage = CommCareApplication.instance().getUserStorage(FormRecord.class); | ||
SqlStorage<FormRecord> formsStorage = | ||
CommCareApplication.instance().getUserStorage(FormRecord.class); | ||
try { | ||
return StorageUtils.getUnsentRecordsForCurrentApp(formsStorage).length; | ||
} catch (SessionUnavailableException e) { | ||
|
@@ -87,16 +92,24 @@ public static Pair<Long, String> getLastSyncTimeAndMessage() { | |
if (lastSyncTime == 0) { | ||
syncTimeMessage = Localization.get("home.sync.message.last.never"); | ||
} else { | ||
syncTimeMessage = DateUtils.formatSameDayTime(lastSyncTime, new Date().getTime(), DateFormat.DEFAULT, DateFormat.DEFAULT); | ||
syncTimeMessage = | ||
DateUtils.formatSameDayTime( | ||
lastSyncTime, | ||
new Date().getTime(), | ||
DateFormat.DEFAULT, | ||
DateFormat.DEFAULT); | ||
} | ||
return new Pair<>(lastSyncTime, Localization.get("home.sync.message.last", new String[]{syncTimeMessage.toString()})); | ||
return new Pair<>( | ||
lastSyncTime, | ||
Localization.get( | ||
"home.sync.message.last", new String[] {syncTimeMessage.toString()})); | ||
} | ||
|
||
|
||
/** | ||
* @return The number of days since the user has last synced, as calculated by the difference | ||
* between the current date and the date of the last sync. -1 if the user hasn't ever synced | ||
* or if the last date of sync is unavailable | ||
* Calculates the number of days synce the user last synced | ||
* | ||
* @return the difference between the current date and the date of the last sync. -1 if the user | ||
* hasn't ever synced or if the last date of sync is unavailable | ||
*/ | ||
public static int getDaysSinceLastSync() { | ||
try { | ||
|
@@ -105,10 +118,11 @@ public static int getDaysSinceLastSync() { | |
return -1; | ||
} | ||
return getDaysBetweenJavaDatetimes(new Date(lastSync), new Date()); | ||
} catch(Exception e) { | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
Logger.log(LogTypes.SOFT_ASSERT,"Error Generating Days since last sync: " + | ||
e.getMessage()); | ||
Logger.log( | ||
LogTypes.SOFT_ASSERT, | ||
"Error Generating Days since last sync: " + e.getMessage()); | ||
return -1; | ||
} | ||
} | ||
|
@@ -122,16 +136,21 @@ public static long getLastSyncTime() { | |
} | ||
|
||
public static long getLastSyncTime(String username) { | ||
SharedPreferences prefs = CommCareApplication.instance().getCurrentApp().getAppPreferences(); | ||
SharedPreferences prefs = | ||
CommCareApplication.instance().getCurrentApp().getAppPreferences(); | ||
return prefs.getLong(getLastSyncKey(username), 0); | ||
} | ||
|
||
public static String getLastSyncKey(String username) { | ||
return LAST_SYNC_KEY_BASE + username; | ||
} | ||
|
||
private static void setSyncSubtextColor(TextView subtext, int numUnsentForms, long lastSyncTime, | ||
int normalColor, int warningColor) { | ||
private static void setSyncSubtextColor( | ||
TextView subtext, | ||
int numUnsentForms, | ||
long lastSyncTime, | ||
int normalColor, | ||
int warningColor) { | ||
if (isSyncStronglyNeeded(numUnsentForms, lastSyncTime)) { | ||
subtext.setTextColor(warningColor); | ||
} else { | ||
|
@@ -140,20 +159,23 @@ private static void setSyncSubtextColor(TextView subtext, int numUnsentForms, lo | |
} | ||
|
||
private static boolean isSyncStronglyNeeded(int numUnsentForms, long lastSyncTime) { | ||
return unsentFormNumberLimitExceeded(numUnsentForms) || | ||
unsentFormTimeLimitExceeded(lastSyncTime); | ||
return unsentFormNumberLimitExceeded(numUnsentForms) | ||
|| unsentFormTimeLimitExceeded(lastSyncTime); | ||
} | ||
|
||
private static boolean unsentFormNumberLimitExceeded(int numUnsentForms) { | ||
SharedPreferences prefs = CommCareApplication.instance().getCurrentApp().getAppPreferences(); | ||
SharedPreferences prefs = | ||
CommCareApplication.instance().getCurrentApp().getAppPreferences(); | ||
int unsentFormNumberLimit = Integer.parseInt(prefs.getString(UNSENT_FORM_NUMBER_KEY, "5")); | ||
return numUnsentForms > unsentFormNumberLimit; | ||
} | ||
|
||
private static boolean unsentFormTimeLimitExceeded(long lastSyncTime) { | ||
SharedPreferences prefs = CommCareApplication.instance().getCurrentApp().getAppPreferences(); | ||
double unsentFormTimeLimitInDays = Double.parseDouble(prefs.getString(UNSENT_FORM_TIME_KEY, "5")); | ||
long unsentFormTimeLimitInMsecs = (int)(unsentFormTimeLimitInDays * 24 * 60 * 60 * 1000); | ||
SharedPreferences prefs = | ||
CommCareApplication.instance().getCurrentApp().getAppPreferences(); | ||
double unsentFormTimeLimitInDays = | ||
Double.parseDouble(prefs.getString(UNSENT_FORM_TIME_KEY, "5")); | ||
long unsentFormTimeLimitInMsecs = (int) (unsentFormTimeLimitInDays * 24 * 60 * 60 * 1000); | ||
|
||
long now = new Date().getTime(); | ||
long msecsSinceLastSync = (now - lastSyncTime); | ||
|