OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.device.vr; | 5 package org.chromium.device.vr; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.os.StrictMode; |
8 | 9 |
9 import com.google.vr.ndk.base.GvrLayout; | 10 import com.google.vr.ndk.base.GvrLayout; |
10 | 11 |
| 12 import org.chromium.base.Log; |
11 import org.chromium.base.annotations.CalledByNative; | 13 import org.chromium.base.annotations.CalledByNative; |
12 import org.chromium.base.annotations.JNINamespace; | 14 import org.chromium.base.annotations.JNINamespace; |
13 | 15 |
14 /** | 16 /** |
15 * This is the implementation of the C++ counterpart GvrDeviceProvider. | 17 * This is the implementation of the C++ counterpart GvrDeviceProvider. |
16 */ | 18 */ |
17 @JNINamespace("device") | 19 @JNINamespace("device") |
18 class GvrDeviceProvider { | 20 class GvrDeviceProvider { |
19 private static final String TAG = "GvrDeviceProvider"; | 21 private static final String TAG = "GvrDeviceProvider"; |
20 private final GvrLayout mLayout; | 22 private final GvrLayout mLayout; |
21 private Thread mGvrInitThread; | |
22 | 23 |
23 private GvrDeviceProvider(Context context) { | 24 private GvrDeviceProvider(Context context) { |
24 mLayout = new GvrLayout(context); | 25 mLayout = new GvrLayout(context); |
25 | |
26 // Initialize the GVR API on a separate thread to avoid strict mode | |
27 // violations. Note that this doesn't fix the underlying issue of | |
28 // blocking on disk reads here. | |
29 mGvrInitThread = new Thread(new Runnable() { | |
30 @Override | |
31 public void run() { | |
32 mLayout.getGvrApi(); | |
33 } | |
34 }); | |
35 mGvrInitThread.start(); | |
36 } | 26 } |
37 | 27 |
38 @CalledByNative | 28 @CalledByNative |
39 private static GvrDeviceProvider create(Context context) { | 29 private static GvrDeviceProvider create(Context context) { |
40 return new GvrDeviceProvider(context); | 30 return new GvrDeviceProvider(context); |
41 } | 31 } |
42 | 32 |
43 @CalledByNative | 33 @CalledByNative |
44 private long getNativeContext() { | 34 private long getNativeContext() { |
| 35 long nativeGvrContext = 0; |
| 36 |
| 37 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
| 38 |
45 try { | 39 try { |
46 mGvrInitThread.join(); | 40 nativeGvrContext = mLayout.getGvrApi().getNativeGvrContext(); |
47 } catch (Exception ex) { | 41 } catch (Exception ex) { |
| 42 Log.e(TAG, "Unable to instantiate GvrApi", ex); |
| 43 return 0; |
| 44 } finally { |
| 45 StrictMode.setThreadPolicy(oldPolicy); |
48 } | 46 } |
49 return mLayout.getGvrApi().getNativeGvrContext(); | 47 |
| 48 return nativeGvrContext; |
50 } | 49 } |
51 | 50 |
52 @CalledByNative | 51 @CalledByNative |
53 private void shutdown() { | 52 private void shutdown() { |
54 mLayout.shutdown(); | 53 mLayout.shutdown(); |
55 } | 54 } |
56 } | 55 } |
OLD | NEW |