Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

moved dropbox classes into remote

made OfflineRemoteClient
deleted RemoteFactory and RemoteTaskRepository and DropboxRemoteTaskRepository
created RemoteClientManager to handle switching of clients
started working on the login/logout issue but it is not complete (by far)
  • Loading branch information...
commit ae29a577d5cbf51c5a9ce83c900bd5a0e54b929c 1 parent a4e6d4f
@timbarlotta authored
Showing with 353 additions and 354 deletions.
  1. +5 −1 res/values/strings.xml
  2. +2 −0  res/xml/preferences.xml
  3. +9 −7 src/com/todotxt/todotxttouch/LoginScreen.java
  4. +22 −12 src/com/todotxt/todotxttouch/Preferences.java
  5. +7 −35 src/com/todotxt/todotxttouch/TodoApplication.java
  6. +7 −20 src/com/todotxt/todotxttouch/TodoTxtTouch.java
  7. +3 −3 src/com/todotxt/todotxttouch/remote/{dropbox → }/DropboxFileRemoteException.java
  8. +30 −34 src/com/todotxt/todotxttouch/remote/{dropbox → }/DropboxLoginAsyncTask.java
  9. +98 −44 src/com/todotxt/todotxttouch/remote/{dropbox → }/DropboxRemoteClient.java
  10. +49 −0 src/com/todotxt/todotxttouch/remote/OfflineRemoteClient.java
  11. +19 −3 src/com/todotxt/todotxttouch/remote/RemoteClient.java
  12. +76 −0 src/com/todotxt/todotxttouch/remote/RemoteClientManager.java
  13. +0 −16 src/com/todotxt/todotxttouch/remote/RemoteFactory.java
  14. +0 −47 src/com/todotxt/todotxttouch/remote/RemoteTaskRepository.java
  15. +0 −82 src/com/todotxt/todotxttouch/remote/dropbox/DropboxRemoteTaskRepository.java
  16. +0 −7 src/com/todotxt/todotxttouch/task/TaskBag.java
  17. +3 −7 src/com/todotxt/todotxttouch/task/TaskBagFactory.java
  18. +23 −36 src/com/todotxt/todotxttouch/task/TaskBagImpl.java
View
6 res/values/strings.xml
@@ -81,6 +81,10 @@ You should have received a copy of the GNU General Public License along with Tod
<string name="dropbox_logout_pref_summary">Log out of Dropbox.</string>
<string name="dropbox_logout_explainer">Are you sure you wish to log out of Dropbox?
</string>
+
+ <string name="dropbox_login_pref_title">Log in</string>
+ <string name="dropbox_login_pref_summary">Log into Dropbox.</string>
+
<string name="TODOTXTPATH_key">todotxtpath</string>
<string name="TODOTXTPATH_title">File location</string>
<string name="TODOTXTPATH_summary">The Dropbox folder where todo.txt is stored.
@@ -132,7 +136,7 @@ You should have received a copy of the GNU General Public License along with Tod
<string name="priority_prompt">Priority</string>
<string name="project_prompt">Project</string>
<string name="context_prompt">Context</string>
-
+
<!-- launcher shortcut -->
<string name="shortcut_addtask_name">Add Task</string>
<!-- share -->
View
2  res/xml/preferences.xml
@@ -47,6 +47,8 @@ You should have received a copy of the GNU General Public License along with Tod
<EditTextPreference android:summary="@string/TODOTXTPATH_summary"
android:defaultValue="@string/TODOTXTPATH_defaultPath" android:title="@string/TODOTXTPATH_title"
android:key="@string/TODOTXTPATH_key" />
+ <Preference android:key="login_dropbox" android:title="@string/dropbox_login_pref_title"
+ android:summary="@string/dropbox_login_pref_summary"/>
<Preference android:key="logout_dropbox" android:title="@string/dropbox_logout_pref_title"
android:summary="@string/dropbox_logout_pref_summary" />
</PreferenceCategory>
View
16 src/com/todotxt/todotxttouch/LoginScreen.java
@@ -35,12 +35,13 @@
import android.view.View.OnClickListener;
import android.widget.Button;
+import com.todotxt.todotxttouch.remote.RemoteClientManager;
import com.todotxt.todotxttouch.remote.RemoteLoginTask;
import com.todotxt.todotxttouch.remote.RemoteClient;
public class LoginScreen extends Activity {
- final static String TAG = TodoTxtTouch.class.getSimpleName();
+ final static String TAG = LoginScreen.class.getSimpleName();
private TodoApplication m_app;
private Button m_LoginButton;
@@ -76,7 +77,7 @@ public void onClick(View v) {
}
});
- final RemoteClient remoteClient = m_app.getRemoteClient();
+ final RemoteClient remoteClient = m_app.getRemoteClientManager().getRemoteClient();
if (remoteClient.isAuthenticated()) {
switchToTodolist();
}
@@ -95,15 +96,16 @@ protected void onDestroy() {
}
void login() {
- final RemoteClient client = m_app.getRemoteClient();
-
- if (client.isLoggedIn()) {
- if ( m_app.authenticate() ) {
+ //TODO do this more generically
+ final RemoteClient remoteClient = m_app.getRemoteClientManager().getRemoteClient(RemoteClientManager.SupportedClient.DROPBOX);
+
+ if (remoteClient.isLoggedIn()) {
+ if ( remoteClient.authenticate() ) {
switchToTodolist();
}
}
- RemoteLoginTask loginTask = client.getLoginTask();
+ RemoteLoginTask loginTask = remoteClient.getLoginTask();
loginTask.showLoginDialog(this);
}
View
34 src/com/todotxt/todotxttouch/Preferences.java
@@ -35,6 +35,7 @@
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
+import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
@@ -46,8 +47,10 @@
private Preference aboutDialog;
private Preference logoutDialog;
+ private Preference loginDialog;
private static final int ABOUT_DIALOG = 1;
private static final int LOGOUT_DIALOG = 2;
+ private static final int LOGIN_DIALOG = 3;
public static final int RESULT_SYNC_LIST = 2;
private String version;
@@ -60,7 +63,7 @@ protected void onCreate(Bundle savedInstanceState) {
try {
packageInfo = getPackageManager().getPackageInfo(getPackageName(),
0);
- Preference versionPref = (Preference) findPreference("app_version");
+ Preference versionPref = findPreference("app_version");
versionPref.setSummary("v" + packageInfo.versionName);
version = packageInfo.versionName;
@@ -69,6 +72,7 @@ protected void onCreate(Bundle savedInstanceState) {
}
aboutDialog = findPreference("app_version");
logoutDialog = findPreference("logout_dropbox");
+ loginDialog = findPreference("login_dropbox");
setOnlineOfflinePrefs();
}
@@ -78,10 +82,12 @@ private void setOnlineOfflinePrefs() {
if (appPreferences.getBoolean("workofflinepref", false)) {
findPreference("todotxtpath").setEnabled(false);
findPreference("logout_dropbox").setEnabled(false);
+ findPreference("login_dropbox").setEnabled(true);
Log.v(TAG, "Work offline is checked, disabling online settings");
} else {
findPreference("todotxtpath").setEnabled(true);
findPreference("logout_dropbox").setEnabled(true);
+ findPreference("login_dropbox").setEnabled(false);
Log.v(TAG, "Work offline is not checked, enabling online settings");
}
}
@@ -99,6 +105,12 @@ public boolean onPreferenceTreeClick(PreferenceScreen screen,
} else if (preference == logoutDialog) {
showDialog(LOGOUT_DIALOG);
}
+ else if (preference == loginDialog) {
+ Intent broadcastLoginIntent = new Intent();
+ broadcastLoginIntent.setAction("com.todotxt.todotxttouch.ACTION_LOGIN");
+ sendBroadcast(broadcastLoginIntent);
+ finish();
+ }
setOnlineOfflinePrefs();
return true;
}
@@ -127,7 +139,8 @@ public void onClick(DialogInterface arg0, int arg1) {
}
});
return aboutAlert.show();
- } else if (id == LOGOUT_DIALOG) {
+ }
+ else if (id == LOGOUT_DIALOG) {
AlertDialog.Builder logoutAlert = new AlertDialog.Builder(this);
logoutAlert.setTitle(R.string.areyousure);
logoutAlert.setMessage(R.string.dropbox_logout_explainer);
@@ -135,17 +148,14 @@ public void onClick(DialogInterface arg0, int arg1) {
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
+ ((CheckBoxPreference)findPreference("workofflinepref")).setChecked(true);
+ findPreference("workofflinepref").setEnabled(false);
+ findPreference("workofflinepref").setSelectable(false);
+ findPreference("logout_dropbox").setSelectable(false);
((TodoApplication) getApplication())
- .unlinkRemoteClient();
- Preferences.this.setResult(RESULT_SYNC_LIST);
-
- // produce a logout intent and broadcast it
- Intent broadcastLogoutIntent = new Intent();
- broadcastLogoutIntent
- .setAction("com.todotxt.todotxttouch.ACTION_LOGOUT");
- sendBroadcast(broadcastLogoutIntent);
-
- Preferences.this.finish();
+ .getRemoteClientManager().getRemoteClient().unauthorize();
+ setResult(RESULT_SYNC_LIST);
+ finish();
}
});
logoutAlert.setNegativeButton(R.string.cancel, null);
View
42 src/com/todotxt/todotxttouch/TodoApplication.java
@@ -32,44 +32,23 @@
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
-
-import com.todotxt.todotxttouch.remote.RemoteClient;
-import com.todotxt.todotxttouch.remote.RemoteFactory;
+import com.todotxt.todotxttouch.remote.RemoteClientManager;
import com.todotxt.todotxttouch.task.TaskBag;
import com.todotxt.todotxttouch.task.TaskBagFactory;
public class TodoApplication extends Application {
-
private final static String TAG = TodoApplication.class.getSimpleName();
-
public SharedPreferences m_prefs;
-
- private RemoteClient remoteClient;
-
public boolean m_syncing = false;
-
+ private RemoteClientManager remoteClientManager;
private TaskBag taskBag;
@Override
public void onCreate() {
super.onCreate();
-
m_prefs = PreferenceManager.getDefaultSharedPreferences(this);
-
- remoteClient = RemoteFactory.getRemoteClient(this);
-
- authenticate();
-
- Log.v(TAG,
- "Work offline set to : "
- + m_prefs.getBoolean("workofflinepref", false));
-
- this.taskBag = TaskBagFactory.getTaskBag(this, m_prefs, getResources()
- .getString(R.string.TODOTXTPATH_defaultPath));
- }
-
- public boolean authenticate() {
- return remoteClient.authenticate();
+ remoteClientManager = new RemoteClientManager(this, m_prefs);
+ this.taskBag = TaskBagFactory.getTaskBag(this, m_prefs);
}
@Override
@@ -77,18 +56,11 @@ public void onTerminate() {
super.onTerminate();
}
- public void unlinkRemoteClient() {
- Log.i(TAG, "Clearing current user data!");
- remoteClient.deauthenticate();
- taskBag.disconnectFromRemote();
- }
+ public RemoteClientManager getRemoteClientManager() {
+ return remoteClientManager;
+ }
public TaskBag getTaskBag() {
return taskBag;
}
-
- public RemoteClient getRemoteClient() {
- return remoteClient;
- }
-
}
View
27 src/com/todotxt/todotxttouch/TodoTxtTouch.java
@@ -86,8 +86,6 @@
OnSharedPreferenceChangeListener {
final static String TAG = TodoTxtTouch.class.getSimpleName();
-
- private final static String INTENT_ACTION_LOGOUT = "com.todotxt.todotxttouch.ACTION_LOGOUT";
private final static String INTENT_ASYNC_SUCCESS = "com.todotxt.todotxttouch.ASYNC_SUCCESS";
private final static String INTENT_ASYNC_FAILED = "com.todotxt.todotxttouch.ASYNC_FAILED";
@@ -98,12 +96,12 @@
private static TodoTxtTouch currentActivityPointer = null;
private TaskBag taskBag;
- ProgressDialog m_ProgressDialog = null;
- String m_DialogText = "";
- Boolean m_DialogActive = false;
+ private ProgressDialog m_ProgressDialog = null;
+ private String m_DialogText = "";
+ private Boolean m_DialogActive = false;
private TaskAdapter m_adapter;
- TodoApplication m_app;
+ private TodoApplication m_app;
// filter variables
private ArrayList<Priority> m_prios = new ArrayList<Priority>();
@@ -133,18 +131,13 @@ public void onCreate(Bundle savedInstanceState) {
// listen to the ACTION_LOGOUT intent, if heard display LoginScreen
// and finish() current activity
IntentFilter intentFilter = new IntentFilter();
- intentFilter.addAction(INTENT_ACTION_LOGOUT);
intentFilter.addAction(INTENT_ASYNC_SUCCESS);
intentFilter.addAction(INTENT_ASYNC_FAILED);
m_broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
- if (intent.getAction().equalsIgnoreCase(INTENT_ACTION_LOGOUT)) {
- Intent i = new Intent(context, LoginScreen.class);
- startActivity(i);
- finish();
- } else if (intent.getAction().equalsIgnoreCase(
+ if (intent.getAction().equalsIgnoreCase(
INTENT_ASYNC_SUCCESS)
|| intent.getAction().equalsIgnoreCase(
INTENT_ASYNC_FAILED)) {
@@ -221,6 +214,7 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
"Switched online/offline mode. Push local changes if necessary.");
taskBag.pushToRemote();
}
+
if (sharedPreferences.getBoolean("workofflinepref", false)) {
findViewById(R.id.btn_title_refresh).setVisibility(View.GONE);
} else {
@@ -733,7 +727,7 @@ private void updateRefreshStatus() {
* TODO: This needs to be nicer
*/
void populateFromExternal() {
- if (m_app.getRemoteClient().isAuthenticated()) {
+ if (m_app.getRemoteClientManager().getRemoteClient().isAuthenticated()) {
m_app.m_syncing = true;
updateRefreshStatus();
@@ -863,13 +857,6 @@ public View getView(int position, View convertView, ViewGroup parent) {
private TextView tasktext;
}
- public void storeKeys(String accessTokenKey, String accessTokenSecret) {
- Editor editor = m_app.m_prefs.edit();
- editor.putString(Constants.PREF_ACCESSTOKEN_KEY, accessTokenKey);
- editor.putString(Constants.PREF_ACCESSTOKEN_SECRET, accessTokenSecret);
- editor.commit();
- }
-
public void showToast(String string) {
Util.showToastLong(this, string);
}
View
6 ...ch/remote/dropbox/DropboxFileRemoteException.java → ...dotxttouch/remote/DropboxFileRemoteException.java
@@ -24,18 +24,18 @@
* @copyright 2011 Tim Barlotta
*/
-package com.todotxt.todotxttouch.remote.dropbox;
+package com.todotxt.todotxttouch.remote;
import com.dropbox.client.DropboxAPI;
import com.todotxt.todotxttouch.remote.RemoteException;
/**
* Represents an error that occurred during Dropbox persistence
- *
+ *
* @author Tim Barlotta
*/
@SuppressWarnings("serial")
-public class DropboxFileRemoteException extends RemoteException {
+class DropboxFileRemoteException extends RemoteException {
private DropboxAPI.FileDownload dropboxFileDownload;
public DropboxFileRemoteException(String message,
View
64 ...xttouch/remote/dropbox/DropboxLoginAsyncTask.java → ...xt/todotxttouch/remote/DropboxLoginAsyncTask.java
@@ -23,13 +23,14 @@
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2009-2011 Tormod Haugen
*/
-package com.todotxt.todotxttouch.remote.dropbox;
+package com.todotxt.todotxttouch.remote;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
+import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.text.Html;
@@ -44,40 +45,31 @@
import com.todotxt.todotxttouch.Constants;
import com.todotxt.todotxttouch.R;
import com.todotxt.todotxttouch.TodoApplication;
-import com.todotxt.todotxttouch.remote.RemoteLoginTask;
import com.todotxt.todotxttouch.util.Util;
-public class DropboxLoginAsyncTask extends AsyncTask<Void, Void, Integer>
+class DropboxLoginAsyncTask extends AsyncTask<Void, Void, Integer>
implements RemoteLoginTask {
- private TodoApplication m_app;
- private DropboxRemoteClient client;
+ private DropboxRemoteClient dropboxRemoteClient;
+ private SharedPreferences sharedPreferences;
private String m_username;
private String m_password;
- public void setUsername(String username) {
- m_username = username;
- }
-
- public void setPassword(String password) {
- m_password = password;
- }
-
- public DropboxLoginAsyncTask(TodoApplication act) {
- m_app = act;
- client = (DropboxRemoteClient) act.getRemoteClient();
+ public DropboxLoginAsyncTask(DropboxRemoteClient dropboxRemoteClient, SharedPreferences sharedPreferences) {
+ this.dropboxRemoteClient = dropboxRemoteClient;
+ this.sharedPreferences = sharedPreferences;
}
@Override
protected Integer doInBackground(Void... params) {
- if (!client.isAuthenticated()) {
- client.login(m_username, m_password);
+ if (!dropboxRemoteClient.isAuthenticated()) {
+ dropboxRemoteClient.login(m_username, m_password);
- final Config config = client.getConfig();
+ final Config config = dropboxRemoteClient.getConfig();
if (config.authStatus != DropboxAPI.STATUS_SUCCESS)
return config.authStatus;
}
- DropboxAPI api = client.getAPI();
+ DropboxAPI api = dropboxRemoteClient.getDropboxApi();
Log.d("Xtra", "" + api.accountInfo());
if (!api.accountInfo().isError()) {
return DropboxAPI.STATUS_SUCCESS;
@@ -89,33 +81,29 @@ protected Integer doInBackground(Void... params) {
@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
- final Config config = client.getConfig();
+ final Config config = dropboxRemoteClient.getConfig();
if (result == DropboxAPI.STATUS_SUCCESS) {
if (null != config
&& config.authStatus == DropboxAPI.STATUS_SUCCESS) {
storeKeys(config.accessTokenKey, config.accessTokenSecret);
- showToast("Logged into Dropbox");
+ dropboxRemoteClient.showToast("Logged into Dropbox");
Intent broadcastLoginIntent = new Intent();
broadcastLoginIntent
.setAction("com.todotxt.todotxttouch.ACTION_LOGIN");
- m_app.sendBroadcast(broadcastLoginIntent);
+ dropboxRemoteClient.sendBroadcast(broadcastLoginIntent);
}
} else {
if (result == DropboxAPI.STATUS_NETWORK_ERROR) {
- showToast("Network error: " + config.authDetail);
+ dropboxRemoteClient.showToast("Network error: " + config.authDetail);
} else {
- showToast("Unsuccessful login.");
+ dropboxRemoteClient.showToast("Unsuccessful login.");
}
}
}
- public void showToast(String string) {
- Util.showToastLong(m_app, string);
- }
-
public void storeKeys(String accessTokenKey, String accessTokenSecret) {
- Editor editor = m_app.m_prefs.edit();
+ Editor editor = sharedPreferences.edit();
editor.putString(Constants.PREF_ACCESSTOKEN_KEY, accessTokenKey);
editor.putString(Constants.PREF_ACCESSTOKEN_SECRET, accessTokenSecret);
editor.commit();
@@ -143,14 +131,22 @@ public void onClick(DialogInterface dialog, int which) {
String u = usernameTV.getText().toString();
String p = passwordTV.getText().toString();
if (u != null && u.length() > 0 && p != null && p.length() > 0) {
- DropboxLoginAsyncTask.this.setUsername(u);
- DropboxLoginAsyncTask.this.setPassword(p);
- DropboxLoginAsyncTask.this.execute();
+ setUsername(u);
+ setPassword(p);
+ execute();
} else {
- DropboxLoginAsyncTask.this.cancel(false);
+ cancel(false);
}
}
});
b.show();
}
+
+ private void setUsername(String username) {
+ m_username = username;
+ }
+
+ private void setPassword(String password) {
+ m_password = password;
+ }
}
View
142 ...otxttouch/remote/dropbox/DropboxRemoteClient.java → ...otxt/todotxttouch/remote/DropboxRemoteClient.java
@@ -26,43 +26,46 @@
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2009-2011 mathias, Gina Trapani, Tormod Haugen, Tim Barlotta
*/
-package com.todotxt.todotxttouch.remote.dropbox;
+package com.todotxt.todotxttouch.remote;
+import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
-import android.preference.PreferenceManager;
-
+import android.os.Environment;
import com.dropbox.client.DropboxAPI;
import com.dropbox.client.DropboxAPI.Config;
import com.todotxt.todotxttouch.Constants;
import com.todotxt.todotxttouch.R;
import com.todotxt.todotxttouch.TodoApplication;
-import com.todotxt.todotxttouch.remote.RemoteClient;
+import com.todotxt.todotxttouch.util.Util;
-public class DropboxRemoteClient implements RemoteClient {
- private static final String TODO_TXT_REMOTE_FILE_NAME = "todo.txt";
+import java.io.File;
+import java.io.IOException;
- private DropboxAPI api = new DropboxAPI();
+class DropboxRemoteClient implements RemoteClient {
+ private static final String TODO_TXT_REMOTE_FILE_NAME = "todo.txt";
+ private static final File TODO_TXT_TMP_FILE = new File(
+ Environment.getExternalStorageDirectory(),
+ "data/com.todotxt.todotxttouch/tmp/todo.txt");
+ private DropboxAPI dropboxApi = new DropboxAPI();
private Config config = null;
+ private TodoApplication todoApplication;
+ private SharedPreferences sharedPreferences;
- private TodoApplication app;
-
- private SharedPreferences preferences;
-
- public DropboxRemoteClient(TodoApplication todoApplication) {
- app = todoApplication;
- preferences = PreferenceManager.getDefaultSharedPreferences(app);
+ public DropboxRemoteClient(TodoApplication todoApplication, SharedPreferences sharedPreferences) {
+ this.todoApplication = todoApplication;
+ this.sharedPreferences = sharedPreferences;
}
/**
* Get the stored key - secret pair for authenticating the user
- *
+ *
* @return a string array with key and secret
*/
private String[] getAuthToken() {
String[] keys = { null, null };
- keys[0] = preferences.getString(Constants.PREF_ACCESSTOKEN_KEY, null);
- keys[1] = preferences
+ keys[0] = sharedPreferences.getString(Constants.PREF_ACCESSTOKEN_KEY, null);
+ keys[1] = sharedPreferences
.getString(Constants.PREF_ACCESSTOKEN_SECRET, null);
return keys;
}
@@ -72,37 +75,44 @@ public DropboxRemoteClient(TodoApplication todoApplication) {
* it
*/
private void clearAuthToken() {
- Editor editor = preferences.edit();
+ Editor editor = sharedPreferences.edit();
editor.clear();
editor.commit();
}
@Override
public boolean authenticate() {
- if (null == config)
+ if (null == config) {
createConfig();
+ }
String[] userAuthToken = getAuthToken();
if (isLoggedIn()) {
- config = api.authenticateToken(userAuthToken[0], userAuthToken[1],
+ config = dropboxApi.authenticateToken(userAuthToken[0], userAuthToken[1],
config);
- if (null != config)
+ if (null != config) {
return true;
+ }
}
clearAuthToken();
return false;
}
+ @Override
+ public void unauthorize() {
+ clearAuthToken();
+ deauthenticate();
+ }
+
@Override
public void deauthenticate() {
- clearAuthToken();
- api.deauthenticate();
+ dropboxApi.deauthenticate();
}
@Override
public boolean isAuthenticated() {
- return api.isAuthenticated();
+ return dropboxApi.isAuthenticated();
}
/**
@@ -110,12 +120,12 @@ public boolean isAuthenticated() {
* Config object.
*/
private void createConfig() {
- String consumerKey = app.getResources()
+ String consumerKey = todoApplication.getResources()
.getText(R.string.dropbox_consumer_key).toString();
- String consumerSecret = app.getText(R.string.dropbox_consumer_secret)
+ String consumerSecret = todoApplication.getText(R.string.dropbox_consumer_secret)
.toString();
- config = api.getConfig(null, false);
+ config = dropboxApi.getConfig(null, false);
config.consumerKey = consumerKey;
config.consumerSecret = consumerSecret;
config.server = "api.dropbox.com";
@@ -139,13 +149,11 @@ Config getConfig() {
* Method enabling logging in with a username and password. Do not store the
* username or password. Config object <code>config</code> will contain user
* key and secret to authenticate w/o username/password later.
- *
+ *
* @param username
* - email registered with dropbox
* @param password
* - password to authenticate with dropbox
- * @param consumer_auth_keys
- * - the key and secret for this application.
* @return true if authenticated, false if not authenticated.
*/
public boolean login(String username, String password) {
@@ -154,7 +162,7 @@ public boolean login(String username, String password) {
if (username != null && username.length() > 0 && password != null
&& password.length() > 0) {
- config = api.authenticate(config, username, password);
+ config = dropboxApi.authenticate(config, username, password);
if (config != null)
return true;
}
@@ -162,21 +170,67 @@ public boolean login(String username, String password) {
return false;
}
- DropboxAPI getAPI() {
- return api;
+ @Override
+ public File pull( ){
+ DropboxAPI.FileDownload fileDownload = dropboxApi.getFileStream(
+ Constants.DROPBOX_MODUS, getRemotePathAndFilename(), null);
+ if (fileDownload.isError()) {
+ if (404 == fileDownload.httpCode) {
+ push(TODO_TXT_TMP_FILE);
+ return TODO_TXT_TMP_FILE;
+ } else {
+ throw new DropboxFileRemoteException(
+ "Error loading from dropbox", fileDownload);
+ }
+ }
+
+ try {
+ Util.writeFile(fileDownload.is, TODO_TXT_TMP_FILE);
+ return TODO_TXT_TMP_FILE;
+ }catch(IOException e) {
+ throw new RemoteException("Error writing to tmp file", e);
+ }
+ }
+
+ @Override
+ public void push(File file) {
+ try {
+ if(!file.exists()) {
+ Util.createParentDirectory(file);
+ file.createNewFile();
+ }
+ }
+ catch(IOException e) {
+ throw new RemoteException("Failed to ensure that file exists", e);
+ }
+
+ dropboxApi.putFile(Constants.DROPBOX_MODUS, getRemotePath(), file);
+ }
+
+ private String getRemotePath() {
+ return sharedPreferences.getString("todotxtpath", todoApplication.getResources()
+ .getString(R.string.TODOTXTPATH_defaultPath));
+ }
+
+ private String getRemotePathAndFilename() {
+ return getRemotePath() + "/" + TODO_TXT_REMOTE_FILE_NAME;
+ }
+
+
+
+ void sendBroadcast(Intent intent) {
+ todoApplication.sendBroadcast(intent);
+ }
+
+ void showToast(String string) {
+ Util.showToastLong(todoApplication, string);
+ }
+
+ DropboxAPI getDropboxApi() {
+ return dropboxApi;
}
public DropboxLoginAsyncTask getLoginTask() {
- return new DropboxLoginAsyncTask(app);
- }
-
- String getRemotePath() {
- return preferences.getString("todotxtpath", app.getResources()
- .getString(R.string.TODOTXTPATH_defaultPath));
- }
-
- String getRemotePathAndFilename() {
- return getRemotePath() + "/" + TODO_TXT_REMOTE_FILE_NAME;
+ return new DropboxLoginAsyncTask(this, sharedPreferences);
}
-
}
View
49 src/com/todotxt/todotxttouch/remote/OfflineRemoteClient.java
@@ -0,0 +1,49 @@
+package com.todotxt.todotxttouch.remote;
+
+import java.io.File;
+
+/**
+ * Class description...
+ *
+ * @author Tim Barlotta
+ */
+public class OfflineRemoteClient implements RemoteClient {
+ @Override
+ public void unauthorize() {
+
+ }
+
+ @Override
+ public boolean authenticate() {
+ return true;
+ }
+
+ @Override
+ public void deauthenticate() {
+ //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ @Override
+ public boolean isAuthenticated() {
+ return true;
+ }
+
+ @Override
+ public boolean isLoggedIn() {
+ return true;
+ }
+
+ @Override
+ public RemoteLoginTask getLoginTask() {
+ return null;
+ }
+
+ @Override
+ public File pull() {
+ return null; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ @Override
+ public void push(File file) {
+ }
+}
View
22 src/com/todotxt/todotxttouch/remote/RemoteClient.java
@@ -25,11 +25,15 @@
*/
package com.todotxt.todotxttouch.remote;
+import java.io.File;
+
public interface RemoteClient {
+ public void unauthorize();
+
/**
* Attempts to authenticate with remote api
- *
+ *
* @return true if successful
*/
public boolean authenticate();
@@ -41,7 +45,7 @@
/**
* Check to see if we are authenticated with remote api
- *
+ *
* @return true if authenticated
*/
public boolean isAuthenticated();
@@ -49,7 +53,7 @@
/**
* Check to see if we have enough information to authenticate with remote
* api
- *
+ *
* @return true if we have authToken, false if we need login information
*/
public boolean isLoggedIn();
@@ -60,4 +64,16 @@
*/
public RemoteLoginTask getLoginTask();
+ /**
+ * Pull the remote todo.txt file
+ * @return
+ */
+ File pull();
+
+ /**
+ * Push mobile
+ * @param file
+ */
+ void push(File file);
+
}
View
76 src/com/todotxt/todotxttouch/remote/RemoteClientManager.java
@@ -0,0 +1,76 @@
+package com.todotxt.todotxttouch.remote;
+
+import android.content.SharedPreferences;
+import android.util.Log;
+import com.todotxt.todotxttouch.TodoApplication;
+
+/**
+ * Class description...
+ *
+ * @author Tim Barlotta
+ */
+public class RemoteClientManager implements SharedPreferences.OnSharedPreferenceChangeListener {
+ private final static String TAG = RemoteClientManager.class.getSimpleName();
+ private SupportedClient currentClientToken;
+ private RemoteClient currentClient;
+ private TodoApplication todoApplication;
+ private SharedPreferences sharedPreferences;
+
+ public RemoteClientManager(TodoApplication todoApplication, SharedPreferences sharedPreferences) {
+ this.todoApplication = todoApplication;
+ sharedPreferences.registerOnSharedPreferenceChangeListener(this);
+ this.sharedPreferences = sharedPreferences;
+ calculateRemoteClient(sharedPreferences);
+ currentClient.authenticate();
+ }
+
+ public RemoteClient getRemoteClient() {
+ return currentClient;
+ }
+
+ /**
+ * Returns the client associated with the passed in token does not switch the client
+ * @param clientToken
+ * @return
+ */
+ public RemoteClient getRemoteClient(SupportedClient clientToken) {
+ //TODO more sophisticated mechanism here
+ RemoteClient client = null;
+ switch(clientToken) {
+ case DROPBOX:
+ client = new DropboxRemoteClient(todoApplication, sharedPreferences);
+ break;
+ default:
+ client = new OfflineRemoteClient();
+ }
+ return client;
+ }
+
+ @Override
+ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
+ if("workofflinepref".equals(key) && calculateRemoteClient(sharedPreferences)) {
+ todoApplication.getTaskBag().pushToRemote();
+ }
+ }
+
+ private boolean calculateRemoteClient(SharedPreferences sharedPreferences) {
+ boolean workOffline = sharedPreferences.getBoolean("workofflinepref", false);
+ Log.v(TAG, "Work offline set to : " + workOffline);
+ SupportedClient newClientToken = workOffline ? SupportedClient.OFFLINE : SupportedClient.DROPBOX;
+ if(newClientToken != currentClientToken) {
+ if(currentClient != null) {
+ currentClient.deauthenticate();
+ }
+ currentClient = getRemoteClient(newClientToken);
+ currentClient.authenticate();
+ currentClientToken = newClientToken;
+ return true;
+ }
+ return false;
+ }
+
+ public enum SupportedClient {
+ OFFLINE,
+ DROPBOX;
+ }
+}
View
16 src/com/todotxt/todotxttouch/remote/RemoteFactory.java
@@ -1,16 +0,0 @@
-package com.todotxt.todotxttouch.remote;
-
-import com.todotxt.todotxttouch.TodoApplication;
-import com.todotxt.todotxttouch.remote.dropbox.DropboxRemoteTaskRepository;
-import com.todotxt.todotxttouch.remote.dropbox.DropboxRemoteClient;
-
-public class RemoteFactory {
-
- public static RemoteClient getRemoteClient(TodoApplication todoApplication) {
- return new DropboxRemoteClient(todoApplication);
- }
-
- public static RemoteTaskRepository getRemoteTaskRepository(TodoApplication todoApplication) {
- return new DropboxRemoteTaskRepository(todoApplication);
- }
-}
View
47 src/com/todotxt/todotxttouch/remote/RemoteTaskRepository.java
@@ -1,47 +0,0 @@
-/**
- *
- * Todo.txt Touch/src/com/todotxt/todotxttouch/remote/RemoteException.java
- *
- * Copyright (c) 2011 Tim Barlotta
- *
- * LICENSE:
- *
- * This file is part of Todo.txt Touch, an Android app for managing your todo.txt file (http://todotxt.com).
- *
- * Todo.txt Touch is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
- * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
- * later version.
- *
- * Todo.txt Touch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
- * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with Todo.txt Touch. If not, see
- * <http://www.gnu.org/licenses/>.
- *
- * @author Tim Barlotta <tim[at]barlotta[dot]net>
- * @license http://www.gnu.org/licenses/gpl.html
- * @copyright 2011 Tim Barlotta
- */
-
-package com.todotxt.todotxttouch.remote;
-
-import java.io.File;
-import java.util.ArrayList;
-
-import com.todotxt.todotxttouch.task.Task;
-
-/**
- * API for interacting with a remote task store
- *
- * @author Tim Barlotta
- */
-public interface RemoteTaskRepository {
- void init(File withLocalFile);
-
- void purge();
-
- ArrayList<Task> load();
-
- void store(ArrayList<Task> tasks);
-}
View
82 src/com/todotxt/todotxttouch/remote/dropbox/DropboxRemoteTaskRepository.java
@@ -1,82 +0,0 @@
-package com.todotxt.todotxttouch.remote.dropbox;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-
-import android.content.SharedPreferences;
-import android.os.Environment;
-import android.preference.PreferenceManager;
-
-import com.dropbox.client.DropboxAPI;
-import com.todotxt.todotxttouch.Constants;
-import com.todotxt.todotxttouch.TodoApplication;
-import com.todotxt.todotxttouch.remote.RemoteException;
-import com.todotxt.todotxttouch.remote.RemoteTaskRepository;
-import com.todotxt.todotxttouch.task.Task;
-import com.todotxt.todotxttouch.util.TaskIo;
-
-public class DropboxRemoteTaskRepository implements RemoteTaskRepository {
- private static final File TODO_TXT_TMP_FILE = new File(
- Environment.getExternalStorageDirectory(),
- "data/com.todotxt.todotxttouch/tmp/todo.txt");
-
- private SharedPreferences preferences;
- private DropboxRemoteClient client;
-
- public DropboxRemoteTaskRepository(TodoApplication app) {
- this.client = (DropboxRemoteClient) app.getRemoteClient();
- preferences = PreferenceManager.getDefaultSharedPreferences(app);
- }
-
- @Override
- public void init(File withLocalFile) {
- if (withLocalFile == null) {
- store(new ArrayList<Task>());
- } else {
- try {
- client.getAPI().putFile(Constants.DROPBOX_MODUS, client.getRemotePath(),
- withLocalFile);
- } catch (Exception e) {
- throw new RemoteException("error creating dropbox file", e);
- }
- }
- }
-
- @Override
- public void purge() {
- TODO_TXT_TMP_FILE.delete();
- }
-
- @Override
- public ArrayList<Task> load() {
- try {
- DropboxAPI.FileDownload file = client.getAPI().getFileStream(
- Constants.DROPBOX_MODUS, client.getRemotePathAndFilename(), null);
- if (file.isError()) {
- if (404 == file.httpCode) {
- init(null);
- return new ArrayList<Task>();
- } else {
- throw new DropboxFileRemoteException(
- "Error loading from dropbox", file);
- }
- }
-
- return TaskIo.loadTasksFromStream(file.is);
- } catch (IOException e) {
- throw new RemoteException("I/O error trying to load from dropbox",
- e);
- }
- }
-
- @Override
- public void store(ArrayList<Task> tasks) {
- boolean useWindowsLineBreaks = preferences.getBoolean("linebreakspref",
- false);
-
- TaskIo.writeToFile(tasks, TODO_TXT_TMP_FILE, useWindowsLineBreaks);
- client.getAPI().putFile(Constants.DROPBOX_MODUS, client.getRemotePath(), TODO_TXT_TMP_FILE);
- }
-
-}
View
7 src/com/todotxt/todotxttouch/task/TaskBag.java
@@ -57,12 +57,6 @@
ArrayList<Priority> getPriorities();
/* REMOTE APIs */
- void initRemote();
-
- void disconnectFromRemote();
-
- // FUTURE make this syncWithRemote()
-
/**
* Push tasks in localRepository into remoteRepository
*/
@@ -72,6 +66,5 @@
* Pulls tasks from remoteRepository, stores in localRepository
*/
void pullFromRemote();
-
/* END REMOTE APIs */
}
View
10 src/com/todotxt/todotxttouch/task/TaskBagFactory.java
@@ -28,8 +28,6 @@
import android.content.SharedPreferences;
import com.todotxt.todotxttouch.TodoApplication;
-import com.todotxt.todotxttouch.remote.RemoteFactory;
-import com.todotxt.todotxttouch.remote.RemoteTaskRepository;
/**
* A factory for creating TaskBags
@@ -37,16 +35,14 @@
* @author Tim Barlotta
*/
public class TaskBagFactory {
- public static TaskBag getTaskBag(TodoApplication application,
- SharedPreferences sharedPreferences, String defaultTodoTxtPath) {
+ public static TaskBag getTaskBag(TodoApplication application, SharedPreferences sharedPreferences) {
TaskBagImpl.Preferences taskBagPreferences = new TaskBagImpl.Preferences(
- sharedPreferences, defaultTodoTxtPath);
+ sharedPreferences);
LocalFileTaskRepository localFileTaskRepository = new LocalFileTaskRepository(
taskBagPreferences);
- RemoteTaskRepository remoteTaskRepository = RemoteFactory.getRemoteTaskRepository(application);
return new TaskBagImpl(taskBagPreferences, localFileTaskRepository,
- remoteTaskRepository);
+ application.getRemoteClientManager());
}
}
View
59 src/com/todotxt/todotxttouch/task/TaskBagImpl.java
@@ -27,6 +27,8 @@
import android.content.SharedPreferences;
import android.util.Log;
+import java.io.File;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@@ -35,7 +37,9 @@
import java.util.List;
import java.util.Set;
-import com.todotxt.todotxttouch.remote.RemoteTaskRepository;
+import com.todotxt.todotxttouch.remote.RemoteClient;
+import com.todotxt.todotxttouch.remote.RemoteClientManager;
+import com.todotxt.todotxttouch.util.TaskIo;
/**
* Implementation of the TaskBag interface
@@ -46,19 +50,15 @@
private static final String TAG = TaskBagImpl.class.getSimpleName();
private Preferences preferences;
private final LocalTaskRepository localRepository;
- private final RemoteTaskRepository remoteTaskRepository;
+ private final RemoteClientManager remoteClientManager;
private ArrayList<Task> tasks = new ArrayList<Task>();
public TaskBagImpl(Preferences preferences,
LocalTaskRepository localRepository,
- RemoteTaskRepository remoteTaskRepository) {
+ RemoteClientManager remoteClientManager) {
this.preferences = preferences;
this.localRepository = localRepository;
- this.remoteTaskRepository = remoteTaskRepository;
- }
-
- public void updatePreferences(TaskBagImpl.Preferences preferences) {
- this.preferences = preferences;
+ this.remoteClientManager = remoteClientManager;
}
@Override
@@ -151,32 +151,25 @@ public void delete(Task task) {
}
}
- /* REMOTE APIS */
- @Override
- public void initRemote() {
- remoteTaskRepository.init(LocalFileTaskRepository.TODO_TXT_FILE);
- reload();
- }
-
- @Override
- public void disconnectFromRemote() {
- remoteTaskRepository.purge();
- }
-
@Override
public void pushToRemote() {
- if (!this.preferences.isWorkOfflineEnabled()) {
- ArrayList<Task> localTasks = localRepository.load();
- remoteTaskRepository.store(localTasks);
- }
+ remoteClientManager.getRemoteClient().push(LocalFileTaskRepository.TODO_TXT_FILE);
}
@Override
public void pullFromRemote() {
- if (!this.preferences.isWorkOfflineEnabled()) {
- this.tasks = remoteTaskRepository.load();
- localRepository.store(tasks);
- }
+ try {
+ if (!this.preferences.isWorkOfflineEnabled()) {
+ File remoteFile = remoteClientManager.getRemoteClient().pull();
+ if(remoteFile!=null && remoteFile.exists()) {
+ ArrayList<Task> remoteTasks = TaskIo.loadTasksFromFile(remoteFile);
+ localRepository.store(remoteTasks);
+ reload();
+ }
+ }
+ } catch(IOException e) {
+ throw new TaskPersistException("Error loading tasks from remote file", e);
+ }
}
@Override
@@ -225,19 +218,13 @@ private static Task find(List<Task> tasks, Task task) {
return null;
}
- public static class Preferences {
- private final String defaultTodoFileDirectory;
+ static class Preferences {
private final SharedPreferences sharedPreferences;
- public Preferences(SharedPreferences sharedPreferences, String defaultTodoFileDirectory) {
+ public Preferences(SharedPreferences sharedPreferences) {
this.sharedPreferences = sharedPreferences;
- this.defaultTodoFileDirectory = defaultTodoFileDirectory;
}
- public String getTodoTextPath() {
- return sharedPreferences.getString("todotxtpath", defaultTodoFileDirectory);
- }
-
public boolean isUseWindowsLineBreaksEnabled() {
return sharedPreferences.getBoolean("linebreakspref", false);
}

2 comments on commit ae29a57

@tormodh

Could you rebase this off of the latest master when you have the time? There are merge conflicts and I'm sorry to say I don't have the time to look at those right now.

@timbarlotta

I am going to create a new branch. Have same conflicts locally (and from what I read you should not rebase public branches). I also think I have a better idea what Gina is looking for ( see mailing list) so I am going to make some mods based on that.

Please sign in to comment.
Something went wrong with that request. Please try again.