1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.car.rotaryplayground;
18 
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.util.DisplayMetrics;
22 import android.view.LayoutInflater;
23 import android.view.SurfaceView;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.view.WindowManager;
27 
28 import androidx.annotation.Nullable;
29 import androidx.fragment.app.Fragment;
30 
31 /** Fragment to demo a {@link SurfaceView} behind some buttons. */
32 public class SurfaceViewFragment extends Fragment {
33 
34     private CustomSurfaceView mSurfaceView;
35 
36     @Override
onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)37     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
38             @Nullable Bundle savedInstanceState) {
39         View view = inflater.inflate(R.layout.surface_view_fragment, container, false);
40         mSurfaceView = view.findViewById(R.id.surface_view);
41 
42         // Some buttons sit on top of the left part of the SurfaceView, so tailor its left bound
43         // so that RotaryService can find the correct nudge target.
44         mSurfaceView.setBoundsOffset(300, 0, 0, 0);
45 
46         makeItFullScreen();
47         return view;
48     }
49 
makeItFullScreen()50     private void makeItFullScreen() {
51         Activity activity = getActivity();
52         activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
53                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
54 
55         DisplayMetrics displayMetrics = new DisplayMetrics();
56         activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
57         ViewGroup.LayoutParams layoutParams = mSurfaceView.getLayoutParams();
58         layoutParams.width = displayMetrics.widthPixels;
59         layoutParams.height = displayMetrics.heightPixels;
60     }
61 }
62