Commit 3109a301 authored by Alan McNatty's avatar Alan McNatty
Browse files

Added map for notification types and set these in preferences and added to API.

Still remains issue of server time - I believe solution will be to get the server to respond with it's time which can be recorded as last sync time. The device time is not relevant.
parent c5159eb8
......@@ -68,8 +68,6 @@ catch (AuthUnknownUserException $e) {
jsonreply(array('fail' => 'Invalid user token'));
}
// error_log(var_dump($USER));
// Add in bits of sync data - let's start with notifications
$lastsync = time();
try {
......@@ -77,11 +75,22 @@ try {
}
catch (ParameterException $e) { }
$notification_types_sql = '';
try {
$notification_types = explode(",", param_variable('notifications'));
if ( count($notification_types) > 0 ) {
$notification_types_sql = ' and a.name IN (' . join(',', array_map('db_quote',$notification_types)) . ')';
}
}
catch (ParameterException $e) { }
// TODO - note this may not work across timezeons as db_format_tsfield doesn't support setting the timezone
// Android (for example) defaults to 'UT' .. i.e. FLOOR(EXTRACT(EPOCH FROM ctime AT TIME ZONE 'UTC')) >= ?
$activity_arr = get_records_sql_array("select n.id, n.subject, n.message
from {notification_internal_activity} n, {activity_type} a
where n.type=a.id and n.read=0
and FLOOR(EXTRACT(EPOCH FROM ctime AT TIME ZONE 'UTC')) >= ?
and n.usr= ? ",
where n.type=a.id and n.read=0 and " .
db_format_tsfield('ctime', '') . " >= ?
and n.usr= ? " . $notification_types_sql,
array($lastsync + 0, $USER->id));
if ( count($activity_arr) > 0 )
$json['activity'] = $activity_arr;
......
......@@ -92,6 +92,19 @@
Note: nothing will be shown until your first sync.</string>
<string name="prefcat_defaults_notification">Default Notification Settings</string>
<string name="pref_sync_notification_feedback_key">feedback</string>
<string name="pref_sync_notification_feedback_summary">Receive notifications when you receive feedback</string>
<string name="pref_sync_notification_feedback_title">Feedback</string>
<string name="pref_sync_notification_newpost_key">newpost</string>
<string name="pref_sync_notification_newpost_summary">Receive notifications about new posts</string>
<string name="pref_sync_notification_newpost_title">New posts</string>
<string name="pref_sync_notification_maharamessage_key">maharamessage</string>
<string name="pref_sync_notification_maharamessage_summary">Receive notifications when you receive a message from Mahara</string>
<string name="pref_sync_notification_maharamessage_title">Mahara messages</string>
<string name="pref_sync_notification_usermessage_key">usermessages</string>
<string name="pref_sync_notification_usermessage_summary">Receive notifications when users send messages to you</string>
<string name="pref_sync_notification_usermessage_title">User messages</string>
<!-- Notification defaults -->
<string name="pref_sync_notification_key">sync.notification</string>
......
......@@ -85,8 +85,13 @@
android:dialogTitle="@string/pref_upload_tags_title" />
</PreferenceScreen>
<PreferenceScreen android:title="@string/prefcat_defaults_notification" xmlns:android="http://schemas.android.com/apk/res/android">
<!--
<CheckBoxPreference
android:key="@string/pref_sync_notification_usermessage_key"
android:enabled="true"
android:defaultValue="true"
android:summary="@string/pref_sync_notification_usermessage_summary"
android:title="@string/pref_sync_notification_usermessage_title" />
<CheckBoxPreference
android:key="@string/pref_sync_notification_feedback_key"
android:enabled="true"
android:defaultValue="true"
......@@ -104,18 +109,19 @@
android:defaultValue="true"
android:summary="@string/pref_sync_notification_maharamessage_summary"
android:title="@string/pref_sync_notification_maharamessage_title" />
<CheckBoxPreference
android:key="@string/pref_sync_notification_usermessage_key"
android:enabled="true"
android:defaultValue="true"
android:summary="@string/pref_sync_notification_usermessage_summary"
android:title="@string/pref_sync_notification_usermessage_title" />
<CheckBoxPreference
<!--
<CheckBoxPreference
android:key="@string/pref_sync_notification_groupmessage_key"
android:enabled="true"
android:defaultValue="true"
android:summary="@string/pref_sync_notification_groupmessage_summary"
android:title="@string/pref_sync_notification_groupmessage_title" />
<CheckBoxPreference
android:key="@string/pref_sync_notification_institutionmessage_key"
android:enabled="true"
android:defaultValue="true"
android:summary="@string/pref_sync_notification_institutionmessage_summary"
android:title="@string/pref_sync_notification_institutionmessage_title" />
<CheckBoxPreference
android:key="@string/pref_sync_notification_watchlist_key"
android:enabled="true"
......@@ -152,12 +158,6 @@
android:defaultValue="true"
android:summary="@string/pref_sync_notification_virusrelease_summary"
android:title="@string/pref_sync_notification_virusrelease_title" />
<CheckBoxPreference
android:key="@string/pref_sync_notification_institutionmessage_key"
android:enabled="true"
android:defaultValue="true"
android:summary="@string/pref_sync_notification_institutionmessage_summary"
android:title="@string/pref_sync_notification_institutionmessage_title" />
-->
</PreferenceScreen>
</PreferenceCategory>
......
......@@ -21,6 +21,10 @@
package nz.net.catalyst.MaharaDroid;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/*
* The GlobalResources class is taken from the GlobalResources class
* written by Russel Stewart (rnstewart@gmail.com) as part of the Flickr Free
......@@ -50,6 +54,16 @@ public class GlobalResources {
public static final String[] CONTENT_TABLES = new String[] { "tag", "blog", "folder" };
public static final String[] CONTENT_FIELDS = new String[] { "ID", "VALUE" };
public static final Map<Integer, String> NOTIFICATIONS;
static {
Map<Integer, String> tmpNOTIFICATIONS = new HashMap<Integer, String>();
tmpNOTIFICATIONS.put(R.string.pref_sync_notification_feedback_key, "feedback");
tmpNOTIFICATIONS.put(R.string.pref_sync_notification_newpost_key, "newpost");
tmpNOTIFICATIONS.put(R.string.pref_sync_notification_maharamessage_key, "maharamessage");
tmpNOTIFICATIONS.put(R.string.pref_sync_notification_usermessage_key, "usermessage");
NOTIFICATIONS = Collections.unmodifiableMap(tmpNOTIFICATIONS);
}
public static final String TEMP_PHOTO_FILENAME = "maharadroid-tmp.jpg";
......
......@@ -21,7 +21,9 @@
package nz.net.catalyst.MaharaDroid;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import org.json.JSONArray;
import org.json.JSONException;
......@@ -173,6 +175,21 @@ public class Utils {
if ( DEBUG ) Log.d(TAG, "setting sync url to '" + sync_url + "'");
return sync_url;
}
public static String getSyncNotificationsPref(Context context) {
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
String notificationString = "";
Iterator<Entry<Integer, String>> it = GlobalResources.NOTIFICATIONS.entrySet().iterator();
while (it.hasNext()) {
Entry<Integer, String> not = it.next();
if ( mPrefs.getBoolean(context.getResources().getString(not.getKey()), true) ) {
notificationString += ( notificationString.length() > 0 ) ? "," : "";
notificationString += not.getValue();
}
}
if ( DEBUG ) Log.d(TAG, "setting notifications string to '" + notificationString + "'");
return ( notificationString == "" ) ? null : notificationString;
}
public static String updateTokenFromResult(JSONObject json, Context context) {
String newToken = null;
......
......@@ -70,8 +70,10 @@ public class MaharaAuthHandler {
Utils.showNotification(NOTIFICATION, mContext.getResources().getText(R.string.login_authenticating),
null, null, mContext);
String syncNotifications = Utils.getSyncNotificationsPref(mContext);
JSONObject result = RestClient.AuthSync(authSyncURI, token, username, lastsync, mContext);
JSONObject result = RestClient.AuthSync(authSyncURI, token, username, lastsync, syncNotifications, mContext);
token = Utils.updateTokenFromResult(result, mContext);
if ( token != null ) {
......
......@@ -68,11 +68,13 @@ public class ThreadedSyncAdapter extends AbstractThreadedSyncAdapter{
String token = mPrefs.getString(mContext.getResources().getString(R.string.pref_auth_token_key).toString(),
"");
Long lastsync = mPrefs.getLong("lastsync", 0);
String syncNotifications = Utils.getSyncNotificationsPref(mContext);
if ( VERBOSE ) Log.v(TAG, "Synchronizing Mahara account '" + username + "', " + "'" + token + "' for details as of lastsync '" + lastsync + "'");
// Get latest details from sync
JSONObject result = RestClient.AuthSync(authSyncURI, token, username, lastsync, mContext);
JSONObject result = RestClient.AuthSync(authSyncURI, token, username, lastsync, syncNotifications, mContext);
if ( Utils.updateTokenFromResult(result, mContext) == null ) {
++syncResult.stats.numAuthExceptions;
......
......@@ -154,7 +154,7 @@ public class EditPreferences extends PreferenceActivity implements OnSharedPrefe
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.unregisterOnSharedPreferenceChangeListener(this);
if(VERBOSE) Log.v(TAG, "On destroy received ... ");
if(VERBOSE) Log.v(TAG, "On destroy received ... " + authDetailsChanged);
// If the username and token have
if ( authDetailsChanged ) {
if(VERBOSE) Log.v(TAG, "Deleting old account ... ");
......@@ -211,7 +211,9 @@ public class EditPreferences extends PreferenceActivity implements OnSharedPrefe
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// refresh displayed values by restarting activity (a hack, but apparently there
// isn't a nicer way)
if ( key == getString(R.string.pref_auth_token_key)) {
if(VERBOSE) Log.v(TAG, "onSharedPreferenceChanged received ... " + authDetailsChanged + ", key is: pref_auth_token_key");
authDetailsChanged = true;
if ( this.getCallingActivity() != null ) {
if ( DEBUG ) Log.d(TAG, "Calling activity is '" + this.getCallingActivity().getClassName().toString());
......@@ -220,10 +222,13 @@ public class EditPreferences extends PreferenceActivity implements OnSharedPrefe
}
}
} else if ( key == getString(R.string.pref_auth_username_key)) {
if(VERBOSE) Log.v(TAG, "onSharedPreferenceChanged received ... " + authDetailsChanged + ", key is: pref_auth_username_key");
authDetailsChanged = true;
} else if ( key == getString(R.string.pref_sync_url_key)) {
if(VERBOSE) Log.v(TAG, "onSharedPreferenceChanged received ... " + authDetailsChanged + ", key is: pref_sync_url_key");
authDetailsChanged = true;
} else if ( key == getString(R.string.pref_base_url_key)) {
if(VERBOSE) Log.v(TAG, "onSharedPreferenceChanged received ... " + authDetailsChanged + ", key is: pref_base_url_key");
authDetailsChanged = true;
} else if ( key == getString(R.string.pref_sync_periodic_key)) {
Account account = Utils.getAccount(this);
......
......@@ -21,6 +21,11 @@
package nz.net.catalyst.MaharaDroid.upload;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import nz.net.catalyst.MaharaDroid.GlobalResources;
import nz.net.catalyst.MaharaDroid.LogConfig;
import nz.net.catalyst.MaharaDroid.R;
......
......@@ -163,7 +163,7 @@ public class RestClient {
return CallFunction(url, paramNames, paramVals, context);
}
public static JSONObject AuthSync(String url, String token, String username, Long lastsync, Context context){
public static JSONObject AuthSync(String url, String token, String username, Long lastsync, String notifications, Context context){
Vector<String> pNames = new Vector<String>();
Vector<String> pVals = new Vector<String>();
......@@ -179,6 +179,14 @@ public class RestClient {
pNames.add("lastsync");
pVals.add(lastsync.toString());
}
if ( lastsync != null ) {
pNames.add("lastsync");
pVals.add(lastsync.toString());
}
if ( notifications != null ) {
pNames.add("notifications");
pVals.add(notifications);
}
String [] paramNames, paramVals;
paramNames = paramVals = new String[]{};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment