Beispiel #1
0
 @Override
 public void run() {
   assertSame(this, Thread.currentThread());
   try {
     while (true) {
       Request request = requestQueue.take();
       Object result;
       try {
         result = invokeMethod(request.methodName, request.arguments);
       } catch (ThreadDeath death) {
         return;
       } catch (InvocationTargetException exception) {
         responseQueue.put(new Response(request.methodName, null, exception.getTargetException()));
         continue;
       } catch (Throwable throwable) {
         responseQueue.put(new Response(request.methodName, null, throwable));
         continue;
       }
       responseQueue.put(new Response(request.methodName, result, null));
     }
   } catch (ThreadDeath death) {
     return;
   } catch (InterruptedException ignored) {
     // SynchronousQueue sometimes throws InterruptedException while the threads are stopping.
   } catch (Throwable uncaught) {
     this.uncaughtThrowable = uncaught;
   }
 }
  public void onActivityResult(int resultCode, Intent data) {
    try {
      if (resultCode != Activity.RESULT_OK) {
        mFilePickerResult.put("");
        return;
      }

      File file = new File(Environment.getExternalStorageDirectory(), sImageName);
      sImageName = "";
      mFilePickerResult.put(file.getAbsolutePath());
    } catch (InterruptedException e) {
      Log.i(LOGTAG, "error returning file picker result", e);
    }
  }
Beispiel #3
0
 private void putNextValue(String value) {
   try {
     res.put(value);
   } catch (InterruptedException interEx) {
     Thread.currentThread().interrupt();
   }
 }
 private void runOnIcThread(Handler icHandler, final Runnable runnable) {
   if (DEBUG) {
     ThreadUtils.assertOnUiThread();
     Log.d(LOGTAG, "runOnIcThread() on thread " + icHandler.getLooper().getThread().getName());
   }
   Runnable runner =
       new Runnable() {
         @Override
         public void run() {
           try {
             Runnable queuedRunnable = mIcRunnableSync.take();
             if (DEBUG && queuedRunnable != runnable) {
               throw new IllegalThreadStateException("sync error");
             }
             queuedRunnable.run();
           } catch (InterruptedException e) {
           }
         }
       };
   try {
     // if we are not inside waitForUiThread(), runner will call the runnable
     icHandler.post(runner);
     // runnable will be called by either runner from above or waitForUiThread()
     mIcRunnableSync.put(runnable);
   } catch (InterruptedException e) {
   } finally {
     // if waitForUiThread() already called runnable, runner should not call it again
     icHandler.removeCallbacks(runner);
   }
 }
Beispiel #5
0
  @Override
  public void messageArrived(String topic, MqttMessage message) throws Exception {
    final String METHOD = "messageArrived";
    if (topic.equals(ServerTopic.RESPONSE.getName())) {
      LoggerUtility.log(
          Level.FINE,
          CLASS_NAME,
          METHOD,
          "Received response from IoT Foundation, topic (" + topic + ")");

      String responsePayload = new String(message.getPayload(), "UTF-8");
      JsonObject jsonResponse = new JsonParser().parse(responsePayload).getAsJsonObject();
      try {
        String reqId = jsonResponse.get("reqId").getAsString();
        LoggerUtility.fine(CLASS_NAME, METHOD, "reqId (" + reqId + "): " + jsonResponse.toString());
        MqttMessage sentMsg = requests.remove(reqId);
        if (sentMsg != null) {
          queue.put(jsonResponse);
        }
      } catch (Exception e) {
        if (jsonResponse.get("reqId") == null) {
          LoggerUtility.warn(
              CLASS_NAME,
              METHOD,
              "The response " + "does not contain 'reqId' field (" + responsePayload + ")");
        } else {
          LoggerUtility.log(Level.SEVERE, CLASS_NAME, METHOD, "Unexpected exception", e);
        }
      }
    } else {
      LoggerUtility.warn(CLASS_NAME, METHOD, "Unknown topic (" + topic + ")");
    }
  }
 public void finishDialog(String aReturn) {
   mInputs = null;
   mDialog = null;
   mSelected = null;
   try {
     mPromptQueue.put(aReturn);
   } catch (Exception ex) {
   }
 }
 // Place an object in the sync queue, indicating that the calling thread is finished,
 // and any waiting thread may continue. Placing an Exception into the queue indicates
 // an error condition has occurred that should be handled by the waiting thread.
 private void finished(Object obj) {
   while (true) {
     try {
       queue.put(obj);
       break;
     } catch (InterruptedException e1) {
       // no-op
     }
   }
 }
 public void endWaitForUiThread() {
   if (DEBUG) {
     ThreadUtils.assertOnUiThread();
     Log.d(LOGTAG, "endWaitForUiThread()");
   }
   try {
     mIcRunnableSync.put(mIcSignalRunnable);
   } catch (InterruptedException e) {
   }
 }
Beispiel #9
0
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    String filePickerResult = "";
    if (data != null && resultCode == RESULT_OK) {
      try {
        ContentResolver cr = getContentResolver();
        Uri uri = data.getData();
        Cursor cursor =
            GeckoApp.mAppContext
                .getContentResolver()
                .query(uri, new String[] {OpenableColumns.DISPLAY_NAME}, null, null, null);
        String name = null;
        if (cursor != null) {
          try {
            if (cursor.moveToNext()) {
              name = cursor.getString(0);
            }
          } finally {
            cursor.close();
          }
        }
        String fileName = "tmp_";
        String fileExt = null;
        int period;
        if (name == null || (period = name.lastIndexOf('.')) == -1) {
          String mimeType = cr.getType(uri);
          fileExt = "." + GeckoAppShell.getExtensionFromMimeType(mimeType);
        } else {
          fileExt = name.substring(period);
          fileName = name.substring(0, period);
        }
        File file = File.createTempFile(fileName, fileExt, sGREDir);

        FileOutputStream fos = new FileOutputStream(file);
        InputStream is = cr.openInputStream(uri);
        byte[] buf = new byte[4096];
        int len = is.read(buf);
        while (len != -1) {
          fos.write(buf, 0, len);
          len = is.read(buf);
        }
        fos.close();
        filePickerResult = file.getAbsolutePath();
      } catch (Exception e) {
        Log.e(LOG_FILE_NAME, "showing file picker", e);
      }
    }
    try {
      mFilePickerResult.put(filePickerResult);
    } catch (InterruptedException e) {
      Log.i(LOG_FILE_NAME, "error returning file picker result", e);
    }
  }
Beispiel #10
0
  public Bitmap loadDrawable(final String imageUrl, final int page, final ImageCallback callback) {
    Bitmap bitmap = getBitmap(imageUrl);
    if (null != bitmap) {
      return bitmap;
    }

    try {
      taskQueue.put(new WorkItem(imageUrl, page, callback));
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    return null;
  }
 public void deliver(Plate p) throws InterruptedException {
   // Only blocks if customer is still
   // eating the previous course:
   placeSetting.put(p);
 }