|
|
|
@ -0,0 +1,585 @@
|
|
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.gtask.remote;//声名gtask下的remote包,用于自动登录账号并更新消息,创建或者获取任务列表
|
|
|
|
|
|
|
|
|
|
import android.accounts.Account;//引用andriod和JSON中的一些操作,以及引用自己的包里的一些类
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class GTaskClient {//实现GTask的登录以及创建GTask任务和任务列表,从网络上获取任务内容
|
|
|
|
|
private static final String TAG = GTaskClient.class.getSimpleName();//设置本类的TAG,作用是日志的打印输出和标识此类
|
|
|
|
|
|
|
|
|
|
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";//发布的URL
|
|
|
|
|
|
|
|
|
|
private static GTaskClient mInstance = null;//后续使用的参数以及变量
|
|
|
|
|
|
|
|
|
|
private DefaultHttpClient mHttpClient;//网络地址客户端
|
|
|
|
|
|
|
|
|
|
private String mGetUrl;//构造函数:初始化各属性
|
|
|
|
|
|
|
|
|
|
private String mPostUrl;
|
|
|
|
|
|
|
|
|
|
private long mClientVersion;
|
|
|
|
|
|
|
|
|
|
private boolean mLoggedin;
|
|
|
|
|
|
|
|
|
|
private long mLastLoginTime;
|
|
|
|
|
|
|
|
|
|
private int mActionId;
|
|
|
|
|
|
|
|
|
|
private Account mAccount;
|
|
|
|
|
|
|
|
|
|
private JSONArray mUpdateArray;
|
|
|
|
|
|
|
|
|
|
private GTaskClient() {//初始化客户端
|
|
|
|
|
mHttpClient = null;//初始化
|
|
|
|
|
mGetUrl = GTASK_GET_URL;//初始化
|
|
|
|
|
mPostUrl = GTASK_POST_URL;//初始化
|
|
|
|
|
mClientVersion = -1;//初始化
|
|
|
|
|
mLoggedin = false;//初始化
|
|
|
|
|
mLastLoginTime = 0;//初始化
|
|
|
|
|
mActionId = 1;//初始化
|
|
|
|
|
mAccount = null;//初始化
|
|
|
|
|
mUpdateArray = null;//初始化
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static synchronized GTaskClient getInstance() {//getInstance函数,在java中,可以使用这种方式使用单例模式创建类的实例
|
|
|
|
|
if (mInstance == null) {//若无实例
|
|
|
|
|
mInstance = new GTaskClient();//则新建一个
|
|
|
|
|
}
|
|
|
|
|
return mInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean login(Activity activity) {//用于实现登录的方法,以activity作为参数
|
|
|
|
|
// we suppose that the cookie would expire after 5 minutes
|
|
|
|
|
// then we need to re-login
|
|
|
|
|
final long interval = 1000 * 60 * 5;//间隔5分钟时间
|
|
|
|
|
if (mLastLoginTime + interval < System.currentTimeMillis()) {//距离上次登录的时间间隔,若超过5分钟,则重置登录状态
|
|
|
|
|
mLoggedin = false;//超过规定时间,则需要重新登录
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// need to re-login after account switch
|
|
|
|
|
if (mLoggedin
|
|
|
|
|
&& !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity
|
|
|
|
|
.getSyncAccountName(activity))) {//在登陆成功的情况下检测到用户名和密码不匹配时登录失败
|
|
|
|
|
mLoggedin = false;//如果没超过时间,则不需要重新登录
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mLoggedin) {//如果登录的时候符合上面的要求则让其显示已登录登陆
|
|
|
|
|
Log.d(TAG, "already logged in");//显示已登录
|
|
|
|
|
return true;//不需要再次登录
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mLastLoginTime = System.currentTimeMillis();//更新登录时间为系统当前时间
|
|
|
|
|
String authToken = loginGoogleAccount(activity, false);//用用户提供的账户密码去登陆Google账户
|
|
|
|
|
if (authToken == null) {//登录失败的情况
|
|
|
|
|
Log.e(TAG, "login google account failed");//显示登录谷歌失败
|
|
|
|
|
return false;//谷歌登录失败
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// login with custom domain if necessary
|
|
|
|
|
if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase()
|
|
|
|
|
.endsWith("googlemail.com"))) {//在google账号登陆成功后,尝试能否使用用户的域名登陆gtask
|
|
|
|
|
StringBuilder url = new StringBuilder(GTASK_URL).append("a/");//append()函数是在文本末添加,这里用于新建一个url
|
|
|
|
|
int index = mAccount.name.indexOf('@') + 1;//返回@第一次出现的位置并把位置+1后记录在index里
|
|
|
|
|
String suffix = mAccount.name.substring(index);//substring() 方法用于提取字符串中介于两个指定下标之间的字符
|
|
|
|
|
url.append(suffix + "/");//均为字符串操作来构建url链接
|
|
|
|
|
mGetUrl = url.toString() + "ig";//设置用户的getUrl
|
|
|
|
|
mPostUrl = url.toString() + "r/ig";//设置用户postURL
|
|
|
|
|
|
|
|
|
|
if (tryToLoginGtask(activity, authToken)) {
|
|
|
|
|
mLoggedin = true;//若不能使用用户域名登录,使用google官方url登录
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// try to login with google official url
|
|
|
|
|
if (!mLoggedin) {//若前面的尝试失败,则尝试使用官方的域名登陆
|
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
|
mPostUrl = GTASK_POST_URL;
|
|
|
|
|
if (!tryToLoginGtask(activity, authToken)) {//第二次登录失败
|
|
|
|
|
return false;//返回false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mLoggedin = true;//如果没有报错说明登录成功
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String loginGoogleAccount(Activity activity, boolean invalidateToken) {//用以具体实现登录Google账号的方法,方法返回账号令牌
|
|
|
|
|
String authToken;//使用登录令牌核实用户信息和判断登录状态
|
|
|
|
|
AccountManager accountManager = AccountManager.get(activity);//账户管理器集中管理已注册账号和密码
|
|
|
|
|
Account[] accounts = accountManager.getAccountsByType("com.google");//将所有以com.google结尾的账号存入accounts数组中
|
|
|
|
|
|
|
|
|
|
if (accounts.length == 0) {//如果没有这样的账号
|
|
|
|
|
Log.e(TAG, "there is no available google account");//信息无有效的google账户
|
|
|
|
|
return null;//显示没有谷歌账户
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String accountName = NotesPreferenceActivity.getSyncAccountName(activity);//匹配活动的账户里是否存在账户
|
|
|
|
|
Account account = null;//遍历account数组,寻找已登录过的信息
|
|
|
|
|
for (Account a : accounts) {//如果找到匹配的就记下来,下次用于自动登录
|
|
|
|
|
if (a.name.equals(accountName)) {//没找到,输出例外信息
|
|
|
|
|
account = a;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (account != null) {//若存在
|
|
|
|
|
mAccount = account;//把这个账户记在mAccount中
|
|
|
|
|
} else {//不存在
|
|
|
|
|
Log.e(TAG, "unable to get an account with the same name in the settings");//显示“不能在设置中获取相同名字的账户”
|
|
|
|
|
return null;//若遍历完之后仍没有用户名匹配的账号,则直接返回
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get the token now
|
|
|
|
|
AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account,
|
|
|
|
|
"goanna_mobile", null, activity, null, null);//从账户中获取目标账户的令牌
|
|
|
|
|
try {
|
|
|
|
|
Bundle authTokenBundle = accountManagerFuture.getResult();//bundle是一个key-value对,这里获取目标账户的最终结果集
|
|
|
|
|
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);//这里获取bundle类的对象的String串并赋值给令牌对象
|
|
|
|
|
if (invalidateToken) {//如果是非法的令牌
|
|
|
|
|
accountManager.invalidateAuthToken("com.google", authToken);//删除存储AccountManager中此账号类型对应的authToken缓存
|
|
|
|
|
loginGoogleAccount(activity, false);//登录账号失败
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {//捕捉令牌获取失败的异常
|
|
|
|
|
Log.e(TAG, "get auth token failed");//获取令牌失败
|
|
|
|
|
authToken = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return authToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean tryToLoginGtask(Activity activity, String authToken) {//用于判断令牌对于登陆gtask账号是否有效
|
|
|
|
|
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");//错误,登录 google 帐户失败
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!loginGtask(authToken)) {//重新获取的令牌再次失效
|
|
|
|
|
Log.e(TAG, "login gtask failed");//错误,登录 gtask 失败
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean loginGtask(String authToken) {//登录gtask的实现函数,根据令牌判断是否登陆成功
|
|
|
|
|
int timeoutConnection = 10000;//连接超时为10000毫秒,即10秒
|
|
|
|
|
int timeoutSocket = 15000;//端口超时为15000毫秒,即15秒
|
|
|
|
|
HttpParams httpParameters = new BasicHttpParams();//申请一个httpParameters参数类的对象
|
|
|
|
|
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);//设置连接超时的时间
|
|
|
|
|
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);//设置端口超时的时间
|
|
|
|
|
mHttpClient = new DefaultHttpClient(httpParameters);//新建一个默认客户端
|
|
|
|
|
BasicCookieStore localBasicCookieStore = new BasicCookieStore();//用来存储本地的cookie值
|
|
|
|
|
mHttpClient.setCookieStore(localBasicCookieStore);//为新建客户端设置cookie存储
|
|
|
|
|
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);//setUseExpectContinu方法:设置http协议1.1中的一个header属性Expect 100 Continue
|
|
|
|
|
|
|
|
|
|
// login gtask
|
|
|
|
|
try {
|
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken;
|
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl);//通过令牌与原始的url设置登录的URL
|
|
|
|
|
HttpResponse response = null;//通过HttpGet向服务器申请
|
|
|
|
|
response = mHttpClient.execute(httpGet);//执行execute()方法之后会返回一个HttpResponse对象,服务器所返回的所有信息就保护在HttpResponse里面
|
|
|
|
|
|
|
|
|
|
// get the cookie now
|
|
|
|
|
List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();//获取cookie
|
|
|
|
|
boolean hasAuthCookie = false;//hasauthcookie默认为false
|
|
|
|
|
for (Cookie cookie : cookies) {//遍历cookies集合中的每个Cookie对象
|
|
|
|
|
if (cookie.getName().contains("GTL")) {//验证cookie信息,这里通过GTL标志来验证
|
|
|
|
|
hasAuthCookie = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!hasAuthCookie) {//若这是未授权的Cookie值
|
|
|
|
|
Log.w(TAG, "it seems that there is no auth cookie");//显示没有授权的cookie
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get the client version
|
|
|
|
|
String resString = getResponseContent(response.getEntity());//获取客户端的内容
|
|
|
|
|
String jsBegin = "_setup(";//取得的内容是html语言,截取内容为"_setup(" 与 ")}</script>" 中间的部分
|
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
|
|
int begin = resString.indexOf(jsBegin);//获取jsbegin中begin的序号
|
|
|
|
|
int end = resString.lastIndexOf(jsEnd);//获取jsend中end的序号
|
|
|
|
|
String jsString = null;
|
|
|
|
|
if (begin != -1 && end != -1 && begin < end) {
|
|
|
|
|
jsString = resString.substring(begin + jsBegin.length(), end);
|
|
|
|
|
}
|
|
|
|
|
JSONObject js = new JSONObject(jsString);
|
|
|
|
|
mClientVersion = js.getLong("v");//设置客户端版本
|
|
|
|
|
} catch (JSONException e) {//异常处理机制
|
|
|
|
|
Log.e(TAG, e.toString());//获取异常类型和异常详细消息
|
|
|
|
|
e.printStackTrace();//在命令行打印异常信息在程序中出错的位置及原因
|
|
|
|
|
return false;
|
|
|
|
|
} catch (Exception e) {//捕获所有的异常
|
|
|
|
|
// simply catch all exceptions
|
|
|
|
|
Log.e(TAG, "httpget gtask_url failed");//截取失败打印日志
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int getActionId() {//获取动作的id号码
|
|
|
|
|
return mActionId++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private HttpPost createHttpPost() {
|
|
|
|
|
HttpPost httpPost = new HttpPost(mPostUrl);//创建一个httppost对象,用来保存URL
|
|
|
|
|
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");//设置httppost对象的头部
|
|
|
|
|
httpPost.setHeader("AT", "1");
|
|
|
|
|
return httpPost;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getResponseContent(HttpEntity entity) throws IOException {//使用getContentEncoding()获取网络上的资源和数据
|
|
|
|
|
String contentEncoding = null;
|
|
|
|
|
if (entity.getContentEncoding() != null) {//如果获取的内容编码不为空
|
|
|
|
|
contentEncoding = entity.getContentEncoding().getValue();//给contentEncoding赋值
|
|
|
|
|
Log.d(TAG, "encoding: " + contentEncoding);//打印日志“encoding:内容编码”
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InputStream input = entity.getContent();
|
|
|
|
|
if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {//如果contentEncoding不为null且contentEncoding与"gzip"相等
|
|
|
|
|
input = new GZIPInputStream(entity.getContent());//使用GZIPInputStream对entity内容进行GZIP解压,把值赋给InputStream类对象input
|
|
|
|
|
} else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {//DEFLATE是一个无专利的压缩算法,它可以实现无损数据压缩
|
|
|
|
|
Inflater inflater = new Inflater(true);
|
|
|
|
|
input = new InflaterInputStream(entity.getContent(), inflater);//实现了一个流过滤器,用于以“deflate”压缩格式解压缩数据
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
InputStreamReader isr = new InputStreamReader(input);//InputStreamReader类:是从字节流到字符流的桥接器,它使用指定的字符集读取字节并将它们解码为字符
|
|
|
|
|
BufferedReader br = new BufferedReader(isr);//缓存读取类,用于快速的读缓存操作
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
while (true) {//将BufferedReader类br的内容逐行读取并存储StringBuilder类的sb中。然后返回sb的string格式。
|
|
|
|
|
String buff = br.readLine();
|
|
|
|
|
if (buff == null) {
|
|
|
|
|
return sb.toString();
|
|
|
|
|
}
|
|
|
|
|
sb = sb.append(buff);
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
input.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {//利用JSON发送请求,返回获取的内容
|
|
|
|
|
if (!mLoggedin) {//用户未登录
|
|
|
|
|
Log.e(TAG, "please login first");//显示请先登录
|
|
|
|
|
throw new ActionFailureException("not logged in");//抛出异常没有登录
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HttpPost httpPost = createHttpPost();//实例化一个httpPost的对象用来向服务器传输数据,发送在js里请求的内容
|
|
|
|
|
try {
|
|
|
|
|
LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();//LinkedList 类:是一个继承于AbstractSequentialList的双向链表
|
|
|
|
|
list.add(new BasicNameValuePair("r", js.toString()));
|
|
|
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");//普通的键值对"UTF-8"
|
|
|
|
|
httpPost.setEntity(entity);//向httpPost对象中添加参数
|
|
|
|
|
|
|
|
|
|
// execute the post
|
|
|
|
|
HttpResponse response = mHttpClient.execute(httpPost);//执行请求
|
|
|
|
|
String jsString = getResponseContent(response.getEntity());
|
|
|
|
|
return new JSONObject(jsString);
|
|
|
|
|
|
|
|
|
|
} catch (ClientProtocolException e) {//下面的四个catch是对4种失败情况的处理和抛出异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new NetworkFailureException("postRequest failed");//post请求失败
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new NetworkFailureException("postRequest failed");
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("unable to convert response content to jsonobject");
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("error occurs when posting request");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void createTask(Task task) throws NetworkFailureException {//创建单个任务
|
|
|
|
|
commitUpdate();//提交更新
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();//用JSON获取Task中的内容,并创建相应的jspost
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
actionList.put(task.getCreateAction(getActionId()));
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);//将要提交的内容放入jsPost对象中
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);//client的版本号
|
|
|
|
|
|
|
|
|
|
// post
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);//通过postRequest获取任务的返回信息
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
|
task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));//设置任务的id
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {//异常处理机制
|
|
|
|
|
Log.e(TAG, e.toString());//获取异常类型和异常详细消息
|
|
|
|
|
e.printStackTrace();//在命令行打印异常信息在程序中出错的位置及原因
|
|
|
|
|
throw new ActionFailureException("create task: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);//post操作
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
|
tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {//异常处理机制
|
|
|
|
|
Log.e(TAG, e.toString());//获取异常类型和异常详细消息
|
|
|
|
|
e.printStackTrace();//在命令行打印异常信息在程序中出错的位置及原因
|
|
|
|
|
throw new ActionFailureException("create tasklist: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void 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);
|
|
|
|
|
mUpdateArray = null;//更新处理完毕,列表置空
|
|
|
|
|
} catch (JSONException e) {//异常处理机制
|
|
|
|
|
Log.e(TAG, e.toString());//获取异常类型和异常详细消息
|
|
|
|
|
e.printStackTrace();//在命令行打印异常信息在程序中出错的位置及原因
|
|
|
|
|
throw new ActionFailureException("commit update: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addUpdateNode(Node node) throws NetworkFailureException {//添加更新节点
|
|
|
|
|
if (node != null) {//如果node节点不是空的
|
|
|
|
|
// too many update items may result in an error
|
|
|
|
|
// set max to 10 items
|
|
|
|
|
if (mUpdateArray != null && mUpdateArray.length() > 10) {//判断现在mUpdateArray是否是空的或者满的
|
|
|
|
|
commitUpdate();//提交更新
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mUpdateArray == null)//空的则新建一个数组
|
|
|
|
|
mUpdateArray = new JSONArray();
|
|
|
|
|
mUpdateArray.put(node.getUpdateAction(getActionId()));//将更新节点加入列表
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void moveTask(Task task, TaskList preParent, TaskList curParent)
|
|
|
|
|
throws NetworkFailureException {//把任务移动到指定的列表中
|
|
|
|
|
commitUpdate();//提交更新数据
|
|
|
|
|
try {//创建新的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) {//当移动发生在同一个任务列表中且该移动不是第一个时
|
|
|
|
|
// 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());//设置优先级ID
|
|
|
|
|
}
|
|
|
|
|
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);//postRequst()进行更新后的发送
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {//异常处理机制
|
|
|
|
|
Log.e(TAG, e.toString());//获取异常类型和异常详细消息
|
|
|
|
|
e.printStackTrace();//在命令行打印异常信息在程序中出错的位置及原因
|
|
|
|
|
throw new ActionFailureException("move task: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void deleteNode(Node node) throws NetworkFailureException {//删除节点操作
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();//新建jsPost,把除了node的其他节点都放入jsPost,并提交
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
node.setDeleted(true);//把要删除的node的成员变量的删除属性设置为true
|
|
|
|
|
actionList.put(node.getUpdateAction(getActionId()));//将该节点要更新的操作的id加入操作列表
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
} catch (JSONException e) {//异常处理机制
|
|
|
|
|
Log.e(TAG, e.toString());//获取异常类型和异常详细消息
|
|
|
|
|
e.printStackTrace();//在命令行打印异常信息在程序中出错的位置及原因
|
|
|
|
|
throw new ActionFailureException("delete node: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public JSONArray getTaskLists() throws NetworkFailureException {//获取所有任务列表
|
|
|
|
|
if (!mLoggedin) {//如果没有登录
|
|
|
|
|
Log.e(TAG, "please login first");//显示请先登录
|
|
|
|
|
throw new ActionFailureException("not logged in");//抛出异常
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
HttpGet httpGet = new HttpGet(mGetUrl);//通过GetURI使用getResponseContent从网上获取初步的数据
|
|
|
|
|
HttpResponse response = null;//初始化Httpresponse (回复)为空
|
|
|
|
|
response = mHttpClient.execute(httpGet);//用httpGet作消息发给客户端执行并回复消息
|
|
|
|
|
|
|
|
|
|
// get the task list
|
|
|
|
|
String resString = getResponseContent(response.getEntity());//获取任务列表
|
|
|
|
|
String jsBegin = "_setup(";//把字符串截取并放入jsStrIng
|
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
|
|
int begin = resString.indexOf(jsBegin);
|
|
|
|
|
int end = resString.lastIndexOf(jsEnd);
|
|
|
|
|
String jsString = null;
|
|
|
|
|
if (begin != -1 && end != -1 && begin < end) {
|
|
|
|
|
jsString = resString.substring(begin + jsBegin.length(), end);
|
|
|
|
|
}
|
|
|
|
|
JSONObject js = new JSONObject(jsString);
|
|
|
|
|
return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS);//获取GTASK_JSON_LISTS
|
|
|
|
|
} catch (ClientProtocolException e) {//捕捉httpget失败异常
|
|
|
|
|
Log.e(TAG, e.toString());//获取异常类型和异常详细消息
|
|
|
|
|
e.printStackTrace();//在命令行打印异常信息在程序中出错的位置及原因
|
|
|
|
|
throw new NetworkFailureException("gettasklists: httpget failed");
|
|
|
|
|
} catch (IOException e) {//异常处理
|
|
|
|
|
Log.e(TAG, e.toString());//获取异常类型和异常详细消息
|
|
|
|
|
e.printStackTrace();//在命令行打印异常信息在程序中出错的位置及原因
|
|
|
|
|
throw new NetworkFailureException("gettasklists: httpget failed");
|
|
|
|
|
} catch (JSONException e) {//异常处理
|
|
|
|
|
Log.e(TAG, e.toString());//获取异常类型和异常详细消息
|
|
|
|
|
e.printStackTrace();//在命令行打印异常信息在程序中出错的位置及原因
|
|
|
|
|
throw new ActionFailureException("get task lists: handing jasonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public JSONArray getTaskList(String listGid) throws NetworkFailureException {//获取任务列表
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();//设置为传入的listGid
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONObject action = new JSONObject();
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,//通过action.put()对JSONObject对象action添加元素
|
|
|
|
|
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);//通过jsPost.put()对jsPost添加相关元素
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);//通过postRequest()提交更新后的请求并返回一个JSONObject的对象
|
|
|
|
|
return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS);//使用jsResponse.getJSONArray( )获取jsResponse中的JSONArray值并作为函数返回值
|
|
|
|
|
} catch (JSONException e) {//异常处理
|
|
|
|
|
Log.e(TAG, e.toString());//获取异常类型和异常详细消息
|
|
|
|
|
e.printStackTrace();//在命令行打印异常信息在程序中出错的位置及原因
|
|
|
|
|
throw new ActionFailureException("get task list: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Account getSyncAccount() {//获取同步账户
|
|
|
|
|
return mAccount;//同步账户
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void resetUpdateArray() {//重置更新内容
|
|
|
|
|
mUpdateArray = null;//更新内容
|
|
|
|
|
}
|
|
|
|
|
}
|