Beispiel #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;
  }
  /**
   * When the instrumentation is created it performs the basic configuration actions of initializing
   * the log, getting the test suite script url, initializing the required command runners to be
   * used by the BasicCommandRunner in order to run the test suite. After the initialization the
   * instrumentation is started.
   *
   * @param arguments the Bundle object with the Instrumentation arguments. The script url should be
   *     defined here
   */
  @Override
  public void onCreate(Bundle arguments) {
    instance = this;
    super.onCreate(arguments);

    // Init log
    Log.initLog(new AndroidLogAppender(TAG_LOG), Log.TRACE);

    boolean stopOnFailure = false;

    // Get the extra params
    if (arguments != null) {
      mainScriptUrl = arguments.getString(EXTRA_SCRIPT_URL);
      stopOnFailure = arguments.containsKey(EXTRA_STOP_ON_FAILURE);
      scriptUrlFromCommandLine = mainScriptUrl;
    }
    if (StringUtil.isNullOrEmpty(mainScriptUrl)) {
      mainScriptUrl = SCRIPT_URL_DEFAULT;
    }

    AndroidBasicRobot basicRobot = new AndroidBasicRobot(this);
    acr = new AndroidCommandRunner(this, basicRobot);
    basicRunner = new BasicScriptRunner();
    basicRunner.addCommandRunner(acr);
    // Setup contacts script runner
    AndroidContactsRobot aContactRobot = new AndroidContactsRobot(this, basicRobot);
    aContactRobot.setScriptRunner(basicRunner);
    ContactsCommandRunner contactsCommandRunner = new ContactsCommandRunner(aContactRobot);
    basicRunner.addCommandRunner(contactsCommandRunner);
    // Setup contacts script runner
    AndroidCalendarsRobot aCalendarsRobot = new AndroidCalendarsRobot(this, basicRobot);
    aCalendarsRobot.setScriptRunner(basicRunner);
    CalendarCommandRunner calendarCommandRunner = new CalendarCommandRunner(aCalendarsRobot);
    basicRunner.addCommandRunner(calendarCommandRunner);
    // Other properties
    basicRunner.setStopOnFailure(stopOnFailure);
    start();
  }
  public void run() {
    if (Log.isLoggable(Log.INFO)) {
      Log.info(TAG_LOG, "Signing up");
    }

    signupScreen.disableSignup();
    signupScreenController.signupStarted();

    // Get the current values from UI
    String serverUrl = signupScreen.getSyncUrl();
    String phoneNumber = signupScreen.getUsername();
    String password = signupScreen.getPassword();
    String token = signupScreen.getCaptchaToken();

    String signupUrl = StringUtil.extractAddressFromUrl(serverUrl);

    SapiHandler sapiHandler = new SapiHandler(signupUrl);
    String jsessionId = signupScreenController.getCurrentJSessionId();
    if (jsessionId != null) {
      sapiHandler.enableJSessionAuthentication(true);
      sapiHandler.forceJSessionId(jsessionId);
    }
    try {
      if (Log.isLoggable(Log.DEBUG)) {
        Log.debug(TAG_LOG, "Sending Signup SAPI request");
      }
      JSONObject request = createMobileSignupRequest(phoneNumber, password);
      Vector params = new Vector();
      StringBuffer tokenParam = new StringBuffer();
      tokenParam.append("token=").append(token);
      params.addElement(tokenParam.toString());
      JSONObject res = sapiHandler.query("mobile", "signup", params, null, request);
      if (!userActivated(res)) {
        if (Log.isLoggable(Log.DEBUG)) {
          Log.debug(TAG_LOG, "Account not activated by the server");
        }
        handleResponseError(res, signupUrl);
        return;
      } else {
        if (Log.isLoggable(Log.INFO)) {
          Log.info(TAG_LOG, "Account activated by the server");
        }
      }
    } catch (NotAuthorizedCallException ex) {
      // This is a non authorized error
      Log.error(TAG_LOG, "Unable to signup", ex);
      signupScreenController.signupFailed(
          localization.getLanguage("signup_failed_generic_message"));
      signupScreenController.promptCredentials();
    } catch (IOException ex) {
      // This is a network failure
      Log.error(TAG_LOG, "Unable to signup", ex);
      signupScreenController.signupFailed(localization.getLanguage("signup_failed_network"));
      signupScreenController.promptCredentials();
      return;
    } catch (Exception ex) {
      // This is a generic failure
      Log.error(TAG_LOG, "Unable to signup", ex);
      signupScreenController.signupFailed(
          localization.getLanguage("signup_failed_generic_message"));
      signupScreenController.promptCredentials();
      return;
    }
    // Signup Succeeded
    signupScreenController.signupSucceeded();
  }