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.chrome.browser.vr_shell; | 5 package org.chromium.chrome.browser.vr_shell; |
6 | 6 |
7 import android.app.Activity; | 7 import android.app.Activity; |
8 import android.content.Intent; | 8 import android.content.Intent; |
9 import android.content.pm.ActivityInfo; | 9 import android.content.pm.ActivityInfo; |
10 import android.os.StrictMode; | 10 import android.os.StrictMode; |
11 import android.view.View; | 11 import android.view.View; |
12 import android.view.ViewGroup; | 12 import android.view.ViewGroup; |
13 import android.view.ViewGroup.LayoutParams; | 13 import android.view.ViewGroup.LayoutParams; |
14 import android.view.WindowManager; | 14 import android.view.WindowManager; |
15 import android.widget.FrameLayout; | 15 import android.widget.FrameLayout; |
16 | 16 |
17 import org.chromium.base.Log; | 17 import org.chromium.base.Log; |
| 18 import org.chromium.base.annotations.CalledByNative; |
| 19 import org.chromium.base.annotations.JNINamespace; |
18 import org.chromium.chrome.browser.ChromeTabbedActivity; | 20 import org.chromium.chrome.browser.ChromeTabbedActivity; |
19 import org.chromium.chrome.browser.tab.Tab; | 21 import org.chromium.chrome.browser.tab.Tab; |
20 | 22 |
21 import java.lang.reflect.Constructor; | 23 import java.lang.reflect.Constructor; |
22 import java.lang.reflect.InvocationTargetException; | 24 import java.lang.reflect.InvocationTargetException; |
23 | 25 |
24 /** | 26 /** |
25 * Manages interactions with the VR Shell. | 27 * Manages interactions with the VR Shell. |
26 */ | 28 */ |
| 29 @JNINamespace("vr_shell") |
27 public class VrShellDelegate { | 30 public class VrShellDelegate { |
28 private static final String TAG = "VrShellDelegate"; | 31 private static final String TAG = "VrShellDelegate"; |
29 | 32 |
30 private ChromeTabbedActivity mActivity; | 33 private ChromeTabbedActivity mActivity; |
31 | 34 |
32 private boolean mVrShellEnabled; | 35 private boolean mVrShellEnabled; |
33 | 36 |
34 private Class<? extends VrShellInterface> mVrShellClass; | 37 private Class<? extends VrShellInterface> mVrShellClass; |
35 private VrShellInterface mVrShell; | 38 private VrShellInterface mVrShell; |
36 private boolean mInVr; | 39 private boolean mInVr; |
37 private int mRestoreSystemUiVisibilityFlag = -1; | 40 private int mRestoreSystemUiVisibilityFlag = -1; |
38 private String mVrExtra; | 41 private String mVrExtra; |
| 42 private long mNativeVrShellDelegate; |
39 | 43 |
40 public VrShellDelegate(ChromeTabbedActivity activity) { | 44 public VrShellDelegate(ChromeTabbedActivity activity) { |
41 mActivity = activity; | 45 mActivity = activity; |
42 | 46 |
43 mVrShellClass = maybeFindVrShell(); | 47 mVrShellClass = maybeFindVrShell(); |
44 if (mVrShellClass != null) { | 48 if (mVrShellClass != null) { |
45 mVrShellEnabled = true; | 49 mVrShellEnabled = true; |
46 try { | 50 try { |
47 mVrExtra = (String) mVrShellClass.getField("VR_EXTRA").get(null)
; | 51 mVrExtra = (String) mVrShellClass.getField("VR_EXTRA").get(null)
; |
48 } catch (IllegalAccessException | IllegalArgumentException | NoSuchF
ieldException e) { | 52 } catch (IllegalAccessException | IllegalArgumentException | NoSuchF
ieldException e) { |
49 Log.e(TAG, "Unable to read VR_EXTRA field", e); | 53 Log.e(TAG, "Unable to read VR_EXTRA field", e); |
50 mVrShellEnabled = false; | 54 mVrShellEnabled = false; |
51 } | 55 } |
52 } | 56 } |
53 } | 57 } |
54 | 58 |
| 59 /** |
| 60 * Should be called once the native library is loaded so that the native por
tion of this |
| 61 * class can be initialized. |
| 62 */ |
| 63 public void onNativeLibraryReady() { |
| 64 if (mVrShellEnabled) { |
| 65 mNativeVrShellDelegate = nativeInit(); |
| 66 } |
| 67 } |
| 68 |
55 @SuppressWarnings("unchecked") | 69 @SuppressWarnings("unchecked") |
56 private Class<? extends VrShellInterface> maybeFindVrShell() { | 70 private Class<? extends VrShellInterface> maybeFindVrShell() { |
57 try { | 71 try { |
58 return (Class<? extends VrShellInterface>) Class | 72 return (Class<? extends VrShellInterface>) Class |
59 .forName("org.chromium.chrome.browser.vr_shell.VrShell"); | 73 .forName("org.chromium.chrome.browser.vr_shell.VrShell"); |
60 } catch (ClassNotFoundException e) { | 74 } catch (ClassNotFoundException e) { |
61 return null; | 75 return null; |
62 } | 76 } |
63 } | 77 } |
64 | 78 |
65 /** | 79 /** |
66 * Enters VR Shell, displaying browser UI and tab contents in VR. | 80 * Enters VR Shell, displaying browser UI and tab contents in VR. |
67 * | 81 * |
68 * This function performs native initialization, and so must only be called
after native | 82 * This function performs native initialization, and so must only be called
after native |
69 * libraries are ready. | 83 * libraries are ready. |
70 * @Returns Whether or not we are in VR when this function returns. | 84 * @param inWebVR If true should begin displaying WebVR content rather than
the VrShell UI. |
| 85 * @return Whether or not we are in VR when this function returns. |
71 */ | 86 */ |
72 public boolean enterVRIfNecessary() { | 87 @CalledByNative |
73 if (!mVrShellEnabled) return false; | 88 public boolean enterVRIfNecessary(boolean inWebVR) { |
| 89 if (!mVrShellEnabled || mNativeVrShellDelegate == 0) return false; |
74 Tab tab = mActivity.getActivityTab(); | 90 Tab tab = mActivity.getActivityTab(); |
75 // TODO(mthiesse): When we have VR UI for opening new tabs, etc., allow
VR Shell to be | 91 // TODO(mthiesse): When we have VR UI for opening new tabs, etc., allow
VR Shell to be |
76 // entered without any current tabs. | 92 // entered without any current tabs. |
77 if (tab == null || tab.getContentViewCore() == null) { | 93 if (tab == null || tab.getContentViewCore() == null) { |
78 return false; | 94 return false; |
79 } | 95 } |
80 if (mInVr) return true; | 96 if (mInVr) return true; |
81 // VrShell must be initialized in Landscape mode due to a bug in the GVR
library. | 97 // VrShell must be initialized in Landscape mode due to a bug in the GVR
library. |
82 mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSC
APE); | 98 mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSC
APE); |
83 if (!createVrShell()) { | 99 if (!createVrShell()) { |
84 mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UN
SPECIFIED); | 100 mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UN
SPECIFIED); |
85 return false; | 101 return false; |
86 } | 102 } |
87 addVrViews(); | 103 addVrViews(); |
88 setupVrModeWindowFlags(); | 104 setupVrModeWindowFlags(); |
89 mVrShell.onNativeLibraryReady(tab); | 105 mVrShell.initializeNative(tab, this); |
| 106 if (inWebVR) mVrShell.setWebVrModeEnabled(true); |
90 mVrShell.setVrModeEnabled(true); | 107 mVrShell.setVrModeEnabled(true); |
91 mInVr = true; | 108 mInVr = true; |
92 return true; | 109 return true; |
93 } | 110 } |
94 | 111 |
| 112 @CalledByNative |
| 113 private boolean exitWebVR() { |
| 114 if (!mInVr) return false; |
| 115 mVrShell.setWebVrModeEnabled(false); |
| 116 // TODO(bajones): Once VR Shell can be invoked outside of WebVR this |
| 117 // should no longer exit the shell outright. Need a way to determine |
| 118 // how VrShell was created. |
| 119 shutdownVR(); |
| 120 return true; |
| 121 } |
| 122 |
95 /** | 123 /** |
96 * Resumes VR Shell. | 124 * Resumes VR Shell. |
97 */ | 125 */ |
98 public void resumeVR() { | 126 public void resumeVR() { |
99 setupVrModeWindowFlags(); | 127 setupVrModeWindowFlags(); |
100 mVrShell.resume(); | 128 mVrShell.resume(); |
101 } | 129 } |
102 | 130 |
103 /** | 131 /** |
104 * Pauses VR Shell. | 132 * Pauses VR Shell. |
105 */ | 133 */ |
106 public void pauseVR() { | 134 public void pauseVR() { |
107 mVrShell.pause(); | 135 mVrShell.pause(); |
108 } | 136 } |
109 | 137 |
110 /** | 138 /** |
| 139 * Exits the current VR mode (WebVR or VRShell) |
| 140 * @return Whether or not we exited VR. |
| 141 */ |
| 142 public boolean exitVRIfNecessary() { |
| 143 if (!mInVr) return false; |
| 144 // If WebVR is presenting instruct it to exit. VR mode should not |
| 145 // exit in this scenario, in case we want to return to the VrShell. |
| 146 if (!nativeExitWebVRIfNecessary(mNativeVrShellDelegate)) { |
| 147 // If WebVR was not presenting, shutdown VR mode entirely. |
| 148 shutdownVR(); |
| 149 } |
| 150 |
| 151 return true; |
| 152 } |
| 153 |
| 154 /** |
111 * Exits VR Shell, performing all necessary cleanup. | 155 * Exits VR Shell, performing all necessary cleanup. |
112 * @Returns Whether or not we exited VR. | |
113 */ | 156 */ |
114 public boolean exitVRIfNecessary() { | 157 private void shutdownVR() { |
115 if (!mInVr) return false; | 158 if (!mInVr) return; |
116 mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPEC
IFIED); | 159 mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPEC
IFIED); |
117 mVrShell.setVrModeEnabled(false); | 160 mVrShell.setVrModeEnabled(false); |
118 mVrShell.pause(); | 161 mVrShell.pause(); |
119 removeVrViews(); | 162 removeVrViews(); |
120 clearVrModeWindowFlags(); | 163 clearVrModeWindowFlags(); |
121 destroyVrShell(); | 164 destroyVrShell(); |
122 mInVr = false; | 165 mInVr = false; |
123 return true; | |
124 } | 166 } |
125 | 167 |
126 private boolean createVrShell() { | 168 private boolean createVrShell() { |
127 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); | 169 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
128 try { | 170 try { |
129 Constructor<?> vrShellConstructor = mVrShellClass.getConstructor(Act
ivity.class); | 171 Constructor<?> vrShellConstructor = mVrShellClass.getConstructor(Act
ivity.class); |
130 mVrShell = (VrShellInterface) vrShellConstructor.newInstance(mActivi
ty); | 172 mVrShell = (VrShellInterface) vrShellConstructor.newInstance(mActivi
ty); |
131 } catch (InstantiationException | IllegalAccessException | IllegalArgume
ntException | 173 } catch (InstantiationException | IllegalAccessException | IllegalArgume
ntException |
132 | InvocationTargetException | NoSuchMethodException e) { | 174 | InvocationTargetException | NoSuchMethodException e) { |
133 Log.e(TAG, "Unable to instantiate VrShell", e); | 175 Log.e(TAG, "Unable to instantiate VrShell", e); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 public boolean isInVR() { | 240 public boolean isInVR() { |
199 return mInVr; | 241 return mInVr; |
200 } | 242 } |
201 | 243 |
202 /** | 244 /** |
203 * @return Whether or not VR Shell is currently enabled. | 245 * @return Whether or not VR Shell is currently enabled. |
204 */ | 246 */ |
205 public boolean isVrShellEnabled() { | 247 public boolean isVrShellEnabled() { |
206 return mVrShellEnabled; | 248 return mVrShellEnabled; |
207 } | 249 } |
| 250 |
| 251 /** |
| 252 * @return Pointer to the native VrShellDelegate object. |
| 253 */ |
| 254 @CalledByNative |
| 255 private long getNativePointer() { |
| 256 return mNativeVrShellDelegate; |
| 257 } |
| 258 |
| 259 private native long nativeInit(); |
| 260 private native boolean nativeExitWebVRIfNecessary(long nativeVrShellDelegate
); |
208 } | 261 } |
OLD | NEW |