1 /* 2 * Copyright (C) 2020 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.settings.common.rotary; 18 19 import android.view.MotionEvent; 20 import android.view.View; 21 import android.view.accessibility.AccessibilityNodeInfo; 22 import android.widget.NumberPicker; 23 24 /** A rotation handler for {@link NumberPicker} instances. */ 25 public class NumberPickerRotationHandler implements View.OnGenericMotionListener { 26 @Override onGenericMotion(View v, MotionEvent event)27 public boolean onGenericMotion(View v, MotionEvent event) { 28 View focusedView = v.findFocus(); 29 if (focusedView instanceof NumberPicker) { 30 NumberPicker numberPicker = (NumberPicker) focusedView; 31 float scroll = event.getAxisValue(MotionEvent.AXIS_SCROLL); 32 // NumberPicker#setValue(int) doesn't notify change listener, so perform an 33 // accessibility action instead. 34 int scrollValue = Math.round(scroll); 35 int scrollDirection = scrollValue > 0 ? AccessibilityNodeInfo.ACTION_SCROLL_FORWARD 36 : AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD; 37 for (int i = 0; i < Math.abs(scrollValue); i++) { 38 numberPicker.getAccessibilityNodeProvider().performAction(View.NO_ID, 39 scrollDirection, /* arguments= */ null); 40 } 41 return true; 42 } 43 return false; 44 } 45 } 46