protected void emailResearcher(Experiment experiment) {
    String contactEmail = experiment.getExperimentDAO().getContactEmail();
    if (Strings.isNullOrEmpty(contactEmail)) {
      contactEmail = experiment.getExperimentDAO().getCreator();
    }

    launchEmailTo(contactEmail);
  }
 public String formatJoinDate(Experiment experiment) {
   String joinDate = experiment.getJoinDate();
   if (joinDate == null) {
     joinDate = experiment.getExperimentDAO().getJoinDate();
   }
   if (joinDate == null) {
     return "";
   }
   DateTime dt = TimeUtil.parseDateWithZone(joinDate);
   return TimeUtil.formatDateLong(dt);
 }
  private List<Experiment> removeSeenInvitations(List<Experiment> invitations) {
    List<Long> seen = getSeenInvitations();
    List<Experiment> unseen = Lists.newArrayList();
    for (Experiment invitation : invitations) {
      final Long invitationId = invitation.getExperimentDAO().getId();
      if (!seen.contains(invitationId)) {
        unseen.add(invitation);
      }
    }

    return unseen;
  }
 private static List<Experiment> initializeExperimentsWatchingAppUsage(
     ExperimentProviderUtil experimentProviderUtil) {
   List<Experiment> joined = experimentProviderUtil.getJoinedExperiments();
   List<Experiment> experimentsNeedingEvent = Lists.newArrayList();
   DateTime now = DateTime.now();
   for (Experiment experiment2 : joined) {
     if (!ActionScheduleGenerator.isOver(now, experiment2.getExperimentDAO())
         && ExperimentHelper.isLogActions(experiment2.getExperimentDAO())) {
       experimentsNeedingEvent.add(experiment2);
     }
   }
   return experimentsNeedingEvent;
 }
    public View getView(int position, View convertView, ViewGroup parent) {
      View view = convertView;
      if (view == null) {
        view = mInflater.inflate(R.layout.experiment_list_row, null);
      }

      Experiment experiment = getItem(position);

      TextView tv = (TextView) view.findViewById(R.id.experimentListRowTitle);
      tv.setText(experiment != null ? experiment.getExperimentDAO().getTitle() : "");
      tv.setOnClickListener(myButtonListener);
      tv.setTag(experiment.getExperimentDAO().getId());

      TextView organizationView = (TextView) view.findViewById(R.id.experimentListRowSubtitle);
      String organization = experiment.getExperimentDAO().getOrganization();
      if (Strings.isNullOrEmpty(organization) || organization.equals("null")) {
        organization = experiment.getExperimentDAO().getContactEmail();
      }
      organizationView.setText(experiment != null ? "by " + organization : "");
      organizationView.setTag(experiment.getExperimentDAO().getId());
      organizationView.setOnClickListener(myButtonListener);

      TextView joinDateView = (TextView) view.findViewById(R.id.experimentListRowJoinDate);
      joinDateView.setText(experiment != null ? "Joined on " + formatJoinDate(experiment) : "");
      joinDateView.setTag(experiment.getExperimentDAO().getId());
      joinDateView.setOnClickListener(myButtonListener);

      ImageButton menuButton = (ImageButton) view.findViewById(R.id.menuButton);
      menuButton.setOnClickListener(myButtonListener);
      menuButton.setTag(experiment.getExperimentDAO().getId());

      menuButton.setEnabled(true);
      menuButton.setOnClickListener(myButtonListener);
      return view;
    }
  protected List<Experiment> removeJoinedExperiments(List<Experiment> invitations) {
    List<Long> joinedExperimentIds = Lists.newArrayList();
    for (Experiment experiment : experiments) {
      joinedExperimentIds.add(experiment.getExperimentDAO().getId());
    }
    List<Experiment> unseen = Lists.newArrayList();
    for (Experiment invitation : invitations) {
      final Long invitationId = invitation.getExperimentDAO().getId();
      if (!joinedExperimentIds.contains(invitationId)) {
        unseen.add(invitation);
      }
    }

    return unseen;
  }
  protected Event createScreenOffPacoEvent(Experiment experiment) {
    Event event = new Event();
    event.setExperimentId(experiment.getId());
    event.setServerExperimentId(experiment.getServerId());
    event.setExperimentName(experiment.getExperimentDAO().getTitle());
    event.setExperimentVersion(experiment.getExperimentDAO().getVersion());
    event.setResponseTime(new DateTime());

    Output responseForInput = new Output();

    responseForInput.setAnswer(new DateTime().toString());
    responseForInput.setName("userNotPresent");
    event.addResponse(responseForInput);
    return event;
  }
  /**
   * Creates a pacot for stopping an experiment
   *
   * @param experiment
   */
  private void createStopEvent(Experiment experiment) {
    Event event = new Event();
    event.setExperimentId(experiment.getId());
    event.setServerExperimentId(experiment.getExperimentDAO().getId());
    event.setExperimentName(experiment.getExperimentDAO().getTitle());
    event.setExperimentVersion(experiment.getExperimentDAO().getVersion());
    event.setResponseTime(new DateTime());

    Output responseForInput = new Output();
    responseForInput.setAnswer("false");
    responseForInput.setName("joined");
    event.addResponse(responseForInput);

    experimentProviderUtil.insertEvent(event);
    startService(new Intent(this, SyncService.class));
  }
 private void markInvitationSeen(Experiment invitation) {
   Long serverId = invitation.getExperimentDAO().getId();
   List<Long> seen = getSeenInvitations();
   if (!seen.contains(serverId)) {
     seen.add(serverId);
     saveSeenInvitations(seen);
   }
 }
  // Visible for testing
  public void deleteExperiment(Experiment experiment2) {
    NotificationCreator nc = NotificationCreator.create(this);
    nc.timeoutNotificationsForExperiment(experiment2.getExperimentDAO().getId());

    createStopEvent(experiment2);

    experimentProviderUtil.deleteExperiment(experiment2.getId());
    if (ExperimentHelper.shouldWatchProcesses(experiment2.getExperimentDAO())) {
      BroadcastTriggerReceiver.initPollingAndLoggingPreference(this);
    }

    new AndroidEsmSignalStore(this)
        .deleteAllSignalsForSurvey(experiment2.getExperimentDAO().getId());

    reloadAdapter();
    startService(new Intent(this, BeeperService.class));
    startService(new Intent(this, ExperimentExpirationManagerService.class));
  }
  protected void showInvitations(final List<Experiment> invitations) {
    if (invitations.size() == 0) {
      invitationLayout.setVisibility(View.GONE);
      return;
    }

    final Experiment invitation = invitations.get(0);

    invitationExperimentName.setText(invitation.getExperimentDAO().getTitle());
    String organization = invitation.getExperimentDAO().getOrganization();
    if (Strings.isNullOrEmpty(organization) || organization.equals("null")) {
      organization = invitation.getExperimentDAO().getContactEmail();
    }
    invitationContactTextView.setText(organization);
    invitationLayout.setVisibility(View.VISIBLE);
    invitationLayout.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            Intent intent = new Intent(MyExperimentsActivity.this, ExperimentDetailActivity.class);
            intent.putExtra(ExperimentDetailActivity.ID_FROM_MY_EXPERIMENTS_FILE, true);
            intent.putExtra(
                Experiment.EXPERIMENT_SERVER_ID_EXTRA_KEY, invitation.getExperimentDAO().getId());
            startActivity(intent);
          }
        });
    invitationCloseButton.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            invitationLayout.setVisibility(View.GONE);
            markInvitationSeen(invitation);
            invitations.remove(invitation);
            showInvitations(invitations);
          }
        });
  }
          @Override
          public void onClick(final View v) {
            final int position = list.getPositionForView(v);
            if (position == ListView.INVALID_POSITION) {
              return;
            } else {
              final Long experimentServerId = (Long) v.getTag();
              final Experiment experiment = experiments.get(position);
              final List<ExperimentGroup> groups = experiment.getExperimentDAO().getGroups();

              if (v.getId() == R.id.menuButton) {
                showPopup(experiment, v);
              } else {
                Intent experimentIntent = null;
                if (groups.size() > 1) {
                  experimentIntent =
                      new Intent(MyExperimentsActivity.this, ExperimentGroupPicker.class);
                  experimentIntent.putExtra(
                      ExperimentGroupPicker.SHOULD_GO_TO_RENDER_NEXT,
                      ExperimentGroupPicker.RENDER_NEXT);
                } else {
                  Class clazz = null;
                  final ExperimentGroup experimentGroup = groups.get(0);
                  if (experimentGroup.getCustomRendering()) {
                    clazz = ExperimentExecutorCustomRendering.class;
                  } else {
                    clazz = ExperimentExecutor.class;
                  }
                  experimentIntent = new Intent(MyExperimentsActivity.this, clazz);
                  experimentIntent.putExtra(
                      Experiment.EXPERIMENT_GROUP_NAME_EXTRA_KEY, experimentGroup.getName());
                }
                experimentIntent.putExtra(
                    Experiment.EXPERIMENT_SERVER_ID_EXTRA_KEY, experimentServerId);
                startActivity(experimentIntent);
              }
            }
          }
 private void showDataForExperiment(Experiment experiment, List<ExperimentGroup> groups) {
   Intent experimentIntent = null;
   if (groups.size() > 1) {
     experimentIntent = new Intent(MyExperimentsActivity.this, ExperimentGroupPicker.class);
     experimentIntent.putExtra(
         ExperimentGroupPicker.SHOULD_GO_TO_RENDER_NEXT, ExperimentGroupPicker.FEEDBACK_NEXT);
   } else {
     experimentIntent = new Intent(MyExperimentsActivity.this, FeedbackActivity.class);
     experimentIntent.putExtra(
         Experiment.EXPERIMENT_GROUP_NAME_EXTRA_KEY, groups.get(0).getName());
   }
   experimentIntent.putExtra(
       Experiment.EXPERIMENT_SERVER_ID_EXTRA_KEY, experiment.getExperimentDAO().getId());
   startActivity(experimentIntent);
 }
 private boolean userCanEditAtLeastOneSchedule(Experiment experiment) {
   List<ExperimentGroup> groups = experiment.getExperimentDAO().getGroups();
   for (ExperimentGroup experimentGroup : groups) {
     List<ActionTrigger> actionTriggers = experimentGroup.getActionTriggers();
     for (ActionTrigger actionTrigger : actionTriggers) {
       if (actionTrigger instanceof ScheduleTrigger) {
         ScheduleTrigger scheduleTrigger = (ScheduleTrigger) actionTrigger;
         List<Schedule> schedules = scheduleTrigger.getSchedules();
         for (Schedule schedule : schedules) {
           if (schedule.getUserEditable()) {
             boolean userCanOnlyEditOnJoin = schedule.getOnlyEditableOnJoin();
             if (!userCanOnlyEditOnJoin) {
               return true;
             }
           }
         }
       }
     }
   }
   return false;
 }
 private void launchScheduleDetailScreen(Experiment experiment) {
   Intent debugIntent = new Intent(this, ScheduleListActivity.class);
   debugIntent.putExtra(
       Experiment.EXPERIMENT_SERVER_ID_EXTRA_KEY, experiment.getExperimentDAO().getId());
   startActivity(debugIntent);
 }