Ejemplo n.º 1
0
  /**
   * Checks according to preferences and charging/wifi state, whether syncthing should be enabled or
   * not.
   *
   * <p>Depending on the result, syncthing is started or stopped, and {@link #onApiChange()} is
   * called.
   */
  public void updateState() {
    // Start syncthing.
    if (mDeviceStateHolder.shouldRun()) {
      if (mCurrentState == State.ACTIVE || mCurrentState == State.STARTING) {
        mStopScheduled = false;
        return;
      }

      // HACK: Make sure there is no syncthing binary left running from an improper
      // shutdown (eg Play Store update).
      // NOTE: This will log an exception if syncthing is not actually running.
      shutdown();

      Log.i(TAG, "Starting syncthing according to current state and preferences");
      mConfig = null;
      try {
        mConfig = new ConfigXml(SyncthingService.this);
      } catch (ConfigXml.OpenConfigException e) {
        mCurrentState = State.ERROR;
        Toast.makeText(this, R.string.config_create_failed, Toast.LENGTH_LONG).show();
      }

      if (mConfig != null) {
        mCurrentState = State.STARTING;

        if (mApi != null) registerOnWebGuiAvailableListener(mApi);
        if (mEventProcessor != null) registerOnWebGuiAvailableListener(mEventProcessor);
        new PollWebGuiAvailableTaskImpl(
                getWebGuiUrl(), getFilesDir() + "/" + HTTPS_CERT_FILE, mConfig.getApiKey())
            .execute();
        mRunnable = new SyncthingRunnable(this, SyncthingRunnable.Command.main);
        new Thread(mRunnable).start();
        updateNotification();
      }
    }
    // Stop syncthing.
    else {
      if (mCurrentState == State.DISABLED) return;

      Log.i(TAG, "Stopping syncthing according to current state and preferences");
      mCurrentState = State.DISABLED;

      shutdown();
    }
    onApiChange();
  }
    @Override
    protected Pair<String, String> doInBackground(Void... voids) {
      // Looper.prepare();
      if (isFirstStart()) {
        Log.i(
            TAG,
            "App started for the first time. "
                + "Copying default config, keys will be generated automatically");
        copyDefaultConfig();
      }

      moveConfigFiles();
      ConfigXml config = new ConfigXml(getConfigFile());
      if (isFirstStart()) {
        config.createCameraRepo();
      }
      config.update();
      return new Pair<String, String>(config.getWebGuiUrl(), config.getApiKey());
    }