diff options
5 files changed, 252 insertions, 8 deletions
diff --git a/RotaryPlayground/res/layout/rotary_menu.xml b/RotaryPlayground/res/layout/rotary_menu.xml index f72d6ec..e2a523a 100644 --- a/RotaryPlayground/res/layout/rotary_menu.xml +++ b/RotaryPlayground/res/layout/rotary_menu.xml @@ -76,4 +76,11 @@ android:layout_weight="1" android:text="Custom FocusAreas" style="@style/tab" /> + <Button + android:id="@+id/surface_view" + android:layout_width="match_parent" + android:layout_height="0dp" + android:layout_weight="1" + android:text="SurfaceView" + style="@style/tab" /> </com.android.car.ui.FocusArea> diff --git a/RotaryPlayground/res/layout/surface_view_fragment.xml b/RotaryPlayground/res/layout/surface_view_fragment.xml new file mode 100644 index 0000000..b2e09f0 --- /dev/null +++ b/RotaryPlayground/res/layout/surface_view_fragment.xml @@ -0,0 +1,58 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright (C) 2021 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<FrameLayout + xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + android:layout_width="match_parent" + android:layout_height="match_parent"> + <!-- The two FocusAreas are overlapping and nudge targeting is ambiguous. So set its + "app:startBoundOffset" to fix that. --> + <com.android.car.ui.FocusArea + android:layout_width="match_parent" + android:layout_height="match_parent" + app:startBoundOffset="300dp"> + <com.android.car.rotaryplayground.CustomSurfaceView + android:id="@+id/surface_view" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:focusable="true" + android:background="#A0A0A0"/> + </com.android.car.ui.FocusArea> + <com.android.car.ui.FocusArea + android:layout_width="wrap_content" + android:layout_height="match_parent" + android:layout_margin="16dp" + android:background="#808080" + android:orientation="vertical"> + <Button + android:layout_width="200dp" + android:layout_height="100dp" + android:text="Button 1"/> + <Button + android:layout_width="200dp" + android:layout_height="100dp" + android:text="Button 2"/> + <Button + android:layout_width="200dp" + android:layout_height="100dp" + android:text="Button 3"/> + <Button + android:layout_width="200dp" + android:layout_height="100dp" + android:text="Button 4"/> + </com.android.car.ui.FocusArea> +</FrameLayout> diff --git a/RotaryPlayground/src/com/android/car/rotaryplayground/CustomSurfaceView.java b/RotaryPlayground/src/com/android/car/rotaryplayground/CustomSurfaceView.java new file mode 100644 index 0000000..ec6d895 --- /dev/null +++ b/RotaryPlayground/src/com/android/car/rotaryplayground/CustomSurfaceView.java @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.car.rotaryplayground; + +import static com.android.car.ui.utils.RotaryConstants.BOTTOM_BOUND_OFFSET_FOR_NUDGE; +import static com.android.car.ui.utils.RotaryConstants.LEFT_BOUND_OFFSET_FOR_NUDGE; +import static com.android.car.ui.utils.RotaryConstants.RIGHT_BOUND_OFFSET_FOR_NUDGE; +import static com.android.car.ui.utils.RotaryConstants.TOP_BOUND_OFFSET_FOR_NUDGE; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.os.Bundle; +import android.util.AttributeSet; +import android.view.SurfaceView; +import android.view.accessibility.AccessibilityNodeInfo; + +import androidx.annotation.Nullable; + +/** + * A SurfaceView that allows to set its perceived bounds for the purposes of finding the nudge + * target. + */ +public class CustomSurfaceView extends SurfaceView { + + /** + * The offset (in pixels) of the SurfaceView's bounds for the purposes of finding the nudge + * target. + */ + private int mLeftOffset; + private int mRightOffset; + private int mTopOffset; + private int mBottomOffset; + + public CustomSurfaceView(Context context) { + super(context); + } + + public CustomSurfaceView(Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + } + + public CustomSurfaceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + public CustomSurfaceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, + int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + } + + /** + * Sets the offset (in pixels) of the SurfaceView's bounds. + * <p> + * It only affects the perceived bounds for the purposes of finding the nudge target. + * This doesn't affect the view's bounds. This method should only be used when this view + * overlaps other focusable views so that nudge targeting is ambiguous. + */ + public void setBoundsOffset(int left, int top, int right, int bottom) { + mLeftOffset = left; + mTopOffset = top; + mRightOffset = right; + mBottomOffset = bottom; + } + + @Override + public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { + super.onInitializeAccessibilityNodeInfo(info); + if (mLeftOffset == 0 && mTopOffset == 0 && mRightOffset == 0 && mBottomOffset == 0) { + return; + } + Bundle bundle = info.getExtras(); + bundle.putInt(LEFT_BOUND_OFFSET_FOR_NUDGE, mLeftOffset); + bundle.putInt(RIGHT_BOUND_OFFSET_FOR_NUDGE, mRightOffset); + bundle.putInt(TOP_BOUND_OFFSET_FOR_NUDGE, mTopOffset); + bundle.putInt(BOTTOM_BOUND_OFFSET_FOR_NUDGE, mBottomOffset); + } + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + + // Draw a text for demonstration purpose. + Paint paint = new Paint(); + paint.setColor(Color.BLACK); + paint.setTextSize(40); + canvas.drawText("SurfaceView", 400, 200, paint); + } +} diff --git a/RotaryPlayground/src/com/android/car/rotaryplayground/RotaryMenu.java b/RotaryPlayground/src/com/android/car/rotaryplayground/RotaryMenu.java index 0e417b4..4b8844d 100644 --- a/RotaryPlayground/src/com/android/car/rotaryplayground/RotaryMenu.java +++ b/RotaryPlayground/src/com/android/car/rotaryplayground/RotaryMenu.java @@ -31,14 +31,15 @@ import androidx.fragment.app.Fragment; */ public class RotaryMenu extends Fragment { - private Fragment mRotaryCards = null; - private Fragment mRotaryGrid = null; - private Fragment mDirectManipulation = null; - private Fragment mSysUiDirectManipulation = null; - private Fragment mNotificationFragment = null; - private Fragment mScrollFragment = null; - private Fragment mWebViewFragment = null; - private Fragment mCustomFocusAreasFragment = null; + private Fragment mRotaryCards; + private Fragment mRotaryGrid; + private Fragment mDirectManipulation; + private Fragment mSysUiDirectManipulation; + private Fragment mNotificationFragment; + private Fragment mScrollFragment; + private Fragment mWebViewFragment; + private Fragment mCustomFocusAreasFragment; + private Fragment mSurfaceViewFragment; @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @@ -93,6 +94,12 @@ public class RotaryMenu extends Fragment { showCustomFocusAreasFragment(); }); + Button surfaceViewButton = view.findViewById(R.id.surface_view); + surfaceViewButton.setOnClickListener(v -> { + selectTab(v); + showSurfaceViewFragment(); + }); + return view; } @@ -162,6 +169,13 @@ public class RotaryMenu extends Fragment { showFragment(mCustomFocusAreasFragment); } + private void showSurfaceViewFragment() { + if (mSurfaceViewFragment == null) { + mSurfaceViewFragment = new SurfaceViewFragment(); + } + showFragment(mSurfaceViewFragment); + } + private void showFragment(Fragment fragment) { getFragmentManager().beginTransaction() .replace(R.id.rotary_content, fragment) diff --git a/RotaryPlayground/src/com/android/car/rotaryplayground/SurfaceViewFragment.java b/RotaryPlayground/src/com/android/car/rotaryplayground/SurfaceViewFragment.java new file mode 100644 index 0000000..8130638 --- /dev/null +++ b/RotaryPlayground/src/com/android/car/rotaryplayground/SurfaceViewFragment.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.car.rotaryplayground; + +import android.app.Activity; +import android.os.Bundle; +import android.util.DisplayMetrics; +import android.view.LayoutInflater; +import android.view.SurfaceView; +import android.view.View; +import android.view.ViewGroup; +import android.view.WindowManager; + +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; + +/** Fragment to demo a {@link SurfaceView} behind some buttons. */ +public class SurfaceViewFragment extends Fragment { + + private CustomSurfaceView mSurfaceView; + + @Override + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, + @Nullable Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.surface_view_fragment, container, false); + mSurfaceView = view.findViewById(R.id.surface_view); + + // Some buttons sit on top of the left part of the SurfaceView, so tailor its left bound + // so that RotaryService can find the correct nudge target. + mSurfaceView.setBoundsOffset(300, 0, 0, 0); + + makeItFullScreen(); + return view; + } + + private void makeItFullScreen() { + Activity activity = getActivity(); + activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN); + + DisplayMetrics displayMetrics = new DisplayMetrics(); + activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); + ViewGroup.LayoutParams layoutParams = mSurfaceView.getLayoutParams(); + layoutParams.width = displayMetrics.widthPixels; + layoutParams.height = displayMetrics.heightPixels; + } +} |