|
|
|
|
@ -1,95 +1,82 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
* 版权声明:此处声明代码的版权属于 MiCode 开放源代码社区,并指定了许可协议为 Apache License 2.0。
|
|
|
|
|
* 提供了许可协议的获取链接。
|
|
|
|
|
* 强调除非适用法律要求或书面同意,否则软件按 “原样” 分发,不提供任何形式的明示或暗示保证。
|
|
|
|
|
* 并指出许可协议中关于权限和限制的具体语言。
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.gtask.remote;
|
|
|
|
|
|
|
|
|
|
import android.accounts.Account;
|
|
|
|
|
import android.accounts.AccountManager;
|
|
|
|
|
import android.accounts.AccountManagerFuture;
|
|
|
|
|
import android.app.Activity;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.gtask.data.Node;
|
|
|
|
|
import net.micode.notes.gtask.data.Task;
|
|
|
|
|
import net.micode.notes.gtask.data.TaskList;
|
|
|
|
|
import net.micode.notes.gtask.exception.ActionFailureException;
|
|
|
|
|
import net.micode.notes.gtask.exception.NetworkFailureException;
|
|
|
|
|
import net.micode.notes.tool.GTaskStringUtils;
|
|
|
|
|
import net.micode.notes.ui.NotesPreferenceActivity;
|
|
|
|
|
|
|
|
|
|
import org.apache.http.HttpEntity;
|
|
|
|
|
import org.apache.http.HttpResponse;
|
|
|
|
|
import org.apache.http.client.ClientProtocolException;
|
|
|
|
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
|
|
import org.apache.http.cookie.Cookie;
|
|
|
|
|
import org.apache.http.impl.client.BasicCookieStore;
|
|
|
|
|
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
|
|
|
import org.apache.http.params.BasicHttpParams;
|
|
|
|
|
import org.apache.http.params.HttpConnectionParams;
|
|
|
|
|
import org.apache.http.params.HttpParams;
|
|
|
|
|
import org.apache.http.params.HttpProtocolParams;
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.zip.GZIPInputStream;
|
|
|
|
|
import java.util.zip.Inflater;
|
|
|
|
|
import java.util.zip.InflaterInputStream;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.accounts.Account; // 导入 Account 类,用于表示用户账户
|
|
|
|
|
import android.accounts.AccountManager; // 导入 AccountManager 类,用于管理账户
|
|
|
|
|
import android.accounts.AccountManagerFuture; // 导入 AccountManagerFuture 类,用于异步获取账户信息
|
|
|
|
|
import android.app.Activity; // 导入 Activity 类,用于表示 Android 应用程序的活动
|
|
|
|
|
import android.os.Bundle; // 导入 Bundle 类,用于传递数据
|
|
|
|
|
import android.text.TextUtils; // 导入 TextUtils 类,用于处理文本操作
|
|
|
|
|
import android.util.Log; // 导入 Log 类,用于记录日志信息
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.gtask.data.Node; // 导入 Node 类,表示任务节点
|
|
|
|
|
import net.micode.notes.gtask.data.Task; // 导入 Task 类,表示任务
|
|
|
|
|
import net.micode.notes.gtask.data.TaskList; // 导入 TaskList 类,表示任务列表
|
|
|
|
|
import net.micode.notes.gtask.exception.ActionFailureException; // 导入 ActionFailureException 类,表示操作失败异常
|
|
|
|
|
import net.micode.notes.gtask.exception.NetworkFailureException; // 导入 NetworkFailureException 类,表示网络失败异常
|
|
|
|
|
import net.micode.notes.tool.GTaskStringUtils; // 导入 GTaskStringUtils 类,提供字符串处理工具
|
|
|
|
|
import net.micode.notes.ui.NotesPreferenceActivity; // 导入 NotesPreferenceActivity 类,用于处理笔记偏好设置
|
|
|
|
|
|
|
|
|
|
import org.apache.http.HttpEntity; // 导入 HttpEntity 类,用于表示 HTTP 响应实体
|
|
|
|
|
import org.apache.http.HttpResponse; // 导入 HttpResponse 类,用于表示 HTTP 响应
|
|
|
|
|
import org.apache.http.client.ClientProtocolException; // 导入 ClientProtocolException 类,表示客户端协议异常
|
|
|
|
|
import org.apache.http.client.entity.UrlEncodedFormEntity; // 导入 UrlEncodedFormEntity 类,用于编码表单实体
|
|
|
|
|
import org.apache.http.client.methods.HttpGet; // 导入 HttpGet 类,用于发送 HTTP GET 请求
|
|
|
|
|
import org.apache.http.client.methods.HttpPost; // 导入 HttpPost 类,用于发送 HTTP POST 请求
|
|
|
|
|
import org.apache.http.cookie.Cookie; // 导入 Cookie 类,用于表示 HTTP Cookie
|
|
|
|
|
import org.apache.http.impl.client.BasicCookieStore; // 导入 BasicCookieStore 类,用于存储 Cookie
|
|
|
|
|
import org.apache.http.impl.client.DefaultHttpClient; // 导入 DefaultHttpClient 类,用于创建 HTTP 客户端
|
|
|
|
|
import org.apache.http.message.BasicNameValuePair; // 导入 BasicNameValuePair 类,用于表示键值对
|
|
|
|
|
import org.apache.http.params.BasicHttpParams; // 导入 BasicHttpParams 类,用于设置 HTTP 参数
|
|
|
|
|
import org.apache.http.params.HttpConnectionParams; // 导入 HttpConnectionParams 类,用于设置连接参数
|
|
|
|
|
import org.apache.http.params.HttpProtocolParams; // 导入 HttpProtocolParams 类,用于设置协议参数
|
|
|
|
|
import org.json.JSONArray; // 导入 JSONArray 类,用于处理 JSON 数组
|
|
|
|
|
import org.json.JSONException; // 导入 JSONException 类,表示 JSON 相关异常
|
|
|
|
|
import org.json.JSONObject; // 导入 JSONObject 类,用于处理 JSON 对象
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader; // 导入 BufferedReader 类,用于读取文本流
|
|
|
|
|
import java.io.IOException; // 导入 IOException 类,表示 I/O 异常
|
|
|
|
|
import java.io.InputStream; // 导入 InputStream 类,表示输入流
|
|
|
|
|
import java.io.InputStreamReader; // 导入 InputStreamReader 类,用于将输入流转换为字符流
|
|
|
|
|
import java.util.LinkedList; // 导入 LinkedList 类,用于创建链表
|
|
|
|
|
import java.util.List; // 导入 List 接口,用于表示列表
|
|
|
|
|
import java.util.zip.GZIPInputStream; // 导入 GZIPInputStream 类,用于处理 GZIP 压缩流
|
|
|
|
|
import java.util.zip.Inflater; // 导入 Inflater 类,用于解压缩
|
|
|
|
|
import java.util.zip.InflaterInputStream; // 导入 InflaterInputStream 类,用于解压缩流
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GTaskClient 类,用于与 Google Tasks 服务进行交互,执行任务和任务列表的创建、更新、删除等操作。
|
|
|
|
|
*/
|
|
|
|
|
public class GTaskClient {
|
|
|
|
|
private static final String TAG = GTaskClient.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
private static final String GTASK_URL = "https://mail.google.com/tasks/";
|
|
|
|
|
|
|
|
|
|
private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig";
|
|
|
|
|
|
|
|
|
|
private static final String GTASK_POST_URL = "https://mail.google.com/tasks/r/ig";
|
|
|
|
|
|
|
|
|
|
private static GTaskClient mInstance = null;
|
|
|
|
|
|
|
|
|
|
private DefaultHttpClient mHttpClient;
|
|
|
|
|
|
|
|
|
|
private String mGetUrl;
|
|
|
|
|
|
|
|
|
|
private String mPostUrl;
|
|
|
|
|
|
|
|
|
|
private long mClientVersion;
|
|
|
|
|
|
|
|
|
|
private boolean mLoggedin;
|
|
|
|
|
|
|
|
|
|
private long mLastLoginTime;
|
|
|
|
|
private static final String TAG = GTaskClient.class.getSimpleName(); // 定义日志标签
|
|
|
|
|
|
|
|
|
|
private int mActionId;
|
|
|
|
|
private static final String GTASK_URL = "https://mail.google.com/tasks/ "; // 定义 Google Tasks 基础 URL
|
|
|
|
|
private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig "; // 定义 Google Tasks GET 请求 URL
|
|
|
|
|
private static final String GTASK_POST_URL = "https://mail.google.com/tasks/r/ig "; // 定义 Google Tasks POST 请求 URL
|
|
|
|
|
|
|
|
|
|
private Account mAccount;
|
|
|
|
|
private static GTaskClient mInstance = null; // 单例实例
|
|
|
|
|
|
|
|
|
|
private JSONArray mUpdateArray;
|
|
|
|
|
private DefaultHttpClient mHttpClient; // HTTP 客户端
|
|
|
|
|
private String mGetUrl; // GET 请求 URL
|
|
|
|
|
private String mPostUrl; // POST 请求 URL
|
|
|
|
|
private long mClientVersion; // 客户端版本
|
|
|
|
|
private boolean mLoggedin; // 登录状态
|
|
|
|
|
private long mLastLoginTime; // 最后登录时间
|
|
|
|
|
private int mActionId; // 操作 ID
|
|
|
|
|
private Account mAccount; // 用户账户
|
|
|
|
|
private JSONArray mUpdateArray; // 更新操作数组
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数,私有化以实现单例模式。
|
|
|
|
|
*/
|
|
|
|
|
private GTaskClient() {
|
|
|
|
|
mHttpClient = null;
|
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
|
@ -102,6 +89,11 @@ public class GTaskClient {
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取 GTaskClient 单例实例。
|
|
|
|
|
*
|
|
|
|
|
* @return 单例实例
|
|
|
|
|
*/
|
|
|
|
|
public static synchronized GTaskClient getInstance() {
|
|
|
|
|
if (mInstance == null) {
|
|
|
|
|
mInstance = new GTaskClient();
|
|
|
|
|
@ -109,18 +101,23 @@ public class GTaskClient {
|
|
|
|
|
return mInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 登录 Google Tasks 服务。
|
|
|
|
|
*
|
|
|
|
|
* @param activity 当前活动
|
|
|
|
|
* @return 登录是否成功
|
|
|
|
|
*/
|
|
|
|
|
public boolean login(Activity activity) {
|
|
|
|
|
// we suppose that the cookie would expire after 5 minutes
|
|
|
|
|
// then we need to re-login
|
|
|
|
|
final long interval = 1000 * 60 * 5;
|
|
|
|
|
// 检查是否需要重新登录
|
|
|
|
|
final long interval = 1000 * 60 * 5; // 5 分钟间隔
|
|
|
|
|
if (mLastLoginTime + interval < System.currentTimeMillis()) {
|
|
|
|
|
mLoggedin = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// need to re-login after account switch
|
|
|
|
|
// 如果切换了账户,需要重新登录
|
|
|
|
|
if (mLoggedin
|
|
|
|
|
&& !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity
|
|
|
|
|
.getSyncAccountName(activity))) {
|
|
|
|
|
.getSyncAccountName(activity))) {
|
|
|
|
|
mLoggedin = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -136,7 +133,7 @@ public class GTaskClient {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// login with custom domain if necessary
|
|
|
|
|
// 如果是自定义域名账户,尝试使用自定义域名登录
|
|
|
|
|
if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase()
|
|
|
|
|
.endsWith("googlemail.com"))) {
|
|
|
|
|
StringBuilder url = new StringBuilder(GTASK_URL).append("a/");
|
|
|
|
|
@ -151,7 +148,7 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// try to login with google official url
|
|
|
|
|
// 尝试使用 Google 官方 URL 登录
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
|
mPostUrl = GTASK_POST_URL;
|
|
|
|
|
@ -164,6 +161,13 @@ public class GTaskClient {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 登录 Google 账户并获取认证令牌。
|
|
|
|
|
*
|
|
|
|
|
* @param activity 当前活动
|
|
|
|
|
* @param invalidateToken 是否使令牌失效
|
|
|
|
|
* @return 认证令牌
|
|
|
|
|
*/
|
|
|
|
|
private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
|
|
|
|
|
String authToken;
|
|
|
|
|
AccountManager accountManager = AccountManager.get(activity);
|
|
|
|
|
@ -189,7 +193,7 @@ public class GTaskClient {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get the token now
|
|
|
|
|
// 获取认证令牌
|
|
|
|
|
AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account,
|
|
|
|
|
"goanna_mobile", null, activity, null, null);
|
|
|
|
|
try {
|
|
|
|
|
@ -207,10 +211,16 @@ public class GTaskClient {
|
|
|
|
|
return authToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 尝试登录 Google Tasks 服务。
|
|
|
|
|
*
|
|
|
|
|
* @param activity 当前活动
|
|
|
|
|
* @param authToken 认证令牌
|
|
|
|
|
* @return 登录是否成功
|
|
|
|
|
*/
|
|
|
|
|
private boolean tryToLoginGtask(Activity activity, String 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);
|
|
|
|
|
if (authToken == null) {
|
|
|
|
|
Log.e(TAG, "login google account failed");
|
|
|
|
|
@ -225,6 +235,12 @@ public class GTaskClient {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 登录 Google Tasks 服务。
|
|
|
|
|
*
|
|
|
|
|
* @param authToken 认证令牌
|
|
|
|
|
* @return 登录是否成功
|
|
|
|
|
*/
|
|
|
|
|
private boolean loginGtask(String authToken) {
|
|
|
|
|
int timeoutConnection = 10000;
|
|
|
|
|
int timeoutSocket = 15000;
|
|
|
|
|
@ -236,14 +252,12 @@ public class GTaskClient {
|
|
|
|
|
mHttpClient.setCookieStore(localBasicCookieStore);
|
|
|
|
|
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);
|
|
|
|
|
|
|
|
|
|
// login gtask
|
|
|
|
|
try {
|
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken;
|
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl);
|
|
|
|
|
HttpResponse response = null;
|
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
HttpResponse response = mHttpClient.execute(httpGet);
|
|
|
|
|
|
|
|
|
|
// get the cookie now
|
|
|
|
|
// 获取 Cookie
|
|
|
|
|
List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
|
|
|
|
|
boolean hasAuthCookie = false;
|
|
|
|
|
for (Cookie cookie : cookies) {
|
|
|
|
|
@ -255,7 +269,7 @@ public class GTaskClient {
|
|
|
|
|
Log.w(TAG, "it seems that there is no auth cookie");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get the client version
|
|
|
|
|
// 获取客户端版本
|
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
|
|
@ -272,7 +286,6 @@ public class GTaskClient {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// simply catch all exceptions
|
|
|
|
|
Log.e(TAG, "httpget gtask_url failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
@ -280,10 +293,20 @@ public class GTaskClient {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取操作 ID。
|
|
|
|
|
*
|
|
|
|
|
* @return 操作 ID
|
|
|
|
|
*/
|
|
|
|
|
private int getActionId() {
|
|
|
|
|
return mActionId++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建 HTTP POST 请求。
|
|
|
|
|
*
|
|
|
|
|
* @return HTTP POST 请求对象
|
|
|
|
|
*/
|
|
|
|
|
private HttpPost createHttpPost() {
|
|
|
|
|
HttpPost httpPost = new HttpPost(mPostUrl);
|
|
|
|
|
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
|
|
|
|
|
@ -291,6 +314,13 @@ public class GTaskClient {
|
|
|
|
|
return httpPost;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取 HTTP 响应内容。
|
|
|
|
|
*
|
|
|
|
|
* @param entity HTTP 响应实体
|
|
|
|
|
* @return 响应内容
|
|
|
|
|
* @throws IOException 如果发生 I/O 异常
|
|
|
|
|
*/
|
|
|
|
|
private String getResponseContent(HttpEntity entity) throws IOException {
|
|
|
|
|
String contentEncoding = null;
|
|
|
|
|
if (entity.getContentEncoding() != null) {
|
|
|
|
|
@ -323,6 +353,13 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送 POST 请求。
|
|
|
|
|
*
|
|
|
|
|
* @param js JSON 对象,请求数据
|
|
|
|
|
* @return 响应 JSON 对象
|
|
|
|
|
* @throws NetworkFailureException 如果发生网络失败异常
|
|
|
|
|
*/
|
|
|
|
|
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
|
@ -336,7 +373,6 @@ public class GTaskClient {
|
|
|
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
|
|
|
|
|
httpPost.setEntity(entity);
|
|
|
|
|
|
|
|
|
|
// execute the post
|
|
|
|
|
HttpResponse response = mHttpClient.execute(httpPost);
|
|
|
|
|
String jsString = getResponseContent(response.getEntity());
|
|
|
|
|
return new JSONObject(jsString);
|
|
|
|
|
@ -360,20 +396,23 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建任务。
|
|
|
|
|
*
|
|
|
|
|
* @param task 任务对象
|
|
|
|
|
* @throws NetworkFailureException 如果发生网络失败异常
|
|
|
|
|
*/
|
|
|
|
|
public void createTask(Task task) throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
actionList.put(task.getCreateAction(getActionId()));
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// post
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
|
@ -386,20 +425,23 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建任务列表。
|
|
|
|
|
*
|
|
|
|
|
* @param tasklist 任务列表对象
|
|
|
|
|
* @throws NetworkFailureException 如果发生网络失败异常
|
|
|
|
|
*/
|
|
|
|
|
public void createTaskList(TaskList tasklist) throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
actionList.put(tasklist.getCreateAction(getActionId()));
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
// client version
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// post
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
|
@ -412,15 +454,18 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 提交更新操作。
|
|
|
|
|
*
|
|
|
|
|
* @throws NetworkFailureException 如果发生网络失败异常
|
|
|
|
|
*/
|
|
|
|
|
public void commitUpdate() throws NetworkFailureException {
|
|
|
|
|
if (mUpdateArray != null) {
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray);
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
@ -433,10 +478,14 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加更新节点。
|
|
|
|
|
*
|
|
|
|
|
* @param node 节点对象
|
|
|
|
|
* @throws NetworkFailureException 如果发生网络失败异常
|
|
|
|
|
*/
|
|
|
|
|
public void addUpdateNode(Node node) throws NetworkFailureException {
|
|
|
|
|
if (node != null) {
|
|
|
|
|
// too many update items may result in an error
|
|
|
|
|
// set max to 10 items
|
|
|
|
|
if (mUpdateArray != null && mUpdateArray.length() > 10) {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
}
|
|
|
|
|
@ -447,6 +496,14 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 移动任务。
|
|
|
|
|
*
|
|
|
|
|
* @param task 任务对象
|
|
|
|
|
* @param preParent 原父任务列表
|
|
|
|
|
* @param curParent 当前父任务列表
|
|
|
|
|
* @throws NetworkFailureException 如果发生网络失败异常
|
|
|
|
|
*/
|
|
|
|
|
public void moveTask(Task task, TaskList preParent, TaskList curParent)
|
|
|
|
|
throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
@ -455,26 +512,21 @@ public class GTaskClient {
|
|
|
|
|
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
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
@ -486,18 +538,22 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除节点。
|
|
|
|
|
*
|
|
|
|
|
* @param node 节点对象
|
|
|
|
|
* @throws NetworkFailureException 如果发生网络失败异常
|
|
|
|
|
*/
|
|
|
|
|
public void deleteNode(Node node) throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
node.setDeleted(true);
|
|
|
|
|
actionList.put(node.getUpdateAction(getActionId()));
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
@ -509,6 +565,12 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取任务列表。
|
|
|
|
|
*
|
|
|
|
|
* @return 任务列表 JSON 数组
|
|
|
|
|
* @throws NetworkFailureException 如果发生网络失败异常
|
|
|
|
|
*/
|
|
|
|
|
public JSONArray getTaskLists() throws NetworkFailureException {
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
|
@ -517,10 +579,8 @@ public class GTaskClient {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
HttpGet httpGet = new HttpGet(mGetUrl);
|
|
|
|
|
HttpResponse response = null;
|
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
HttpResponse response = mHttpClient.execute(httpGet);
|
|
|
|
|
|
|
|
|
|
// get the task list
|
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
|
|
@ -547,6 +607,13 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取指定任务列表中的任务。
|
|
|
|
|
*
|
|
|
|
|
* @param listGid 任务列表 ID
|
|
|
|
|
* @return 任务 JSON 数组
|
|
|
|
|
* @throws NetworkFailureException 如果发生网络失败异常
|
|
|
|
|
*/
|
|
|
|
|
public JSONArray getTaskList(String listGid) throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
@ -554,7 +621,6 @@ public class GTaskClient {
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONObject action = new JSONObject();
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL);
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
|
|
|
|
|
@ -563,7 +629,6 @@ public class GTaskClient {
|
|
|
|
|
actionList.put(action);
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
@ -575,11 +640,19 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取同步账户。
|
|
|
|
|
*
|
|
|
|
|
* @return 账户对象
|
|
|
|
|
*/
|
|
|
|
|
public Account getSyncAccount() {
|
|
|
|
|
return mAccount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 重置更新操作数组。
|
|
|
|
|
*/
|
|
|
|
|
public void resetUpdateArray() {
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|