diff options
author | Yabin Huang <yabinh@google.com> | 2021-07-21 15:45:53 -0700 |
---|---|---|
committer | Yabin Huang <yabinh@google.com> | 2021-07-24 00:01:23 +0000 |
commit | 695c6b7f0d4e7a1b4906e495e0bcfc21ef7553c6 (patch) | |
tree | 7efd4a416fcc1497a969bc292017168faf8745b7 /RotaryPlayground/src/com/android/car/rotaryplayground | |
parent | b5867e944fe0796c976b4d81a97c09188804498a (diff) | |
download | tests-695c6b7f0d4e7a1b4906e495e0bcfc21ef7553c6.tar.gz |
Add an example of overlapped SurfaceView
Bug: 194148552
Test: manual
Change-Id: I6e2509a8eda887cb5a1edebd6f48530f33448f2c
Diffstat (limited to 'RotaryPlayground/src/com/android/car/rotaryplayground')
3 files changed, 187 insertions, 8 deletions
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; + } +} |