Search in sources :

Example 1 with ShutdownReceiver

use of com.fsck.k9.service.ShutdownReceiver in project k-9 by k9mail.

the class K9 method registerReceivers.

/**
     * Register BroadcastReceivers programmatically because doing it from manifest
     * would make K-9 auto-start. We don't want auto-start because the initialization
     * sequence isn't safe while some events occur (SD card unmount).
     */
protected void registerReceivers() {
    final StorageGoneReceiver receiver = new StorageGoneReceiver();
    final IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_MEDIA_EJECT);
    filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
    filter.addDataScheme("file");
    final BlockingQueue<Handler> queue = new SynchronousQueue<Handler>();
    // starting a new thread to handle unmount events
    new Thread(new Runnable() {

        @Override
        public void run() {
            Looper.prepare();
            try {
                queue.put(new Handler());
            } catch (InterruptedException e) {
                Timber.e(e);
            }
            Looper.loop();
        }
    }, "Unmount-thread").start();
    try {
        final Handler storageGoneHandler = queue.take();
        registerReceiver(receiver, filter, null, storageGoneHandler);
        Timber.i("Registered: unmount receiver");
    } catch (InterruptedException e) {
        Timber.e(e, "Unable to register unmount receiver");
    }
    registerReceiver(new ShutdownReceiver(), new IntentFilter(Intent.ACTION_SHUTDOWN));
    Timber.i("Registered: shutdown receiver");
}
Also used : StorageGoneReceiver(com.fsck.k9.service.StorageGoneReceiver) IntentFilter(android.content.IntentFilter) SynchronousQueue(java.util.concurrent.SynchronousQueue) Handler(android.os.Handler) ShutdownReceiver(com.fsck.k9.service.ShutdownReceiver)

Aggregations

IntentFilter (android.content.IntentFilter)1 Handler (android.os.Handler)1 ShutdownReceiver (com.fsck.k9.service.ShutdownReceiver)1 StorageGoneReceiver (com.fsck.k9.service.StorageGoneReceiver)1 SynchronousQueue (java.util.concurrent.SynchronousQueue)1