Commit bb985c22 authored by Alan McNatty's avatar Alan McNatty
Browse files

Basic back-end Mahara sync API completed - the threadedsyncadapter raises...

Basic back-end Mahara sync API completed - the threadedsyncadapter raises notifications for activity on the JSON as a test.
parent 21fea824
......@@ -26,8 +26,12 @@ import java.io.File;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.ConnectivityManager;
......@@ -149,4 +153,38 @@ public class Utils {
}
return newToken;
}
/**
* Show a notification while this service is running.
*/
public static void showNotification(int id, CharSequence title, CharSequence description, Intent intent, Context mContext) {
NotificationManager mNM = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.icon_notify, title,
System.currentTimeMillis());
PendingIntent contentIntent = null;
// The PendingIntent to launch our activity if the user selects this notification
if ( intent != null ) {
contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
}
if ( description == null ) {
description = title;
}
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(mContext, title, description, contentIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL;
// Send the notification.
mNM.notify(id, notification);
}
public static void cancelNotification(int id, Context mContext) {
NotificationManager mNM = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
mNM.cancel(id);
}
}
......@@ -19,15 +19,15 @@ package nz.net.catalyst.MaharaDroid.authenticator;
import nz.net.catalyst.MaharaDroid.R;
import nz.net.catalyst.MaharaDroid.authenticator.AuthenticatorActivity;
import nz.net.catalyst.MaharaDroid.provider.MaharaProvider;
import nz.net.catalyst.MaharaDroid.ui.ArtefactExpandableListAdapterActivity;
import nz.net.catalyst.MaharaDroid.ui.EditPreferences;
import nz.net.catalyst.MaharaDroid.GlobalResources;
import nz.net.catalyst.MaharaDroid.LogConfig;
import nz.net.catalyst.MaharaDroid.Utils;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorActivity;
import android.accounts.AccountManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Intent;
import android.os.Bundle;
......@@ -87,9 +87,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity {
if ( DEBUG ) Log.d(TAG, "AuthenticatorActivity request new: " + mRequestNewAccount);
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
showNotification(getText(R.string.login_authenticating), null, null);
Utils.showNotification(NOTIFICATION, getText(R.string.login_authenticating), null, null, this);
MaharaAuthHandler.attemptAuth(mUsername, mHandler, AuthenticatorActivity.this);
finish();
......@@ -135,16 +133,17 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity {
if (authToken != null) {
finishLogin(username, authToken);
showNotification(getText(R.string.auth_result_success), null, null);
Utils.showNotification(NOTIFICATION, getText(R.string.auth_result_success), null,
new Intent(this, ArtefactExpandableListAdapterActivity.class), this);
} else {
Log.e(TAG, "onAuthenticationResult: failed to authenticate");
// In this sample, we'll use the same text for the ticker and the expanded notification
showNotification(getText(R.string.auth_result_fail_short),
Utils.showNotification(NOTIFICATION, getText(R.string.auth_result_fail_short),
getText(R.string.auth_result_fail_long),
new Intent(this, EditPreferences.class));
new Intent(this, EditPreferences.class), this);
//Toast.makeText(this, getString(R.string.auth_result_fail), Toast.LENGTH_LONG).show();
}
......@@ -153,31 +152,4 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity {
public void isAuthenctiated() {
}
/**
* Show a notification while this service is running.
*/
private void showNotification(CharSequence title, CharSequence description, Intent intent) {
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.icon_notify, title,
System.currentTimeMillis());
PendingIntent contentIntent = null;
// The PendingIntent to launch our activity if the user selects this notification
if ( intent != null ) {
contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
}
if ( description == null ) {
description = title;
}
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, title, description, contentIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL;
// Send the notification.
mNM.notify(NOTIFICATION, notification);
}
private void cancelNotification() {
mNM.cancel(NOTIFICATION);
}
}
\ No newline at end of file
......@@ -65,8 +65,9 @@ public class MaharaAuthHandler {
if ( username == null ) {
username = mPrefs.getString(context.getResources().getString(R.string.pref_auth_username_key), "");
}
JSONObject result = RestClient.AuthSync(authSyncURI, token, username, context);
Long lastsync = mPrefs.getLong("lastsync", 0);
JSONObject result = RestClient.AuthSync(authSyncURI, token, username, lastsync, context);
token = Utils.updateTokenFromResult(result, context);
sendResult(username, token, handler, context);
......
package nz.net.catalyst.MaharaDroid.syncadapter;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import nz.net.catalyst.MaharaDroid.R;
import nz.net.catalyst.MaharaDroid.Utils;
import nz.net.catalyst.MaharaDroid.data.ArtefactDataSQLHelper;
import nz.net.catalyst.MaharaDroid.ui.ArtefactExpandableListAdapterActivity;
import nz.net.catalyst.MaharaDroid.upload.http.RestClient;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.AbstractThreadedSyncAdapter;
import android.content.ContentProviderClient;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SyncResult;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.format.Time;
import android.util.Log;
/**
......@@ -30,6 +41,7 @@ public class ThreadedSyncAdapter extends AbstractThreadedSyncAdapter{
private static final String TAG = "ThreadedSyncAdapter";
private AccountManager mAccountManager;
private NotificationManager mNM;
public ThreadedSyncAdapter(Context context) {
super(context, true);
......@@ -53,9 +65,11 @@ public class ThreadedSyncAdapter extends AbstractThreadedSyncAdapter{
"");
String token = mPrefs.getString(mContext.getResources().getString(R.string.pref_auth_token_key).toString(),
"");
Long lastsync = (long) 0; //mPrefs.getLong("lastsync", 0);
Log.v(TAG, "Synchronizing Mahara account '" + username + "', " + "'" + token + "'");
JSONObject result = RestClient.AuthSync(authSyncURI, token, username, mContext);
JSONObject result = RestClient.AuthSync(authSyncURI, token, username, lastsync, mContext);
Utils.updateTokenFromResult(result, mContext);
......@@ -65,5 +79,31 @@ public class ThreadedSyncAdapter extends AbstractThreadedSyncAdapter{
//ArtefactDataSQLHelper artefactData = new ArtefactDataSQLHelper(mContext);
//artefactData.uploadAllSavedArtefacts();
if ( result.has("sync") ) {
try {
JSONObject syncObj = result.getJSONObject("sync");
Log.i(TAG, syncObj.toString());
if ( syncObj.has("activity") ) {
JSONArray notArr = syncObj.getJSONArray("activity");
for (int i=0; i<notArr.length(); i++) {
Utils.showNotification(Integer.parseInt(notArr.getJSONObject(i).getString("id")),
notArr.getJSONObject(i).getString("subject"), notArr.getJSONObject(i).getString("message"),
null, mContext);
(syncResult.stats).numUpdates++;
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Save last sync time
mPrefs.edit()
.putLong("lastsync", System.currentTimeMillis()/1000)
.commit()
;
}
}
......@@ -97,6 +97,9 @@ public class EditPreferences extends PreferenceActivity implements OnSharedPrefe
@Override
protected void onDestroy() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.unregisterOnSharedPreferenceChangeListener(this);
if(VERBOSE) Log.v(TAG, "On destroy received ... ");
// If the username and token have
if ( authDetailsChanged ) {
......@@ -105,9 +108,6 @@ public class EditPreferences extends PreferenceActivity implements OnSharedPrefe
startActivity(new Intent(this, AuthenticatorActivity.class));
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy();
}
......@@ -172,6 +172,8 @@ public class EditPreferences extends PreferenceActivity implements OnSharedPrefe
}
} else if ( key == getString(R.string.pref_auth_username_key)) {
authDetailsChanged = true;
} else if ( key == getString(R.string.pref_sync_url_key)) {
authDetailsChanged = true;
}
}
......
......@@ -164,7 +164,7 @@ public class RestClient {
return CallFunction(url, paramNames, paramVals, context);
}
public static JSONObject AuthSync(String url, String token, String username, Context context){
public static JSONObject AuthSync(String url, String token, String username, Long lastsync, Context context){
Vector<String> pNames = new Vector<String>();
Vector<String> pVals = new Vector<String>();
......@@ -176,6 +176,10 @@ public class RestClient {
pNames.add("username");
pVals.add(username);
}
if ( lastsync != null ) {
pNames.add("lastsync");
pVals.add(lastsync.toString());
}
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