// 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));
  }
  @Override
  public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    if (running) {
      Log.i(PacoConstants.TAG, "Paco App Usage Poller.onStart() -- Already running");
      stopSelf();
      return;
    } else {
      Log.i(PacoConstants.TAG, "Paco App Usage Poller.onStart()");

      UsageStatsManager am = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
      final AppUsageEventsService usageEventsService =
          new AppUsageEventsService(
              am, BroadcastTriggerReceiver.getFrequency(getApplicationContext()));
      if (!usageEventsService.canGetStats()) {
        Log.i(PacoConstants.TAG, "no access to Usage Stats. Please turn on setting.");
        stopSelf();
        return;
      }

      final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
      final PowerManager.WakeLock wl =
          pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Paco App Usage Poller Service wakelock");
      wl.acquire();

      Runnable runnable =
          new Runnable() {
            public void run() {
              running = true;
              LollipopAppUsageMonitor lpcm = createAppUsageMonitor(usageEventsService);

              try {
                while (pm.isScreenOn()
                    && BroadcastTriggerReceiver.shouldWatchProcesses(getApplicationContext())) {
                  synchronized (this) {
                    try {
                      Log.i(
                          PacoConstants.TAG, "polling on: runnable instance = " + this.toString());
                      Log.i(PacoConstants.TAG, "==================================");
                      lpcm.detectUsageEvents();
                      int sleepTime =
                          1000; // BroadcastTriggerReceiver.getFrequency(LollipopProcessMonitorService.this) * 1000;
                      Log.i(PacoConstants.TAG, "sleepTime = " + sleepTime);
                      wait(sleepTime);
                    } catch (Exception e) {
                    }
                  }
                }
                if (!pm.isScreenOn()
                    && BroadcastTriggerReceiver.shouldWatchProcesses(getApplicationContext())) {
                  createScreenOffPacoEvents(getApplicationContext());
                }
                Log.i(
                    PacoConstants.TAG,
                    "polling stopping: instance = "
                        + LollipopProcessMonitorService.this.toString());
              } finally {
                wl.release();
                stopSelf();
                running = false;
              }
            }

            public LollipopAppUsageMonitor createAppUsageMonitor(
                final AppUsageEventsService usageEventsService) {
              ExperimentProviderUtil experimentProviderUtil =
                  new ExperimentProviderUtil(getApplicationContext());
              AppUsageTriggerHelper apmh = new AppUsageTriggerHelper(experimentProviderUtil);
              List<String> appOpenTasks = apmh.getAppStartTasksToWatch();
              List<String> appCloseTasks = apmh.getAppCloseTasksToWatch();
              List<Experiment> experimentsWatchingAppUsage =
                  apmh.initializeExperimentsWatchingAppUsage();

              AppUsageEventLogger pueb =
                  new AppUsageEventLogger(
                      getApplicationContext(), experimentsWatchingAppUsage, experimentProviderUtil);

              LollipopAppUsageMonitor lpcm =
                  new LollipopAppUsageMonitor(
                      appOpenTasks,
                      appCloseTasks,
                      getApplicationContext(),
                      usageEventsService,
                      pueb);
              return lpcm;
            }
          };
      (new Thread(runnable)).start();
    }
  }