Exemplo n.º 1
0
  private boolean canSync() {
    if (!AccountEntity.hasAccount(context)) // 没有帐号则不进行同步
    {
      return false;
    }

    if (!NetworkControl.getNetworkState(context)) // 没有网络不进行同步
    {
      return false;
    }

    if (StringUtil.isNullOrEmpty(new MyAccountManager(context).getAccount().getToken())) { // 无token
      return false;
    }
    return true;
  }
Exemplo n.º 2
0
  public void run() {
    // 判断网络连接apn,如果是wap则设置代理,供HttpConnectionAdapter使用
    NetType netType = NetworkControl.getNetType(context);
    if (netType != null && netType.isWap()) {
      SyncService.proxy =
          new Proxy(
              java.net.Proxy.Type.HTTP,
              new InetSocketAddress(netType.getProxy(), netType.getPort()));
    } else {
      SyncService.proxy = null;
    }

    MyAccountManager accountControl = new MyAccountManager(this.context);
    AccountEntity account = accountControl.getAccount();
    if (account.getGoogleAccountState() == AccountEntity.GOOGLE_USER_BIND) // 有google帐号时才同步google
    {
      SyncControl.updateLastSyncTime(context, System.currentTimeMillis());
      SyncControl.setNextSync(context);
      SyncService.syncGoogleCalendar(true, this.handler, this.context);
    }
  }
Exemplo n.º 3
0
  // 同步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);
    }
  }