mirror of https://github.com/tasks/tasks
RTM Synchronization: got authentication working. now to do the synchronize bit.
parent
79be2aed86
commit
db20f92769
@ -0,0 +1,15 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<PreferenceScreen
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
|
||||||
|
<PreferenceCategory
|
||||||
|
android:title="@string/sync_pref_group">
|
||||||
|
|
||||||
|
<CheckBoxPreference
|
||||||
|
android:key="@string/p_sync_rtm"
|
||||||
|
android:title="@string/sync_rtm_title"
|
||||||
|
android:summary="@string/sync_rtm_desc" />
|
||||||
|
|
||||||
|
</PreferenceCategory>
|
||||||
|
|
||||||
|
</PreferenceScreen>
|
||||||
@ -0,0 +1,15 @@
|
|||||||
|
package com.timsu.astrid.activities;
|
||||||
|
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.preference.PreferenceActivity;
|
||||||
|
|
||||||
|
import com.timsu.astrid.R;
|
||||||
|
|
||||||
|
public class SyncPreferences extends PreferenceActivity {
|
||||||
|
@Override
|
||||||
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
|
addPreferencesFromResource(R.xml.sync_preferences);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,119 @@
|
|||||||
|
package com.timsu.astrid.sync;
|
||||||
|
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.content.DialogInterface;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.res.Resources;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.mdt.rtm.ApplicationInfo;
|
||||||
|
import com.mdt.rtm.ServiceImpl;
|
||||||
|
import com.mdt.rtm.data.RtmList;
|
||||||
|
import com.mdt.rtm.data.RtmLists;
|
||||||
|
import com.mdt.rtm.data.RtmAuth.Perms;
|
||||||
|
import com.timsu.astrid.R;
|
||||||
|
import com.timsu.astrid.utilities.DialogUtilities;
|
||||||
|
import com.timsu.astrid.utilities.Preferences;
|
||||||
|
|
||||||
|
public class RTMSyncService implements SynchronizationService {
|
||||||
|
|
||||||
|
private ServiceImpl rtmService = null;
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
public RTMSyncService(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void synchronize(Activity activity) {
|
||||||
|
authenticate(activity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void synchronizationDisabled(Activity activity) {
|
||||||
|
Preferences.setSyncRTMToken(activity, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Perform authentication with RTM. Will open the SyncBrowser if necessary */
|
||||||
|
private void authenticate(final Activity activity) {
|
||||||
|
try {
|
||||||
|
String apiKey = "bd9883b3384a21ead17501da38bb1e68";
|
||||||
|
String sharedSecret = "a19b2a020345219b";
|
||||||
|
String appName = null;
|
||||||
|
String authToken = Preferences.getSyncRTMToken(activity);
|
||||||
|
|
||||||
|
// check if our auth token works
|
||||||
|
if(authToken != null) {
|
||||||
|
rtmService = new ServiceImpl(new ApplicationInfo(
|
||||||
|
apiKey, sharedSecret, appName, authToken));
|
||||||
|
if(!rtmService.isServiceAuthorized()) // re-do login
|
||||||
|
authToken = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(authToken == null) {
|
||||||
|
// try completing the authorization.
|
||||||
|
if(rtmService != null) {
|
||||||
|
try {
|
||||||
|
String token = rtmService.completeAuthorization();
|
||||||
|
Log.w("astrid", "got RTM token: " + token);
|
||||||
|
Preferences.setSyncRTMToken(activity, token);
|
||||||
|
performSync(activity);
|
||||||
|
|
||||||
|
return;
|
||||||
|
} catch (Exception e) {
|
||||||
|
// didn't work. do the process again.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rtmService = new ServiceImpl(new ApplicationInfo(
|
||||||
|
apiKey, sharedSecret, appName));
|
||||||
|
final String url = rtmService.beginAuthorization(Perms.delete);
|
||||||
|
|
||||||
|
Resources r = activity.getResources();
|
||||||
|
DialogUtilities.okCancelDialog(activity,
|
||||||
|
r.getString(R.string.sync_auth_request, "RTM"),
|
||||||
|
new DialogInterface.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
Intent intent = new Intent(Intent.ACTION_VIEW,
|
||||||
|
Uri.parse(url));
|
||||||
|
activity.startActivity(intent);
|
||||||
|
}
|
||||||
|
}, null);
|
||||||
|
} else {
|
||||||
|
performSync(activity);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
Synchronizer.showError(activity, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void performSync(Activity activity) {
|
||||||
|
try {
|
||||||
|
Log.i("astrid", "isAuthorized: " + rtmService.isServiceAuthorized());
|
||||||
|
RtmLists lists = rtmService.lists_getList();
|
||||||
|
for(Entry<String, RtmList> list : lists.getLists().entrySet()) {
|
||||||
|
Log.i("astrid", "look, " + list.getKey());
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetch tasks that've changed since last sync
|
||||||
|
|
||||||
|
// grab my own list of tasks that have changed since last sync
|
||||||
|
|
||||||
|
|
||||||
|
// if we find a conflict... remember and ignore
|
||||||
|
|
||||||
|
|
||||||
|
// update tasks that have changed
|
||||||
|
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
Synchronizer.showError(activity, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
package com.timsu.astrid.sync;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
|
||||||
|
/** A service that synchronizes with Astrid
|
||||||
|
*
|
||||||
|
* @author timsu
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public interface SynchronizationService {
|
||||||
|
|
||||||
|
/** Synchronize with the service */
|
||||||
|
void synchronize(Activity activity);
|
||||||
|
|
||||||
|
/** Called when synchronization with this service is turned off */
|
||||||
|
void synchronizationDisabled(Activity activity);
|
||||||
|
}
|
||||||
@ -1,71 +1,62 @@
|
|||||||
package com.timsu.astrid.sync;
|
package com.timsu.astrid.sync;
|
||||||
|
|
||||||
import java.util.Map.Entry;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Intent;
|
import android.content.Context;
|
||||||
import android.net.Uri;
|
import android.content.DialogInterface;
|
||||||
|
import android.content.res.Resources;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.mdt.rtm.ApplicationInfo;
|
import com.timsu.astrid.R;
|
||||||
import com.mdt.rtm.ServiceImpl;
|
import com.timsu.astrid.utilities.DialogUtilities;
|
||||||
import com.mdt.rtm.data.RtmList;
|
import com.timsu.astrid.utilities.Preferences;
|
||||||
import com.mdt.rtm.data.RtmLists;
|
|
||||||
import com.mdt.rtm.data.RtmAuth.Perms;
|
|
||||||
import com.timsu.astrid.utilities.Constants;
|
|
||||||
|
|
||||||
public class Synchronizer {
|
public class Synchronizer {
|
||||||
|
|
||||||
private static ServiceImpl rtmService = null;
|
/* Synchronization Service ID's */
|
||||||
|
private static final int SYNC_ID_RTM = 1;
|
||||||
|
|
||||||
public static void authenticate(Activity activity) {
|
/** Service map */
|
||||||
try {
|
private static Map<Integer, SynchronizationService> services =
|
||||||
String apiKey = "127d19adab1a7b6922d8dfda3ef09645";
|
new HashMap<Integer, SynchronizationService>();
|
||||||
String sharedSecret = "503816890a685753";
|
static {
|
||||||
String appName = null;
|
services.put(SYNC_ID_RTM, new RTMSyncService(SYNC_ID_RTM));
|
||||||
String authToken = null;
|
|
||||||
|
|
||||||
// check if our auth token works
|
|
||||||
if(authToken != null) {
|
|
||||||
rtmService = new ServiceImpl(new ApplicationInfo(
|
|
||||||
apiKey, sharedSecret, appName, authToken));
|
|
||||||
if(!rtmService.isServiceAuthorized()) // re-do login
|
|
||||||
authToken = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(authToken == null) {
|
// --- public interface
|
||||||
rtmService = new ServiceImpl(new ApplicationInfo(
|
|
||||||
apiKey, sharedSecret, appName));
|
|
||||||
String url = rtmService.beginAuthorization(Perms.delete);
|
|
||||||
|
|
||||||
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
|
/** Synchronize all activated sync services */
|
||||||
Uri.parse(url));
|
public static void synchronize(Activity activity) {
|
||||||
activity.startActivityForResult(browserIntent,
|
// RTM sync
|
||||||
Constants.RESULT_SYNCHRONIZE);
|
if(Preferences.shouldSyncRTM(activity)) {
|
||||||
} else {
|
services.get(SYNC_ID_RTM).synchronize(activity);
|
||||||
performSync(activity, false);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
/** Clears tokens if services are disabled */
|
||||||
Log.e("astrid", "error parsing", e); // TODO dialog box
|
public static void synchronizerStatusUpdated(Activity activity) {
|
||||||
|
if(!Preferences.shouldSyncRTM(activity)) {
|
||||||
|
services.get(SYNC_ID_RTM).synchronizationDisabled(activity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void performSync(Activity activity, boolean justLoggedIn) {
|
// --- package utilities
|
||||||
try {
|
|
||||||
// store token
|
|
||||||
if(justLoggedIn) {
|
|
||||||
String token = rtmService.completeAuthorization();
|
|
||||||
Log.w("astrid", "LOOK A TOKEN " + token);
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("isAuthorized: " + rtmService.isServiceAuthorized());
|
/** Utility class for showing synchronization errors */
|
||||||
RtmLists lists = rtmService.lists_getList();
|
static void showError(Context context, Throwable e) {
|
||||||
for(Entry<String, RtmList> list : lists.getLists().entrySet()) {
|
Log.e("astrid", "Synchronization Error", e);
|
||||||
Log.i("astrid", "look, " + list.getKey());
|
|
||||||
}
|
Resources r = context.getResources();
|
||||||
} catch (Exception e) {
|
DialogUtilities.okDialog(context,
|
||||||
Log.e("astrid", "error parsing", e); // TODO dialog box
|
r.getString(R.string.sync_error) + " " +
|
||||||
|
e.getLocalizedMessage(), new DialogInterface.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
// do nothing?
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue