The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

android: Revise MonadoView so it doesn't require an Activity.

+73 -51
+73 -51
src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/MonadoView.java
··· 10 10 package org.freedesktop.monado.auxiliary; 11 11 12 12 import android.app.Activity; 13 + import android.content.Context; 13 14 import android.os.Build; 14 15 import android.util.DisplayMetrics; 15 16 import android.util.Log; ··· 41 42 | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 42 43 // we want sticky immersive 43 44 | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; 44 - /// The activity we've connected to. 45 - private final Activity activity; 46 - 45 + private final Context context; 47 46 /// Guards currentSurfaceHolder 48 47 private final Object currentSurfaceHolderSync = new Object(); 48 + /// The activity we've connected to. 49 + @Nullable 50 + private final 51 + Activity activity; 49 52 private final Method viewSetSysUiVis; 50 - private NativeCounterpart nativeCounterpart; 51 - 52 53 public int width = -1; 53 54 public int height = -1; 54 55 public int format = -1; 55 - 56 + private NativeCounterpart nativeCounterpart; 56 57 /// Guarded by currentSurfaceHolderSync 57 58 private SurfaceHolder currentSurfaceHolder = null; 58 59 60 + public MonadoView(Context context) { 61 + super(context); 62 + this.context = context; 63 + Activity activity; 64 + if (context instanceof Activity) { 65 + activity = (Activity) context; 66 + } else { 67 + activity = null; 68 + } 69 + this.activity = activity; 70 + viewSetSysUiVis = getSystemUiVisMethod(); 71 + } 72 + 59 73 public MonadoView(Activity activity) { 60 74 super(activity); 75 + this.context = activity; 61 76 this.activity = activity; 77 + 78 + viewSetSysUiVis = getSystemUiVisMethod(); 79 + } 80 + 81 + private MonadoView(Activity activity, long nativePointer) { 82 + this(activity); 83 + nativeCounterpart = new NativeCounterpart(nativePointer); 84 + } 85 + 86 + private static Method getSystemUiVisMethod() { 62 87 Method method; 63 88 try { 64 - method = activity.getWindow().getDecorView().getClass().getMethod("setSystemUiVisibility", int.class); 89 + method = android.view.View.class.getMethod("setSystemUiVisibility", int.class); 65 90 } catch (NoSuchMethodException e) { 66 91 // ok 67 92 method = null; 68 93 } 69 - viewSetSysUiVis = method; 94 + return method; 70 95 } 71 96 72 - private MonadoView(Activity activity, long nativePointer) { 73 - this(activity); 74 - nativeCounterpart = new NativeCounterpart(nativePointer); 97 + /** 98 + * Construct and start attaching a MonadoView to a client application. 99 + * 100 + * @param activity The activity to attach to. 101 + * @return The MonadoView instance created and asynchronously attached. 102 + */ 103 + @NonNull 104 + @Keep 105 + @SuppressWarnings("deprecation") 106 + public static MonadoView attachToActivity(@NonNull final Activity activity, long nativePointer) { 107 + final MonadoView view = new MonadoView(activity, nativePointer); 108 + view.createSurfaceInActivity(); 109 + return view; 75 110 } 76 111 77 - private void createSurface() { 78 - createSurface(false); 112 + @NonNull 113 + @Keep 114 + public static MonadoView attachToActivity(@NonNull final Activity activity) { 115 + final MonadoView view = new MonadoView(activity); 116 + view.createSurfaceInActivity(); 117 + return view; 118 + } 119 + 120 + @NonNull 121 + @Keep 122 + public static DisplayMetrics getDisplayMetrics(Activity activity) { 123 + DisplayMetrics displayMetrics = new DisplayMetrics(); 124 + activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 125 + return displayMetrics; 126 + } 127 + 128 + private void createSurfaceInActivity() { 129 + createSurfaceInActivity(false); 79 130 } 80 131 81 132 /** 82 133 * @param focusable Indicates MonadoView should be focusable or not 83 134 */ 84 - private void createSurface(boolean focusable) { 135 + private void createSurfaceInActivity(boolean focusable) { 85 136 Log.i(TAG, "Starting to add a new surface!"); 86 137 activity.runOnUiThread(() -> { 87 138 Log.i(TAG, "Starting runOnUiThread"); ··· 92 143 if (focusable) { 93 144 lp.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN; 94 145 } else { 95 - // There are 2 problems if view is focusable on all-in-one device: 96 - // 1. Navigation bar won't go away because view gets focus. 97 - // 2. Underlying activity lost focus and can not receive input. 146 + // There are 2 problems if view is focusable on all-in-one device: 147 + // 1. Navigation bar won't go away because view gets focus. 148 + // 2. Underlying activity lost focus and can not receive input. 98 149 lp.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN | 99 - WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | 100 - WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; 150 + WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | 151 + WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; 101 152 } 102 153 windowManager.addView(this, lp); 103 154 if (focusable) { ··· 110 161 } 111 162 112 163 /** 113 - * Construct and start attaching a MonadoView to a client application. 114 - * 115 - * @param activity The activity to attach to. 116 - * @return The MonadoView instance created and asynchronously attached. 117 - */ 118 - @NonNull 119 - @Keep 120 - @SuppressWarnings("deprecation") 121 - public static MonadoView attachToActivity(@NonNull final Activity activity, long nativePointer) { 122 - final MonadoView view = new MonadoView(activity, nativePointer); 123 - view.createSurface(); 124 - return view; 125 - } 126 - 127 - @NonNull 128 - @Keep 129 - public static MonadoView attachToActivity(@NonNull final Activity activity) { 130 - final MonadoView view = new MonadoView(activity); 131 - view.createSurface(); 132 - return view; 133 - } 134 - 135 - /** 136 164 * Block up to a specified amount of time, waiting for the surfaceCreated callback to be fired 137 165 * and populate the currentSurfaceHolder. 138 166 * <p> ··· 199 227 } 200 228 return true; 201 229 } 202 - 203 230 204 231 /** 205 232 * Add a listener so that if our system UI display state doesn't include all we want, we re-apply. ··· 207 234 @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB) 208 235 @SuppressWarnings("deprecation") 209 236 private void setSystemUiVisChangeListener() { 237 + if (activity == null) { 238 + return; 239 + } 210 240 activity.getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(visibility -> { 211 241 // If not fullscreen, fix it. 212 242 if (0 == (visibility & View.SYSTEM_UI_FLAG_FULLSCREEN)) { ··· 266 296 public void surfaceRedrawNeeded(@NonNull SurfaceHolder surfaceHolder) { 267 297 // currentSurfaceHolder = surfaceHolder; 268 298 Log.i(TAG, "surfaceRedrawNeeded"); 269 - } 270 - 271 - @NonNull 272 - @Keep 273 - public static DisplayMetrics getDisplayMetrics(Activity activity) { 274 - DisplayMetrics displayMetrics = new DisplayMetrics(); 275 - activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 276 - return displayMetrics; 277 299 } 278 300 279 301 }