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;
|
||||
|
||||
import java.util.Map.Entry;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.res.Resources;
|
||||
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.utilities.Constants;
|
||||
import com.timsu.astrid.R;
|
||||
import com.timsu.astrid.utilities.DialogUtilities;
|
||||
import com.timsu.astrid.utilities.Preferences;
|
||||
|
||||
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) {
|
||||
try {
|
||||
String apiKey = "127d19adab1a7b6922d8dfda3ef09645";
|
||||
String sharedSecret = "503816890a685753";
|
||||
String appName = null;
|
||||
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;
|
||||
}
|
||||
/** Service map */
|
||||
private static Map<Integer, SynchronizationService> services =
|
||||
new HashMap<Integer, SynchronizationService>();
|
||||
static {
|
||||
services.put(SYNC_ID_RTM, new RTMSyncService(SYNC_ID_RTM));
|
||||
}
|
||||
|
||||
if(authToken == null) {
|
||||
rtmService = new ServiceImpl(new ApplicationInfo(
|
||||
apiKey, sharedSecret, appName));
|
||||
String url = rtmService.beginAuthorization(Perms.delete);
|
||||
// --- public interface
|
||||
|
||||
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
|
||||
Uri.parse(url));
|
||||
activity.startActivityForResult(browserIntent,
|
||||
Constants.RESULT_SYNCHRONIZE);
|
||||
} else {
|
||||
performSync(activity, false);
|
||||
}
|
||||
/** Synchronize all activated sync services */
|
||||
public static void synchronize(Activity activity) {
|
||||
// RTM sync
|
||||
if(Preferences.shouldSyncRTM(activity)) {
|
||||
services.get(SYNC_ID_RTM).synchronize(activity);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.e("astrid", "error parsing", e); // TODO dialog box
|
||||
/** Clears tokens if services are disabled */
|
||||
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) {
|
||||
try {
|
||||
// store token
|
||||
if(justLoggedIn) {
|
||||
String token = rtmService.completeAuthorization();
|
||||
Log.w("astrid", "LOOK A TOKEN " + token);
|
||||
}
|
||||
// --- package utilities
|
||||
|
||||
/** Utility class for showing synchronization errors */
|
||||
static void showError(Context context, Throwable e) {
|
||||
Log.e("astrid", "Synchronization Error", e);
|
||||
|
||||
System.out.println("isAuthorized: " + rtmService.isServiceAuthorized());
|
||||
RtmLists lists = rtmService.lists_getList();
|
||||
for(Entry<String, RtmList> list : lists.getLists().entrySet()) {
|
||||
Log.i("astrid", "look, " + list.getKey());
|
||||
Resources r = context.getResources();
|
||||
DialogUtilities.okDialog(context,
|
||||
r.getString(R.string.sync_error) + " " +
|
||||
e.getLocalizedMessage(), new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
// do nothing?
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e("astrid", "error parsing", e); // TODO dialog box
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue