|
|
|
|
@ -60,698 +60,534 @@ import java.util.zip.GZIPInputStream;
|
|
|
|
|
import java.util.zip.Inflater;
|
|
|
|
|
import java.util.zip.InflaterInputStream;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GTaskClient 类
|
|
|
|
|
* Google Tasks API客户端,负责与Google Tasks服务器进行通信
|
|
|
|
|
* 使用单例模式,提供登录、创建任务、更新任务、获取任务列表等功能
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public class GTaskClient {
|
|
|
|
|
// 日志标签
|
|
|
|
|
private static final String TAG = GTaskClient.class.getSimpleName();
|
|
|
|
|
private static final String TAG = GTaskClient.class.getSimpleName(); // 日志标签
|
|
|
|
|
|
|
|
|
|
// Google Tasks基础URL(用于普通访问)
|
|
|
|
|
// Google Tasks API URL常量
|
|
|
|
|
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提交数据的URL
|
|
|
|
|
private static final String GTASK_POST_URL = "https://mail.google.com/tasks/r/ig";
|
|
|
|
|
|
|
|
|
|
// 单例实例
|
|
|
|
|
private static GTaskClient mInstance = null;
|
|
|
|
|
|
|
|
|
|
// HTTP客户端,用于发送网络请求
|
|
|
|
|
private DefaultHttpClient mHttpClient;
|
|
|
|
|
// 获取数据的URL(可能根据账户域名变化)
|
|
|
|
|
private String mGetUrl;
|
|
|
|
|
// 提交数据的URL(可能根据账户域名变化)
|
|
|
|
|
private String mPostUrl;
|
|
|
|
|
// 客户端版本号(从服务器获取)
|
|
|
|
|
private long mClientVersion;
|
|
|
|
|
// 登录状态标志
|
|
|
|
|
private boolean mLoggedin;
|
|
|
|
|
// 最后登录时间戳
|
|
|
|
|
private long mLastLoginTime;
|
|
|
|
|
// 动作ID计数器(用于标识每个请求)
|
|
|
|
|
private int mActionId;
|
|
|
|
|
// 当前同步的Google账户
|
|
|
|
|
private Account mAccount;
|
|
|
|
|
// 待提交的更新数组(批量更新用)
|
|
|
|
|
private JSONArray mUpdateArray;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 私有构造函数
|
|
|
|
|
* 初始化所有字段为默认值
|
|
|
|
|
*/
|
|
|
|
|
private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig"; // 获取数据URL
|
|
|
|
|
private static final String GTASK_POST_URL = "https://mail.google.com/tasks/r/ig"; // 提交数据URL
|
|
|
|
|
|
|
|
|
|
private static GTaskClient mInstance = null; // 单例实例
|
|
|
|
|
|
|
|
|
|
// HTTP客户端和相关参数
|
|
|
|
|
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计数器,用于生成唯一的操作ID
|
|
|
|
|
private Account mAccount; // 当前登录的Google账户
|
|
|
|
|
private JSONArray mUpdateArray; // 批量更新操作的JSON数组
|
|
|
|
|
|
|
|
|
|
private GTaskClient() {
|
|
|
|
|
// 初始化所有成员变量
|
|
|
|
|
mHttpClient = null;
|
|
|
|
|
mGetUrl = GTASK_GET_URL; // 默认获取URL
|
|
|
|
|
mPostUrl = GTASK_POST_URL; // 默认提交URL
|
|
|
|
|
mClientVersion = -1; // 未获取版本号
|
|
|
|
|
mLoggedin = false; // 未登录状态
|
|
|
|
|
mLastLoginTime = 0; // 最后登录时间为0
|
|
|
|
|
mActionId = 1; // 动作ID从1开始
|
|
|
|
|
mAccount = null; // 未设置账户
|
|
|
|
|
mUpdateArray = null; // 无待提交更新
|
|
|
|
|
mGetUrl = GTASK_GET_URL; // 默认GET URL
|
|
|
|
|
mPostUrl = GTASK_POST_URL; // 默认POST URL
|
|
|
|
|
mClientVersion = -1; // 未初始化的版本号
|
|
|
|
|
mLoggedin = false; // 初始状态为未登录
|
|
|
|
|
mLastLoginTime = 0; // 初始登录时间为0
|
|
|
|
|
mActionId = 1; // 操作ID从1开始
|
|
|
|
|
mAccount = null; // 初始账户为空
|
|
|
|
|
mUpdateArray = null; // 初始更新数组为空
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取GTaskClient单例实例
|
|
|
|
|
*
|
|
|
|
|
* @return GTaskClient单例实例
|
|
|
|
|
*/
|
|
|
|
|
// 获取单例实例
|
|
|
|
|
public static synchronized GTaskClient getInstance() {
|
|
|
|
|
if (mInstance == null) {
|
|
|
|
|
mInstance = new GTaskClient();
|
|
|
|
|
mInstance = new GTaskClient(); // 第一次调用时创建实例
|
|
|
|
|
}
|
|
|
|
|
return mInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 登录到Google Tasks
|
|
|
|
|
* 处理Google账户认证和Tasks服务登录
|
|
|
|
|
*
|
|
|
|
|
* @param activity 活动上下文,用于账户管理器交互
|
|
|
|
|
* @return 登录是否成功
|
|
|
|
|
*/
|
|
|
|
|
// 登录方法,返回登录是否成功
|
|
|
|
|
public boolean login(Activity activity) {
|
|
|
|
|
// 假设Cookie在5分钟后过期,需要重新登录
|
|
|
|
|
final long interval = 1000 * 60 * 5; // 5分钟
|
|
|
|
|
// 检查Cookie是否过期(假设5分钟后过期)
|
|
|
|
|
final long interval = 1000 * 60 * 5; // 5分钟的毫秒数
|
|
|
|
|
if (mLastLoginTime + interval < System.currentTimeMillis()) {
|
|
|
|
|
mLoggedin = false; // 超过5分钟,标记为未登录
|
|
|
|
|
mLoggedin = false; // 如果超过5分钟,标记为未登录
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 切换账户后需要重新登录
|
|
|
|
|
// 检查账户是否切换,需要重新登录
|
|
|
|
|
if (mLoggedin
|
|
|
|
|
&& !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity
|
|
|
|
|
.getSyncAccountName(activity))) {
|
|
|
|
|
mLoggedin = false; // 账户不匹配,标记为未登录
|
|
|
|
|
mLoggedin = false; // 账户已切换,需要重新登录
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果已经登录,直接返回成功
|
|
|
|
|
if (mLoggedin) {
|
|
|
|
|
Log.d(TAG, "already logged in");
|
|
|
|
|
Log.d(TAG, "already logged in"); // 已经登录,直接返回
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 记录当前时间为最后登录时间
|
|
|
|
|
mLastLoginTime = System.currentTimeMillis();
|
|
|
|
|
|
|
|
|
|
// 获取Google账户认证令牌
|
|
|
|
|
String authToken = loginGoogleAccount(activity, false);
|
|
|
|
|
mLastLoginTime = System.currentTimeMillis(); // 更新最后登录时间
|
|
|
|
|
String authToken = loginGoogleAccount(activity, false); // 获取Google账户授权令牌
|
|
|
|
|
if (authToken == null) {
|
|
|
|
|
Log.e(TAG, "login google account failed");
|
|
|
|
|
return false; // 获取认证令牌失败
|
|
|
|
|
Log.e(TAG, "login google account failed"); // 获取授权令牌失败
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果账户不是gmail.com或googlemail.com,使用自定义域名登录
|
|
|
|
|
// 如果不是gmail.com或googlemail.com账户,尝试使用自定义域名登录
|
|
|
|
|
if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase()
|
|
|
|
|
.endsWith("googlemail.com"))) {
|
|
|
|
|
StringBuilder url = new StringBuilder(GTASK_URL).append("a/");
|
|
|
|
|
int index = mAccount.name.indexOf('@') + 1;
|
|
|
|
|
String suffix = mAccount.name.substring(index);
|
|
|
|
|
url.append(suffix + "/");
|
|
|
|
|
mGetUrl = url.toString() + "ig"; // 设置自定义获取URL
|
|
|
|
|
mPostUrl = url.toString() + "r/ig"; // 设置自定义提交URL
|
|
|
|
|
|
|
|
|
|
// 尝试使用自定义域名登录
|
|
|
|
|
if (tryToLoginGtask(activity, authToken)) {
|
|
|
|
|
StringBuilder url = new StringBuilder(GTASK_URL).append("a/"); // 构建自定义域名URL
|
|
|
|
|
int index = mAccount.name.indexOf('@') + 1; // 找到@符号位置
|
|
|
|
|
String suffix = mAccount.name.substring(index); // 提取域名后缀
|
|
|
|
|
url.append(suffix + "/"); // 添加域名后缀
|
|
|
|
|
mGetUrl = url.toString() + "ig"; // 设置自定义GET URL
|
|
|
|
|
mPostUrl = url.toString() + "r/ig"; // 设置自定义POST URL
|
|
|
|
|
|
|
|
|
|
if (tryToLoginGtask(activity, authToken)) { // 尝试使用自定义域名登录
|
|
|
|
|
mLoggedin = true; // 登录成功
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果自定义域名登录失败或不是自定义域名,尝试使用官方URL登录
|
|
|
|
|
// 如果自定义域名登录失败,尝试使用Google官方URL登录
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
mGetUrl = GTASK_GET_URL; // 恢复默认获取URL
|
|
|
|
|
mPostUrl = GTASK_POST_URL; // 恢复默认提交URL
|
|
|
|
|
if (!tryToLoginGtask(activity, authToken)) {
|
|
|
|
|
mGetUrl = GTASK_GET_URL; // 重置为默认GET URL
|
|
|
|
|
mPostUrl = GTASK_POST_URL; // 重置为默认POST URL
|
|
|
|
|
if (!tryToLoginGtask(activity, authToken)) { // 尝试使用官方URL登录
|
|
|
|
|
return false; // 登录失败
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mLoggedin = true; // 标记为已登录
|
|
|
|
|
return true; // 登录成功
|
|
|
|
|
return true; // 登录成功
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 登录Google账户并获取认证令牌
|
|
|
|
|
*
|
|
|
|
|
* @param activity 活动上下文
|
|
|
|
|
* @param invalidateToken 是否使旧令牌失效
|
|
|
|
|
* @return 认证令牌,失败返回null
|
|
|
|
|
*/
|
|
|
|
|
// 登录Google账户并获取授权令牌
|
|
|
|
|
private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
|
|
|
|
|
String authToken;
|
|
|
|
|
AccountManager accountManager = AccountManager.get(activity);
|
|
|
|
|
|
|
|
|
|
// 获取所有Google账户
|
|
|
|
|
Account[] accounts = accountManager.getAccountsByType("com.google");
|
|
|
|
|
AccountManager accountManager = AccountManager.get(activity); // 获取账户管理器
|
|
|
|
|
Account[] accounts = accountManager.getAccountsByType("com.google"); // 获取所有Google账户
|
|
|
|
|
|
|
|
|
|
if (accounts.length == 0) {
|
|
|
|
|
Log.e(TAG, "there is no available google account");
|
|
|
|
|
return null; // 没有可用的Google账户
|
|
|
|
|
Log.e(TAG, "there is no available google account"); // 没有可用的Google账户
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从设置中获取同步账户名称
|
|
|
|
|
// 获取设置中配置的同步账户名
|
|
|
|
|
String accountName = NotesPreferenceActivity.getSyncAccountName(activity);
|
|
|
|
|
Account account = null;
|
|
|
|
|
|
|
|
|
|
// 查找与设置中名称匹配的账户
|
|
|
|
|
for (Account a : accounts) {
|
|
|
|
|
if (a.name.equals(accountName)) {
|
|
|
|
|
if (a.name.equals(accountName)) { // 查找匹配的账户
|
|
|
|
|
account = a;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (account != null) {
|
|
|
|
|
mAccount = account; // 设置当前账户
|
|
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, "unable to get an account with the same name in the settings");
|
|
|
|
|
return null; // 找不到匹配的账户
|
|
|
|
|
Log.e(TAG, "unable to get an account with the same name in the settings"); // 未找到匹配账户
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取认证令牌
|
|
|
|
|
// 获取授权令牌
|
|
|
|
|
AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account,
|
|
|
|
|
"goanna_mobile", null, activity, null, null);
|
|
|
|
|
"goanna_mobile", null, activity, null, null); // 请求授权令牌
|
|
|
|
|
try {
|
|
|
|
|
Bundle authTokenBundle = accountManagerFuture.getResult();
|
|
|
|
|
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
|
|
|
|
|
|
|
|
|
|
// 如果需要使令牌失效
|
|
|
|
|
if (invalidateToken) {
|
|
|
|
|
accountManager.invalidateAuthToken("com.google", authToken);
|
|
|
|
|
// 重新获取新令牌
|
|
|
|
|
return loginGoogleAccount(activity, false);
|
|
|
|
|
Bundle authTokenBundle = accountManagerFuture.getResult(); // 获取结果
|
|
|
|
|
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN); // 提取授权令牌
|
|
|
|
|
if (invalidateToken) { // 如果需要使令牌失效
|
|
|
|
|
accountManager.invalidateAuthToken("com.google", authToken); // 使当前令牌失效
|
|
|
|
|
loginGoogleAccount(activity, false); // 重新获取令牌
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "get auth token failed");
|
|
|
|
|
authToken = null; // 获取令牌失败
|
|
|
|
|
Log.e(TAG, "get auth token failed"); // 获取授权令牌失败
|
|
|
|
|
authToken = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return authToken;
|
|
|
|
|
return authToken; // 返回授权令牌
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 尝试登录Google Tasks服务
|
|
|
|
|
*
|
|
|
|
|
* @param activity 活动上下文
|
|
|
|
|
* @param authToken 认证令牌
|
|
|
|
|
* @return 登录是否成功
|
|
|
|
|
*/
|
|
|
|
|
// 尝试登录Google Tasks
|
|
|
|
|
private boolean tryToLoginGtask(Activity activity, String authToken) {
|
|
|
|
|
// 第一次尝试登录
|
|
|
|
|
if (!loginGtask(authToken)) {
|
|
|
|
|
// 令牌可能已过期,使令牌失效并重新获取
|
|
|
|
|
authToken = loginGoogleAccount(activity, true);
|
|
|
|
|
if (!loginGtask(authToken)) { // 第一次登录尝试
|
|
|
|
|
// 授权令牌可能已过期,尝试重新获取令牌并登录
|
|
|
|
|
authToken = loginGoogleAccount(activity, true); // 重新获取授权令牌
|
|
|
|
|
if (authToken == null) {
|
|
|
|
|
Log.e(TAG, "login google account failed");
|
|
|
|
|
return false; // 重新获取令牌失败
|
|
|
|
|
Log.e(TAG, "login google account failed"); // 重新获取令牌失败
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 使用新令牌再次尝试登录
|
|
|
|
|
if (!loginGtask(authToken)) {
|
|
|
|
|
Log.e(TAG, "login gtask failed");
|
|
|
|
|
return false; // 再次登录失败
|
|
|
|
|
if (!loginGtask(authToken)) { // 使用新令牌再次尝试登录
|
|
|
|
|
Log.e(TAG, "login gtask failed"); // 登录失败
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true; // 登录成功
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 实际执行Google Tasks登录
|
|
|
|
|
* 发送HTTP请求获取Cookie和客户端版本
|
|
|
|
|
*
|
|
|
|
|
* @param authToken 认证令牌
|
|
|
|
|
* @return 登录是否成功
|
|
|
|
|
*/
|
|
|
|
|
// 实际的Google Tasks登录逻辑
|
|
|
|
|
private boolean loginGtask(String authToken) {
|
|
|
|
|
// 设置HTTP连接参数
|
|
|
|
|
int timeoutConnection = 10000; // 连接超时10秒
|
|
|
|
|
int timeoutSocket = 15000; // 套接字超时15秒
|
|
|
|
|
HttpParams httpParameters = new BasicHttpParams();
|
|
|
|
|
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
|
|
|
|
|
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
|
|
|
|
|
|
|
|
|
|
// 创建HTTP客户端
|
|
|
|
|
mHttpClient = new DefaultHttpClient(httpParameters);
|
|
|
|
|
BasicCookieStore localBasicCookieStore = new BasicCookieStore();
|
|
|
|
|
int timeoutConnection = 10000; // 连接超时时间(10秒)
|
|
|
|
|
int timeoutSocket = 15000; // Socket超时时间(15秒)
|
|
|
|
|
HttpParams httpParameters = new BasicHttpParams(); // 创建HTTP参数
|
|
|
|
|
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); // 设置连接超时
|
|
|
|
|
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); // 设置Socket超时
|
|
|
|
|
mHttpClient = new DefaultHttpClient(httpParameters); // 创建HTTP客户端
|
|
|
|
|
BasicCookieStore localBasicCookieStore = new BasicCookieStore(); // 创建Cookie存储
|
|
|
|
|
mHttpClient.setCookieStore(localBasicCookieStore); // 设置Cookie存储
|
|
|
|
|
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false); // 禁用Expect-Continue
|
|
|
|
|
|
|
|
|
|
// 登录Google Tasks
|
|
|
|
|
try {
|
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken;
|
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl);
|
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken; // 构建登录URL
|
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl); // 创建GET请求
|
|
|
|
|
HttpResponse response = null;
|
|
|
|
|
response = mHttpClient.execute(httpGet); // 执行GET请求
|
|
|
|
|
response = mHttpClient.execute(httpGet); // 执行HTTP请求
|
|
|
|
|
|
|
|
|
|
// 检查是否获取到认证Cookie
|
|
|
|
|
List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
|
|
|
|
|
// 检查Cookie中是否包含认证信息
|
|
|
|
|
List<Cookie> cookies = mHttpClient.getCookieStore().getCookies(); // 获取所有Cookie
|
|
|
|
|
boolean hasAuthCookie = false;
|
|
|
|
|
for (Cookie cookie : cookies) {
|
|
|
|
|
if (cookie.getName().contains("GTL")) {
|
|
|
|
|
hasAuthCookie = true; // 找到认证Cookie
|
|
|
|
|
if (cookie.getName().contains("GTL")) { // 查找包含"GTL"的Cookie(Google Tasks认证)
|
|
|
|
|
hasAuthCookie = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!hasAuthCookie) {
|
|
|
|
|
Log.w(TAG, "it seems that there is no auth cookie");
|
|
|
|
|
Log.w(TAG, "it seems that there is no auth cookie"); // 警告:未找到认证Cookie
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从响应中解析客户端版本号
|
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
|
|
int begin = resString.indexOf(jsBegin);
|
|
|
|
|
int end = resString.lastIndexOf(jsEnd);
|
|
|
|
|
// 从响应中提取客户端版本号
|
|
|
|
|
String resString = getResponseContent(response.getEntity()); // 获取响应内容
|
|
|
|
|
String jsBegin = "_setup("; // JavaScript响应开始标记
|
|
|
|
|
String jsEnd = ")}</script>"; // JavaScript响应结束标记
|
|
|
|
|
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);
|
|
|
|
|
if (begin != -1 && end != -1 && begin < end) { // 确保找到有效位置
|
|
|
|
|
jsString = resString.substring(begin + jsBegin.length(), end); // 提取JavaScript字符串
|
|
|
|
|
}
|
|
|
|
|
JSONObject js = new JSONObject(jsString);
|
|
|
|
|
JSONObject js = new JSONObject(jsString); // 解析为JSON对象
|
|
|
|
|
mClientVersion = js.getLong("v"); // 获取客户端版本号
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
Log.e(TAG, e.toString()); // JSON解析异常
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false; // JSON解析失败
|
|
|
|
|
return false;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// 捕获所有异常
|
|
|
|
|
Log.e(TAG, "httpget gtask_url failed");
|
|
|
|
|
return false; // HTTP请求失败
|
|
|
|
|
// 捕获所有其他异常
|
|
|
|
|
Log.e(TAG, "httpget gtask_url failed"); // HTTP GET请求失败
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true; // 登录成功
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取下一个动作ID
|
|
|
|
|
* 每次调用递增,确保每个请求有唯一ID
|
|
|
|
|
*
|
|
|
|
|
* @return 动作ID
|
|
|
|
|
*/
|
|
|
|
|
// 获取下一个操作ID
|
|
|
|
|
private int getActionId() {
|
|
|
|
|
return mActionId++;
|
|
|
|
|
return mActionId++; // 返回当前操作ID并递增
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建HTTP POST请求对象
|
|
|
|
|
* 设置必要的请求头
|
|
|
|
|
*
|
|
|
|
|
* @return 配置好的HttpPost对象
|
|
|
|
|
*/
|
|
|
|
|
// 创建HTTP POST请求对象
|
|
|
|
|
private HttpPost createHttpPost() {
|
|
|
|
|
HttpPost httpPost = new HttpPost(mPostUrl);
|
|
|
|
|
// 设置请求头
|
|
|
|
|
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
|
|
|
|
|
httpPost.setHeader("AT", "1"); // 认证令牌头
|
|
|
|
|
HttpPost httpPost = new HttpPost(mPostUrl); // 创建POST请求
|
|
|
|
|
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); // 设置内容类型
|
|
|
|
|
httpPost.setHeader("AT", "1"); // 设置AT头部(认证令牌)
|
|
|
|
|
return httpPost;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 从HTTP响应实体获取内容
|
|
|
|
|
* 支持GZIP和Deflate压缩格式
|
|
|
|
|
*
|
|
|
|
|
* @param entity HTTP响应实体
|
|
|
|
|
* @return 响应内容字符串
|
|
|
|
|
* @throws IOException 读取响应内容时发生错误
|
|
|
|
|
*/
|
|
|
|
|
// 从HTTP实体获取响应内容,支持GZIP和Deflate压缩
|
|
|
|
|
private String getResponseContent(HttpEntity entity) throws IOException {
|
|
|
|
|
String contentEncoding = null;
|
|
|
|
|
if (entity.getContentEncoding() != null) {
|
|
|
|
|
contentEncoding = entity.getContentEncoding().getValue();
|
|
|
|
|
Log.d(TAG, "encoding: " + contentEncoding);
|
|
|
|
|
contentEncoding = entity.getContentEncoding().getValue(); // 获取内容编码
|
|
|
|
|
Log.d(TAG, "encoding: " + contentEncoding); // 记录编码类型
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InputStream input = entity.getContent();
|
|
|
|
|
// 根据压缩格式创建相应的输入流
|
|
|
|
|
InputStream input = entity.getContent(); // 获取输入流
|
|
|
|
|
if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
|
|
|
|
|
input = new GZIPInputStream(entity.getContent()); // GZIP解压
|
|
|
|
|
input = new GZIPInputStream(entity.getContent()); // 如果是GZIP编码,使用GZIP输入流
|
|
|
|
|
} else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {
|
|
|
|
|
Inflater inflater = new Inflater(true);
|
|
|
|
|
input = new InflaterInputStream(entity.getContent(), inflater); // Deflate解压
|
|
|
|
|
Inflater inflater = new Inflater(true); // 创建Inflater对象
|
|
|
|
|
input = new InflaterInputStream(entity.getContent(), inflater); // 使用Inflater输入流
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 读取响应内容
|
|
|
|
|
InputStreamReader isr = new InputStreamReader(input);
|
|
|
|
|
BufferedReader br = new BufferedReader(isr);
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
InputStreamReader isr = new InputStreamReader(input); // 创建输入流读取器
|
|
|
|
|
BufferedReader br = new BufferedReader(isr); // 创建缓冲读取器
|
|
|
|
|
StringBuilder sb = new StringBuilder(); // 创建字符串构建器
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
String buff = br.readLine();
|
|
|
|
|
String buff = br.readLine(); // 读取一行
|
|
|
|
|
if (buff == null) {
|
|
|
|
|
return sb.toString(); // 读取完成
|
|
|
|
|
return sb.toString(); // 读取完成,返回内容
|
|
|
|
|
}
|
|
|
|
|
sb = sb.append(buff);
|
|
|
|
|
sb = sb.append(buff); // 添加到字符串构建器
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
input.close(); // 确保关闭输入流
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送POST请求到Google Tasks服务器
|
|
|
|
|
*
|
|
|
|
|
* @param js 要发送的JSON对象
|
|
|
|
|
* @return 服务器响应的JSON对象
|
|
|
|
|
* @throws NetworkFailureException 网络请求失败
|
|
|
|
|
*/
|
|
|
|
|
// 发送POST请求到Google Tasks API
|
|
|
|
|
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
|
|
|
|
|
// 检查登录状态
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
|
Log.e(TAG, "please login first"); // 未登录错误
|
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HttpPost httpPost = createHttpPost();
|
|
|
|
|
HttpPost httpPost = createHttpPost(); // 创建POST请求
|
|
|
|
|
try {
|
|
|
|
|
// 准备请求参数
|
|
|
|
|
LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
|
|
|
|
|
list.add(new BasicNameValuePair("r", js.toString())); // JSON数据作为参数
|
|
|
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
|
|
|
|
|
httpPost.setEntity(entity);
|
|
|
|
|
LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>(); // 创建参数列表
|
|
|
|
|
list.add(new BasicNameValuePair("r", js.toString())); // 添加JSON参数
|
|
|
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8"); // 创建URL编码实体
|
|
|
|
|
httpPost.setEntity(entity); // 设置请求实体
|
|
|
|
|
|
|
|
|
|
// 执行POST请求
|
|
|
|
|
HttpResponse response = mHttpClient.execute(httpPost);
|
|
|
|
|
String jsString = getResponseContent(response.getEntity());
|
|
|
|
|
return new JSONObject(jsString); // 解析响应为JSON对象
|
|
|
|
|
HttpResponse response = mHttpClient.execute(httpPost); // 执行请求
|
|
|
|
|
String jsString = getResponseContent(response.getEntity()); // 获取响应内容
|
|
|
|
|
return new JSONObject(jsString); // 解析为JSON对象并返回
|
|
|
|
|
|
|
|
|
|
} catch (ClientProtocolException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
Log.e(TAG, e.toString()); // HTTP协议异常
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new NetworkFailureException("postRequest failed");
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
Log.e(TAG, e.toString()); // IO异常
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new NetworkFailureException("postRequest failed");
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
Log.e(TAG, e.toString()); // JSON解析异常
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("unable to convert response content to jsonobject");
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
Log.e(TAG, e.toString()); // 其他异常
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("error occurs when posting request");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 在Google Tasks中创建新任务
|
|
|
|
|
*
|
|
|
|
|
* @param task 要创建的任务对象
|
|
|
|
|
* @throws NetworkFailureException 网络请求失败
|
|
|
|
|
*/
|
|
|
|
|
// 创建任务
|
|
|
|
|
public void createTask(Task task) throws NetworkFailureException {
|
|
|
|
|
commitUpdate(); // 先提交所有待更新
|
|
|
|
|
commitUpdate(); // 提交之前的更新
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONObject jsPost = new JSONObject(); // 创建POST请求JSON
|
|
|
|
|
JSONArray actionList = new JSONArray(); // 创建操作列表
|
|
|
|
|
|
|
|
|
|
// 构建动作列表
|
|
|
|
|
actionList.put(task.getCreateAction(getActionId()));
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
// 添加创建任务的操作
|
|
|
|
|
actionList.put(task.getCreateAction(getActionId())); // 获取任务的创建操作
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // 添加操作列表
|
|
|
|
|
|
|
|
|
|
// 添加客户端版本
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送请求
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
// 从响应中提取新任务的ID
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost); // 发送POST请求
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
|
task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0); // 获取结果数组的第一个元素
|
|
|
|
|
task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID)); // 设置任务的GID
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
Log.e(TAG, e.toString()); // JSON处理异常
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("create task: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 在Google Tasks中创建新任务列表
|
|
|
|
|
*
|
|
|
|
|
* @param tasklist 要创建的任务列表对象
|
|
|
|
|
* @throws NetworkFailureException 网络请求失败
|
|
|
|
|
*/
|
|
|
|
|
// 创建任务列表
|
|
|
|
|
public void createTaskList(TaskList tasklist) throws NetworkFailureException {
|
|
|
|
|
commitUpdate(); // 先提交所有待更新
|
|
|
|
|
commitUpdate(); // 提交之前的更新
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONObject jsPost = new JSONObject(); // 创建POST请求JSON
|
|
|
|
|
JSONArray actionList = new JSONArray(); // 创建操作列表
|
|
|
|
|
|
|
|
|
|
// 构建动作列表
|
|
|
|
|
actionList.put(tasklist.getCreateAction(getActionId()));
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
// 添加创建任务列表的操作
|
|
|
|
|
actionList.put(tasklist.getCreateAction(getActionId())); // 获取任务列表的创建操作
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // 添加操作列表
|
|
|
|
|
|
|
|
|
|
// 添加客户端版本
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送请求
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
// 从响应中提取新任务列表的ID
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost); // 发送POST请求
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
|
tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0); // 获取结果数组的第一个元素
|
|
|
|
|
tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID)); // 设置任务列表的GID
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
Log.e(TAG, e.toString()); // JSON处理异常
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("create tasklist: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 提交所有待更新的操作
|
|
|
|
|
* 批量提交更新,提高效率
|
|
|
|
|
*
|
|
|
|
|
* @throws NetworkFailureException 网络请求失败
|
|
|
|
|
*/
|
|
|
|
|
// 提交批量更新
|
|
|
|
|
public void commitUpdate() throws NetworkFailureException {
|
|
|
|
|
if (mUpdateArray != null) {
|
|
|
|
|
if (mUpdateArray != null) { // 如果有待提交的更新
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONObject jsPost = new JSONObject(); // 创建POST请求JSON
|
|
|
|
|
|
|
|
|
|
// 添加动作列表
|
|
|
|
|
// 添加操作列表
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray);
|
|
|
|
|
|
|
|
|
|
// 添加客户端版本
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送请求
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
postRequest(jsPost); // 发送请求
|
|
|
|
|
mUpdateArray = null; // 清空更新数组
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
Log.e(TAG, e.toString()); // JSON处理异常
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("commit update: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加节点更新到待提交数组
|
|
|
|
|
* 当更新项过多时自动提交
|
|
|
|
|
*
|
|
|
|
|
* @param node 要更新的节点
|
|
|
|
|
* @throws NetworkFailureException 网络请求失败
|
|
|
|
|
*/
|
|
|
|
|
// 添加节点到更新数组
|
|
|
|
|
public void addUpdateNode(Node node) throws NetworkFailureException {
|
|
|
|
|
if (node != null) {
|
|
|
|
|
// 更新项过多时自动提交(最多10项)
|
|
|
|
|
// 如果更新数组过大(超过10个),先提交当前更新
|
|
|
|
|
if (mUpdateArray != null && mUpdateArray.length() > 10) {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
commitUpdate(); // 提交当前更新
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化更新数组(如果为空)
|
|
|
|
|
if (mUpdateArray == null)
|
|
|
|
|
if (mUpdateArray == null) // 如果更新数组为空,创建新数组
|
|
|
|
|
mUpdateArray = new JSONArray();
|
|
|
|
|
// 添加更新动作
|
|
|
|
|
mUpdateArray.put(node.getUpdateAction(getActionId()));
|
|
|
|
|
mUpdateArray.put(node.getUpdateAction(getActionId())); // 添加节点的更新操作
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 移动任务到新的任务列表
|
|
|
|
|
*
|
|
|
|
|
* @param task 要移动的任务
|
|
|
|
|
* @param preParent 原始父任务列表
|
|
|
|
|
* @param curParent 目标父任务列表
|
|
|
|
|
* @throws NetworkFailureException 网络请求失败
|
|
|
|
|
*/
|
|
|
|
|
// 移动任务
|
|
|
|
|
public void moveTask(Task task, TaskList preParent, TaskList curParent)
|
|
|
|
|
throws NetworkFailureException {
|
|
|
|
|
commitUpdate(); // 先提交所有待更新
|
|
|
|
|
commitUpdate(); // 提交之前的更新
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONObject action = new JSONObject();
|
|
|
|
|
JSONObject jsPost = new JSONObject(); // 创建POST请求JSON
|
|
|
|
|
JSONArray actionList = new JSONArray(); // 创建操作列表
|
|
|
|
|
JSONObject action = new JSONObject(); // 创建移动操作
|
|
|
|
|
|
|
|
|
|
// 构建移动动作
|
|
|
|
|
// 构建移动操作
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
|
|
// 在同一任务列表内移动时,设置前兄弟节点ID
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_MOVE); // 设置操作类型为移动
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId()); // 设置操作ID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ID, task.getGid()); // 设置任务ID
|
|
|
|
|
if (preParent == curParent && task.getPriorSibling() != null) {
|
|
|
|
|
// 如果是在同一个任务列表中移动,且任务不是第一个,设置前一个兄弟节点的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());
|
|
|
|
|
|
|
|
|
|
// 在不同任务列表间移动时,设置目标列表ID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid()); // 设置源列表ID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid()); // 设置目标父节点ID
|
|
|
|
|
if (preParent != curParent) {
|
|
|
|
|
// 如果是在不同任务列表之间移动,设置目标列表ID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
actionList.put(action);
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
actionList.put(action); // 将操作添加到操作列表
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // 添加操作列表到请求
|
|
|
|
|
|
|
|
|
|
// 添加客户端版本
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送请求
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
postRequest(jsPost); // 发送请求
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
Log.e(TAG, e.toString()); // JSON处理异常
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("move task: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除节点(任务或任务列表)
|
|
|
|
|
*
|
|
|
|
|
* @param node 要删除的节点
|
|
|
|
|
* @throws NetworkFailureException 网络请求失败
|
|
|
|
|
*/
|
|
|
|
|
// 删除节点
|
|
|
|
|
public void deleteNode(Node node) throws NetworkFailureException {
|
|
|
|
|
commitUpdate(); // 先提交所有待更新
|
|
|
|
|
commitUpdate(); // 提交之前的更新
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONObject jsPost = new JSONObject(); // 创建POST请求JSON
|
|
|
|
|
JSONArray actionList = new JSONArray(); // 创建操作列表
|
|
|
|
|
|
|
|
|
|
// 设置节点为删除状态
|
|
|
|
|
node.setDeleted(true);
|
|
|
|
|
// 构建更新动作
|
|
|
|
|
actionList.put(node.getUpdateAction(getActionId()));
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
// 添加删除操作
|
|
|
|
|
node.setDeleted(true); // 标记节点为已删除
|
|
|
|
|
actionList.put(node.getUpdateAction(getActionId())); // 获取节点的更新操作(包含删除标记)
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // 添加操作列表
|
|
|
|
|
|
|
|
|
|
// 添加客户端版本
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送请求
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
postRequest(jsPost); // 发送请求
|
|
|
|
|
mUpdateArray = null; // 清空更新数组
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
Log.e(TAG, e.toString()); // JSON处理异常
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("delete node: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取所有任务列表
|
|
|
|
|
*
|
|
|
|
|
* @return 任务列表的JSON数组
|
|
|
|
|
* @throws NetworkFailureException 网络请求失败
|
|
|
|
|
*/
|
|
|
|
|
// 获取所有任务列表
|
|
|
|
|
public JSONArray getTaskLists() throws NetworkFailureException {
|
|
|
|
|
// 检查登录状态
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
|
Log.e(TAG, "please login first"); // 未登录错误
|
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
HttpGet httpGet = new HttpGet(mGetUrl);
|
|
|
|
|
HttpGet httpGet = new HttpGet(mGetUrl); // 创建GET请求
|
|
|
|
|
HttpResponse response = null;
|
|
|
|
|
response = mHttpClient.execute(httpGet); // 执行GET请求
|
|
|
|
|
|
|
|
|
|
// 解析响应内容
|
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
|
|
int begin = resString.indexOf(jsBegin);
|
|
|
|
|
int end = resString.lastIndexOf(jsEnd);
|
|
|
|
|
response = mHttpClient.execute(httpGet); // 执行请求
|
|
|
|
|
|
|
|
|
|
// 从响应中提取任务列表数据
|
|
|
|
|
String resString = getResponseContent(response.getEntity()); // 获取响应内容
|
|
|
|
|
String jsBegin = "_setup("; // JavaScript响应开始标记
|
|
|
|
|
String jsEnd = ")}</script>"; // JavaScript响应结束标记
|
|
|
|
|
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);
|
|
|
|
|
if (begin != -1 && end != -1 && begin < end) { // 确保找到有效位置
|
|
|
|
|
jsString = resString.substring(begin + jsBegin.length(), end); // 提取JavaScript字符串
|
|
|
|
|
}
|
|
|
|
|
JSONObject js = new JSONObject(jsString);
|
|
|
|
|
// 提取任务列表数组
|
|
|
|
|
return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS);
|
|
|
|
|
|
|
|
|
|
JSONObject js = new JSONObject(jsString); // 解析为JSON对象
|
|
|
|
|
return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS); // 返回任务列表数组
|
|
|
|
|
} catch (ClientProtocolException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
Log.e(TAG, e.toString()); // HTTP协议异常
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new NetworkFailureException("gettasklists: httpget failed");
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
Log.e(TAG, e.toString()); // IO异常
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new NetworkFailureException("gettasklists: httpget failed");
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
Log.e(TAG, e.toString()); // JSON解析异常
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("get task lists: handing jasonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取指定任务列表的所有任务
|
|
|
|
|
*
|
|
|
|
|
* @param listGid 任务列表的Google ID
|
|
|
|
|
* @return 任务的JSON数组
|
|
|
|
|
* @throws NetworkFailureException 网络请求失败
|
|
|
|
|
*/
|
|
|
|
|
// 获取特定任务列表的所有任务
|
|
|
|
|
public JSONArray getTaskList(String listGid) throws NetworkFailureException {
|
|
|
|
|
commitUpdate(); // 先提交所有待更新
|
|
|
|
|
commitUpdate(); // 提交之前的更新
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONObject action = new JSONObject();
|
|
|
|
|
JSONObject jsPost = new JSONObject(); // 创建POST请求JSON
|
|
|
|
|
JSONArray actionList = new JSONArray(); // 创建操作列表
|
|
|
|
|
JSONObject action = new JSONObject(); // 创建获取所有任务的操作
|
|
|
|
|
|
|
|
|
|
// 构建获取所有任务的动作
|
|
|
|
|
// 构建获取所有任务的操作
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL);
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid);
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false); // 不获取已删除的任务
|
|
|
|
|
actionList.put(action);
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL); // 设置操作类型为获取所有
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId()); // 设置操作ID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid); // 设置任务列表ID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false); // 设置不获取已删除的任务
|
|
|
|
|
actionList.put(action); // 将操作添加到操作列表
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // 添加操作列表到请求
|
|
|
|
|
|
|
|
|
|
// 添加客户端版本
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送请求并获取响应
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS);
|
|
|
|
|
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost); // 发送请求
|
|
|
|
|
return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS); // 返回任务数组
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
Log.e(TAG, e.toString()); // JSON处理异常
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("get task list: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取当前同步的账户
|
|
|
|
|
*
|
|
|
|
|
* @return 当前同步的Google账户
|
|
|
|
|
*/
|
|
|
|
|
// 获取当前同步账户
|
|
|
|
|
public Account getSyncAccount() {
|
|
|
|
|
return mAccount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 重置更新数组
|
|
|
|
|
* 清空所有待提交的更新
|
|
|
|
|
*/
|
|
|
|
|
// 重置更新数组
|
|
|
|
|
public void resetUpdateArray() {
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
}
|
|
|
|
|
|