Beispiel #1
0
 /**
  * This method is called by SDL using JNI.
  *
  * @return result of getSystemService(name) but executed on UI thread.
  */
 public Object getSystemServiceFromUiThread(final String name) {
   final Object lock = new Object();
   final Object[] results = new Object[2]; // array for writable variables
   synchronized (lock) {
     runOnUiThread(
         new Runnable() {
           @Override
           public void run() {
             synchronized (lock) {
               results[0] = getSystemService(name);
               results[1] = Boolean.TRUE;
               lock.notify();
             }
           }
         });
     if (results[1] == null) {
       try {
         lock.wait();
       } catch (InterruptedException ex) {
         ex.printStackTrace();
       }
     }
   }
   return results[0];
 }
 public void run() {
   int timedCount = 0;
   while (!Thread.interrupted()) {
     try {
       Thread.sleep(SEC_POLL_DELAY);
     } catch (InterruptedException e) {
       break;
     }
     // 5400 seconds = 90 min
     if (timedCount == 5400) {
       timedCount = 0;
       Intent dialogIntent = new Intent(getBaseContext(), BlockerActivity.class);
       dialogIntent.addFlags(
           Intent.FLAG_ACTIVITY_CLEAR_TOP
               | Intent.FLAG_ACTIVITY_NEW_TASK
               | Intent.FLAG_ACTIVITY_CLEAR_TASK);
       dialogIntent.putExtra(
           "AlertInfo", "You've used your phone for 90 minutes NON-STOP. Please take a break!");
       getApplication().startActivity(dialogIntent);
     }
     timedCount++;
     synchronized (mPauseLock) {
       while (mPaused) {
         try {
           mPauseLock.wait();
         } catch (InterruptedException e) {
           e.printStackTrace();
         }
       }
     }
   }
 }
Beispiel #3
0
  /**
   * This method is called by SDL using JNI. Shows the messagebox from UI thread and block calling
   * thread. buttonFlags, buttonIds and buttonTexts must have same length.
   *
   * @param buttonFlags array containing flags for every button.
   * @param buttonIds array containing id for every button.
   * @param buttonTexts array containing text for every button.
   * @param colors null for default or array of length 5 containing colors.
   * @return button id or -1.
   */
  public int messageboxShowMessageBox(
      final int flags,
      final String title,
      final String message,
      final int[] buttonFlags,
      final int[] buttonIds,
      final String[] buttonTexts,
      final int[] colors) {

    messageboxSelection[0] = -1;

    // sanity checks

    if ((buttonFlags.length != buttonIds.length) && (buttonIds.length != buttonTexts.length)) {
      return -1; // implementation broken
    }

    // collect arguments for Dialog

    final Bundle args = new Bundle();
    args.putInt("flags", flags);
    args.putString("title", title);
    args.putString("message", message);
    args.putIntArray("buttonFlags", buttonFlags);
    args.putIntArray("buttonIds", buttonIds);
    args.putStringArray("buttonTexts", buttonTexts);
    args.putIntArray("colors", colors);

    // trigger Dialog creation on UI thread

    runOnUiThread(
        new Runnable() {
          @Override
          public void run() {
            showDialog(dialogs++, args);
          }
        });

    // block the calling thread

    synchronized (messageboxSelection) {
      try {
        messageboxSelection.wait();
      } catch (InterruptedException ex) {
        ex.printStackTrace();
        return -1;
      }
    }

    // return selected value

    return messageboxSelection[0];
  }