Now notifications are cleaned by age

This commit is contained in:
Juan Miguel Boyero Corral 2016-01-30 14:41:58 +01:00
parent 6aea5d3382
commit a6047aa4a0
9 changed files with 54 additions and 84 deletions

2
.gitignore vendored
View File

@ -5,4 +5,6 @@ gradlew
gradlew.bat
import-summary.txt
local.properties
/SWADroid/SWADroid-SWADroid.iml
/SWADroid.iml
app/lint.xml

View File

@ -13,7 +13,7 @@
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="jdk" jdkName="Android API 3 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -65,6 +65,10 @@ public class Constants {
* Connection timeout (in milliseconds)
*/
public static final int CONNECTION_TIMEOUT = 60000;
/**
* Threshold for clean old notifications (in seconds)
*/
public static final int CLEAN_NOTIFICATIONS_THRESHOLD = 2592000; // 30 days
/**
* Null value returned by webservices when a field is empty
*/

View File

@ -33,11 +33,11 @@ import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import es.ugr.swad.swadroid.Constants;
import es.ugr.swad.swadroid.preferences.Preferences;
import es.ugr.swad.swadroid.analytics.SWADroidTracker;
import es.ugr.swad.swadroid.model.Course;
import es.ugr.swad.swadroid.model.Event;
@ -53,6 +53,7 @@ import es.ugr.swad.swadroid.model.TestQuestion;
import es.ugr.swad.swadroid.model.TestTag;
import es.ugr.swad.swadroid.model.User;
import es.ugr.swad.swadroid.model.UserAttendance;
import es.ugr.swad.swadroid.preferences.Preferences;
import es.ugr.swad.swadroid.utils.Crypto;
import es.ugr.swad.swadroid.utils.OldCrypto;
import es.ugr.swad.swadroid.utils.Utils;
@ -1829,6 +1830,20 @@ public class DataBaseHelper {
for (Entity ent : rows) {
ent.delete();
}
}/**
* Removes all rows from a database table matching the given condition
*
* @param where condition to remove a row
* @return numRowsDeleted Number of deleted rows
*/
public int removeAllRows(String table, String where) {
List<Entity> rows = db.getEntityList(table, where);
for (Entity ent : rows) {
ent.delete();
}
return rows.size();
}
/**
@ -2026,13 +2041,13 @@ public class DataBaseHelper {
}
/**
* Clear old notifications
* Clean old notifications by size
*
* @param size Max table size
*/
public void clearOldNotifications(int size) {
public void cleanOldNotificationsBySize(int size) {
String where = null;
String orderby = "eventTime ASC";
String orderby = "CAST(eventTime as INTEGER) ASC";
List<Entity> rows = db.getEntityList(DataBaseHelper.DB_TABLE_NOTIFICATIONS, where, orderby);
int numRows = rows.size();
int numDeletions = numRows - size;
@ -2043,6 +2058,21 @@ public class DataBaseHelper {
}
}
/**
* Clean old notifications by age
*
* @param age Max age in seconds
* @return numRowsDeleted Number of deleted notifications
*/
public int cleanOldNotificationsByAge(int age) {
long now = Calendar.getInstance().getTime().getTime() / 1000; // in seconds
// Remove notifications older than 'age' seconds
String where = "CAST(eventTime as INTEGER) < " + String.valueOf(now - age);
return removeAllRows(DataBaseHelper.DB_TABLE_NOTIFICATIONS, where);
}
/**
* Encrypts the notifications data
*/

View File

@ -49,15 +49,14 @@ import java.util.List;
import java.util.Vector;
import es.ugr.swad.swadroid.Constants;
import es.ugr.swad.swadroid.preferences.Preferences;
import es.ugr.swad.swadroid.R;
import es.ugr.swad.swadroid.analytics.SWADroidTracker;
import es.ugr.swad.swadroid.database.DataBaseHelper;
import es.ugr.swad.swadroid.gui.AlertNotificationFactory;
import es.ugr.swad.swadroid.model.Model;
import es.ugr.swad.swadroid.model.SWADNotification;
import es.ugr.swad.swadroid.modules.login.Login;
import es.ugr.swad.swadroid.modules.Module;
import es.ugr.swad.swadroid.modules.login.Login;
import es.ugr.swad.swadroid.sync.SyncUtils;
import es.ugr.swad.swadroid.utils.DateTimeUtils;
import es.ugr.swad.swadroid.utils.Utils;
@ -74,9 +73,9 @@ import es.ugr.swad.swadroid.webservices.SOAPClient;
public class Notifications extends Module implements
SwipeRefreshLayout.OnRefreshListener {
/**
* Max size to store notifications
* Unique identifier for notification alerts
*/
private int SIZE_LIMIT;
public static final int NOTIF_ALERT_ID = 1982;
/**
* Cursor orderby parameter
*/
@ -89,10 +88,6 @@ public class Notifications extends Module implements
* Error message returned by the synchronization service
*/
private String errorMessage;
/**
* Unique identifier for notification alerts
*/
private final int NOTIF_ALERT_ID = 1982;
/**
* Notifications tag name for Logcat
*/
@ -326,7 +321,6 @@ public class Notifications extends Module implements
setMETHOD_NAME("getNotifications");
receiver = new SyncReceiver(this);
account = new Account(getString(R.string.app_name), accountType);
SIZE_LIMIT = Preferences.getNotifLimit();
}
/**
@ -385,8 +379,7 @@ public class Notifications extends Module implements
*/
@Override
protected void requestService() throws Exception {
// Download new notifications from the server
SIZE_LIMIT = Preferences.getNotifLimit();
int numDeletedNotif = 0;
if (SyncUtils.isSyncAutomatically(getApplicationContext())) {
Log.i(TAG,
@ -467,8 +460,9 @@ public class Notifications extends Module implements
Log.i(TAG, "Retrieved " + numNotif + " notifications ("
+ notifCount + " unread)");
// Clear old notifications to control database size
dbHelper.clearOldNotifications(SIZE_LIMIT);
// Clean old notifications to control database size
numDeletedNotif = dbHelper.cleanOldNotificationsByAge(Constants.CLEAN_NOTIFICATIONS_THRESHOLD);
Log.i(TAG, "Deleted " + numDeletedNotif + " notifications from database");
dbHelper.endTransaction(true);
}
@ -510,12 +504,6 @@ public class Notifications extends Module implements
if (!SyncUtils.isSyncAutomatically(getApplicationContext())) {
if (notifCount > 0) {
// If the notifications counter exceeds the limit, set it to the
// max allowed
if (notifCount > SIZE_LIMIT) {
notifCount = SIZE_LIMIT;
}
notif = AlertNotificationFactory.createAlertNotification(getApplicationContext(),
getString(R.string.app_name),
notifCount + " "

View File

@ -77,8 +77,6 @@ public class NotificationsSyncAdapterService extends Service {
private static SecureConnection conn;
private static SyncAdapterImpl sSyncAdapter = null;
private static int notifCount;
private static final int NOTIF_ALERT_ID = 1982;
private static int SIZE_LIMIT;
private static DataBaseHelper dbHelper;
private static IWebserviceClient webserviceClient;
private static String METHOD_NAME = "";
@ -112,7 +110,6 @@ public class NotificationsSyncAdapterService extends Service {
int httpStatusCode;
try {
SIZE_LIMIT = Preferences.getNotifLimit();
NotificationsSyncAdapterService.performSync(mContext, account, extras, authority, provider, syncResult);
//If synchronization was successful, update last synchronization time in preferences
@ -328,6 +325,8 @@ public class NotificationsSyncAdapterService extends Service {
}
private static void getNotifications() throws Exception {
int numDeletedNotif = 0;
Log.d(TAG, "Logged");
//Calculates next timestamp to be requested
@ -385,8 +384,9 @@ public class NotificationsSyncAdapterService extends Service {
//Request finalized without errors
Log.i(TAG, "Retrieved " + numNotif + " notifications (" + notifCount + " unread)");
//Clear old notifications to control database size
dbHelper.clearOldNotifications(SIZE_LIMIT);
//Clean old notifications to control database size
numDeletedNotif = dbHelper.cleanOldNotificationsByAge(Constants.CLEAN_NOTIFICATIONS_THRESHOLD);
Log.i(TAG, "Deleted " + numDeletedNotif + " notifications from database");
dbHelper.endTransaction(true);
}
@ -474,11 +474,6 @@ public class NotificationsSyncAdapterService extends Service {
getNotifications();
if (notifCount > 0) {
//If the notifications counter exceeds the limit, set it to the max allowed
if (notifCount > SIZE_LIMIT) {
notifCount = SIZE_LIMIT;
}
notif = AlertNotificationFactory.createAlertNotification(context,
context.getString(R.string.app_name),
notifCount + " "
@ -492,7 +487,7 @@ public class NotificationsSyncAdapterService extends Service {
false,
false);
AlertNotificationFactory.showAlertNotification(context, notif, NOTIF_ALERT_ID);
AlertNotificationFactory.showAlertNotification(context, notif, Notifications.NOTIF_ALERT_ID);
}
sendReadedNotifications(context);

View File

@ -121,10 +121,6 @@ public class Preferences {
* Synchronization enable preference name
*/
public static final String SYNCENABLEPREF = "prefSyncEnable";
/**
* Notifications limit preference name
*/
public static final String NOTIFLIMITPREF = "prefNotifLimit";
/**
* Last synchronization time preference name
*/
@ -308,25 +304,6 @@ public class Preferences {
return prefs.getString(DBKEYPREF, "");
}
/**
* Gets the max number of notifications to be stored
*
* @return The max number of notifications to be stored
*/
public static int getNotifLimit() {
return prefs.getInt(NOTIFLIMITPREF, 25);
}
/**
* Sets the max number of notifications to be stored
*
* @param notifLimit The max number of notifications to be stored
*/
public static void setNotifLimit(int notifLimit) {
editor = editor.putInt(NOTIFLIMITPREF, notifLimit);
editor.commit();
}
/**
* Gets the synchronization time
*
@ -491,10 +468,6 @@ public class Preferences {
SyncUtils.removePeriodicSync(Constants.AUTHORITY, Bundle.EMPTY, context);
}
}
public static void clearOldNotifications(int size) {
dbHelper.clearOldNotifications(size);
}
public static boolean isPreferencesChanged() {
return preferencesChanged;

View File

@ -106,10 +106,6 @@ public class PreferencesActivity extends PreferenceActivity implements OnPrefere
* Synchronization enable preference
*/
private static CheckBoxPreference syncEnablePref;
/**
* Notifications limit preference
*/
private static SeekBarDialogPreference notifLimitPref;
/**
* Notifications sound enable preference
*/
@ -196,7 +192,6 @@ public class PreferencesActivity extends PreferenceActivity implements OnPrefere
sharePref = findPreference(Preferences.SHAREPREF);
syncTimePref = findPreference(Preferences.SYNCTIMEPREF);
syncEnablePref = (CheckBoxPreference) findPreference(Preferences.SYNCENABLEPREF);
notifLimitPref = (SeekBarDialogPreference) findPreference(Preferences.NOTIFLIMITPREF);
notifSoundEnablePref = (CheckBoxPreference) findPreference(Preferences.NOTIFSOUNDENABLEPREF);
notifVibrateEnablePref = (CheckBoxPreference) findPreference(Preferences.NOTIFVIBRATEENABLEPREF);
notifLightsEnablePref = (CheckBoxPreference) findPreference(Preferences.NOTIFLIGHTSENABLEPREF);
@ -207,16 +202,12 @@ public class PreferencesActivity extends PreferenceActivity implements OnPrefere
googlePlusPref.setOnPreferenceChangeListener(this);
blogPref.setOnPreferenceChangeListener(this);
sharePref.setOnPreferenceChangeListener(this);
//serverPref.setOnPreferenceChangeListener(this);
notifLimitPref.setOnPreferenceChangeListener(this);
syncEnablePref.setOnPreferenceChangeListener(this);
syncTimePref.setOnPreferenceChangeListener(this);
notifSoundEnablePref.setOnPreferenceChangeListener(this);
notifVibrateEnablePref.setOnPreferenceChangeListener(this);
notifLightsEnablePref.setOnPreferenceChangeListener(this);
notifLimitPref.setProgress(Preferences.getNotifLimit());
logOutPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
@ -391,10 +382,6 @@ public class PreferencesActivity extends PreferenceActivity implements OnPrefere
syncTimePref.setSummary(prefSyncTimeEntry);
syncPrefsChanged = true;
} else if(Preferences.NOTIFLIMITPREF.equals(key)) {
int notifLimit = (Integer) newValue;
Preferences.setNotifLimit(notifLimit);
Preferences.clearOldNotifications(notifLimit);
} else if(Preferences.NOTIFSOUNDENABLEPREF.equals(key)) {
boolean notifSoundEnabled = (Boolean) newValue;
Preferences.setNotifSoundEnabled(notifSoundEnabled);

View File

@ -26,15 +26,6 @@
android:title="@string/prefSyncTimeTitle" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/prefCatNotifTitle" >
<es.ugr.swad.swadroid.gui.widget.SeekBarDialogPreference
android:defaultValue="25"
android:dialogMessage="@string/prefNotifLimitDialogMessage"
android:key="@string/prefNotifLimitKey"
android:max="100"
android:title="@string/prefNotifLimitTitle"
custom:min="1"
custom:progressTextSuffix="@string/prefNotifLimitSuffix" />
<CheckBoxPreference
android:defaultValue="true"
android:key="@string/prefNotifSoundKey"