private void addActionChangeFlag() {
   final StringListDialog dialog = new StringListDialog();
   dialog.setTitle("Alterar importância para");
   dialog.setStringListId(R.array.flags_meaning_list);
   dialog.setInitialIndex(mIssuesEntity.getFlagType());
   dialog.show(getFragmentManager(), CHANGE_FLAG_DIALOG);
 }
 private void addActionClose() {
   if (mIssuesEntity.getState() != IssueUtils.STATE_CLOSED) {
     TextDialog textDialog = new TextDialog();
     textDialog.setTitle("Fechar");
     textDialog.setHint("Motivo do fechamento aqui");
     textDialog.show(getFragmentManager(), ACTION_CLOSE_TAG);
   }
 }
 private void addActionAssignToMe() {
   final UsersManager usersManager = new UsersManager(this);
   final UsersEntity currentUser = usersManager.getCurrentUser();
   if (!currentUser.getId().equals(mIssuesEntity.getOwnerId())) {
     final IssuesManager issuesManager = new IssuesManager(this);
     mIssuesEntity.setOwnerId(currentUser.getId());
     issuesManager.refresh(mIssuesEntity);
     mTxtOwner.setText(currentUser.getShortName());
     final String timeStamp = TimeStampUtils.dateToTimeStamp(new Date());
     ActionsManager actionsManager = new ActionsManager(this);
     ActionsEntity actionsEntity =
         new ActionsEntity(
             null,
             mIssuesEntity.getId(),
             timeStamp,
             "Atribuição",
             currentUser.getShortName() + " assumiu o incidente",
             currentUser.getId());
     actionsManager.refresh(actionsEntity);
   }
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
   // Inflate the menu; this adds items to the action bar if it is present.
   getMenuInflater().inflate(R.menu.issue_activity, menu);
   MenuItem menuItem = menu.findItem(R.id.action_follow);
   final FollowersManager followersManager = new FollowersManager(this);
   final UsersManager usersManager = new UsersManager(this);
   menuItem.setChecked(
       followersManager.userFollowsIssue(
           usersManager.getCurrentUser().getId(), mIssuesEntity.getId()));
   return true;
 }
  @Override
  public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    switch (id) {
      case ACTIONS_LOADER_ID:
        return new CursorLoader(
            this,
            Contract.Actions.CONTENT_URI,
            null,
            Contract.Actions.ISSUE_ID_SELECTION,
            new String[] {mIssuesEntity.getId().toString()},
            Contract.Actions.TIME_STAMP_DESC);

      default:
        throw new IllegalArgumentException("Invalid loader id=" + id);
    }
  }
  private void addActionAssignToOther() {
    final UsersManager usersManager = new UsersManager(this);
    final UsersEntity currentUser = usersManager.getCurrentUser();
    final IssuesManager issuesManager = new IssuesManager(this);
    final ActionsManager actionsManager = new ActionsManager(this);

    List<Long> excludeCandidates = new ArrayList<Long>();
    excludeCandidates.add(currentUser.getId());
    excludeCandidates.add(mIssuesEntity.getOwnerId());
    final List<String> candidates = usersManager.getAllUsersShortName(excludeCandidates);

    ArrayAdapter<String> adapter =
        new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
    adapter.addAll(candidates);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Atribuir a");
    builder.setSingleChoiceItems(
        adapter,
        0,
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {

            final UsersEntity entity = usersManager.getUserFromShortName(candidates.get(which));
            mIssuesEntity.setOwnerId(entity.getId());
            issuesManager.refresh(mIssuesEntity);
            mTxtOwner.setText(candidates.get(which));

            final String timeStamp = TimeStampUtils.dateToTimeStamp(new Date());
            ActionsEntity actionsEntity =
                new ActionsEntity(
                    null,
                    mIssuesEntity.getId(),
                    timeStamp,
                    "Atribuição",
                    "Incidente atribuído a " + candidates.get(which),
                    currentUser.getId());
            actionsManager.refresh(actionsEntity);

            dialog.dismiss();
          }
        });
    builder.create().show();
  }
  @Override
  public void onTextSet(String tag, String actualText) {

    if (ACTION_UPDATE_TAG.equals(tag)) {
      final ActionsManager actionsManager = new ActionsManager(this);
      final String timeStamp = TimeStampUtils.dateToTimeStamp(new Date());
      final UsersManager usersManager = new UsersManager(this);
      final UsersEntity currentUser = usersManager.getCurrentUser();

      ActionsEntity entity =
          new ActionsEntity(
              null,
              mIssuesEntity.getId(),
              timeStamp,
              "Atualização",
              actualText,
              currentUser.getId());
      actionsManager.refresh(entity);

    } else if (ACTION_CLOSE_TAG.equals(tag)) {
      final IssuesManager issuesManager = new IssuesManager(this);
      final ActionsManager actionsManager = new ActionsManager(this);
      final UsersManager usersManager = new UsersManager(this);
      final UsersEntity currentUser = usersManager.getCurrentUser();

      mIssuesEntity.setState(IssueUtils.STATE_CLOSED);
      issuesManager.refresh(mIssuesEntity);
      mImgFlagAndClockType.setImageResource(
          DashboardUtils.getImageResourceIdForStateFlagTypeAndClockType(
              mIssuesEntity.getState(),
              mIssuesEntity.getFlagType(),
              mIssuesEntity.getClockType(),
              true));

      final String timeStamp = TimeStampUtils.dateToTimeStamp(new Date());
      ActionsEntity entity =
          new ActionsEntity(
              null,
              mIssuesEntity.getId(),
              timeStamp,
              "Fechamento",
              actualText,
              currentUser.getId());
      actionsManager.refresh(entity);

    } else {
      throw new IllegalArgumentException("Invalid tag=" + tag);
    }
  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    final int itemId = item.getItemId();

    switch (itemId) {
      case R.id.action_follow:
        final UsersManager usersManager = new UsersManager(this);
        final FollowersManager followersManager = new FollowersManager(this);
        final Long currentUserId = usersManager.getCurrentUser().getId();
        final Long issueId = mIssuesEntity.getId();
        followersManager.toggleUserFollowsIssue(currentUserId, issueId);
        mTxtFollowers.setText(followersManager.humanPhraseFromIssueId(issueId, currentUserId));
        item.setChecked(followersManager.userFollowsIssue(currentUserId, issueId));
        return true;
      case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }
  @Override
  public void onStringSelected(String tag, int chosenIndex) {
    if (ACTION_TYPE_DIALOG.equals(tag)) {
      addNewAction(chosenIndex);
    } else if (CHANGE_FLAG_DIALOG.equals(tag)) {
      final IssuesManager issuesManager = new IssuesManager(this);
      final ActionsManager actionsManager = new ActionsManager(this);
      final UsersManager usersManager = new UsersManager(this);
      final int oldFlagType = mIssuesEntity.getFlagType();
      mIssuesEntity.setFlagType(chosenIndex);
      issuesManager.refresh(mIssuesEntity);
      mImgFlagAndClockType.setImageResource(
          DashboardUtils.getImageResourceIdForStateFlagTypeAndClockType(
              mIssuesEntity.getState(),
              mIssuesEntity.getFlagType(),
              mIssuesEntity.getClockType(),
              true));

      final String timeStamp = TimeStampUtils.dateToTimeStamp(new Date());
      final String[] flagMeaningArray = getResources().getStringArray(R.array.flags_meaning_list);
      ActionsEntity actionsEntity =
          new ActionsEntity(
              null,
              mIssuesEntity.getId(),
              timeStamp,
              "Mudança de importância",
              "Incidente mudou de '"
                  + flagMeaningArray[oldFlagType]
                  + "' para '"
                  + flagMeaningArray[mIssuesEntity.getFlagType()]
                  + "'",
              usersManager.getCurrentUser().getId());
      actionsManager.refresh(actionsEntity);
    } else {
      throw new IllegalArgumentException("Invalid tag=" + tag);
    }
  }
 private void refreshScreen() {
   mImgFlagAndClockType.setImageResource(
       DashboardUtils.getImageResourceIdForStateFlagTypeAndClockType(
           mIssuesEntity.getState(),
           mIssuesEntity.getFlagType(),
           mIssuesEntity.getClockType(),
           true));
   mTxtSummary.setText(mIssuesEntity.getSummary());
   mTxtAcronymId.setText(mIssuesEntity.getAcronymId());
   mTxtDescription.setText(mIssuesEntity.getDescription());
   final UsersManager usersManager = new UsersManager(this);
   final String reportPhrase =
       "por "
           + usersManager.userFromId(mIssuesEntity.getReporterId()).getShortName()
           + " em "
           + TimeStampUtils.timeStampToFormattedString(mIssuesEntity.getTimeStamp());
   mTxtReporter.setText(reportPhrase);
   mTxtOwner.setText(
       mIssuesEntity.getOwnerId() == null
           ? "(não atribuído)"
           : usersManager.userFromId(mIssuesEntity.getOwnerId()).getShortName());
   final FollowersManager followersManager = new FollowersManager(this);
   mTxtFollowers.setText(
       followersManager.humanPhraseFromIssueId(
           mIssuesEntity.getId(), new UsersManager(this).getCurrentUser().getId()));
 }
 @Override
 protected void onSaveInstanceState(@SuppressWarnings("NullableProblems") Bundle outState) {
   super.onSaveInstanceState(outState);
   outState.putLong(ISSUE_ID_KEY, mIssuesEntity.getId());
 }