Example usage for android.os Looper myLooper

List of usage examples for android.os Looper myLooper

Introduction

In this page you can find the example usage for android.os Looper myLooper.

Prototype

public static @Nullable Looper myLooper() 

Source Link

Document

Return the Looper object associated with the current thread.

Usage

From source file:rx.receiver.local.android.RxReceiverLocal.java

/**
 *
 * @param context//w  ww  . j a va2s . c  o  m
 * @param intentFilter
 * @return
 */
@NonNull
@CheckResult
public static Observable<Intent> receives(@NonNull final Context context,
        @NonNull final IntentFilter intentFilter) {
    return Observable.create(new Action1<Emitter<Intent>>() {
        @Override
        public void call(final Emitter<Intent> emitter) {
            final BroadcastReceiver receiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    emitter.onNext(intent);
                }
            };

            emitter.setCancellation(new Cancellable() {
                @Override
                public void cancel() throws Exception {
                    if (Looper.getMainLooper() == Looper.myLooper()) {
                        LocalBroadcastManager.getInstance(context).unregisterReceiver(receiver);
                    } else {
                        final Scheduler.Worker inner = mainThread().createWorker();
                        inner.schedule(new Action0() {
                            @Override
                            public void call() {
                                LocalBroadcastManager.getInstance(context).unregisterReceiver(receiver);
                                inner.unsubscribe();
                            }
                        });
                    }
                }
            });

            LocalBroadcastManager.getInstance(context).registerReceiver(receiver, intentFilter);
        }
    }, Emitter.BackpressureMode.BUFFER);
}

From source file:rx2.receiver.local.android.RxReceiverLocal.java

/**
 *
 * @param context//from   w ww  .j  ava2 s.  c o  m
 * @param intentFilter
 * @return
 */
@NonNull
public static Observable<Intent> receives(@NonNull final Context context,
        @NonNull final IntentFilter intentFilter) {
    return Observable.create(new ObservableOnSubscribe<Intent>() {
        @Override
        public void subscribe(final ObservableEmitter<Intent> emitter) {
            final BroadcastReceiver receiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    emitter.onNext(intent);
                }
            };

            LocalBroadcastManager.getInstance(context).registerReceiver(receiver, intentFilter);

            emitter.setCancellable(new Cancellable() {
                @Override
                public void cancel() throws Exception {
                    if (Looper.getMainLooper() == Looper.myLooper()) {
                        LocalBroadcastManager.getInstance(context).unregisterReceiver(receiver);
                    } else {
                        final Scheduler.Worker inner = mainThread().createWorker();
                        inner.schedule(new Runnable() {
                            @Override
                            public void run() {
                                LocalBroadcastManager.getInstance(context).unregisterReceiver(receiver);
                                inner.dispose();
                            }
                        });
                    }
                }
            });
        }
    });
}

From source file:com.simas.vc.background_tasks.FFprobe.java

/**
 * This operation is synchronous and cannot be run on the UI thread.
 */// www .  j  a v a2s  . com
@SuppressWarnings("ResultOfMethodCallIgnored")
public synchronized static FileAttributes parseAttributes(File inputFile) throws VCException {
    if (Looper.myLooper() == Looper.getMainLooper()) {
        throw new IllegalStateException("parseAttributes cannot be run on the UI thread!");
    }

    /* Executable call used
       ./ffprobe -i 'nature/bee.mp4' \
       -v quiet -print_format json \
       -show_format -show_entries format=duration,size,format_name,format_long_name,filename,nb_streams \
       -show_streams -show_entries stream=codec_name,codec_long_name,codec_type,sample_rate,channels,duration,display_aspect_ratio,width,height,time_base,codec_time_base,r_frame_rate
    */

    // Check if input exists
    if (!inputFile.exists()) {
        throw new VCException(Utils.getString(R.string.input_not_found));
    }

    // Create a temporary file to hold the stdout output
    File tmpFile;
    try {
        tmpFile = File.createTempFile("vc-out", null);
        tmpFile.delete();
        tmpFile.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
        throw new VCException(Utils.getString(R.string.tmp_not_created));
    }

    // Create arguments for ffprobe
    final String[] args = new ArgumentBuilder(TAG).add("-i").addSpaced("%s", inputFile.getPath()) // Spaced input file path
            .add("-v quiet -print_format json") // Output quietly in JSON
            // Format entries to show
            .add("-show_format -show_entries format=%s,%s,%s,%s,%s,%s",
                    Utils.getString(R.string.format_duration), Utils.getString(R.string.format_size),
                    Utils.getString(R.string.format_name), Utils.getString(R.string.format_long_name),
                    Utils.getString(R.string.format_filename), Utils.getString(R.string.format_stream_count))
            // Stream entries to show
            .add("-show_streams -show_entries stream=%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s",
                    Utils.getString(R.string.stream_name), Utils.getString(R.string.stream_long_name),
                    Utils.getString(R.string.stream_type), Utils.getString(R.string.stream_sample_rate),
                    Utils.getString(R.string.stream_channels), Utils.getString(R.string.stream_duration),
                    Utils.getString(R.string.stream_aspect_ratio), Utils.getString(R.string.stream_width),
                    Utils.getString(R.string.stream_height), Utils.getString(R.string.stream_tbn),
                    Utils.getString(R.string.stream_tbc), Utils.getString(R.string.stream_tbr),
                    Utils.getString(R.string.stream_codec_tag))
            .build();

    if (cFFprobe(args, tmpFile.getPath()) != 0) {
        throw new VCException(Utils.getString(R.string.ffprobe_fail));
    }

    BufferedReader reader = null;
    FileAttributes fa = null;
    try {
        // Parse file
        reader = new BufferedReader(new FileReader(tmpFile));
        StringBuilder sb = new StringBuilder();
        String line = reader.readLine();

        while (line != null) {
            sb.append(line);
            sb.append('\n');
            line = reader.readLine();
        }
        String content = sb.toString();

        int firstOpeningBrace = content.indexOf('{');
        int lastClosingBrace = content.lastIndexOf('}');
        if (firstOpeningBrace == -1 || lastClosingBrace == -1) {
            return null;
        }
        String json = content.substring(firstOpeningBrace, lastClosingBrace + 1);

        // Parse JSON
        fa = parseJsonAttributes(json);
        Log.i(TAG, "Parsed attributes: " + fa);
    } catch (IOException e) {
        e.printStackTrace();
        throw new VCException(Utils.getString(R.string.ffprobe_fail));
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // Delete the tmp file
    tmpFile.delete();

    // No support for files with no audio/video streams (for now?)
    if (fa != null) {
        if (fa.getAudioStreams().size() == 0) {
            throw new VCException(Utils.getString(R.string.audioless_unsupported));
        } else if (fa.getVideoStreams().size() == 0) {
            throw new VCException(Utils.getString(R.string.videoless_unsupported));
        }
    }

    return fa;
}

From source file:org.apache.cordova.engine.crosswalk.XWalkExposedJsApi.java

@JavascriptInterface
public String exec(int bridgeSecret, String service, String action, String callbackId, String arguments)
        throws JSONException, IllegalAccessException {
    if (Looper.myLooper() == null) {
        Looper.prepare();//from  ww  w. j  a  va 2s  .  c  o  m
    }
    return bridge.jsExec(bridgeSecret, service, action, callbackId, arguments);
}

From source file:com.frand.easyandroid.http.FFHttpRespHandler.java

/**
 * FFBaseRespHandler?????//from  w  ww. ja  va2 s .  c om
 */
@SuppressLint("HandlerLeak")
public FFHttpRespHandler() {
    if (Looper.myLooper() != null) {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                FFHttpRespHandler.this.handleMessage(msg);
            }
        };
    }
}

From source file:sxkeji.net.dailydiary.http.HttpResponseHandler.java

public HttpResponseHandler() {
    if (Looper.myLooper() != null) {
        handler = new Handler() {
            @Override/*from   w w w  . j a  v  a2s .co  m*/
            public void handleMessage(Message msg) {
                HttpResponseHandler.this.handleMessage(msg);
            }
        };
    }
}

From source file:com.polyvi.xface.http.XAsyncHttpResponseHandler.java

public XAsyncHttpResponseHandler() {
    if (Looper.myLooper() != null) {
        mHandler = new Handler() {
            @Override//from   w  w  w.ja  v a2 s.  c o  m
            public void handleMessage(Message msg) {
                XAsyncHttpResponseHandler.this.handleMessage(msg);
            }
        };
    }
}

From source file:org.solovyev.android.Threads.java

/**
 * @return true if current thread is UI thread (= main application thread)
 *///from  w ww  .  ja  v a 2s  . co m
public static boolean isUiThread() {
    return Looper.myLooper() == Looper.getMainLooper();
}

From source file:org.jitsi.service.osgi.OSGiFragment.java

/**
 * Convenience method for running code on UI thread looper(instead of
 * getActivity().runOnUIThread()). It is never guaranteed that
 * <tt>getActivity()</tt> will return not <tt>null</tt> value, hence it must
 * be checked in the <tt>action</tt>.
 *
 * @param action <tt>Runnable</tt> action to execute on UI thread.
 *///ww  w .j a v  a  2  s . c  o  m
public void runOnUiThread(Runnable action) {
    if (Looper.myLooper() == Looper.getMainLooper()) {
        action.run();
        return;
    }
    // Post action to the ui looper
    OSGiActivity.uiHandler.post(action);
}

From source file:com.fihmi.tools.minet.MiHttpResponseHandler.java

/**
 * Creates a new AsyncHttpResponseHandler
 *//*  w w  w  . j  a v  a 2 s  .co m*/
@SuppressLint("HandlerLeak")
public MiHttpResponseHandler() {
    // Set up a handler to post events back to the correct thread if possible
    if (Looper.myLooper() != null) {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                MiHttpResponseHandler.this.handleMessage(msg);
            }
        };
    }
}