|
|
|
@ -62,35 +62,37 @@ import java.util.zip.InflaterInputStream;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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/";
|
|
|
|
|
|
|
|
|
|
// 用于获取数据的URL
|
|
|
|
|
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";
|
|
|
|
|
|
|
|
|
|
// GTaskClient的单例实例
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// 当前登录的账户
|
|
|
|
|
private Account mAccount;
|
|
|
|
|
|
|
|
|
|
// 更新数据数组
|
|
|
|
|
private JSONArray mUpdateArray;
|
|
|
|
|
|
|
|
|
|
private GTaskClient() {
|
|
|
|
|
|
|
|
|
|
mHttpClient = null;
|
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
|
mPostUrl = GTASK_POST_URL;
|
|
|
|
@ -109,15 +111,17 @@ public class GTaskClient {
|
|
|
|
|
return mInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean login(Activity activity) {
|
|
|
|
|
public boolean login(Activity activity) { // 假设cookie在5分钟后过期,需要重新登录
|
|
|
|
|
// we suppose that the cookie would expire after 5 minutes
|
|
|
|
|
// then we need to re-login
|
|
|
|
|
final long interval = 1000 * 60 * 5;
|
|
|
|
|
// 切换账户后需要重新登录
|
|
|
|
|
if (mLastLoginTime + interval < System.currentTimeMillis()) {
|
|
|
|
|
mLoggedin = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// need to re-login after account switch
|
|
|
|
|
// 如果已经登录,直接返回true
|
|
|
|
|
if (mLoggedin
|
|
|
|
|
&& !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity
|
|
|
|
|
.getSyncAccountName(activity))) {
|
|
|
|
@ -128,14 +132,15 @@ public class GTaskClient {
|
|
|
|
|
Log.d(TAG, "already logged in");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新最后一次登录时间
|
|
|
|
|
mLastLoginTime = System.currentTimeMillis();
|
|
|
|
|
// 获取Google账户的认证令牌
|
|
|
|
|
String authToken = loginGoogleAccount(activity, false);
|
|
|
|
|
if (authToken == null) {
|
|
|
|
|
Log.e(TAG, "login google account failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果账户不是gmail.com或googlemail.com,使用自定义域名登录
|
|
|
|
|
// login with custom domain if necessary
|
|
|
|
|
if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase()
|
|
|
|
|
.endsWith("googlemail.com"))) {
|
|
|
|
@ -159,29 +164,33 @@ public class GTaskClient {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置登录状态为true并返回true表示登录成功
|
|
|
|
|
mLoggedin = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 登录Google账户的函数,接收一个Activity实例和一个标志位来决定是否使当前的token无效
|
|
|
|
|
private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
|
|
|
|
|
String authToken;
|
|
|
|
|
AccountManager accountManager = AccountManager.get(activity);
|
|
|
|
|
Account[] accounts = accountManager.getAccountsByType("com.google");
|
|
|
|
|
|
|
|
|
|
String authToken;// 用于存储获取到的认证token
|
|
|
|
|
AccountManager accountManager = AccountManager.get(activity);// 获取AccountManager实例
|
|
|
|
|
Account[] accounts = accountManager.getAccountsByType("com.google");// 获取所有Google类型的账户
|
|
|
|
|
// 如果没有可用的Google账户,则记录错误日志并返回null
|
|
|
|
|
if (accounts.length == 0) {
|
|
|
|
|
Log.e(TAG, "there is no available google account");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从设置中获取同步账户的名称
|
|
|
|
|
String accountName = NotesPreferenceActivity.getSyncAccountName(activity);
|
|
|
|
|
Account account = null;
|
|
|
|
|
// 遍历所有账户,找到与设置中的账户名匹配的账户
|
|
|
|
|
for (Account a : accounts) {
|
|
|
|
|
if (a.name.equals(accountName)) {
|
|
|
|
|
account = a;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 如果找到了匹配的账户,则将其存储在mAccount变量中,否则记录错误日志并返回null
|
|
|
|
|
if (account != null) {
|
|
|
|
|
mAccount = account;
|
|
|
|
|
} else {
|
|
|
|
@ -190,60 +199,66 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get the token now
|
|
|
|
|
// 获取认证token
|
|
|
|
|
AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account,
|
|
|
|
|
"goanna_mobile", null, activity, null, null);
|
|
|
|
|
try {
|
|
|
|
|
Bundle authTokenBundle = accountManagerFuture.getResult();
|
|
|
|
|
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
|
|
|
|
|
Bundle authTokenBundle = accountManagerFuture.getResult();// 获取结果
|
|
|
|
|
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);// 从结果中获取token
|
|
|
|
|
// 如果需要使token无效,则调用invalidateAuthToken并递归调用loginGoogleAccount以获取新的token
|
|
|
|
|
if (invalidateToken) {
|
|
|
|
|
accountManager.invalidateAuthToken("com.google", authToken);
|
|
|
|
|
loginGoogleAccount(activity, false);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// 如果获取token失败,则记录错误日志并将authToken设置为null
|
|
|
|
|
Log.e(TAG, "get auth token failed");
|
|
|
|
|
authToken = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return authToken;
|
|
|
|
|
return authToken;// 返回获取到的token
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 尝试使用Google账户登录Google Tasks的函数
|
|
|
|
|
private boolean tryToLoginGtask(Activity activity, String authToken) {
|
|
|
|
|
// 尝试使用提供的token登录Google Tasks
|
|
|
|
|
if (!loginGtask(authToken)) {
|
|
|
|
|
// 如果登录失败,可能是因为token已过期,因此使token无效并尝试重新登录
|
|
|
|
|
// maybe the auth token is out of date, now let's invalidate the
|
|
|
|
|
// token and try again
|
|
|
|
|
authToken = loginGoogleAccount(activity, true);
|
|
|
|
|
if (authToken == null) {
|
|
|
|
|
if (authToken == null) {// 如果重新登录失败,则记录错误日志并返回false
|
|
|
|
|
Log.e(TAG, "login google account failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 使用新的token再次尝试登录Google Tasks
|
|
|
|
|
if (!loginGtask(authToken)) {
|
|
|
|
|
Log.e(TAG, "login gtask failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
return true;// 登录成功返回true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 使用提供的token登录Google Tasks的函数
|
|
|
|
|
private boolean loginGtask(String authToken) {
|
|
|
|
|
// 设置HTTP连接的超时时间
|
|
|
|
|
int timeoutConnection = 10000;
|
|
|
|
|
int timeoutSocket = 15000;
|
|
|
|
|
HttpParams httpParameters = new BasicHttpParams();
|
|
|
|
|
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
|
|
|
|
|
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
|
|
|
|
|
mHttpClient = new DefaultHttpClient(httpParameters);
|
|
|
|
|
BasicCookieStore localBasicCookieStore = new BasicCookieStore();
|
|
|
|
|
mHttpClient.setCookieStore(localBasicCookieStore);
|
|
|
|
|
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);
|
|
|
|
|
mHttpClient = new DefaultHttpClient(httpParameters);// 创建HttpClient实例
|
|
|
|
|
BasicCookieStore localBasicCookieStore = new BasicCookieStore();// 创建Cookie存储实例
|
|
|
|
|
mHttpClient.setCookieStore(localBasicCookieStore);// 设置HttpClient的Cookie存储
|
|
|
|
|
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);// 禁用Expect-Continue握手
|
|
|
|
|
|
|
|
|
|
// login gtask
|
|
|
|
|
// 构造登录Google Tasks的URL
|
|
|
|
|
try {
|
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken;
|
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl);
|
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl);// 创建HttpGet请求
|
|
|
|
|
HttpResponse response = null;
|
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
response = mHttpClient.execute(httpGet);// 执行HttpGet请求
|
|
|
|
|
|
|
|
|
|
// get the cookie now
|
|
|
|
|
// 检查响应中的Cookie,看是否有认证Cookie
|
|
|
|
|
List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
|
|
|
|
|
boolean hasAuthCookie = false;
|
|
|
|
|
for (Cookie cookie : cookies) {
|
|
|
|
@ -255,7 +270,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>";
|
|
|
|
@ -265,25 +280,25 @@ public class GTaskClient {
|
|
|
|
|
if (begin != -1 && end != -1 && begin < end) {
|
|
|
|
|
jsString = resString.substring(begin + jsBegin.length(), end);
|
|
|
|
|
}
|
|
|
|
|
JSONObject js = new JSONObject(jsString);
|
|
|
|
|
mClientVersion = js.getLong("v");
|
|
|
|
|
JSONObject js = new JSONObject(jsString);// 解析JSON字符串
|
|
|
|
|
mClientVersion = js.getLong("v");// 获取客户端版本
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
Log.e(TAG, e.toString());// 如果解析JSON时出错,则记录错误日志并返回false
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
} catch (Exception e) { // 捕获所有异常并记录错误日志,返回false
|
|
|
|
|
// simply catch all exceptions
|
|
|
|
|
Log.e(TAG, "httpget gtask_url failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
return true;// 登录成功返回true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取一个唯一的动作ID的函数
|
|
|
|
|
private int getActionId() {
|
|
|
|
|
return mActionId++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建一个HttpPost请求的函数
|
|
|
|
|
private HttpPost createHttpPost() {
|
|
|
|
|
HttpPost httpPost = new HttpPost(mPostUrl);
|
|
|
|
|
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
|
|
|
|
@ -292,89 +307,112 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getResponseContent(HttpEntity entity) throws IOException {
|
|
|
|
|
// 初始化内容编码变量
|
|
|
|
|
// 获取内容编码,如果存在的话
|
|
|
|
|
String contentEncoding = null;
|
|
|
|
|
if (entity.getContentEncoding() != null) {
|
|
|
|
|
contentEncoding = entity.getContentEncoding().getValue();
|
|
|
|
|
// 打印内容编码
|
|
|
|
|
Log.d(TAG, "encoding: " + contentEncoding);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取输入流
|
|
|
|
|
InputStream input = entity.getContent();
|
|
|
|
|
if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
|
|
|
|
|
// 根据内容编码进行解压缩
|
|
|
|
|
if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) { // 如果是gzip编码,使用GZIPInputStream解压缩
|
|
|
|
|
input = new GZIPInputStream(entity.getContent());
|
|
|
|
|
} else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {
|
|
|
|
|
} else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {// 如果是deflate编码,使用InflaterInputStream解压缩
|
|
|
|
|
Inflater inflater = new Inflater(true);
|
|
|
|
|
input = new InflaterInputStream(entity.getContent(), inflater);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 读取输入流并转换为字符串
|
|
|
|
|
try {
|
|
|
|
|
// 创建InputStreamReader,指定UTF-8编码
|
|
|
|
|
InputStreamReader isr = new InputStreamReader(input);
|
|
|
|
|
// 创建BufferedReader以高效读取文本
|
|
|
|
|
BufferedReader br = new BufferedReader(isr);
|
|
|
|
|
// 创建StringBuilder以构建响应内容字符串·
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
String buff = br.readLine();
|
|
|
|
|
if (buff == null) {
|
|
|
|
|
return sb.toString();
|
|
|
|
|
}
|
|
|
|
|
} // 将读取的行追加到StringBuilder
|
|
|
|
|
sb = sb.append(buff);
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
} finally { // 确保输入流在完成后被关闭
|
|
|
|
|
input.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
|
|
|
|
|
private JSONObject postRequest(JSONObject js) throws NetworkFailureException { // 检查用户是否已登录,如果未登录,则抛出异常
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建HTTP POST请求对象
|
|
|
|
|
HttpPost httpPost = createHttpPost();
|
|
|
|
|
try {
|
|
|
|
|
try { // 创建参数列表,用于存储请求参数
|
|
|
|
|
LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
|
|
|
|
|
// 将JSON对象转换为字符串,并作为参数添加到列表
|
|
|
|
|
list.add(new BasicNameValuePair("r", js.toString()));
|
|
|
|
|
// 创建UrlEncodedFormEntity对象,用于封装请求参数
|
|
|
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
|
|
|
|
|
// 设置请求的实体内容
|
|
|
|
|
httpPost.setEntity(entity);
|
|
|
|
|
|
|
|
|
|
// execute the post
|
|
|
|
|
// 执行HTTP POST请求,获取响应
|
|
|
|
|
HttpResponse response = mHttpClient.execute(httpPost);
|
|
|
|
|
// 获取响应内容,并将其转换为字符串
|
|
|
|
|
String jsString = getResponseContent(response.getEntity());
|
|
|
|
|
// 将响应字符串转换为JSON对象
|
|
|
|
|
return new JSONObject(jsString);
|
|
|
|
|
|
|
|
|
|
} catch (ClientProtocolException e) {
|
|
|
|
|
// 处理HTTP协议异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new NetworkFailureException("postRequest failed");
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
// 处理I/O异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new NetworkFailureException("postRequest failed");
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
// 处理JSON解析异常
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建任务的方法
|
|
|
|
|
public void createTask(Task task) throws NetworkFailureException {
|
|
|
|
|
// 提交之前的更新
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
// 创建用于POST请求的JSON对象
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
// 创建动作列表
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
|
|
|
|
|
// 获取任务的创建动作,并添加到动作列表
|
|
|
|
|
actionList.put(task.getCreateAction(getActionId()));
|
|
|
|
|
// 将动作列表添加到POST请求的JSON对象
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
// 设置客户端版本信息
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// post
|
|
|
|
|
// 发送POST请求,并获取响应
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
// 从响应中获取结果,并设置任务的GID
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
|
task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
|
|
|
|
@ -387,10 +425,10 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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(); // 创建动作列表
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
actionList.put(tasklist.getCreateAction(getActionId()));
|
|
|
|
@ -413,19 +451,21 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void commitUpdate() throws NetworkFailureException {
|
|
|
|
|
// 检查是否有更新数组
|
|
|
|
|
if (mUpdateArray != null) {
|
|
|
|
|
try {
|
|
|
|
|
try { // 创建用于POST请求的JSON对象
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
// 将更新数组添加到POST请求的JSON对象
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray);
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
// 设置客户端版本信息
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送POST请求
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
// 清空更新数组
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
// 处理JSON解析异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("commit update: handing jsonobject failed");
|
|
|
|
@ -433,40 +473,41 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addUpdateNode(Node 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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果更新数组为空,则初始化
|
|
|
|
|
if (mUpdateArray == null)
|
|
|
|
|
mUpdateArray = new JSONArray();
|
|
|
|
|
mUpdateArray = new JSONArray(); // 将节点的更新动作添加到更新数组
|
|
|
|
|
mUpdateArray.put(node.getUpdateAction(getActionId()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
// 创建动作列表
|
|
|
|
|
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) {
|
|
|
|
|
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
|
|
|
|
|
// 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());
|
|
|
|
|
} // 设置源任务列表的GID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid()); // 设置目标父任务的GID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid()); // 如果在不同任务列表间移动,则设置目标任务列表的GID
|
|
|
|
|
if (preParent != curParent) {
|
|
|
|
|
// put the dest_list only if moving between tasklists
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid());
|
|
|
|
@ -487,91 +528,99 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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(); // 创建动作列表的JSONArray
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
// 将节点标记为已删除,并获取更新动作,添加到动作列表中
|
|
|
|
|
node.setDeleted(true);
|
|
|
|
|
actionList.put(node.getUpdateAction(getActionId()));
|
|
|
|
|
actionList.put(node.getUpdateAction(getActionId())); // 将动作列表添加到POST请求的JSON对象中
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
// 添加客户端版本信息到POST请求的JSON对象中
|
|
|
|
|
// client_version
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送POST请求
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
mUpdateArray = null; // 清空更新数组
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("delete node: handing jsonobject failed");
|
|
|
|
|
Log.e(TAG, e.toString()); // 如果处理JSON时发生异常,记录错误日志
|
|
|
|
|
e.printStackTrace(); // 打印异常堆栈信息
|
|
|
|
|
throw new ActionFailureException("delete node: handing jsonobject failed"); // 抛出动作失败异常
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public JSONArray getTaskLists() throws NetworkFailureException {
|
|
|
|
|
public JSONArray getTaskLists() throws NetworkFailureException { // 检查是否已登录
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
|
Log.e(TAG, "please login first"); // 如果未登录,记录错误日志
|
|
|
|
|
throw new ActionFailureException("not logged in"); // 抛出动作失败异常
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
try { // 创建HTTP GET请求
|
|
|
|
|
HttpGet httpGet = new HttpGet(mGetUrl);
|
|
|
|
|
HttpResponse response = null;
|
|
|
|
|
HttpResponse response = null; // 执行HTTP GET请求,获取响应
|
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
|
|
|
|
|
// 获取响应内容
|
|
|
|
|
// get the task list
|
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
|
// 定义JSON字符串的开始和结束标记
|
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
|
|
// 查找JSON字符串的开始和结束位置
|
|
|
|
|
int begin = resString.indexOf(jsBegin);
|
|
|
|
|
int end = resString.lastIndexOf(jsEnd);
|
|
|
|
|
String jsString = null;
|
|
|
|
|
String jsString = null; // 提取JSON字符串
|
|
|
|
|
if (begin != -1 && end != -1 && begin < end) {
|
|
|
|
|
jsString = resString.substring(begin + jsBegin.length(), end);
|
|
|
|
|
}
|
|
|
|
|
JSONObject js = new JSONObject(jsString);
|
|
|
|
|
} // 将提取的JSON字符串转换为JSONObject.
|
|
|
|
|
JSONObject js = new JSONObject(jsString);
|
|
|
|
|
// 从JSONObject中获取任务列表的JSONArray
|
|
|
|
|
return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS);
|
|
|
|
|
} catch (ClientProtocolException e) {
|
|
|
|
|
} catch (ClientProtocolException e) { // 如果HTTP协议错误,记录错误日志
|
|
|
|
|
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");
|
|
|
|
|
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");
|
|
|
|
|
Log.e(TAG, e.toString()); // 如果I/O错误,记录错误日志
|
|
|
|
|
e.printStackTrace(); // 打印异常堆栈信息
|
|
|
|
|
throw new ActionFailureException("get task lists: handing jasonobject failed"); // 抛出网络失败异常
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public JSONArray getTaskList(String listGid) throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
commitUpdate(); // 先提交之前的所有更新
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONObject action = new JSONObject();
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
// 创建用于POST请求的JSON对象
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
// 创建动作列表的JSONArray
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
// 创建动作的JSONObject
|
|
|
|
|
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());
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid);
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false);
|
|
|
|
|
actionList.put(action);
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL); // 设置动作ID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId()); // 设置任务列表ID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid); // 设置是否获取已删除任务,此处为否
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false); // 将动作添加到动作列表中
|
|
|
|
|
actionList.put(action); // 将动作列表添加到POST请求的JSON对象中
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
// 添加客户端版本信息
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
// 发送POST请求并获取响应
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost); // 从响应中获取任务数组
|
|
|
|
|
return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS);
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("get task list: handing jsonobject failed");
|
|
|
|
|
Log.e(TAG, e.toString()); // 记录JSON异常
|
|
|
|
|
e.printStackTrace(); // 打印异常堆栈信息
|
|
|
|
|
throw new ActionFailureException("get task list: handing jsonobject failed"); // 抛出操作失败异常
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|