|
|
|
@ -60,7 +60,7 @@ import java.util.zip.GZIPInputStream;
|
|
|
|
|
import java.util.zip.Inflater;
|
|
|
|
|
import java.util.zip.InflaterInputStream;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//主要功能:实现GTASK的登录操作,进行GTASK任务的创建,创建任务列表,从网络上获取任务和任务列表的内容
|
|
|
|
|
public class GTaskClient {
|
|
|
|
|
private static final String TAG = GTaskClient.class.getSimpleName();
|
|
|
|
|
|
|
|
|
@ -102,49 +102,51 @@ public class GTaskClient {
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static synchronized GTaskClient getInstance() {
|
|
|
|
|
public static synchronized GTaskClient getInstance() {//用来获取的实例化对象
|
|
|
|
|
if (mInstance == null) {
|
|
|
|
|
mInstance = new GTaskClient();
|
|
|
|
|
}
|
|
|
|
|
return mInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean login(Activity activity) {
|
|
|
|
|
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;
|
|
|
|
|
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))) {
|
|
|
|
|
mLoggedin = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//如果没超时就不用登录
|
|
|
|
|
if (mLoggedin) {
|
|
|
|
|
Log.d(TAG, "already logged in");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mLastLoginTime = System.currentTimeMillis();
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// login with custom domain if necessary
|
|
|
|
|
if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase()
|
|
|
|
|
.endsWith("googlemail.com"))) {
|
|
|
|
|
//尝试使用用户自己的域名登录
|
|
|
|
|
if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase()//将用户名改为统一格式后判断是否为googl账户
|
|
|
|
|
.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";
|
|
|
|
|
mPostUrl = url.toString() + "r/ig";
|
|
|
|
|
mGetUrl = url.toString() + "ig";//设置用户对应的geturl
|
|
|
|
|
mPostUrl = url.toString() + "r/ig";//设置用户对应的posturl
|
|
|
|
|
|
|
|
|
|
if (tryToLoginGtask(activity, authToken)) {
|
|
|
|
|
mLoggedin = true;
|
|
|
|
@ -152,6 +154,7 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// try to login with google official url
|
|
|
|
|
//如果用户无法登录使用官方的url进行登录
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
|
mPostUrl = GTASK_POST_URL;
|
|
|
|
@ -163,11 +166,14 @@ public class GTaskClient {
|
|
|
|
|
mLoggedin = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
|
|
|
|
|
//具体实现登录账户方法
|
|
|
|
|
//使用令牌
|
|
|
|
|
//使用accountmanager进行管理注册账号
|
|
|
|
|
//返回账号的令牌
|
|
|
|
|
private String loginGoogleAccount(Activity activity, boolean invalidateToken) {//使用令牌登录google账号
|
|
|
|
|
String authToken;
|
|
|
|
|
AccountManager accountManager = AccountManager.get(activity);
|
|
|
|
|
Account[] accounts = accountManager.getAccountsByType("com.google");
|
|
|
|
|
AccountManager accountManager = AccountManager.get(activity);//提供给用户注册的接口
|
|
|
|
|
Account[] accounts = accountManager.getAccountsByType("com.google");//获取全部以com.google结尾的account
|
|
|
|
|
|
|
|
|
|
if (accounts.length == 0) {
|
|
|
|
|
Log.e(TAG, "there is no available google account");
|
|
|
|
@ -176,6 +182,7 @@ public class GTaskClient {
|
|
|
|
|
|
|
|
|
|
String accountName = NotesPreferenceActivity.getSyncAccountName(activity);
|
|
|
|
|
Account account = null;
|
|
|
|
|
//查询是否已经注册过,查找注册库信息
|
|
|
|
|
for (Account a : accounts) {
|
|
|
|
|
if (a.name.equals(accountName)) {
|
|
|
|
|
account = a;
|
|
|
|
@ -190,6 +197,7 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get the token now
|
|
|
|
|
//获取选中账号的令牌
|
|
|
|
|
AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account,
|
|
|
|
|
"goanna_mobile", null, activity, null, null);
|
|
|
|
|
try {
|
|
|
|
@ -204,9 +212,9 @@ public class GTaskClient {
|
|
|
|
|
authToken = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return authToken;
|
|
|
|
|
return authToken;//令牌
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//尝试登陆Gtask,这只是一个预先判断令牌是否是有效以及是否能登上GTask的方法,而不是具体实现登陆的方法
|
|
|
|
|
private boolean tryToLoginGtask(Activity activity, String authToken) {
|
|
|
|
|
if (!loginGtask(authToken)) {
|
|
|
|
|
// maybe the auth token is out of date, now let's invalidate the
|
|
|
|
@ -224,26 +232,27 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//实现登录GTask的具体操作
|
|
|
|
|
private boolean loginGtask(String authToken) {
|
|
|
|
|
int timeoutConnection = 10000;
|
|
|
|
|
int timeoutSocket = 15000;
|
|
|
|
|
int timeoutSocket = 15000;//socket是一种通信连接实现数据的交换的端口
|
|
|
|
|
HttpParams httpParameters = new BasicHttpParams();
|
|
|
|
|
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
|
|
|
|
|
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
|
|
|
|
|
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);//设置连接超时时间
|
|
|
|
|
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);//设置设置端口超时时间
|
|
|
|
|
mHttpClient = new DefaultHttpClient(httpParameters);
|
|
|
|
|
BasicCookieStore localBasicCookieStore = new BasicCookieStore();
|
|
|
|
|
BasicCookieStore localBasicCookieStore = new BasicCookieStore();//设置本地cookie
|
|
|
|
|
mHttpClient.setCookieStore(localBasicCookieStore);
|
|
|
|
|
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);
|
|
|
|
|
|
|
|
|
|
// login gtask
|
|
|
|
|
try {
|
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken;
|
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl);
|
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken;//设置登录的url
|
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl);//通过登录的uri实例化网页上资源的查找
|
|
|
|
|
HttpResponse response = null;
|
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
|
|
|
|
|
// get the cookie now
|
|
|
|
|
//获取CookieStore里存放的cookie
|
|
|
|
|
List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
|
|
|
|
|
boolean hasAuthCookie = false;
|
|
|
|
|
for (Cookie cookie : cookies) {
|
|
|
|
@ -256,6 +265,7 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get the client version
|
|
|
|
|
//获取client的内容,具体操作是在返回的Content中截取从_setup(开始到)}</script>中间的字符串内容,也就是gtask_url的内容
|
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
|
@ -290,7 +300,7 @@ public class GTaskClient {
|
|
|
|
|
httpPost.setHeader("AT", "1");
|
|
|
|
|
return httpPost;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//通过URL获取响应后返回的数据,也就是网络上的数据和资源
|
|
|
|
|
private String getResponseContent(HttpEntity entity) throws IOException {
|
|
|
|
|
String contentEncoding = null;
|
|
|
|
|
if (entity.getContentEncoding() != null) {
|
|
|
|
@ -299,7 +309,7 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InputStream input = entity.getContent();
|
|
|
|
|
if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
|
|
|
|
|
if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {//压缩数据库
|
|
|
|
|
input = new GZIPInputStream(entity.getContent());
|
|
|
|
|
} else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {
|
|
|
|
|
Inflater inflater = new Inflater(true);
|
|
|
|
@ -322,7 +332,10 @@ public class GTaskClient {
|
|
|
|
|
input.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//通过JSON发送请求,请求的具体内容在json的实例化对象js中然后传入
|
|
|
|
|
// 利用UrlEncodedFormEntity entity和httpPost.setEntity(entity)方法把js中的内容放置到httpPost中
|
|
|
|
|
//执行请求后使用getResponseContent方法得到返回的数据和资源
|
|
|
|
|
// 将资源再次放入json后返回
|
|
|
|
|
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
@ -359,7 +372,7 @@ public class GTaskClient {
|
|
|
|
|
throw new ActionFailureException("error occurs when posting request");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//创建单个任务,传入的参数是.gtask.data.task包里的task类对象,利用json获取task里的内容,并且创建相应的jsPost
|
|
|
|
|
public void createTask(Task task) throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
@ -385,7 +398,7 @@ public class GTaskClient {
|
|
|
|
|
throw new ActionFailureException("create task: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//创建一个任务列表,与createTask几乎一样,区别就是最后设置的是tasklist的gid
|
|
|
|
|
public void createTaskList(TaskList tasklist) throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
@ -411,7 +424,7 @@ public class GTaskClient {
|
|
|
|
|
throw new ActionFailureException("create tasklist: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//同步更新操作,使用jsonobject进行数据存储
|
|
|
|
|
public void commitUpdate() throws NetworkFailureException {
|
|
|
|
|
if (mUpdateArray != null) {
|
|
|
|
|
try {
|
|
|
|
@ -432,7 +445,7 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//添加更新事项
|
|
|
|
|
public void addUpdateNode(Node node) throws NetworkFailureException {
|
|
|
|
|
if (node != null) {
|
|
|
|
|
// too many update items may result in an error
|
|
|
|
@ -446,7 +459,12 @@ public class GTaskClient {
|
|
|
|
|
mUpdateArray.put(node.getUpdateAction(getActionId()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 移动task,比如讲task移动到不同的task列表中去
|
|
|
|
|
* 通过getGid获取task所属列表的gid
|
|
|
|
|
* 通过JSONObject.put(String name, Object value)函数设置移动后的task的相关属性值,从而达到移动的目的
|
|
|
|
|
* 最后还是通过postRequest进行更新后的发送
|
|
|
|
|
*/
|
|
|
|
|
public void moveTask(Task task, TaskList preParent, TaskList curParent)
|
|
|
|
|
throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
@ -485,7 +503,11 @@ public class GTaskClient {
|
|
|
|
|
throw new ActionFailureException("move task: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 删除操作节点
|
|
|
|
|
* 还是利用JSON
|
|
|
|
|
* 删除过后使用postRequest发送删除后的结果
|
|
|
|
|
*/
|
|
|
|
|
public void deleteNode(Node node) throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
@ -508,7 +530,11 @@ public class GTaskClient {
|
|
|
|
|
throw new ActionFailureException("delete node: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 获取任务列表
|
|
|
|
|
* 首先通过GetURI使用getResponseContent从网上获取数据
|
|
|
|
|
* 然后筛选出"_setup("到)}</script>的部分,并且从中获取GTASK_JSON_LISTS的内容返回
|
|
|
|
|
*/
|
|
|
|
|
public JSONArray getTaskLists() throws NetworkFailureException {
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
@ -546,7 +572,7 @@ public class GTaskClient {
|
|
|
|
|
throw new ActionFailureException("get task lists: handing jasonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//通过传入的TASKList的gid,从网络上获取相应属于这个任务列表的任务
|
|
|
|
|
public JSONArray getTaskList(String listGid) throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
@ -579,6 +605,7 @@ public class GTaskClient {
|
|
|
|
|
return mAccount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//重置更新的内容
|
|
|
|
|
public void resetUpdateArray() {
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
}
|
|
|
|
|