private void synchronize( boolean riliSource, boolean eventSource, boolean noteSource, boolean birthSource, boolean googleSync) { if (!canSync()) return; // FIXME: // if (ThirdPartyConstant.isTrial(context)) { // birthSource = false; // } if (!riliSource && !eventSource && !noteSource && !birthSource) // 如果啥都不同步直接返回 return; if (!addLock()) { isCalendarWait |= riliSource; isScheduleWait |= eventSource; isNoteWait |= noteSource; isBirthWait |= birthSource; isGoogleWait |= googleSync; return; // 如果加锁失败,表明已经锁住了,直接返回不执行同步 } // 开始同步 if (this.handler != null) { handler.sendEmptyMessage(SyncService.BEGIN_SYNC_MSG); } isCalendarWait = isScheduleWait = isNoteWait = isBirthWait = isGoogleWait = false; new Thread( new SyncThread( this.context, this.handler, riliSource, eventSource, noteSource, birthSource, googleSync)) .start(); }
public SyncService(Context context) { this.handler = SyncHandler.getInstance(); this.context = context; }
// 同步google public static void syncGoogleCalendar(boolean googleSync, SyncHandler handler, Context context) { if (!googleSync) return; String regUrl = GlobalAttributes.SYNC_GOOGLE; try { if (handler != null) { handler.sendEmptyMessage(SyncService.BEGIN_GOOGLE_SYNC); } // 没有网络则不同步 NetworkStatus networkStatus = new NetworkStatus(context); if (networkStatus != null && !networkStatus.isConnected()) { return; } MyAccountManager accountControl = new MyAccountManager(context); AccountEntity account = accountControl.getAccount(); HttpClient httpClient = NetworkControl.getHttpClient(context); HttpPost httpPost = NetworkControl.getHttpPost(regUrl); httpPost.setHeader(AccountEntity.KEY_TOKEN, account.getToken()); // httpPost.setHeader("Authorization", BasicAuthUtils.encodeAuth(account.getUserid(), // account.getAccountPassword())); httpPost.setHeader("coco-ua", NetworkControl.getMyHeader(context)); String clientToken = account.getAuthToken(); String googleAcount = account.getGoogleAccount(); // 如果是clientAUTH,则提交本地的gmail和token if ((clientToken != null) && (clientToken.length() > 0) && (googleAcount != null) && (googleAcount.length() > 0)) { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("googleAccount", googleAcount)); params.add(new BasicNameValuePair("clientToken", clientToken)); httpPost.setEntity(new UrlEncodedFormEntity(params)); } String result = ""; HttpResponse response = httpClient.execute(httpPost); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK && response.getEntity() != null) { result = EntityUtils.toString(response.getEntity(), "UTF8"); } if (result.length() > 0) { JSONObject json = new JSONObject(result); String state = json.getString("state"); if (state.equals(State.OK)) { } else if (state.equals(State.NO_USER_GOOGLE)) // 表示服务器已经取消user_google绑定记录,本地也应该删除对应的记录 { account.setGoogleAccount(""); accountControl.saveAccount(account); } else if (state.equals(State.TOKEN_ERROR)) { // 如果是clientAUTH,则更新token if ((clientToken != null) && (clientToken.length() > 0) && (googleAcount != null) && (googleAcount.length() > 0)) { AccountManager manager = AccountManager.get(context); manager.invalidateAuthToken("com.google", clientToken); // 设置token失效 Account[] accts = manager.getAccountsByType("com.google"); Account gAccount = null; for (int i = 0; i < accts.length; i++) { if (accts[i].name.equals(googleAcount)) { gAccount = accts[i]; break; } } clientToken = manager.blockingGetAuthToken(gAccount, "cl", true); account.setAuthToken(clientToken); accountControl.saveAccount(account); // 重新发起一次同步请求 httpClient = NetworkControl.getHttpClient(context); httpPost = NetworkControl.getHttpPost(regUrl); httpPost.setHeader(AccountEntity.KEY_TOKEN, account.getToken()); httpPost.setHeader("coco-ua", NetworkControl.getMyHeader(context)); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("googleAccount", googleAcount)); params.add(new BasicNameValuePair("clientToken", clientToken)); httpPost.setEntity(new UrlEncodedFormEntity(params)); response = httpClient.execute(httpPost); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK && response.getEntity() != null) { result = EntityUtils.toString(response.getEntity(), "UTF8"); } if (result.length() > 0) { json = new JSONObject(result); state = json.getString("state"); if (state.equals(State.OK)) { } else if (state.equals( State.NO_USER_GOOGLE)) // 表示服务器已经取消user_google绑定记录,本地也应该删除对应的记录 { account.setGoogleAccount(""); accountControl.saveAccount(account); } else if (state.equals(State.TOKEN_ERROR)) { if (handler != null) handler.sendEmptyMessage(SyncService.GOOGLE_TOKEN_ERROR); } else { if (handler != null) handler.sendEmptyMessage(SyncService.SYNC_GOOGLE_ERROR); } } } else // 如果是OAUTH,直接显示错误信息 { if (handler != null) handler.sendEmptyMessage(SyncService.GOOGLE_TOKEN_ERROR); } } else { if (handler != null) handler.sendEmptyMessage(SyncService.SYNC_GOOGLE_ERROR); } } } catch (Exception e) { e.printStackTrace(); if (handler != null) handler.sendEmptyMessage(SyncService.SYNC_GOOGLE_ERROR); } }