• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.base;
6 
7 import android.annotation.TargetApi;
8 import android.os.Build;
9 import android.os.Handler;
10 import android.os.HandlerThread;
11 
12 import org.chromium.base.annotations.CalledByNative;
13 import org.chromium.base.annotations.JNINamespace;
14 
15 /**
16  * This class is an internal detail of the native counterpart.
17  * It is instantiated and owned by the native object.
18  */
19 @JNINamespace("base::android")
20 class JavaHandlerThread {
21     final HandlerThread mThread;
22 
JavaHandlerThread(String name)23     private JavaHandlerThread(String name) {
24         mThread = new HandlerThread(name);
25     }
26 
27     @CalledByNative
create(String name)28     private static JavaHandlerThread create(String name) {
29         return new JavaHandlerThread(name);
30     }
31 
32     @CalledByNative
start(final long nativeThread, final long nativeEvent)33     private void start(final long nativeThread, final long nativeEvent) {
34         mThread.start();
35         new Handler(mThread.getLooper()).post(new Runnable() {
36             @Override
37             public void run() {
38                 nativeInitializeThread(nativeThread, nativeEvent);
39             }
40         });
41     }
42 
43     @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
44     @CalledByNative
stop(final long nativeThread, final long nativeEvent)45     private void stop(final long nativeThread, final long nativeEvent) {
46         final boolean quitSafely = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2;
47         new Handler(mThread.getLooper()).post(new Runnable() {
48             @Override
49             public void run() {
50                 nativeStopThread(nativeThread, nativeEvent);
51                 if (!quitSafely) mThread.quit();
52             }
53         });
54         if (quitSafely) mThread.quitSafely();
55     }
56 
nativeInitializeThread(long nativeJavaHandlerThread, long nativeEvent)57     private native void nativeInitializeThread(long nativeJavaHandlerThread, long nativeEvent);
nativeStopThread(long nativeJavaHandlerThread, long nativeEvent)58     private native void nativeStopThread(long nativeJavaHandlerThread, long nativeEvent);
59 }
60