Пример #1
0
  /**
   * Hook method called when a new instance of PalantiriPresenter is created. One time
   * initialization code goes here, e.g., storing a WeakReference to the View layer and initializing
   * the Model layer.
   *
   * @param view A reference to the View layer.
   */
  @Override
  public void onCreate(MVP.RequiredViewOps view) {
    // Set the WeakReference.
    mView = new WeakReference<>(view);

    // Invoke the special onCreate() method in GenericPresenter,
    // passing in the PalantiriModel class to instantiate/manage
    // and "this" to provide this MVP.RequiredModelOps instance.
    super.onCreate(PalantiriModel.class, this);

    // Get the intent used to start the Activity.
    final Intent intent = view.getIntent();

    // Initialize the Options singleton using the extras contained
    // in the intent.
    if (Options.instance().parseArgs(view.getActivityContext(), makeArgv(intent)) == false)
      Utils.showToast(view.getActivityContext(), "Arguments were incorrect");

    // A runtime configuration change has not yet occurred.
    mConfigurationChangeOccurred = false;
  }
Пример #2
0
  /** Perform the Being gazing logic. */
  @Override
  public Void doInBackground(PalantiriPresenter... presenters) {
    // Don't start the threads immediately.
    Utils.pauseThread(500);

    // Initialize local variables.
    int i = 0;
    Palantir palantir = null;
    final PalantiriPresenter presenter = presenters[0];

    // Try to gaze at a palantir the designated number of times.
    for (; i < Options.instance().gazingIterations(); ++i)
      try {
        // Break out of the loop if the BeingAsyncTask has
        // been cancelled.
        // you fill in here by replacing "false" with
        // the appropriate method call to an AsyncTask method.
        if (isCancelled()) {
          // If we've been instructed to stop gazing, notify
          // the UI and exit gracefully.
          presenter.mView.get().threadShutdown(mIndex);
          break;
        }

        // Show that we're waiting on the screen.
        // you fill in here with the appropriate
        // call to an AsyncTask method.
        publishProgress(presenter.mView.get().markWaiting(mIndex));
        // Get a Palantir - this call blocks if there are no
        // available Palantiri.
        palantir = presenter.getModel().acquirePalantir();

        if (palantir == null)
          Log.d(
              TAG,
              "Received a null palantir in "
                  + Thread.currentThread().getId()
                  + " for Being "
                  + mIndex);

        // Make sure we were supposed to get a Palantir.
        if (!incrementGazingCountAndCheck(presenter)) break;

        // Mark it as used on the screen.
        // you fill in here with the appropriate
        // call to an AsyncTask method.
        publishProgress(presenter.mView.get().markUsed(palantir.getId()));
        // Show that we're gazing on the screen.
        // you fill in here with the appropriate
        // call to an AsyncTask method.
        publishProgress(presenter.mView.get().markGazing(mIndex));
        // Gaze at my Palantir for the alloted time.
        palantir.gaze();

        // Show that we're no longer gazing.
        // you fill in here with the appropriate
        // call to an AsyncTask method.
        publishProgress(presenter.mView.get().markIdle(mIndex));
        Utils.pauseThread(500);

        // Mark the Palantir as being free.
        // you fill in here with the appropriate call
        // to an AsyncTask method.
        publishProgress(presenter.mView.get().markFree(palantir.getId()));
        Utils.pauseThread(500);

        // Tell the double-checker that we're about to
        // give up a Palantir.
        decrementGazingCount(presenter);
      } catch (Exception e) {
        Log.d(TAG, "Exception caught in index " + mIndex);

        // If we're interrupted by an exception, notify the UI and
        // exit gracefully.
        presenter.mView.get().threadShutdown(mIndex);
      } finally {
        // Give it back to the manager.
        presenter.getModel().releasePalantir(palantir);
      }

    Log.d(
        TAG,
        "Thread "
            + mIndex
            + " has finished "
            + i
            + " of its "
            + Options.instance().gazingIterations()
            + " gazing iterations");
    return (Void) null;
  }