@Override
 protected void onPostResume() {
   super.onPostResume();
   final Flow flow = getCurrentlyFocusedFlow();
   Log.d(TAG, "onPostResume: focusedFlow: " + flow);
   if (flow != null) {
     updateControlsForFlow(flow);
     if (Strings.isNullOrEmpty(flow.getImagePath())) {
       if (mShowCamera && mConfig.getEnableAutoTakePhoto()) {
         mCameraFragment.schedulePicture();
       }
     }
   }
 }
  private void updateControlsForFlow(Flow flow) {
    if (flow == null) {
      // Tap is inactive.
      mControlsFlipper.setDisplayedChild(0);
      return;
    }

    mControlsFlipper.setDisplayedChild(1);

    if (!mConfig.useAccounts()) {
      mClaimPourButton.setVisibility(View.GONE);
      mShoutText.setVisibility(View.GONE);
      mDrinkerName.setVisibility(View.GONE);
      mDrinkerImage.setVisibility(View.GONE);
    } else {
      boolean imageWasReplaced = false;
      mClaimPourButton.setEnabled(true);

      if (flow.isAnonymous()) {
        mClaimPourButton.setVisibility(View.VISIBLE);
        mDrinkerName.setVisibility(View.GONE);
      } else {
        final String username = flow.getUsername();
        mClaimPourButton.setVisibility(View.GONE);
        mDrinkerName.setVisibility(View.VISIBLE);
        mDrinkerName.setText("Pouring: " + username);

        final AuthenticationManager authManager = mCore.getAuthenticationManager();
        final User user = authManager.getUserDetail(username);
        if (user != null && user.hasImage()) {
          // NOTE(mikey): Use the full-sized image rather than the thumbnail;
          // in many cases the former will already be in the cache from
          // DrinkerSelectActivity.
          final String thumbnailUrl = user.getImage().getThumbnailUrl();
          if (!Strings.isNullOrEmpty(thumbnailUrl)) {
            mImageDownloader.download(thumbnailUrl, mDrinkerImage);
            imageWasReplaced = true;
          }
        } else {
          Log.d(TAG, "No user info.");
        }
      }

      if (!imageWasReplaced) {
        mDrinkerImage.setImageBitmap(null);
        Utils.setBackground(mDrinkerImage, getResources().getDrawable(R.drawable.unknown_drinker));
      }
    }
  }
  private void refreshFlows() {
    final Set<Flow> activeFlows = Sets.newLinkedHashSet(mFlowManager.getAllActiveFlows());
    mActiveFlows.addAll(activeFlows);
    final Set<Flow> oldFlows = Sets.newLinkedHashSet(Sets.difference(mActiveFlows, activeFlows));
    for (final Flow oldFlow : oldFlows) {
      Log.d(TAG, "Removing inactive flow: " + oldFlow);
      mActiveFlows.remove(oldFlow);
    }

    if (mShowCamera) {
      mCameraFragment.setEnabled(!mActiveFlows.isEmpty());
    }

    if (mActiveFlows.isEmpty()) {
      cancelIdleWarning();
      finish();
      return;
    }

    long largestIdleTime = Long.MIN_VALUE;
    for (final Flow flow : mActiveFlows) {
      if (flow.getTap() == null) {
        continue;
      }

      if (DEBUG) {
        Log.d(TAG, "Refreshing with flow: " + flow);
      }

      final long idleTimeMs = flow.getIdleTimeMs();
      if (idleTimeMs > largestIdleTime) {
        largestIdleTime = idleTimeMs;
      }
    }

    if (largestIdleTime >= mConfig.getIdleWarningMs()) {
      sendIdleWarning();
    } else {
      cancelIdleWarning();
    }

    scrollToMostActiveTap();
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate()");

    mCore = KegbotCore.getInstance(this);
    mFlowManager = mCore.getFlowManager();
    mConfig = mCore.getConfiguration();
    mImageDownloader = mCore.getImageDownloader();

    final ActionBar actionBar = getActionBar();
    if (actionBar != null) {
      actionBar.hide();
    }
    setContentView(R.layout.pour_in_progress_activity);

    mTapPager = (ViewPager) findViewById(R.id.tapPager);
    mPouringTapAdapter = new PouringTapAdapter(getFragmentManager());
    mTapPager.setAdapter(mPouringTapAdapter);
    mTapPager.setOnPageChangeListener(mPageChangeListener);

    mControlsFlipper = (ViewFlipper) findViewById(R.id.pour_controls_flipper);
    mClaimPourButton = (Button) findViewById(R.id.claimPourButton);
    mDrinkerName = (TextView) findViewById(R.id.pourDrinkerName);
    mDoneButton = (Button) findViewById(R.id.pourEndButton);
    mDrinkerImage = (ImageView) findViewById(R.id.pourDrinkerImage);
    mShoutText = (TextView) findViewById(R.id.shoutText);

    mClaimPourButton.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            final Flow flow = getCurrentlyFocusedFlow();
            if (flow == null || flow.isAuthenticated() || flow.isFinished()) {
              return;
            }

            Log.d(TAG, "Attempting to claim flow id=" + flow.getFlowId());
            final Intent intent =
                KegtabCommon.getAuthDrinkerActivityIntent(PourInProgressActivity.this);
            intent.putExtra(EXTRA_FLOW_ID, flow.getFlowId());
            startActivityForResult(intent, REQUEST_AUTH_DRINKER);
          }
        });

    mDoneButton.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View v) {
            final FlowManager flowManager = mCore.getFlowManager();
            final Flow flow = getCurrentlyFocusedFlow();
            if (flow == null) {
              return;
            }
            Log.d(TAG, "Done button pressed, ending flow " + flow.getFlowId());
            flowManager.endFlow(flow);

            // If we're finishing a non-dormant flow, and other dormant flows
            // exist, assume those were started optimistically and finish them
            // now.
            if (flow.getVolumeMl() > 0) {
              final AppConfiguration config = mCore.getConfiguration();
              final long minVolume = config.getMinimumVolumeMl();
              for (final Flow suspectFlow : flowManager.getAllActiveFlows()) {
                if (suspectFlow.getVolumeMl() < minVolume) {
                  Log.d(TAG, "Also ending dormant flow: " + suspectFlow.getFlowId());
                  flowManager.endFlow(suspectFlow);
                }
              }
            }
          }
        });

    mShoutText = (EditText) findViewById(R.id.shoutText);
    mShoutText.addTextChangedListener(
        new TextWatcher() {
          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {}

          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

          @Override
          public void afterTextChanged(Editable s) {
            final Flow flow = getCurrentlyFocusedFlow();
            if (flow == null) {
              Log.w(TAG, "Flow went away, dropping shout.");
              return;
            }
            flow.setShout(s.toString());
            flow.pokeActivity();
          }
        });

    mShowCamera = true;
    mCameraFragment = (CameraFragment) getFragmentManager().findFragmentById(R.id.camera);
    if (!mConfig.getUseCamera()) {
      mShowCamera = false;
      getFragmentManager().beginTransaction().hide(mCameraFragment).commit();
    }

    refreshFlows();
  }