master
何越 2 years ago
parent 6b12b41243
commit 727d916a6e

@ -19,14 +19,17 @@ package net.micode.notes.gtask.exception;
public class NetworkFailureException extends Exception { public class NetworkFailureException extends Exception {
private static final long serialVersionUID = 2107610287180234136L; private static final long serialVersionUID = 2107610287180234136L;
// 无参构造函数
public NetworkFailureException() { public NetworkFailureException() {
super(); super();
} }
// 构造函数,接受一个字符串消息作为参数
public NetworkFailureException(String paramString) { public NetworkFailureException(String paramString) {
super(paramString); super(paramString);
} }
// 构造函数,接受一个字符串消息和一个可抛出对象作为参数
public NetworkFailureException(String paramString, Throwable paramThrowable) { public NetworkFailureException(String paramString, Throwable paramThrowable) {
super(paramString, paramThrowable); super(paramString, paramThrowable);
} }

@ -15,102 +15,96 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.gtask.remote;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import net.micode.notes.R;
import net.micode.notes.ui.NotesListActivity;
import net.micode.notes.ui.NotesPreferenceActivity;
public class GTaskASyncTask extends AsyncTask<Void, String, Integer> { public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
private static int GTASK_SYNC_NOTIFICATION_ID = 5234235; private static int GTASK_SYNC_NOTIFICATION_ID = 5234235;
public interface OnCompleteListener { // 定义回调接口 OnCompleteListener
void onComplete(); public interface OnCompleteListener {
} void onComplete();
}
private Context mContext;
private Context mContext;
private NotificationManager mNotifiManager;
private GTaskManager mTaskManager;
private OnCompleteListener mOnCompleteListener;
// 构造函数,初始化成员变量
public GTaskASyncTask(Context context, OnCompleteListener listener) {
mContext = context;
mOnCompleteListener = listener;
mNotifiManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
mTaskManager = GTaskManager.getInstance();
}
// 取消同步
public void cancelSync() {
mTaskManager.cancelSync();
}
// 发布进度
public void publishProgess(String message) {
publishProgress(new String[] {
message
});
}
// 显示通知
private void showNotification(int tickerId, String content) {
Notification notification = new Notification(R.drawable.notification, mContext
.getString(tickerId), System.currentTimeMillis());
notification.defaults = Notification.DEFAULT_LIGHTS;
notification.flags = Notification.FLAG_AUTO_CANCEL;
PendingIntent pendingIntent;
if (tickerId != R.string.ticker_success) {
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
NotesPreferenceActivity.class), 0);
} else {
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
NotesListActivity.class), 0);
}
//notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content,
// pendingIntent);
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
}
@Override
protected Integer doInBackground(Void... unused) {
publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity
.getSyncAccountName(mContext)));
// 执行同步任务,返回状态码
return mTaskManager.sync(mContext, this);
}
@Override
protected void onProgressUpdate(String... progress) {
showNotification(R.string.ticker_syncing, progress[0]);
if (mContext instanceof GTaskSyncService) {
((GTaskSyncService) mContext).sendBroadcast(progress[0]);
}
}
@Override
protected void onPostExecute(Integer result) {
// 根据状态码显示通知
if (result == GTaskManager.STATE_SUCCESS) {
showNotification(R.string.ticker_success, mContext.getString(
R.string.success_sync_account, mTaskManager.getSyncAccount()));
NotesPreferenceActivity.setLastSyncTime(mContext, System.currentTimeMillis());
} else if (result == GTaskManager.STATE_NETWORK_ERROR) {
showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_network));
} else if (result == GTaskManager.STATE_INTERNAL_ERROR) {
showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_internal));
} else if (result == GTaskManager.STATE_SYNC_CANCELLED) {
showNotification(R.string.ticker_cancel, mContext
.getString(R.string.error_sync_cancelled));
}
private NotificationManager mNotifiManager;
private GTaskManager mTaskManager;
private OnCompleteListener mOnCompleteListener;
public GTaskASyncTask(Context context, OnCompleteListener listener) {
mContext = context;
mOnCompleteListener = listener;
mNotifiManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
mTaskManager = GTaskManager.getInstance();
}
public void cancelSync() {
mTaskManager.cancelSync();
}
public void publishProgess(String message) {
publishProgress(new String[] {
message
});
}
private void showNotification(int tickerId, String content) {
Notification notification = new Notification(R.drawable.notification, mContext
.getString(tickerId), System.currentTimeMillis());
notification.defaults = Notification.DEFAULT_LIGHTS;
notification.flags = Notification.FLAG_AUTO_CANCEL;
PendingIntent pendingIntent;
if (tickerId != R.string.ticker_success) {
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
NotesPreferenceActivity.class), 0);
} else {
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
NotesListActivity.class), 0);
}
/*notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content,
pendingIntent);*/
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
}
@Override
protected Integer doInBackground(Void... unused) {
publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity
.getSyncAccountName(mContext)));
return mTaskManager.sync(mContext, this);
}
@Override
protected void onProgressUpdate(String... progress) {
showNotification(R.string.ticker_syncing, progress[0]);
if (mContext instanceof GTaskSyncService) {
((GTaskSyncService) mContext).sendBroadcast(progress[0]);
}
}
@Override
protected void onPostExecute(Integer result) {
if (result == GTaskManager.STATE_SUCCESS) {
showNotification(R.string.ticker_success, mContext.getString(
R.string.success_sync_account, mTaskManager.getSyncAccount()));
NotesPreferenceActivity.setLastSyncTime(mContext, System.currentTimeMillis());
} else if (result == GTaskManager.STATE_NETWORK_ERROR) {
showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_network));
} else if (result == GTaskManager.STATE_INTERNAL_ERROR) {
showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_internal));
} else if (result == GTaskManager.STATE_SYNC_CANCELLED) {
showNotification(R.string.ticker_cancel, mContext
.getString(R.string.error_sync_cancelled));
}
if (mOnCompleteListener != null) { if (mOnCompleteListener != null) {
new Thread(new Runnable() { new Thread(new Runnable() {

@ -90,6 +90,10 @@ public class GTaskClient {
private JSONArray mUpdateArray; private JSONArray mUpdateArray;
// 定义一个GTaskClient类用于管理Google任务API
public class GTaskClient {
// 初始化GTaskClient类的私有构造函数
private GTaskClient() { private GTaskClient() {
mHttpClient = null; mHttpClient = null;
mGetUrl = GTASK_GET_URL; mGetUrl = GTASK_GET_URL;
@ -102,6 +106,7 @@ public class GTaskClient {
mUpdateArray = null; mUpdateArray = null;
} }
// 创建一个静态的GTaskClient实例确保线程安全
public static synchronized GTaskClient getInstance() { public static synchronized GTaskClient getInstance() {
if (mInstance == null) { if (mInstance == null) {
mInstance = new GTaskClient(); mInstance = new GTaskClient();
@ -110,17 +115,14 @@ public class GTaskClient {
} }
public boolean login(Activity activity) { public boolean login(Activity activity) {
// we suppose that the cookie would expire after 5 minutes // 假设cookie在5分钟后会过期所以需要重新登录
// then we need to re-login
final long interval = 1000 * 60 * 5; final long interval = 1000 * 60 * 5;
if (mLastLoginTime + interval < System.currentTimeMillis()) { if (mLastLoginTime + interval < System.currentTimeMillis()) {
mLoggedin = false; mLoggedin = false;
} }
// need to re-login after account switch // 如果帐户发生变化,则需要重新登录
if (mLoggedin if (mLoggedin && !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity.getSyncAccountName(activity))) {
&& !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity
.getSyncAccountName(activity))) {
mLoggedin = false; mLoggedin = false;
} }
@ -136,9 +138,8 @@ public class GTaskClient {
return false; return false;
} }
// login with custom domain if necessary // 如果需要,使用自定义域名进行登录
if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase() if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase().endsWith("googlemail.com"))) {
.endsWith("googlemail.com"))) {
StringBuilder url = new StringBuilder(GTASK_URL).append("a/"); StringBuilder url = new StringBuilder(GTASK_URL).append("a/");
int index = mAccount.name.indexOf('@') + 1; int index = mAccount.name.indexOf('@') + 1;
String suffix = mAccount.name.substring(index); String suffix = mAccount.name.substring(index);
@ -151,7 +152,7 @@ public class GTaskClient {
} }
} }
// try to login with google official url // 尝试使用Google官方URL进行登录
if (!mLoggedin) { if (!mLoggedin) {
mGetUrl = GTASK_GET_URL; mGetUrl = GTASK_GET_URL;
mPostUrl = GTASK_POST_URL; mPostUrl = GTASK_POST_URL;
@ -164,6 +165,7 @@ public class GTaskClient {
return true; return true;
} }
// 登录Google账户获取授权令牌
private String loginGoogleAccount(Activity activity, boolean invalidateToken) { private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
String authToken; String authToken;
AccountManager accountManager = AccountManager.get(activity); AccountManager accountManager = AccountManager.get(activity);
@ -189,13 +191,14 @@ public class GTaskClient {
return null; return null;
} }
// get the token now // 获取授权令牌
AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account, AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account,
"goanna_mobile", null, activity, null, null); "goanna_mobile", null, activity, null, null);
try { try {
Bundle authTokenBundle = accountManagerFuture.getResult(); Bundle authTokenBundle = accountManagerFuture.getResult();
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN); authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
if (invalidateToken) { if (invalidateToken) {
// 使令牌无效,并重新获取令牌
accountManager.invalidateAuthToken("com.google", authToken); accountManager.invalidateAuthToken("com.google", authToken);
loginGoogleAccount(activity, false); loginGoogleAccount(activity, false);
} }
@ -207,379 +210,462 @@ public class GTaskClient {
return authToken; return authToken;
} }
// 尝试使用授权令牌进行Gtask登录
/**
* 使 Gtask
*
* @param activity Activity
* @param authToken Gtask
* @return true false
*/
private boolean tryToLoginGtask(Activity activity, String authToken) { private boolean tryToLoginGtask(Activity activity, String authToken) {
// 如果无法成功登录 Gtask 服务,则进行以下处理。
if (!loginGtask(authToken)) { if (!loginGtask(authToken)) {
// maybe the auth token is out of date, now let's invalidate the // 可能是身份验证令牌过期了,现在我们需要将令牌作废并再次尝试登录。
// token and try again
authToken = loginGoogleAccount(activity, true); authToken = loginGoogleAccount(activity, true);
// 如果登录谷歌帐号失败,则返回 false。
if (authToken == null) { if (authToken == null) {
Log.e(TAG, "login google account failed"); Log.e(TAG, "login google account failed");
return false; return false;
} }
// 如果仍然无法成功登录 Gtask 服务,则返回 false。
if (!loginGtask(authToken)) { if (!loginGtask(authToken)) {
Log.e(TAG, "login gtask failed"); Log.e(TAG, "login gtask failed");
return false; return false;
} }
} }
// 如果登录成功,则返回 true。
return true; return true;
} }
private boolean loginGtask(String authToken) { /**
int timeoutConnection = 10000; * 使 Gtask
int timeoutSocket = 15000; *
HttpParams httpParameters = new BasicHttpParams(); * @param authToken Gtask
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); * @return true false
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); */
mHttpClient = new DefaultHttpClient(httpParameters); private boolean loginGtask(String authToken) {
BasicCookieStore localBasicCookieStore = new BasicCookieStore(); // 配置 HTTP 连接参数。
mHttpClient.setCookieStore(localBasicCookieStore); int timeoutConnection = 10000;
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false); int timeoutSocket = 15000;
HttpParams httpParameters = new BasicHttpParams();
// login gtask HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
try { HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
String loginUrl = mGetUrl + "?auth=" + authToken;
HttpGet httpGet = new HttpGet(loginUrl); // 创建 HTTP 客户端。
HttpResponse response = null; mHttpClient = new DefaultHttpClient(httpParameters);
response = mHttpClient.execute(httpGet); BasicCookieStore localBasicCookieStore = new BasicCookieStore();
mHttpClient.setCookieStore(localBasicCookieStore);
// get the cookie now HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);
List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
boolean hasAuthCookie = false; // 使用身份验证令牌登录 Gtask 服务。
for (Cookie cookie : cookies) { try {
if (cookie.getName().contains("GTL")) { String loginUrl = mGetUrl + "?auth=" + authToken;
hasAuthCookie = true; HttpGet httpGet = new HttpGet(loginUrl);
} HttpResponse response = mHttpClient.execute(httpGet);
}
if (!hasAuthCookie) { // 获取 Cookie。
Log.w(TAG, "it seems that there is no auth cookie"); List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
} boolean hasAuthCookie = false;
for (Cookie cookie : cookies) {
// get the client version if (cookie.getName().contains("GTL")) {
String resString = getResponseContent(response.getEntity()); hasAuthCookie = true;
String jsBegin = "_setup(";
String jsEnd = ")}</script>";
int begin = resString.indexOf(jsBegin);
int end = resString.lastIndexOf(jsEnd);
String jsString = null;
if (begin != -1 && end != -1 && begin < end) {
jsString = resString.substring(begin + jsBegin.length(), end);
} }
JSONObject js = new JSONObject(jsString); }
mClientVersion = js.getLong("v"); if (!hasAuthCookie) {
} catch (JSONException e) { Log.w(TAG, "it seems that there is no auth cookie");
Log.e(TAG, e.toString());
e.printStackTrace();
return false;
} catch (Exception e) {
// simply catch all exceptions
Log.e(TAG, "httpget gtask_url failed");
return false;
} }
return true; // 获取客户端版本。
String resString = getResponseContent(response.getEntity());
String jsBegin = "_setup(";
String jsEnd = ")}</script>";
int begin = resString.indexOf(jsBegin);
int end = resString.lastIndexOf(jsEnd);
String jsString = null;
if (begin != -1 && end != -1 && begin < end) {
jsString = resString.substring(begin + jsBegin.length(), end);
}
JSONObject js = new JSONObject(jsString);
mClientVersion = js.getLong("v");
} catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
return false;
} catch (Exception e) {
Log.e(TAG, "httpget gtask_url failed");
return false;
} }
private int getActionId() { return true;
return mActionId++; }
}
/**
* ID
*
* @return ID
*/
private int getActionId() {
return mActionId++;
}
private HttpPost createHttpPost() { /**
HttpPost httpPost = new HttpPost(mPostUrl); * HTTP POST
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); *
httpPost.setHeader("AT", "1"); * @return HTTP POST
return httpPost; */
private HttpPost createHttpPost() {
HttpPost httpPost = new HttpPost(mPostUrl);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
httpPost.setHeader("AT", "1");
return httpPost;
}
/**
* HttpEntity
* @param entity HttpEntity
* @return
* @throws IOException
*/
private String getResponseContent(HttpEntity entity) throws IOException {
String contentEncoding = null;
// 获取响应编码方式
if (entity.getContentEncoding() != null) {
contentEncoding = entity.getContentEncoding().getValue();
Log.d(TAG, "encoding: " + contentEncoding);
} }
private String getResponseContent(HttpEntity entity) throws IOException { InputStream input = entity.getContent();
String contentEncoding = null; // 根据编码方式设置InputStream
if (entity.getContentEncoding() != null) { if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
contentEncoding = entity.getContentEncoding().getValue(); input = new GZIPInputStream(entity.getContent());
Log.d(TAG, "encoding: " + contentEncoding); } else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {
} Inflater inflater = new Inflater(true);
input = new InflaterInputStream(entity.getContent(), inflater);
}
InputStream input = entity.getContent(); try {
if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) { // 将InputStream转为字符流并读取响应内容
input = new GZIPInputStream(entity.getContent()); InputStreamReader isr = new InputStreamReader(input);
} else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) { BufferedReader br = new BufferedReader(isr);
Inflater inflater = new Inflater(true); StringBuilder sb = new StringBuilder();
input = new InflaterInputStream(entity.getContent(), inflater);
}
try { while (true) {
InputStreamReader isr = new InputStreamReader(input); String buff = br.readLine();
BufferedReader br = new BufferedReader(isr); if (buff == null) {
StringBuilder sb = new StringBuilder(); return sb.toString();
while (true) {
String buff = br.readLine();
if (buff == null) {
return sb.toString();
}
sb = sb.append(buff);
} }
} finally { sb = sb.append(buff);
input.close();
} }
} finally {
// 关闭InputStream
input.close();
} }
}
private JSONObject postRequest(JSONObject js) throws NetworkFailureException { /**
if (!mLoggedin) { * HttpPostJSONObject
Log.e(TAG, "please login first"); * @param js JSONObject
throw new ActionFailureException("not logged in"); * @return JSONObject
} * @throws NetworkFailureException
*/
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
if (!mLoggedin) {
Log.e(TAG, "please login first");
throw new ActionFailureException("not logged in");
}
HttpPost httpPost = createHttpPost(); HttpPost httpPost = createHttpPost();
try { try {
LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>(); // 创建请求参数并设置到HttpPost对象中
list.add(new BasicNameValuePair("r", js.toString())); LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8"); list.add(new BasicNameValuePair("r", js.toString()));
httpPost.setEntity(entity); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
httpPost.setEntity(entity);
// 执行HttpPost请求
HttpResponse response = mHttpClient.execute(httpPost);
// 获取响应内容并将其转为JSONObject对象
String jsString = getResponseContent(response.getEntity());
return new JSONObject(jsString);
} catch (ClientProtocolException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new NetworkFailureException("postRequest failed");
} catch (IOException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new NetworkFailureException("postRequest failed");
} catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new ActionFailureException("unable to convert response content to jsonobject");
} catch (Exception e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new ActionFailureException("error occurs when posting request");
}
}
// execute the post public void createTask(Task task) throws NetworkFailureException {
HttpResponse response = mHttpClient.execute(httpPost); commitUpdate(); // 提交更新
String jsString = getResponseContent(response.getEntity()); try {
return new JSONObject(jsString); JSONObject jsPost = new JSONObject(); // 创建一个JSON对象
JSONArray actionList = new JSONArray(); // 创建一个JSON数组
// action_list
// 将Task对象的创建操作添加到JSON数组中
actionList.put(task.getCreateAction(getActionId()));
// 将JSON数组添加到JSON对象中
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
// client_version
// 将客户端版本添加到JSON对象中
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
// post
// 发送post请求并接收响应
JSONObject jsResponse = postRequest(jsPost);
// 获取响应中的结果数组并获取第一个结果对象
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
// 将新任务的ID设置为响应中的新ID
task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
} catch (JSONException e) {
Log.e(TAG, e.toString()); // 输出异常日志
e.printStackTrace(); // 打印异常堆栈信息
throw new ActionFailureException("create task: handing jsonobject failed"); // 抛出自定义异常
}
}
} catch (ClientProtocolException e) { public void createTaskList(TaskList tasklist) throws NetworkFailureException {
Log.e(TAG, e.toString()); commitUpdate(); // 提交更新
e.printStackTrace(); try {
throw new NetworkFailureException("postRequest failed"); JSONObject jsPost = new JSONObject(); // 创建一个JSON对象
} catch (IOException e) { JSONArray actionList = new JSONArray(); // 创建一个JSON数组
Log.e(TAG, e.toString());
e.printStackTrace(); // action_list
throw new NetworkFailureException("postRequest failed"); // 将TaskList对象的创建操作添加到JSON数组中
} catch (JSONException e) { actionList.put(tasklist.getCreateAction(getActionId()));
Log.e(TAG, e.toString()); // 将JSON数组添加到JSON对象中
e.printStackTrace(); jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
throw new ActionFailureException("unable to convert response content to jsonobject");
} catch (Exception e) { // client version
Log.e(TAG, e.toString()); // 将客户端版本添加到JSON对象中
e.printStackTrace(); jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
throw new ActionFailureException("error occurs when posting request");
} // post
// 发送post请求并接收响应
JSONObject jsResponse = postRequest(jsPost);
// 获取响应中的结果数组并获取第一个结果对象
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
// 将新任务列表的ID设置为响应中的新ID
tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
} catch (JSONException e) {
Log.e(TAG, e.toString()); // 输出异常日志
e.printStackTrace(); // 打印异常堆栈信息
throw new ActionFailureException("create tasklist: handing jsonobject failed"); // 抛出自定义异常
} }
}
public void createTask(Task task) throws NetworkFailureException { public void commitUpdate() throws NetworkFailureException {
commitUpdate(); // 如果 mUpdateArray 不为空
if (mUpdateArray != null) {
try { try {
// 新建一个 JSON 对象
JSONObject jsPost = new JSONObject(); JSONObject jsPost = new JSONObject();
JSONArray actionList = new JSONArray();
// action_list // 将 mUpdateArray 放入 JSON 对象中的 action_list 字段
actionList.put(task.getCreateAction(getActionId())); jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray);
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
// client_version // 将 mClientVersion 放入 JSON 对象中的 client_version 字段
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
// post // 发送 POST 请求
JSONObject jsResponse = postRequest(jsPost); postRequest(jsPost);
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
// 清空 mUpdateArray
mUpdateArray = null;
} catch (JSONException e) { } catch (JSONException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
e.printStackTrace(); e.printStackTrace();
throw new ActionFailureException("create task: handing jsonobject failed");
// 抛出异常
throw new ActionFailureException("commit update: handing jsonobject failed");
} }
} }
}
public void createTaskList(TaskList tasklist) throws NetworkFailureException { public void addUpdateNode(Node node) throws NetworkFailureException {
commitUpdate(); // 如果 node 不为空
try { if (node != null) {
JSONObject jsPost = new JSONObject(); // 如果 mUpdateArray 不为空且长度超过 10提交更新
JSONArray actionList = new JSONArray(); if (mUpdateArray != null && mUpdateArray.length() > 10) {
commitUpdate();
}
// action_list // 如果 mUpdateArray 为空,新建一个 JSON 数组
actionList.put(tasklist.getCreateAction(getActionId())); if (mUpdateArray == null)
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); mUpdateArray = new JSONArray();
// client version // 将 node 对象的更新操作添加到 mUpdateArray 中
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); mUpdateArray.put(node.getUpdateAction(getActionId()));
}
}
// post
JSONObject jsResponse = postRequest(jsPost);
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
} catch (JSONException e) { public void moveTask(Task task, TaskList preParent, TaskList curParent)
Log.e(TAG, e.toString()); throws NetworkFailureException {
e.printStackTrace(); commitUpdate(); // 提交之前所有的更新操作
throw new ActionFailureException("create tasklist: handing jsonobject failed");
}
}
public void commitUpdate() throws NetworkFailureException { try {
if (mUpdateArray != null) { JSONObject jsPost = new JSONObject(); // 创建一个 JSON 对象
try { JSONArray actionList = new JSONArray(); // 创建一个 JSON 数组,用于存放操作列表
JSONObject jsPost = new JSONObject(); JSONObject action = new JSONObject(); // 创建一个操作对象
// action_list // action_list
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray); action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, // 设置操作类型
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_MOVE);
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId()); // 设置操作 ID
action.put(GTaskStringUtils.GTASK_JSON_ID, task.getGid()); // 设置要移动的任务 ID
// client_version if (preParent == curParent && task.getPriorSibling() != null) {
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); // 如果任务在同一个任务列表中移动且不是第一个任务,则添加其前一个任务的 ID
action.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, task.getPriorSibling());
}
postRequest(jsPost); action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid()); // 设置原任务列表的 ID
mUpdateArray = null; action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid()); // 设置目标任务列表的 ID
} catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new ActionFailureException("commit update: handing jsonobject failed");
}
}
}
public void addUpdateNode(Node node) throws NetworkFailureException { if (preParent != curParent) {
if (node != null) { // 如果任务在不同的任务列表中移动,则添加目标任务列表的 ID
// too many update items may result in an error action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid());
// set max to 10 items }
if (mUpdateArray != null && mUpdateArray.length() > 10) {
commitUpdate();
}
if (mUpdateArray == null) actionList.put(action); // 将操作添加到操作列表中
mUpdateArray = new JSONArray(); jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // 将操作列表添加到 JSON 对象中
mUpdateArray.put(node.getUpdateAction(getActionId()));
}
}
public void moveTask(Task task, TaskList preParent, TaskList curParent) // client_version
throws NetworkFailureException { jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); // 添加客户端版本信息
commitUpdate();
try {
JSONObject jsPost = new JSONObject();
JSONArray actionList = new JSONArray();
JSONObject action = new JSONObject();
// action_list
action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_MOVE);
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
action.put(GTaskStringUtils.GTASK_JSON_ID, task.getGid());
if (preParent == curParent && task.getPriorSibling() != null) {
// put prioring_sibing_id only if moving within the tasklist and
// it is not the first one
action.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, task.getPriorSibling());
}
action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid());
action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid());
if (preParent != curParent) {
// put the dest_list only if moving between tasklists
action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid());
}
actionList.put(action);
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
// client_version postRequest(jsPost); // 发送请求
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); } catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new ActionFailureException("move task: handing jsonobject failed");
}
}
postRequest(jsPost); public void deleteNode(Node node) throws NetworkFailureException {
commitUpdate(); // 提交之前所有的更新操作
} catch (JSONException e) { try {
Log.e(TAG, e.toString()); JSONObject jsPost = new JSONObject(); // 创建一个 JSON 对象
e.printStackTrace(); JSONArray actionList = new JSONArray(); // 创建一个 JSON 数组,用于存放操作列表
throw new ActionFailureException("move task: handing jsonobject failed");
}
}
public void deleteNode(Node node) throws NetworkFailureException { // action_list
commitUpdate(); node.setDeleted(true); // 将节点标记为已删除
try { actionList.put(node.getUpdateAction(getActionId())); // 将更新操作添加到操作列表中
JSONObject jsPost = new JSONObject(); jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // 将操作列表添加到 JSON 对象中
JSONArray actionList = new JSONArray();
// action_list // client_version
node.setDeleted(true); jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); // 添加客户端版本信息
actionList.put(node.getUpdateAction(getActionId()));
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
// client_version postRequest(jsPost); // 发送请求
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); mUpdateArray = null; // 重置更新操作数组
} catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new ActionFailureException("delete node: handing jsonobject failed");
}
}
postRequest(jsPost);
mUpdateArray = null; public JSONArray getTaskLists() throws NetworkFailureException {
} catch (JSONException e) { // 如果用户没有登录,则抛出异常
Log.e(TAG, e.toString()); if (!mLoggedin) {
e.printStackTrace(); Log.e(TAG, "please login first");
throw new ActionFailureException("delete node: handing jsonobject failed"); throw new ActionFailureException("not logged in");
}
} }
public JSONArray getTaskLists() throws NetworkFailureException { try {
if (!mLoggedin) { // 发送 HTTP GET 请求,获取任务列表
Log.e(TAG, "please login first"); HttpGet httpGet = new HttpGet(mGetUrl);
throw new ActionFailureException("not logged in"); HttpResponse response = null;
response = mHttpClient.execute(httpGet);
// 从响应中提取 JSON 字符串
String resString = getResponseContent(response.getEntity());
String jsBegin = "_setup(";
String jsEnd = ")}</script>";
int begin = resString.indexOf(jsBegin);
int end = resString.lastIndexOf(jsEnd);
String jsString = null;
if (begin != -1 && end != -1 && begin < end) {
jsString = resString.substring(begin + jsBegin.length(), end);
} }
try { // 解析 JSON 字符串,返回任务列表
HttpGet httpGet = new HttpGet(mGetUrl); JSONObject js = new JSONObject(jsString);
HttpResponse response = null; return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS);
response = mHttpClient.execute(httpGet); } catch (ClientProtocolException e) {
Log.e(TAG, e.toString());
// get the task list e.printStackTrace();
String resString = getResponseContent(response.getEntity()); throw new NetworkFailureException("gettasklists: httpget failed");
String jsBegin = "_setup("; } catch (IOException e) {
String jsEnd = ")}</script>"; Log.e(TAG, e.toString());
int begin = resString.indexOf(jsBegin); e.printStackTrace();
int end = resString.lastIndexOf(jsEnd); throw new NetworkFailureException("gettasklists: httpget failed");
String jsString = null; } catch (JSONException e) {
if (begin != -1 && end != -1 && begin < end) { Log.e(TAG, e.toString());
jsString = resString.substring(begin + jsBegin.length(), end); e.printStackTrace();
} throw new ActionFailureException("get task lists: handing jasonobject failed");
JSONObject js = new JSONObject(jsString);
return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS);
} catch (ClientProtocolException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new NetworkFailureException("gettasklists: httpget failed");
} catch (IOException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new NetworkFailureException("gettasklists: httpget failed");
} catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new ActionFailureException("get task lists: handing jasonobject failed");
}
} }
}
public JSONArray getTaskList(String listGid) throws NetworkFailureException { public JSONArray getTaskList(String listGid) throws NetworkFailureException {
commitUpdate(); // 提交未提交的更改
try { commitUpdate();
JSONObject jsPost = new JSONObject(); try {
JSONArray actionList = new JSONArray(); JSONObject jsPost = new JSONObject();
JSONObject action = new JSONObject(); JSONArray actionList = new JSONArray();
JSONObject action = new JSONObject();
// action_list
action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, // action_list
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL); action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId()); GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL);
action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid); action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false); action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid);
actionList.put(action); action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false);
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); actionList.put(action);
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
// client_version
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); // client_version
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
JSONObject jsResponse = postRequest(jsPost);
return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS); // 发送 POST 请求,获取指定任务列表的任务
} catch (JSONException e) { JSONObject jsResponse = postRequest(jsPost);
Log.e(TAG, e.toString()); return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS);
e.printStackTrace(); } catch (JSONException e) {
throw new ActionFailureException("get task list: handing jsonobject failed"); Log.e(TAG, e.toString());
} e.printStackTrace();
throw new ActionFailureException("get task list: handing jsonobject failed");
} }
}
public Account getSyncAccount() { public Account getSyncAccount() {
return mAccount; // 获取同步账户
} return mAccount;
}
public void resetUpdateArray() { public void resetUpdateArray() {
mUpdateArray = null; // 重置未提交的更改
} mUpdateArray = null;
} }

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save