Chromium Code Reviews| 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 mNativeVrShellDelegate = nativeInit(); | |
|
Ted C
2016/09/21 17:59:24
do you need a check for mVrShellEnabled here? oth
| |
| 65 } | |
| 66 | |
| 55 @SuppressWarnings("unchecked") | 67 @SuppressWarnings("unchecked") |
| 56 private Class<? extends VrShellInterface> maybeFindVrShell() { | 68 private Class<? extends VrShellInterface> maybeFindVrShell() { |
| 57 try { | 69 try { |
| 58 return (Class<? extends VrShellInterface>) Class | 70 return (Class<? extends VrShellInterface>) Class |
| 59 .forName("org.chromium.chrome.browser.vr_shell.VrShell"); | 71 .forName("org.chromium.chrome.browser.vr_shell.VrShell"); |
| 60 } catch (ClassNotFoundException e) { | 72 } catch (ClassNotFoundException e) { |
| 61 return null; | 73 return null; |
| 62 } | 74 } |
| 63 } | 75 } |
| 64 | 76 |
| 65 /** | 77 /** |
| 66 * Enters VR Shell, displaying browser UI and tab contents in VR. | 78 * Enters VR Shell, displaying browser UI and tab contents in VR. |
| 67 * | 79 * |
| 68 * This function performs native initialization, and so must only be called after native | 80 * This function performs native initialization, and so must only be called after native |
| 69 * libraries are ready. | 81 * libraries are ready. |
| 70 * @Returns Whether or not we are in VR when this function returns. | 82 * @Returns Whether or not we are in VR when this function returns. |
|
Ted C
2016/09/21 17:59:24
We should add an @param for inWebVR here
Also if
| |
| 71 */ | 83 */ |
| 72 public boolean enterVRIfNecessary() { | 84 @CalledByNative |
| 73 if (!mVrShellEnabled) return false; | 85 public boolean enterVRIfNecessary(boolean inWebVR) { |
| 86 if (!mVrShellEnabled || mNativeVrShellDelegate == 0) return false; | |
| 74 Tab tab = mActivity.getActivityTab(); | 87 Tab tab = mActivity.getActivityTab(); |
| 75 // TODO(mthiesse): When we have VR UI for opening new tabs, etc., allow VR Shell to be | 88 // TODO(mthiesse): When we have VR UI for opening new tabs, etc., allow VR Shell to be |
| 76 // entered without any current tabs. | 89 // entered without any current tabs. |
| 77 if (tab == null || tab.getContentViewCore() == null) { | 90 if (tab == null || tab.getContentViewCore() == null) { |
| 78 return false; | 91 return false; |
| 79 } | 92 } |
| 80 if (mInVr) return true; | 93 if (mInVr) return true; |
| 81 // VrShell must be initialized in Landscape mode due to a bug in the GVR library. | 94 // VrShell must be initialized in Landscape mode due to a bug in the GVR library. |
| 82 mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSC APE); | 95 mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSC APE); |
| 83 if (!createVrShell()) { | 96 if (!createVrShell()) { |
| 84 mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UN SPECIFIED); | 97 mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UN SPECIFIED); |
| 85 return false; | 98 return false; |
| 86 } | 99 } |
| 87 addVrViews(); | 100 addVrViews(); |
| 88 setupVrModeWindowFlags(); | 101 setupVrModeWindowFlags(); |
| 89 mVrShell.onNativeLibraryReady(tab); | 102 mVrShell.onNativeLibraryReady(tab, this); |
|
Ted C
2016/09/21 17:59:25
to me, it's weird to have this not in the onNative
bajones
2016/09/21 19:16:25
While "something" seems like a great function name
| |
| 103 if (inWebVR) { | |
|
Ted C
2016/09/21 17:59:25
in java land (if it is readable), you can put the
| |
| 104 mVrShell.setWebVrModeEnabled(true); | |
| 105 } | |
| 90 mVrShell.setVrModeEnabled(true); | 106 mVrShell.setVrModeEnabled(true); |
| 91 mInVr = true; | 107 mInVr = true; |
| 92 return true; | 108 return true; |
| 93 } | 109 } |
| 94 | 110 |
| 111 @CalledByNative | |
| 112 public boolean exitWebVR() { | |
|
Ted C
2016/09/21 17:59:25
does this only get accessed by native? If so, you
| |
| 113 if (!mInVr) return false; | |
| 114 mVrShell.setWebVrModeEnabled(false); | |
|
Ted C
2016/09/21 17:59:25
Could we just move this line into shutdownVR (is i
bajones
2016/09/21 19:16:25
I think the comment below illustrates the reason f
| |
| 115 // TODO(bajones): Once VR Shell can be invoked outside of WebVR this | |
| 116 // should no longer exit the shell outright. Need a way to determine | |
| 117 // how VrShell was created. | |
| 118 shutdownVR(); | |
| 119 return true; | |
| 120 } | |
| 121 | |
| 95 /** | 122 /** |
| 96 * Resumes VR Shell. | 123 * Resumes VR Shell. |
| 97 */ | 124 */ |
| 98 public void resumeVR() { | 125 public void resumeVR() { |
| 99 setupVrModeWindowFlags(); | 126 setupVrModeWindowFlags(); |
| 100 mVrShell.resume(); | 127 mVrShell.resume(); |
| 101 } | 128 } |
| 102 | 129 |
| 103 /** | 130 /** |
| 104 * Pauses VR Shell. | 131 * Pauses VR Shell. |
| 105 */ | 132 */ |
| 106 public void pauseVR() { | 133 public void pauseVR() { |
| 107 mVrShell.pause(); | 134 mVrShell.pause(); |
| 108 } | 135 } |
| 109 | 136 |
| 110 /** | 137 /** |
| 138 * Exits the current VR mode (WebVR or VRShell) | |
| 139 * @Returns Whether or not we exited VR. | |
|
Ted C
2016/09/21 17:59:25
@return
| |
| 140 */ | |
| 141 public boolean exitVRIfNecessary() { | |
| 142 if (!mInVr) return false; | |
| 143 // If WebVR is presenting instruct it to exit. VR mode should not | |
| 144 // exit in this scenario, in case we want to return to the VrShell. | |
| 145 if (!nativeExitWebVRIfNecessary(mNativeVrShellDelegate)) { | |
| 146 // If WebVR was not presenting, shutdown VR mode entirely. | |
| 147 shutdownVR(); | |
| 148 } | |
| 149 | |
| 150 return true; | |
| 151 } | |
| 152 | |
| 153 /** | |
| 111 * Exits VR Shell, performing all necessary cleanup. | 154 * Exits VR Shell, performing all necessary cleanup. |
| 112 * @Returns Whether or not we exited VR. | |
| 113 */ | 155 */ |
| 114 public boolean exitVRIfNecessary() { | 156 private void shutdownVR() { |
| 115 if (!mInVr) return false; | 157 if (!mInVr) return; |
| 116 mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPEC IFIED); | 158 mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPEC IFIED); |
| 117 mVrShell.setVrModeEnabled(false); | 159 mVrShell.setVrModeEnabled(false); |
| 118 mVrShell.pause(); | 160 mVrShell.pause(); |
| 119 removeVrViews(); | 161 removeVrViews(); |
| 120 clearVrModeWindowFlags(); | 162 clearVrModeWindowFlags(); |
| 121 destroyVrShell(); | 163 destroyVrShell(); |
| 122 mInVr = false; | 164 mInVr = false; |
| 123 return true; | |
| 124 } | 165 } |
| 125 | 166 |
| 126 private boolean createVrShell() { | 167 private boolean createVrShell() { |
| 127 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); | 168 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
| 128 try { | 169 try { |
| 129 Constructor<?> vrShellConstructor = mVrShellClass.getConstructor(Act ivity.class); | 170 Constructor<?> vrShellConstructor = mVrShellClass.getConstructor(Act ivity.class); |
| 130 mVrShell = (VrShellInterface) vrShellConstructor.newInstance(mActivi ty); | 171 mVrShell = (VrShellInterface) vrShellConstructor.newInstance(mActivi ty); |
| 131 } catch (InstantiationException | IllegalAccessException | IllegalArgume ntException | 172 } catch (InstantiationException | IllegalAccessException | IllegalArgume ntException |
| 132 | InvocationTargetException | NoSuchMethodException e) { | 173 | InvocationTargetException | NoSuchMethodException e) { |
| 133 Log.e(TAG, "Unable to instantiate VrShell", e); | 174 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() { | 239 public boolean isInVR() { |
| 199 return mInVr; | 240 return mInVr; |
| 200 } | 241 } |
| 201 | 242 |
| 202 /** | 243 /** |
| 203 * @return Whether or not VR Shell is currently enabled. | 244 * @return Whether or not VR Shell is currently enabled. |
| 204 */ | 245 */ |
| 205 public boolean isVrShellEnabled() { | 246 public boolean isVrShellEnabled() { |
| 206 return mVrShellEnabled; | 247 return mVrShellEnabled; |
| 207 } | 248 } |
| 249 | |
| 250 /** | |
| 251 * @return Pointer to the native VrShellDelegate object. | |
| 252 */ | |
| 253 @CalledByNative | |
| 254 public long getNativePointer() { | |
|
Ted C
2016/09/21 17:59:24
should be private
| |
| 255 return mNativeVrShellDelegate; | |
| 256 } | |
| 257 | |
| 258 private native long nativeInit(); | |
| 259 private native boolean nativeExitWebVRIfNecessary(long nativeVrShellDelegate ); | |
| 208 } | 260 } |
| OLD | NEW |