1 /*
2  * Copyright (C) 2023 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.example.android.vdmdemo.common;
18 
19 import android.os.Bundle;
20 import android.util.Log;
21 import android.view.KeyEvent;
22 import android.view.MotionEvent;
23 import android.view.View;
24 import android.widget.ImageButton;
25 
26 import androidx.fragment.app.Fragment;
27 
28 import dagger.hilt.android.AndroidEntryPoint;
29 
30 import java.util.function.Consumer;
31 
32 /** Fragment to show UI for a Dpad. */
33 @AndroidEntryPoint(Fragment.class)
34 public final class DpadFragment extends Hilt_DpadFragment {
35     private static final String TAG = "DpadFragment";
36 
37     private static final int[] BUTTONS = {
38             R.id.dpad_center, R.id.dpad_down, R.id.dpad_left, R.id.dpad_up, R.id.dpad_right
39     };
40 
41     private Consumer<KeyEvent> mInputEventListener;
42 
DpadFragment()43     public DpadFragment() {
44         super(R.layout.dpad_fragment);
45     }
46 
setInputEventListener(Consumer<KeyEvent> listener)47     public void setInputEventListener(Consumer<KeyEvent> listener) {
48         mInputEventListener = listener;
49     }
50 
51     @Override
onViewCreated(View view, Bundle savedInstanceState)52     public void onViewCreated(View view, Bundle savedInstanceState) {
53         for (int buttonId : BUTTONS) {
54             ImageButton button = view.requireViewById(buttonId);
55             button.setOnTouchListener(this::onDpadButtonClick);
56         }
57     }
58 
onDpadButtonClick(View v, MotionEvent e)59     private boolean onDpadButtonClick(View v, MotionEvent e) {
60         int action;
61         if (e.getAction() == MotionEvent.ACTION_DOWN) {
62             action = KeyEvent.ACTION_DOWN;
63         } else if (e.getAction() == MotionEvent.ACTION_UP) {
64             action = KeyEvent.ACTION_UP;
65         } else {
66             return false;
67         }
68 
69         int keyCode;
70         int id = v.getId();
71         if (id == R.id.dpad_center) {
72             keyCode = KeyEvent.KEYCODE_DPAD_CENTER;
73         } else if (id == R.id.dpad_down) {
74             keyCode = KeyEvent.KEYCODE_DPAD_DOWN;
75         } else if (id == R.id.dpad_left) {
76             keyCode = KeyEvent.KEYCODE_DPAD_LEFT;
77         } else if (id == R.id.dpad_up) {
78             keyCode = KeyEvent.KEYCODE_DPAD_UP;
79         } else if (id == R.id.dpad_right) {
80             keyCode = KeyEvent.KEYCODE_DPAD_RIGHT;
81         } else {
82             Log.w(TAG, "onDpadButtonClick: Method called from a non Dpad button");
83             return false;
84         }
85 
86         if (mInputEventListener != null) {
87             mInputEventListener.accept(
88                     new KeyEvent(
89                             /* downTime= */ System.currentTimeMillis(),
90                             /* eventTime= */ System.currentTimeMillis(),
91                             action,
92                             keyCode,
93                             /* repeat= */ 0));
94         }
95         return true;
96     }
97 }
98