|
|
|
|
@ -61,35 +61,58 @@ import java.util.zip.Inflater;
|
|
|
|
|
import java.util.zip.InflaterInputStream;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Google Tasks客户端类
|
|
|
|
|
* 负责与Google Tasks服务器进行通信,处理登录、任务创建、更新、删除等操作
|
|
|
|
|
* 采用单例模式实现
|
|
|
|
|
*/
|
|
|
|
|
public class GTaskClient {
|
|
|
|
|
// 日志标签
|
|
|
|
|
private static final String TAG = GTaskClient.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
// Google Tasks基础URL
|
|
|
|
|
private static final String GTASK_URL = "https://mail.google.com/tasks/";
|
|
|
|
|
|
|
|
|
|
// Google Tasks GET请求URL
|
|
|
|
|
private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig";
|
|
|
|
|
|
|
|
|
|
// Google Tasks POST请求URL
|
|
|
|
|
private static final String GTASK_POST_URL = "https://mail.google.com/tasks/r/ig";
|
|
|
|
|
|
|
|
|
|
// 单例实例
|
|
|
|
|
private static GTaskClient mInstance = null;
|
|
|
|
|
|
|
|
|
|
// HTTP客户端实例
|
|
|
|
|
private DefaultHttpClient mHttpClient;
|
|
|
|
|
|
|
|
|
|
// 当前使用的GET请求URL
|
|
|
|
|
private String mGetUrl;
|
|
|
|
|
|
|
|
|
|
// 当前使用的POST请求URL
|
|
|
|
|
private String mPostUrl;
|
|
|
|
|
|
|
|
|
|
// 客户端版本号
|
|
|
|
|
private long mClientVersion;
|
|
|
|
|
|
|
|
|
|
// 是否已登录
|
|
|
|
|
private boolean mLoggedin;
|
|
|
|
|
|
|
|
|
|
// 上次登录时间
|
|
|
|
|
private long mLastLoginTime;
|
|
|
|
|
|
|
|
|
|
// 操作ID计数器
|
|
|
|
|
private int mActionId;
|
|
|
|
|
|
|
|
|
|
// 登录的Google账户
|
|
|
|
|
private Account mAccount;
|
|
|
|
|
|
|
|
|
|
// 更新操作的JSON数组
|
|
|
|
|
private JSONArray mUpdateArray;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 私有构造函数
|
|
|
|
|
* 初始化客户端参数
|
|
|
|
|
*/
|
|
|
|
|
private GTaskClient() {
|
|
|
|
|
mHttpClient = null;
|
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
|
@ -102,6 +125,10 @@ public class GTaskClient {
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取单例实例
|
|
|
|
|
* @return GTaskClient实例
|
|
|
|
|
*/
|
|
|
|
|
public static synchronized GTaskClient getInstance() {
|
|
|
|
|
if (mInstance == null) {
|
|
|
|
|
mInstance = new GTaskClient();
|
|
|
|
|
@ -109,15 +136,20 @@ public class GTaskClient {
|
|
|
|
|
return mInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 登录Google Tasks
|
|
|
|
|
* 检查是否需要重新登录,处理Google账户认证和Tasks登录
|
|
|
|
|
* @param activity 调用此方法的Activity实例
|
|
|
|
|
* @return 是否登录成功
|
|
|
|
|
*/
|
|
|
|
|
public boolean login(Activity activity) {
|
|
|
|
|
// we suppose that the cookie would expire after 5 minutes
|
|
|
|
|
// then we need to re-login
|
|
|
|
|
// 假设Cookie 5分钟后过期,需要重新登录
|
|
|
|
|
final long interval = 1000 * 60 * 5;
|
|
|
|
|
if (mLastLoginTime + interval < System.currentTimeMillis()) {
|
|
|
|
|
mLoggedin = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// need to re-login after account switch
|
|
|
|
|
// 切换账户后需要重新登录
|
|
|
|
|
if (mLoggedin
|
|
|
|
|
&& !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity
|
|
|
|
|
.getSyncAccountName(activity))) {
|
|
|
|
|
@ -136,7 +168,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 +183,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 +196,12 @@ public class GTaskClient {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 登录Google账户,获取认证令牌
|
|
|
|
|
* @param activity 调用此方法的Activity实例
|
|
|
|
|
* @param invalidateToken 是否使现有令牌失效
|
|
|
|
|
* @return 认证令牌,如果获取失败则返回null
|
|
|
|
|
*/
|
|
|
|
|
private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
|
|
|
|
|
String authToken;
|
|
|
|
|
AccountManager accountManager = AccountManager.get(activity);
|
|
|
|
|
@ -189,7 +227,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 +245,16 @@ public class GTaskClient {
|
|
|
|
|
return authToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 尝试登录Google Tasks
|
|
|
|
|
* 如果登录失败,尝试使令牌失效并重新登录
|
|
|
|
|
* @param activity 调用此方法的Activity实例
|
|
|
|
|
* @param authToken Google账户认证令牌
|
|
|
|
|
* @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 +269,12 @@ public class GTaskClient {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 使用认证令牌登录Google Tasks
|
|
|
|
|
* 设置HTTP客户端,执行登录请求并获取Cookie和客户端版本
|
|
|
|
|
* @param authToken Google账户认证令牌
|
|
|
|
|
* @return 是否登录成功
|
|
|
|
|
*/
|
|
|
|
|
private boolean loginGtask(String authToken) {
|
|
|
|
|
int timeoutConnection = 10000;
|
|
|
|
|
int timeoutSocket = 15000;
|
|
|
|
|
@ -236,14 +286,14 @@ public class GTaskClient {
|
|
|
|
|
mHttpClient.setCookieStore(localBasicCookieStore);
|
|
|
|
|
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);
|
|
|
|
|
|
|
|
|
|
// login gtask
|
|
|
|
|
// 登录Google Tasks
|
|
|
|
|
try {
|
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken;
|
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl);
|
|
|
|
|
HttpResponse response = null;
|
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
|
|
|
|
|
// get the cookie now
|
|
|
|
|
// 获取认证Cookie
|
|
|
|
|
List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
|
|
|
|
|
boolean hasAuthCookie = false;
|
|
|
|
|
for (Cookie cookie : cookies) {
|
|
|
|
|
@ -255,7 +305,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 +322,7 @@ 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 +330,20 @@ public class GTaskClient {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取操作ID
|
|
|
|
|
* 自增并返回当前操作ID
|
|
|
|
|
* @return 唯一的操作ID
|
|
|
|
|
*/
|
|
|
|
|
private int getActionId() {
|
|
|
|
|
return mActionId++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建HTTP POST请求
|
|
|
|
|
* 设置请求头和URL
|
|
|
|
|
* @return HttpPost实例
|
|
|
|
|
*/
|
|
|
|
|
private HttpPost createHttpPost() {
|
|
|
|
|
HttpPost httpPost = new HttpPost(mPostUrl);
|
|
|
|
|
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
|
|
|
|
|
@ -291,6 +351,13 @@ public class GTaskClient {
|
|
|
|
|
return httpPost;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取HTTP响应内容
|
|
|
|
|
* 处理不同的编码格式(gzip、deflate等)
|
|
|
|
|
* @param entity HTTP响应实体
|
|
|
|
|
* @return 响应内容字符串
|
|
|
|
|
* @throws IOException 如果读取响应内容时发生错误
|
|
|
|
|
*/
|
|
|
|
|
private String getResponseContent(HttpEntity entity) throws IOException {
|
|
|
|
|
String contentEncoding = null;
|
|
|
|
|
if (entity.getContentEncoding() != null) {
|
|
|
|
|
@ -323,6 +390,13 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送POST请求
|
|
|
|
|
* 向Google Tasks服务器发送请求并返回响应
|
|
|
|
|
* @param js 请求的JSON对象
|
|
|
|
|
* @return 响应的JSON对象
|
|
|
|
|
* @throws NetworkFailureException 如果网络请求失败
|
|
|
|
|
*/
|
|
|
|
|
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
|
@ -336,7 +410,7 @@ public class GTaskClient {
|
|
|
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
|
|
|
|
|
httpPost.setEntity(entity);
|
|
|
|
|
|
|
|
|
|
// execute the post
|
|
|
|
|
// 执行POST请求
|
|
|
|
|
HttpResponse response = mHttpClient.execute(httpPost);
|
|
|
|
|
String jsString = getResponseContent(response.getEntity());
|
|
|
|
|
return new JSONObject(jsString);
|
|
|
|
|
@ -360,6 +434,12 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建任务
|
|
|
|
|
* 向Google Tasks服务器发送创建任务请求
|
|
|
|
|
* @param task 要创建的任务对象
|
|
|
|
|
* @throws NetworkFailureException 如果网络请求失败
|
|
|
|
|
*/
|
|
|
|
|
public void createTask(Task task) throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
@ -373,7 +453,7 @@ public class GTaskClient {
|
|
|
|
|
// 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,6 +466,12 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建任务列表
|
|
|
|
|
* 向Google Tasks服务器发送创建任务列表请求
|
|
|
|
|
* @param tasklist 要创建的任务列表对象
|
|
|
|
|
* @throws NetworkFailureException 如果网络请求失败
|
|
|
|
|
*/
|
|
|
|
|
public void createTaskList(TaskList tasklist) throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
@ -396,10 +482,10 @@ public class GTaskClient {
|
|
|
|
|
actionList.put(tasklist.getCreateAction(getActionId()));
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
// client version
|
|
|
|
|
// 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,6 +498,11 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 提交更新
|
|
|
|
|
* 将待更新的操作发送到Google Tasks服务器
|
|
|
|
|
* @throws NetworkFailureException 如果网络请求失败
|
|
|
|
|
*/
|
|
|
|
|
public void commitUpdate() throws NetworkFailureException {
|
|
|
|
|
if (mUpdateArray != null) {
|
|
|
|
|
try {
|
|
|
|
|
@ -433,10 +524,17 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加更新节点
|
|
|
|
|
* 将节点的更新操作添加到待更新列表中
|
|
|
|
|
* 如果待更新列表超过10项,则立即提交更新
|
|
|
|
|
* @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
|
|
|
|
|
// 过多的更新项可能导致错误
|
|
|
|
|
// 最大设置为10项
|
|
|
|
|
if (mUpdateArray != null && mUpdateArray.length() > 10) {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
}
|
|
|
|
|
@ -447,6 +545,14 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 移动任务
|
|
|
|
|
* 将任务从一个任务列表移动到另一个任务列表,或在同一任务列表内移动
|
|
|
|
|
* @param task 要移动的任务
|
|
|
|
|
* @param preParent 移动前的父任务列表
|
|
|
|
|
* @param curParent 移动后的父任务列表
|
|
|
|
|
* @throws NetworkFailureException 如果网络请求失败
|
|
|
|
|
*/
|
|
|
|
|
public void moveTask(Task task, TaskList preParent, TaskList curParent)
|
|
|
|
|
throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
@ -461,14 +567,13 @@ public class GTaskClient {
|
|
|
|
|
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
|
|
|
|
|
// 只有在同一任务列表内移动且不是第一个任务时,才设置prior_sibling_id
|
|
|
|
|
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
|
|
|
|
|
// 只有在不同任务列表之间移动时,才设置dest_list
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid());
|
|
|
|
|
}
|
|
|
|
|
actionList.put(action);
|
|
|
|
|
@ -486,6 +591,12 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除节点
|
|
|
|
|
* 将节点标记为已删除并发送到服务器
|
|
|
|
|
* @param node 要删除的节点
|
|
|
|
|
* @throws NetworkFailureException 如果网络请求失败
|
|
|
|
|
*/
|
|
|
|
|
public void deleteNode(Node node) throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
@ -509,6 +620,12 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取所有任务列表
|
|
|
|
|
* 从Google Tasks服务器获取用户的所有任务列表
|
|
|
|
|
* @return 任务列表的JSON数组
|
|
|
|
|
* @throws NetworkFailureException 如果网络请求失败
|
|
|
|
|
*/
|
|
|
|
|
public JSONArray getTaskLists() throws NetworkFailureException {
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
|
@ -547,6 +664,13 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取指定任务列表的任务
|
|
|
|
|
* 从Google Tasks服务器获取指定任务列表中的所有任务
|
|
|
|
|
* @param listGid 任务列表的GID
|
|
|
|
|
* @return 任务的JSON数组
|
|
|
|
|
* @throws NetworkFailureException 如果网络请求失败
|
|
|
|
|
*/
|
|
|
|
|
public JSONArray getTaskList(String listGid) throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
@ -575,10 +699,18 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取当前同步账户
|
|
|
|
|
* @return 当前使用的Google账户
|
|
|
|
|
*/
|
|
|
|
|
public Account getSyncAccount() {
|
|
|
|
|
return mAccount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 重置更新数组
|
|
|
|
|
* 清空待更新的操作列表
|
|
|
|
|
*/
|
|
|
|
|
public void resetUpdateArray() {
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
}
|
|
|
|
|
|