|
|
|
@ -64,30 +64,40 @@ import java.util.zip.InflaterInputStream;
|
|
|
|
|
public class GTaskClient {
|
|
|
|
|
private static final String TAG = GTaskClient.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
// Google任务的URL
|
|
|
|
|
private static final String GTASK_URL = "https://mail.google.com/tasks/";
|
|
|
|
|
|
|
|
|
|
// Google任务的获取URL
|
|
|
|
|
private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig";
|
|
|
|
|
|
|
|
|
|
// Google任务的发布URL
|
|
|
|
|
private static final String GTASK_POST_URL = "https://mail.google.com/tasks/r/ig";
|
|
|
|
|
|
|
|
|
|
private static GTaskClient mInstance = null;
|
|
|
|
|
|
|
|
|
|
// Http客户端
|
|
|
|
|
private DefaultHttpClient mHttpClient;
|
|
|
|
|
|
|
|
|
|
private String mGetUrl;
|
|
|
|
|
|
|
|
|
|
private String mPostUrl;
|
|
|
|
|
|
|
|
|
|
// 客户端版本号
|
|
|
|
|
private long mClientVersion;
|
|
|
|
|
|
|
|
|
|
// 是否已登录
|
|
|
|
|
private boolean mLoggedin;
|
|
|
|
|
|
|
|
|
|
// 上次登录时间
|
|
|
|
|
private long mLastLoginTime;
|
|
|
|
|
|
|
|
|
|
// 操作ID
|
|
|
|
|
private int mActionId;
|
|
|
|
|
|
|
|
|
|
// 账户信息
|
|
|
|
|
private Account mAccount;
|
|
|
|
|
|
|
|
|
|
// 更新任务的JSONArray
|
|
|
|
|
private JSONArray mUpdateArray;
|
|
|
|
|
|
|
|
|
|
private GTaskClient() {
|
|
|
|
@ -110,14 +120,13 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean login(Activity activity) {
|
|
|
|
|
// we suppose that the cookie would expire after 5 minutes
|
|
|
|
|
// then we need to re-login
|
|
|
|
|
// cookie过期时间为5分钟
|
|
|
|
|
final long interval = 1000 * 60 * 5;
|
|
|
|
|
if (mLastLoginTime + interval < System.currentTimeMillis()) {
|
|
|
|
|
mLoggedin = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// need to re-login after account switch
|
|
|
|
|
// 账户切换后需要重新登录
|
|
|
|
|
if (mLoggedin
|
|
|
|
|
&& !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity
|
|
|
|
|
.getSyncAccountName(activity))) {
|
|
|
|
@ -129,14 +138,17 @@ public class GTaskClient {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 记录上次登录时间
|
|
|
|
|
mLastLoginTime = System.currentTimeMillis();
|
|
|
|
|
|
|
|
|
|
// 登录Google账户
|
|
|
|
|
String authToken = loginGoogleAccount(activity, false);
|
|
|
|
|
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"))) {
|
|
|
|
|
StringBuilder url = new StringBuilder(GTASK_URL).append("a/");
|
|
|
|
@ -151,7 +163,7 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// try to login with google official url
|
|
|
|
|
// 尝试使用Google官方URL登录
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
|
mPostUrl = GTASK_POST_URL;
|
|
|
|
@ -159,24 +171,27 @@ public class GTaskClient {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mLoggedin = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
|
|
|
|
|
String authToken;
|
|
|
|
|
// 从 Google 帐户管理器中获取指定类型的所有帐户
|
|
|
|
|
AccountManager accountManager = AccountManager.get(activity);
|
|
|
|
|
Account[] accounts = accountManager.getAccountsByType("com.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;
|
|
|
|
@ -185,38 +200,44 @@ public class GTaskClient {
|
|
|
|
|
if (account != null) {
|
|
|
|
|
mAccount = account;
|
|
|
|
|
} else {
|
|
|
|
|
// 如果无法获取与应用程序设置中的帐户名称匹配的帐户,则返回 null
|
|
|
|
|
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();
|
|
|
|
|
// 获取令牌字符串
|
|
|
|
|
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
|
|
|
|
|
// 如果需要使令牌无效,则使其无效并重新登录
|
|
|
|
|
if (invalidateToken) {
|
|
|
|
|
accountManager.invalidateAuthToken("com.google", authToken);
|
|
|
|
|
loginGoogleAccount(activity, false);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// 如果无法获取令牌,则返回 null
|
|
|
|
|
Log.e(TAG, "get auth token failed");
|
|
|
|
|
authToken = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回令牌字符串
|
|
|
|
|
return authToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 尝试使用给定的令牌登录到 Gtask
|
|
|
|
|
private boolean tryToLoginGtask(Activity activity, String authToken) {
|
|
|
|
|
if (!loginGtask(authToken)) {
|
|
|
|
|
// maybe the auth token is out of date, now let's invalidate the
|
|
|
|
|
// token and try again
|
|
|
|
|
// 如果登录失败,则尝试使用新的令牌重新登录
|
|
|
|
|
authToken = loginGoogleAccount(activity, true);
|
|
|
|
|
if (authToken == null) {
|
|
|
|
|
Log.e(TAG, "login google account failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果新的令牌也无法登录,则返回 false
|
|
|
|
|
if (!loginGtask(authToken)) {
|
|
|
|
|
Log.e(TAG, "login gtask failed");
|
|
|
|
|
return false;
|
|
|
|
@ -225,7 +246,9 @@ public class GTaskClient {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 使用给定的令牌登录到 Gtask
|
|
|
|
|
private boolean loginGtask(String authToken) {
|
|
|
|
|
// 设置 HTTP 连接和套接字超时时间
|
|
|
|
|
int timeoutConnection = 10000;
|
|
|
|
|
int timeoutSocket = 15000;
|
|
|
|
|
HttpParams httpParameters = new BasicHttpParams();
|
|
|
|
@ -238,12 +261,14 @@ public class GTaskClient {
|
|
|
|
|
|
|
|
|
|
// login gtask
|
|
|
|
|
try {
|
|
|
|
|
// 构造登录URL
|
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken;
|
|
|
|
|
// 创建一个 HTTP GET 请求
|
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl);
|
|
|
|
|
HttpResponse response = null;
|
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
// 发送请求并获取响应
|
|
|
|
|
HttpResponse response = mHttpClient.execute(httpGet);
|
|
|
|
|
|
|
|
|
|
// get the cookie now
|
|
|
|
|
// 获取响应中的 cookie
|
|
|
|
|
List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
|
|
|
|
|
boolean hasAuthCookie = false;
|
|
|
|
|
for (Cookie cookie : cookies) {
|
|
|
|
@ -255,7 +280,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>";
|
|
|
|
@ -277,20 +302,21 @@ public class GTaskClient {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回值为 true 表示请求成功
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//这是一个私有方法,返回一个自增的整数值,即操作ID。在应用程序中,每个HTTP请求都有一个唯一的操作ID,用于跟踪该请求。
|
|
|
|
|
private int getActionId() {
|
|
|
|
|
return mActionId++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//这个方法返回一个HttpPost对象,该对象具有两个请求头:Content-Type和AT。Content-Type指定请求正文的媒体类型,AT是一个自定义请求头,表示访问令牌
|
|
|
|
|
private HttpPost createHttpPost() {
|
|
|
|
|
HttpPost httpPost = new HttpPost(mPostUrl);
|
|
|
|
|
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
|
|
|
|
|
httpPost.setHeader("AT", "1");
|
|
|
|
|
return httpPost;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//这个方法返回HTTP响应实体的内容。它首先检查响应实体的内容编码是否为gzip或deflate,并相应地创建一个解压缩输入流。然后,它使用InputStreamReader和BufferedReader逐行读取响应实体的内容,并将其添加到StringBuilder中。最后,返回StringBuilder的字符串表示形式
|
|
|
|
|
private String getResponseContent(HttpEntity entity) throws IOException {
|
|
|
|
|
String contentEncoding = null;
|
|
|
|
|
if (entity.getContentEncoding() != null) {
|
|
|
|
@ -323,23 +349,30 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送HTTP POST请求以向Google任务API提交JSON数据
|
|
|
|
|
*
|
|
|
|
|
* @param js 包含JSON数据的JSONObject对象
|
|
|
|
|
* @return 响应数据的JSONObject对象
|
|
|
|
|
* @throws NetworkFailureException 如果发送请求失败,则抛出NetworkFailureException异常
|
|
|
|
|
*/
|
|
|
|
|
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
if (!mLoggedin) { // 如果未登录,则抛出异常
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HttpPost httpPost = createHttpPost();
|
|
|
|
|
HttpPost httpPost = createHttpPost(); // 创建HttpPost对象
|
|
|
|
|
try {
|
|
|
|
|
LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
|
|
|
|
|
list.add(new BasicNameValuePair("r", js.toString()));
|
|
|
|
|
list.add(new BasicNameValuePair("r", js.toString())); // 将JSON数据作为参数添加到HttpPost对象中
|
|
|
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
|
|
|
|
|
httpPost.setEntity(entity);
|
|
|
|
|
httpPost.setEntity(entity); // 设置HttpPost对象的实体
|
|
|
|
|
|
|
|
|
|
// execute the post
|
|
|
|
|
HttpResponse response = mHttpClient.execute(httpPost);
|
|
|
|
|
String jsString = getResponseContent(response.getEntity());
|
|
|
|
|
return new JSONObject(jsString);
|
|
|
|
|
// 执行HttpPost请求
|
|
|
|
|
HttpResponse response = mHttpClient.execute(httpPost); // 发送HttpPost请求并接收响应
|
|
|
|
|
String jsString = getResponseContent(response.getEntity()); // 获取响应的实体内容
|
|
|
|
|
return new JSONObject(jsString); // 将响应的实体内容转换为JSONObject对象并返回
|
|
|
|
|
|
|
|
|
|
} catch (ClientProtocolException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
@ -360,20 +393,26 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建新任务
|
|
|
|
|
*
|
|
|
|
|
* @param task 新任务
|
|
|
|
|
* @throws NetworkFailureException 如果发送请求失败,则抛出NetworkFailureException异常
|
|
|
|
|
*/
|
|
|
|
|
public void createTask(Task task) throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
commitUpdate(); // 提交更改
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
// 添加“创建任务”操作到操作列表中
|
|
|
|
|
actionList.put(task.getCreateAction(getActionId()));
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
// 将操作列表和客户端版本添加到jsPost对象中
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// post
|
|
|
|
|
// 发送HttpPost请求,并从响应中获取新任务的ID并将其设置到任务对象中
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
@ -385,10 +424,10 @@ public class GTaskClient {
|
|
|
|
|
throw new ActionFailureException("create task: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//createTaskList 方法创建一个新的任务列表
|
|
|
|
|
public void createTaskList(TaskList tasklist) throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
try {//JSONObject 和 JSONArray 是 Java 中的 JSON 对象和数组,getActionId() 方法返回任务的 ID。
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
|
|
|
|
@ -400,21 +439,25 @@ public class GTaskClient {
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// post
|
|
|
|
|
//postRequest 方法向服务器发送请求并返回响应的 JSON 对象。
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
|
tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
//JSONException 是在处理 JSON 时可能出现的异常。
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("create tasklist: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}//JSONException 是在处理 JSON 时可能出现的异常。
|
|
|
|
|
//commitUpdate 方法提交之前对任务列表的修改
|
|
|
|
|
public void commitUpdate() throws NetworkFailureException {
|
|
|
|
|
if (mUpdateArray != null) {
|
|
|
|
|
//if 语句检查是否存在要提交的更新
|
|
|
|
|
try {
|
|
|
|
|
//mUpdateArray 是一个 JSONArray 对象,包含要提交的更新操作。
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
@ -423,7 +466,7 @@ public class GTaskClient {
|
|
|
|
|
// client_version
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
postRequest(jsPost);//postRequest 方法向服务器发送请求并返回响应的 JSON 对象
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
@ -432,13 +475,13 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//addUpdateNode 方法向任务列表中添加一个更新操作。
|
|
|
|
|
public void addUpdateNode(Node node) throws NetworkFailureException {
|
|
|
|
|
if (node != null) {
|
|
|
|
|
// too many update items may result in an error
|
|
|
|
|
// set max to 10 items
|
|
|
|
|
// set max to 10 itemsmUpdateArray.length() > 10 判断要提交的更新操作数量是否超过 10 个。
|
|
|
|
|
if (mUpdateArray != null && mUpdateArray.length() > 10) {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
commitUpdate();//commitUpdate() 方法提交之前对任务列表的修改。
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mUpdateArray == null)
|
|
|
|
@ -446,7 +489,7 @@ public class GTaskClient {
|
|
|
|
|
mUpdateArray.put(node.getUpdateAction(getActionId()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//在 moveTask 方法中,首先调用了 commitUpdate 方法,然后创建了一个 JSON 对象 jsPost,它包含一个 JSON 数组 actionList
|
|
|
|
|
public void moveTask(Task task, TaskList preParent, TaskList curParent)
|
|
|
|
|
throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
@ -485,7 +528,7 @@ public class GTaskClient {
|
|
|
|
|
throw new ActionFailureException("move task: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//在 deleteNode 方法中,也调用了 commitUpdate 方法,然后创建了一个 JSON 对象 jsPost,它包含一个 JSON 数组 actionList。然后将 node 对象的 deleted 属性设置为 true,并将其 updateAction 添加到 actionList 中。最后,将客户端版本设置为 jsPost 对象的属性,并使用 postRequest 方法将其发送到 Google Tasks API。如果出现任何异常,则会抛出 ActionFailureException
|
|
|
|
|
public void deleteNode(Node node) throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|