Java Code Examples for android.os.HandlerThread#quitSafely()

The following examples show how to use android.os.HandlerThread#quitSafely() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TesseractOCR.java    From polling-station-app with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Starts (enqueues) a stop routine in a new thread, then returns immediately.
 */
public void stopScanner() {
    HandlerThread ht = new HandlerThread("stopper");
    ht.start();
    cleanHandler = new Handler(ht.getLooper());
    cleanHandler.post(new Runnable() {
        @Override
        public void run() {
            cleanup();
        }
    });
    ht.quitSafely();
}
 
Example 2
Source File: FrameSender.java    From AndroidInstantVideo with Apache License 2.0 5 votes vote down vote up
public FrameSender(final FrameSenderCallback frameSenderCallback) {
    this.frameSenderCallback = frameSenderCallback;
    final HandlerThread sendHandlerThread = new HandlerThread("send_thread");
    sendHandlerThread.start();
    sendHandler = new Handler(sendHandlerThread.getLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if (msg.what == MESSAGE_READY_TO_CLOSE) {
                if (msg.obj != null) {
                    addFrame((FramePool.Frame) msg.obj);
                }
                sendFrame(msg.arg1);

                frameSenderCallback.close();
                sendHandlerThread.quitSafely();
            } else if (msg.what == MSG_ADD_FRAME) {
                if (msg.obj != null) {
                    addFrame((FramePool.Frame) msg.obj);
                }
                sendFrame(msg.arg1);
            } else if (msg.what == MSG_START) {
                frameSenderCallback.onStart();
            }
        }
    };
}
 
Example 3
Source File: BitmapPrinter.java    From ViewPrinter with Apache License 2.0 4 votes vote down vote up
@Override
public void print(final String printId, @NonNull final File directory, @NonNull String filename) {
    Context context = mDocument.getContext();
    if (!checkPermission(context, directory)) return;
    if (!checkPreview(printId, directory, filename)) return;

    if (filename.toLowerCase().endsWith(mFormat)) {
        filename = filename.substring(0, filename.length() - mFormat.length());
    }
    if (mDocument.getPageCount() == 0) return;


    final Handler ui = new Handler();
    final HandlerThread thread = new HandlerThread(getClass().getSimpleName() + "Worker");
    thread.start();
    final Handler worker = new Handler(thread.getLooper());

    dispatchOnPrePrint(mDocument);
    int count = mPrintAll ? mDocument.getPageCount() : mPrintable.length;
    for (int i = 0; i < count; i++) {
        final int page = mPrintAll ? i : mPrintable[i];
        String suffix = count == 1 ? mFormat : "-" + (page + 1) + mFormat;
        final File file = new File(directory, filename + suffix);
        if (!checkFile(printId, file)) {
            thread.quitSafely();
            return; // Error!
        }

        PrintSize size = mDocument.getPrintSize();
        final Bitmap bitmap;
        float realWidth = size.widthPixels(context);
        float realHeight = size.heightPixels(context);
        float scale = mScale;
        scale = Math.min(scale, (float) mScaleMaxWidth / realWidth);
        scale = Math.min(scale, (float) mScaleMaxHeight / realHeight);
        final int outWidth = (int) (realWidth * scale);
        final int outHeight = (int) (realHeight * scale);
        if (Build.VERSION.SDK_INT >= 26) {
            bitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888, true);
        } else {
            bitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
        }
        Canvas canvas = new Canvas(bitmap);
        canvas.scale(scale, scale);
        DocumentPage view = mDocument.getPageAt(page);
        Drawable background = null;
        if (!mPrintBackground) {
            background = view.getBackground();
            view.setBackground(null);
        }

        // Tried this to have shadows drawing but no success.
        // view.setWillNotCacheDrawing(false);
        // view.destroyDrawingCache();
        // view.buildDrawingCache();
        // canvas.drawBitmap(view.getDrawingCache(), 0, 0, null);

        view.draw(canvas);
        if (!mPrintBackground) {
            view.setBackground(background);
        }

        worker.post(new Runnable() {
            @Override
            public void run() {
                try (OutputStream stream = new BufferedOutputStream(new FileOutputStream(file))) {
                    bitmap.compress(mCompressFormat, getPrintQuality(), stream);
                    bitmap.recycle();
                    ui.post(new Runnable() {
                        @Override
                        public void run() {
                            mCallback.onPrint(printId, file);
                        }
                    });

                } catch (final IOException e) {
                    LOG.e("print:", "got error on page:", page, "error:", e);
                    ui.post(new Runnable() {
                        @Override
                        public void run() {
                            Throwable error = new RuntimeException("Invalid file: " + file, e);
                            mCallback.onPrintFailed(printId, error);
                        }
                    });
                }
            }
        });
    }
    dispatchOnPostPrint(mDocument);

    worker.post(new Runnable() {
        @Override
        public void run() {
            LOG.i("print:", "done, closing worker thread.");
            thread.quitSafely();
        }
    });
}
 
Example 4
Source File: HandlerThreadCompat.java    From attendee-checkin with Apache License 2.0 4 votes vote down vote up
@Override
public boolean quitSafely(HandlerThread thread) {
    return thread.quitSafely();
}