| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.hardware.input; |
| 18 |
|
| 19 |
import android.Manifest; |
| 20 |
import android.annotation.FloatRange; |
| 21 |
import android.annotation.IntDef; |
| 22 |
import android.annotation.NonNull; |
| 23 |
import android.annotation.Nullable; |
| 24 |
import android.annotation.RequiresPermission; |
| 25 |
import android.annotation.SdkConstant; |
| 26 |
import android.annotation.SdkConstant.SdkConstantType; |
| 27 |
import android.annotation.SystemService; |
| 28 |
import android.annotation.TestApi; |
| 29 |
import android.app.ActivityThread; |
| 30 |
import android.compat.annotation.ChangeId; |
| 31 |
import android.compat.annotation.UnsupportedAppUsage; |
| 32 |
import android.content.Context; |
| 33 |
import android.hardware.BatteryState; |
| 34 |
import android.hardware.SensorManager; |
| 35 |
import android.hardware.lights.Light; |
| 36 |
import android.hardware.lights.LightState; |
| 37 |
import android.hardware.lights.LightsManager; |
| 38 |
import android.hardware.lights.LightsRequest; |
| 39 |
import android.os.Binder; |
| 40 |
import android.os.BlockUntrustedTouchesMode; |
| 41 |
import android.os.Build; |
| 42 |
import android.os.CombinedVibration; |
| 43 |
import android.os.Handler; |
| 44 |
import android.os.IBinder; |
| 45 |
import android.os.IVibratorStateListener; |
| 46 |
import android.os.InputEventInjectionSync; |
| 47 |
import android.os.Looper; |
| 48 |
import android.os.Message; |
| 49 |
import android.os.Process; |
| 50 |
import android.os.RemoteException; |
| 51 |
import android.os.ServiceManager; |
| 52 |
import android.os.ServiceManager.ServiceNotFoundException; |
| 53 |
import android.os.SystemClock; |
| 54 |
import android.os.VibrationEffect; |
| 55 |
import android.os.Vibrator; |
| 56 |
import android.os.VibratorManager; |
| 57 |
import android.provider.Settings; |
| 58 |
import android.provider.Settings.SettingNotFoundException; |
| 59 |
import android.util.Log; |
| 60 |
import android.util.SparseArray; |
| 61 |
import android.view.InputDevice; |
| 62 |
import android.view.InputEvent; |
| 63 |
import android.view.InputMonitor; |
| 64 |
import android.view.KeyEvent; |
| 65 |
import android.view.MotionEvent; |
| 66 |
import android.view.PointerIcon; |
| 67 |
import android.view.VerifiedInputEvent; |
| 68 |
import android.view.WindowManager.LayoutParams; |
| 69 |
|
| 70 |
import com.android.internal.annotations.GuardedBy; |
| 71 |
import com.android.internal.annotations.VisibleForTesting; |
| 72 |
import com.android.internal.os.SomeArgs; |
| 73 |
import com.android.internal.util.ArrayUtils; |
| 74 |
|
| 75 |
import java.lang.annotation.Retention; |
| 76 |
import java.lang.annotation.RetentionPolicy; |
| 77 |
import java.util.ArrayList; |
| 78 |
import java.util.List; |
| 79 |
import java.util.Objects; |
| 80 |
import java.util.concurrent.Executor; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
@SystemService(Context.INPUT_SERVICE) |
| 86 |
public final class InputManager { |
| 87 |
private static final String TAG = "InputManager"; |
| 88 |
private static final boolean DEBUG = false; |
| 89 |
|
| 90 |
private static final int MSG_DEVICE_ADDED = 1; |
| 91 |
private static final int MSG_DEVICE_REMOVED = 2; |
| 92 |
private static final int MSG_DEVICE_CHANGED = 3; |
| 93 |
|
| 94 |
|
| 95 |
public static final int[] BLOCK_UNTRUSTED_TOUCHES_MODES = { |
| 96 |
BlockUntrustedTouchesMode.DISABLED, |
| 97 |
BlockUntrustedTouchesMode.PERMISSIVE, |
| 98 |
BlockUntrustedTouchesMode.BLOCK |
| 99 |
}; |
| 100 |
|
| 101 |
private static InputManager sInstance; |
| 102 |
|
| 103 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 104 |
private final IInputManager mIm; |
| 105 |
|
| 106 |
|
| 107 |
private final Object mInputDevicesLock = new Object(); |
| 108 |
private SparseArray<InputDevice> mInputDevices; |
| 109 |
private InputDevicesChangedListener mInputDevicesChangedListener; |
| 110 |
private final ArrayList<InputDeviceListenerDelegate> mInputDeviceListeners = |
| 111 |
new ArrayList<InputDeviceListenerDelegate>(); |
| 112 |
|
| 113 |
|
| 114 |
private final Object mTabletModeLock = new Object(); |
| 115 |
private TabletModeChangedListener mTabletModeChangedListener; |
| 116 |
private List<OnTabletModeChangedListenerDelegate> mOnTabletModeChangedListeners; |
| 117 |
|
| 118 |
private final Object mBatteryListenersLock = new Object(); |
| 119 |
|
| 120 |
|
| 121 |
@GuardedBy("mBatteryListenersLock") |
| 122 |
private SparseArray<RegisteredBatteryListeners> mBatteryListeners; |
| 123 |
@GuardedBy("mBatteryListenersLock") |
| 124 |
private IInputDeviceBatteryListener mInputDeviceBatteryListener; |
| 125 |
|
| 126 |
private InputDeviceSensorManager mInputDeviceSensorManager; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 176 |
public static final String ACTION_QUERY_KEYBOARD_LAYOUTS = |
| 177 |
"android.hardware.input.action.QUERY_KEYBOARD_LAYOUTS"; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public static final String META_DATA_KEYBOARD_LAYOUTS = |
| 188 |
"android.hardware.input.metadata.KEYBOARD_LAYOUTS"; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public static final int MIN_POINTER_SPEED = -7; |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public static final int MAX_POINTER_SPEED = 7; |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public static final int DEFAULT_POINTER_SPEED = 0; |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public static final float DEFAULT_MAXIMUM_OBSCURING_OPACITY_FOR_TOUCH = .8f; |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
@BlockUntrustedTouchesMode |
| 219 |
public static final int DEFAULT_BLOCK_UNTRUSTED_TOUCHES_MODE = |
| 220 |
BlockUntrustedTouchesMode.BLOCK; |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
@TestApi |
| 229 |
@ChangeId |
| 230 |
public static final long BLOCK_UNTRUSTED_TOUCHES = 158002302L; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public static final int INJECT_INPUT_EVENT_MODE_ASYNC = InputEventInjectionSync.NONE; |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public static final int INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT = |
| 248 |
InputEventInjectionSync.WAIT_FOR_RESULT; |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
@UnsupportedAppUsage(trackingBug = 171972397) |
| 256 |
public static final int INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH = |
| 257 |
InputEventInjectionSync.WAIT_FOR_FINISHED; |
| 258 |
|
| 259 |
|
| 260 |
@Retention(RetentionPolicy.SOURCE) |
| 261 |
@IntDef(prefix = { "SWITCH_STATE_" }, value = { |
| 262 |
SWITCH_STATE_UNKNOWN, |
| 263 |
SWITCH_STATE_OFF, |
| 264 |
SWITCH_STATE_ON |
| 265 |
}) |
| 266 |
public @interface SwitchState {} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
public static final int SWITCH_STATE_UNKNOWN = -1; |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
public static final int SWITCH_STATE_OFF = 0; |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
public static final int SWITCH_STATE_ON = 1; |
| 287 |
|
| 288 |
private InputManager(IInputManager im) { |
| 289 |
mIm = im; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
@VisibleForTesting |
| 300 |
public static InputManager resetInstance(IInputManager inputManagerService) { |
| 301 |
synchronized (InputManager.class) { |
| 302 |
sInstance = new InputManager(inputManagerService); |
| 303 |
return sInstance; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
@VisibleForTesting |
| 313 |
public static void clearInstance() { |
| 314 |
synchronized (InputManager.class) { |
| 315 |
sInstance = null; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
@UnsupportedAppUsage |
| 327 |
public static InputManager getInstance() { |
| 328 |
synchronized (InputManager.class) { |
| 329 |
if (sInstance == null) { |
| 330 |
try { |
| 331 |
sInstance = new InputManager(IInputManager.Stub |
| 332 |
.asInterface(ServiceManager.getServiceOrThrow(Context.INPUT_SERVICE))); |
| 333 |
} catch (ServiceNotFoundException e) { |
| 334 |
throw new IllegalStateException(e); |
| 335 |
} |
| 336 |
} |
| 337 |
return sInstance; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
public InputDevice getInputDevice(int id) { |
| 347 |
synchronized (mInputDevicesLock) { |
| 348 |
populateInputDevicesLocked(); |
| 349 |
|
| 350 |
int index = mInputDevices.indexOfKey(id); |
| 351 |
if (index < 0) { |
| 352 |
return null; |
| 353 |
} |
| 354 |
|
| 355 |
InputDevice inputDevice = mInputDevices.valueAt(index); |
| 356 |
if (inputDevice == null) { |
| 357 |
try { |
| 358 |
inputDevice = mIm.getInputDevice(id); |
| 359 |
} catch (RemoteException ex) { |
| 360 |
throw ex.rethrowFromSystemServer(); |
| 361 |
} |
| 362 |
if (inputDevice != null) { |
| 363 |
mInputDevices.setValueAt(index, inputDevice); |
| 364 |
} |
| 365 |
} |
| 366 |
return inputDevice; |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
public InputDevice getInputDeviceByDescriptor(String descriptor) { |
| 377 |
if (descriptor == null) { |
| 378 |
throw new IllegalArgumentException("descriptor must not be null."); |
| 379 |
} |
| 380 |
|
| 381 |
synchronized (mInputDevicesLock) { |
| 382 |
populateInputDevicesLocked(); |
| 383 |
|
| 384 |
int numDevices = mInputDevices.size(); |
| 385 |
for (int i = 0; i < numDevices; i++) { |
| 386 |
InputDevice inputDevice = mInputDevices.valueAt(i); |
| 387 |
if (inputDevice == null) { |
| 388 |
int id = mInputDevices.keyAt(i); |
| 389 |
try { |
| 390 |
inputDevice = mIm.getInputDevice(id); |
| 391 |
} catch (RemoteException ex) { |
| 392 |
throw ex.rethrowFromSystemServer(); |
| 393 |
} |
| 394 |
if (inputDevice == null) { |
| 395 |
continue; |
| 396 |
} |
| 397 |
mInputDevices.setValueAt(i, inputDevice); |
| 398 |
} |
| 399 |
if (descriptor.equals(inputDevice.getDescriptor())) { |
| 400 |
return inputDevice; |
| 401 |
} |
| 402 |
} |
| 403 |
return null; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
public int[] getInputDeviceIds() { |
| 412 |
synchronized (mInputDevicesLock) { |
| 413 |
populateInputDevicesLocked(); |
| 414 |
|
| 415 |
final int count = mInputDevices.size(); |
| 416 |
final int[] ids = new int[count]; |
| 417 |
for (int i = 0; i < count; i++) { |
| 418 |
ids[i] = mInputDevices.keyAt(i); |
| 419 |
} |
| 420 |
return ids; |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
public boolean isInputDeviceEnabled(int id) { |
| 434 |
try { |
| 435 |
return mIm.isInputDeviceEnabled(id); |
| 436 |
} catch (RemoteException ex) { |
| 437 |
Log.w(TAG, "Could not check enabled status of input device with id = " + id); |
| 438 |
throw ex.rethrowFromSystemServer(); |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
public void enableInputDevice(int id) { |
| 453 |
try { |
| 454 |
mIm.enableInputDevice(id); |
| 455 |
} catch (RemoteException ex) { |
| 456 |
Log.w(TAG, "Could not enable input device with id = " + id); |
| 457 |
throw ex.rethrowFromSystemServer(); |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
public void disableInputDevice(int id) { |
| 472 |
try { |
| 473 |
mIm.disableInputDevice(id); |
| 474 |
} catch (RemoteException ex) { |
| 475 |
Log.w(TAG, "Could not disable input device with id = " + id); |
| 476 |
throw ex.rethrowFromSystemServer(); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
public void registerInputDeviceListener(InputDeviceListener listener, Handler handler) { |
| 491 |
if (listener == null) { |
| 492 |
throw new IllegalArgumentException("listener must not be null"); |
| 493 |
} |
| 494 |
|
| 495 |
synchronized (mInputDevicesLock) { |
| 496 |
populateInputDevicesLocked(); |
| 497 |
int index = findInputDeviceListenerLocked(listener); |
| 498 |
if (index < 0) { |
| 499 |
mInputDeviceListeners.add(new InputDeviceListenerDelegate(listener, handler)); |
| 500 |
} |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
public void unregisterInputDeviceListener(InputDeviceListener listener) { |
| 512 |
if (listener == null) { |
| 513 |
throw new IllegalArgumentException("listener must not be null"); |
| 514 |
} |
| 515 |
|
| 516 |
synchronized (mInputDevicesLock) { |
| 517 |
int index = findInputDeviceListenerLocked(listener); |
| 518 |
if (index >= 0) { |
| 519 |
InputDeviceListenerDelegate d = mInputDeviceListeners.get(index); |
| 520 |
d.removeCallbacksAndMessages(null); |
| 521 |
mInputDeviceListeners.remove(index); |
| 522 |
} |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
private int findInputDeviceListenerLocked(InputDeviceListener listener) { |
| 527 |
final int numListeners = mInputDeviceListeners.size(); |
| 528 |
for (int i = 0; i < numListeners; i++) { |
| 529 |
if (mInputDeviceListeners.get(i).mListener == listener) { |
| 530 |
return i; |
| 531 |
} |
| 532 |
} |
| 533 |
return -1; |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
@SwitchState |
| 544 |
public int isInTabletMode() { |
| 545 |
try { |
| 546 |
return mIm.isInTabletMode(); |
| 547 |
} catch (RemoteException ex) { |
| 548 |
throw ex.rethrowFromSystemServer(); |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
public void registerOnTabletModeChangedListener( |
| 561 |
OnTabletModeChangedListener listener, Handler handler) { |
| 562 |
if (listener == null) { |
| 563 |
throw new IllegalArgumentException("listener must not be null"); |
| 564 |
} |
| 565 |
synchronized (mTabletModeLock) { |
| 566 |
if (mOnTabletModeChangedListeners == null) { |
| 567 |
initializeTabletModeListenerLocked(); |
| 568 |
} |
| 569 |
int idx = findOnTabletModeChangedListenerLocked(listener); |
| 570 |
if (idx < 0) { |
| 571 |
OnTabletModeChangedListenerDelegate d = |
| 572 |
new OnTabletModeChangedListenerDelegate(listener, handler); |
| 573 |
mOnTabletModeChangedListeners.add(d); |
| 574 |
} |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
public void unregisterOnTabletModeChangedListener(OnTabletModeChangedListener listener) { |
| 585 |
if (listener == null) { |
| 586 |
throw new IllegalArgumentException("listener must not be null"); |
| 587 |
} |
| 588 |
synchronized (mTabletModeLock) { |
| 589 |
int idx = findOnTabletModeChangedListenerLocked(listener); |
| 590 |
if (idx >= 0) { |
| 591 |
OnTabletModeChangedListenerDelegate d = mOnTabletModeChangedListeners.remove(idx); |
| 592 |
d.removeCallbacksAndMessages(null); |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
private void initializeTabletModeListenerLocked() { |
| 598 |
final TabletModeChangedListener listener = new TabletModeChangedListener(); |
| 599 |
try { |
| 600 |
mIm.registerTabletModeChangedListener(listener); |
| 601 |
} catch (RemoteException ex) { |
| 602 |
throw ex.rethrowFromSystemServer(); |
| 603 |
} |
| 604 |
mTabletModeChangedListener = listener; |
| 605 |
mOnTabletModeChangedListeners = new ArrayList<>(); |
| 606 |
} |
| 607 |
|
| 608 |
private int findOnTabletModeChangedListenerLocked(OnTabletModeChangedListener listener) { |
| 609 |
final int N = mOnTabletModeChangedListeners.size(); |
| 610 |
for (int i = 0; i < N; i++) { |
| 611 |
if (mOnTabletModeChangedListeners.get(i).mListener == listener) { |
| 612 |
return i; |
| 613 |
} |
| 614 |
} |
| 615 |
return -1; |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
@SwitchState |
| 626 |
public int isMicMuted() { |
| 627 |
try { |
| 628 |
return mIm.isMicMuted(); |
| 629 |
} catch (RemoteException ex) { |
| 630 |
throw ex.rethrowFromSystemServer(); |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
public KeyboardLayout[] getKeyboardLayouts() { |
| 647 |
try { |
| 648 |
return mIm.getKeyboardLayouts(); |
| 649 |
} catch (RemoteException ex) { |
| 650 |
throw ex.rethrowFromSystemServer(); |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
@TestApi |
| 668 |
@NonNull |
| 669 |
public List<String> getKeyboardLayoutDescriptorsForInputDevice(@NonNull InputDevice device) { |
| 670 |
KeyboardLayout[] layouts = getKeyboardLayoutsForInputDevice(device.getIdentifier()); |
| 671 |
List<String> res = new ArrayList<>(); |
| 672 |
for (KeyboardLayout kl : layouts) { |
| 673 |
res.add(kl.getDescriptor()); |
| 674 |
} |
| 675 |
return res; |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
@NonNull |
| 693 |
public KeyboardLayout[] getKeyboardLayoutsForInputDevice( |
| 694 |
@NonNull InputDeviceIdentifier identifier) { |
| 695 |
try { |
| 696 |
return mIm.getKeyboardLayoutsForInputDevice(identifier); |
| 697 |
} catch (RemoteException ex) { |
| 698 |
throw ex.rethrowFromSystemServer(); |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
public KeyboardLayout getKeyboardLayout(String keyboardLayoutDescriptor) { |
| 712 |
if (keyboardLayoutDescriptor == null) { |
| 713 |
throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null"); |
| 714 |
} |
| 715 |
|
| 716 |
try { |
| 717 |
return mIm.getKeyboardLayout(keyboardLayoutDescriptor); |
| 718 |
} catch (RemoteException ex) { |
| 719 |
throw ex.rethrowFromSystemServer(); |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
@TestApi |
| 732 |
@Nullable |
| 733 |
public String getCurrentKeyboardLayoutForInputDevice( |
| 734 |
@NonNull InputDeviceIdentifier identifier) { |
| 735 |
try { |
| 736 |
return mIm.getCurrentKeyboardLayoutForInputDevice(identifier); |
| 737 |
} catch (RemoteException ex) { |
| 738 |
throw ex.rethrowFromSystemServer(); |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
@TestApi |
| 755 |
@RequiresPermission(Manifest.permission.SET_KEYBOARD_LAYOUT) |
| 756 |
public void setCurrentKeyboardLayoutForInputDevice(@NonNull InputDeviceIdentifier identifier, |
| 757 |
@NonNull String keyboardLayoutDescriptor) { |
| 758 |
if (identifier == null) { |
| 759 |
throw new IllegalArgumentException("identifier must not be null"); |
| 760 |
} |
| 761 |
if (keyboardLayoutDescriptor == null) { |
| 762 |
throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null"); |
| 763 |
} |
| 764 |
|
| 765 |
try { |
| 766 |
mIm.setCurrentKeyboardLayoutForInputDevice(identifier, |
| 767 |
keyboardLayoutDescriptor); |
| 768 |
} catch (RemoteException ex) { |
| 769 |
throw ex.rethrowFromSystemServer(); |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
public String[] getEnabledKeyboardLayoutsForInputDevice(InputDeviceIdentifier identifier) { |
| 782 |
if (identifier == null) { |
| 783 |
throw new IllegalArgumentException("inputDeviceDescriptor must not be null"); |
| 784 |
} |
| 785 |
|
| 786 |
try { |
| 787 |
return mIm.getEnabledKeyboardLayoutsForInputDevice(identifier); |
| 788 |
} catch (RemoteException ex) { |
| 789 |
throw ex.rethrowFromSystemServer(); |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
@RequiresPermission(Manifest.permission.SET_KEYBOARD_LAYOUT) |
| 806 |
public void addKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier, |
| 807 |
String keyboardLayoutDescriptor) { |
| 808 |
if (identifier == null) { |
| 809 |
throw new IllegalArgumentException("inputDeviceDescriptor must not be null"); |
| 810 |
} |
| 811 |
if (keyboardLayoutDescriptor == null) { |
| 812 |
throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null"); |
| 813 |
} |
| 814 |
|
| 815 |
try { |
| 816 |
mIm.addKeyboardLayoutForInputDevice(identifier, keyboardLayoutDescriptor); |
| 817 |
} catch (RemoteException ex) { |
| 818 |
throw ex.rethrowFromSystemServer(); |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
@TestApi |
| 835 |
@RequiresPermission(Manifest.permission.SET_KEYBOARD_LAYOUT) |
| 836 |
public void removeKeyboardLayoutForInputDevice(@NonNull InputDeviceIdentifier identifier, |
| 837 |
@NonNull String keyboardLayoutDescriptor) { |
| 838 |
if (identifier == null) { |
| 839 |
throw new IllegalArgumentException("inputDeviceDescriptor must not be null"); |
| 840 |
} |
| 841 |
if (keyboardLayoutDescriptor == null) { |
| 842 |
throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null"); |
| 843 |
} |
| 844 |
|
| 845 |
try { |
| 846 |
mIm.removeKeyboardLayoutForInputDevice(identifier, keyboardLayoutDescriptor); |
| 847 |
} catch (RemoteException ex) { |
| 848 |
throw ex.rethrowFromSystemServer(); |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
public TouchCalibration getTouchCalibration(String inputDeviceDescriptor, int surfaceRotation) { |
| 862 |
try { |
| 863 |
return mIm.getTouchCalibrationForInputDevice(inputDeviceDescriptor, surfaceRotation); |
| 864 |
} catch (RemoteException ex) { |
| 865 |
throw ex.rethrowFromSystemServer(); |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
public void setTouchCalibration(String inputDeviceDescriptor, int surfaceRotation, |
| 882 |
TouchCalibration calibration) { |
| 883 |
try { |
| 884 |
mIm.setTouchCalibrationForInputDevice(inputDeviceDescriptor, surfaceRotation, calibration); |
| 885 |
} catch (RemoteException ex) { |
| 886 |
throw ex.rethrowFromSystemServer(); |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
public int getPointerSpeed(Context context) { |
| 904 |
int speed = DEFAULT_POINTER_SPEED; |
| 905 |
try { |
| 906 |
speed = Settings.System.getInt(context.getContentResolver(), |
| 907 |
Settings.System.POINTER_SPEED); |
| 908 |
} catch (SettingNotFoundException snfe) { |
| 909 |
} |
| 910 |
return speed; |
| 911 |
} |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
public void setPointerSpeed(Context context, int speed) { |
| 926 |
if (speed < MIN_POINTER_SPEED || speed > MAX_POINTER_SPEED) { |
| 927 |
throw new IllegalArgumentException("speed out of range"); |
| 928 |
} |
| 929 |
|
| 930 |
Settings.System.putInt(context.getContentResolver(), |
| 931 |
Settings.System.POINTER_SPEED, speed); |
| 932 |
} |
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
public void tryPointerSpeed(int speed) { |
| 946 |
if (speed < MIN_POINTER_SPEED || speed > MAX_POINTER_SPEED) { |
| 947 |
throw new IllegalArgumentException("speed out of range"); |
| 948 |
} |
| 949 |
|
| 950 |
try { |
| 951 |
mIm.tryPointerSpeed(speed); |
| 952 |
} catch (RemoteException ex) { |
| 953 |
throw ex.rethrowFromSystemServer(); |
| 954 |
} |
| 955 |
} |
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
@FloatRange(from = 0, to = 1) |
| 970 |
public float getMaximumObscuringOpacityForTouch() { |
| 971 |
Context context = ActivityThread.currentApplication(); |
| 972 |
return Settings.Global.getFloat(context.getContentResolver(), |
| 973 |
Settings.Global.MAXIMUM_OBSCURING_OPACITY_FOR_TOUCH, |
| 974 |
DEFAULT_MAXIMUM_OBSCURING_OPACITY_FOR_TOUCH); |
| 975 |
} |
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
@TestApi |
| 1002 |
@RequiresPermission(Manifest.permission.WRITE_SECURE_SETTINGS) |
| 1003 |
public void setMaximumObscuringOpacityForTouch(@FloatRange(from = 0, to = 1) float opacity) { |
| 1004 |
if (opacity < 0 || opacity > 1) { |
| 1005 |
throw new IllegalArgumentException( |
| 1006 |
"Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 1007 |
} |
| 1008 |
Context context = ActivityThread.currentApplication(); |
| 1009 |
Settings.Global.putFloat(context.getContentResolver(), |
| 1010 |
Settings.Global.MAXIMUM_OBSCURING_OPACITY_FOR_TOUCH, opacity); |
| 1011 |
} |
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
@TestApi |
| 1024 |
@BlockUntrustedTouchesMode |
| 1025 |
public int getBlockUntrustedTouchesMode(@NonNull Context context) { |
| 1026 |
int mode = Settings.Global.getInt(context.getContentResolver(), |
| 1027 |
Settings.Global.BLOCK_UNTRUSTED_TOUCHES_MODE, DEFAULT_BLOCK_UNTRUSTED_TOUCHES_MODE); |
| 1028 |
if (!ArrayUtils.contains(BLOCK_UNTRUSTED_TOUCHES_MODES, mode)) { |
| 1029 |
Log.w(TAG, "Unknown block untrusted touches feature mode " + mode + ", using " |
| 1030 |
+ "default " + DEFAULT_BLOCK_UNTRUSTED_TOUCHES_MODE); |
| 1031 |
return DEFAULT_BLOCK_UNTRUSTED_TOUCHES_MODE; |
| 1032 |
} |
| 1033 |
return mode; |
| 1034 |
} |
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
@TestApi |
| 1047 |
@RequiresPermission(Manifest.permission.WRITE_SECURE_SETTINGS) |
| 1048 |
public void setBlockUntrustedTouchesMode(@NonNull Context context, |
| 1049 |
@BlockUntrustedTouchesMode int mode) { |
| 1050 |
if (!ArrayUtils.contains(BLOCK_UNTRUSTED_TOUCHES_MODES, mode)) { |
| 1051 |
throw new IllegalArgumentException("Invalid feature mode " + mode); |
| 1052 |
} |
| 1053 |
Settings.Global.putInt(context.getContentResolver(), |
| 1054 |
Settings.Global.BLOCK_UNTRUSTED_TOUCHES_MODE, mode); |
| 1055 |
} |
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
public boolean[] deviceHasKeys(int[] keyCodes) { |
| 1069 |
return deviceHasKeys(-1, keyCodes); |
| 1070 |
} |
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
public boolean[] deviceHasKeys(int id, int[] keyCodes) { |
| 1085 |
boolean[] ret = new boolean[keyCodes.length]; |
| 1086 |
try { |
| 1087 |
mIm.hasKeys(id, InputDevice.SOURCE_ANY, keyCodes, ret); |
| 1088 |
} catch (RemoteException e) { |
| 1089 |
throw e.rethrowFromSystemServer(); |
| 1090 |
} |
| 1091 |
return ret; |
| 1092 |
} |
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
public int getKeyCodeForKeyLocation(int deviceId, int locationKeyCode) { |
| 1116 |
try { |
| 1117 |
return mIm.getKeyCodeForKeyLocation(deviceId, locationKeyCode); |
| 1118 |
} catch (RemoteException e) { |
| 1119 |
throw e.rethrowFromSystemServer(); |
| 1120 |
} |
| 1121 |
} |
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
@RequiresPermission(Manifest.permission.INJECT_EVENTS) |
| 1152 |
public boolean injectInputEvent(InputEvent event, int mode, int targetUid) { |
| 1153 |
if (event == null) { |
| 1154 |
throw new IllegalArgumentException("event must not be null"); |
| 1155 |
} |
| 1156 |
if (mode != InputEventInjectionSync.NONE |
| 1157 |
&& mode != InputEventInjectionSync.WAIT_FOR_FINISHED |
| 1158 |
&& mode != InputEventInjectionSync.WAIT_FOR_RESULT) { |
| 1159 |
throw new IllegalArgumentException("mode is invalid"); |
| 1160 |
} |
| 1161 |
|
| 1162 |
try { |
| 1163 |
return mIm.injectInputEventToTarget(event, mode, targetUid); |
| 1164 |
} catch (RemoteException ex) { |
| 1165 |
throw ex.rethrowFromSystemServer(); |
| 1166 |
} |
| 1167 |
} |
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
@RequiresPermission(Manifest.permission.INJECT_EVENTS) |
| 1190 |
@UnsupportedAppUsage |
| 1191 |
public boolean injectInputEvent(InputEvent event, int mode) { |
| 1192 |
return injectInputEvent(event, mode, Process.INVALID_UID); |
| 1193 |
} |
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
public @Nullable VerifiedInputEvent verifyInputEvent(@NonNull InputEvent event) { |
| 1209 |
try { |
| 1210 |
return mIm.verifyInputEvent(event); |
| 1211 |
} catch (RemoteException ex) { |
| 1212 |
throw ex.rethrowFromSystemServer(); |
| 1213 |
} |
| 1214 |
} |
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
@UnsupportedAppUsage |
| 1225 |
public void setPointerIconType(int iconId) { |
| 1226 |
try { |
| 1227 |
mIm.setPointerIconType(iconId); |
| 1228 |
} catch (RemoteException ex) { |
| 1229 |
throw ex.rethrowFromSystemServer(); |
| 1230 |
} |
| 1231 |
} |
| 1232 |
|
| 1233 |
|
| 1234 |
public void setCustomPointerIcon(PointerIcon icon) { |
| 1235 |
try { |
| 1236 |
mIm.setCustomPointerIcon(icon); |
| 1237 |
} catch (RemoteException ex) { |
| 1238 |
throw ex.rethrowFromSystemServer(); |
| 1239 |
} |
| 1240 |
} |
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
public void requestPointerCapture(IBinder windowToken, boolean enable) { |
| 1254 |
try { |
| 1255 |
mIm.requestPointerCapture(windowToken, enable); |
| 1256 |
} catch (RemoteException ex) { |
| 1257 |
throw ex.rethrowFromSystemServer(); |
| 1258 |
} |
| 1259 |
} |
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
public InputMonitor monitorGestureInput(String name, int displayId) { |
| 1267 |
try { |
| 1268 |
return mIm.monitorGestureInput(new Binder(), name, displayId); |
| 1269 |
} catch (RemoteException ex) { |
| 1270 |
throw ex.rethrowFromSystemServer(); |
| 1271 |
} |
| 1272 |
} |
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
public InputSensorInfo[] getSensorList(int deviceId) { |
| 1280 |
try { |
| 1281 |
return mIm.getSensorList(deviceId); |
| 1282 |
} catch (RemoteException ex) { |
| 1283 |
throw ex.rethrowFromSystemServer(); |
| 1284 |
} |
| 1285 |
} |
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
public boolean enableSensor(int deviceId, int sensorType, int samplingPeriodUs, |
| 1293 |
int maxBatchReportLatencyUs) { |
| 1294 |
try { |
| 1295 |
return mIm.enableSensor(deviceId, sensorType, samplingPeriodUs, |
| 1296 |
maxBatchReportLatencyUs); |
| 1297 |
} catch (RemoteException ex) { |
| 1298 |
throw ex.rethrowFromSystemServer(); |
| 1299 |
} |
| 1300 |
} |
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
public void disableSensor(int deviceId, int sensorType) { |
| 1308 |
try { |
| 1309 |
mIm.disableSensor(deviceId, sensorType); |
| 1310 |
} catch (RemoteException ex) { |
| 1311 |
throw ex.rethrowFromSystemServer(); |
| 1312 |
} |
| 1313 |
} |
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
public boolean flushSensor(int deviceId, int sensorType) { |
| 1321 |
try { |
| 1322 |
return mIm.flushSensor(deviceId, sensorType); |
| 1323 |
} catch (RemoteException ex) { |
| 1324 |
throw ex.rethrowFromSystemServer(); |
| 1325 |
} |
| 1326 |
} |
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
public boolean registerSensorListener(IInputSensorEventListener listener) { |
| 1334 |
try { |
| 1335 |
return mIm.registerSensorListener(listener); |
| 1336 |
} catch (RemoteException ex) { |
| 1337 |
throw ex.rethrowFromSystemServer(); |
| 1338 |
} |
| 1339 |
} |
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
public void unregisterSensorListener(IInputSensorEventListener listener) { |
| 1347 |
try { |
| 1348 |
mIm.unregisterSensorListener(listener); |
| 1349 |
} catch (RemoteException ex) { |
| 1350 |
throw ex.rethrowFromSystemServer(); |
| 1351 |
} |
| 1352 |
} |
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
public int getBatteryStatus(int deviceId) { |
| 1360 |
try { |
| 1361 |
return mIm.getBatteryStatus(deviceId); |
| 1362 |
} catch (RemoteException ex) { |
| 1363 |
throw ex.rethrowFromSystemServer(); |
| 1364 |
} |
| 1365 |
} |
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
public int getBatteryCapacity(int deviceId) { |
| 1373 |
try { |
| 1374 |
return mIm.getBatteryCapacity(deviceId); |
| 1375 |
} catch (RemoteException ex) { |
| 1376 |
throw ex.rethrowFromSystemServer(); |
| 1377 |
} |
| 1378 |
} |
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
public void addPortAssociation(@NonNull String inputPort, int displayPort) { |
| 1391 |
try { |
| 1392 |
mIm.addPortAssociation(inputPort, displayPort); |
| 1393 |
} catch (RemoteException ex) { |
| 1394 |
throw ex.rethrowFromSystemServer(); |
| 1395 |
} |
| 1396 |
} |
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
public void removePortAssociation(@NonNull String inputPort) { |
| 1408 |
try { |
| 1409 |
mIm.removePortAssociation(inputPort); |
| 1410 |
} catch (RemoteException ex) { |
| 1411 |
throw ex.rethrowFromSystemServer(); |
| 1412 |
} |
| 1413 |
} |
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
public void addUniqueIdAssociation(@NonNull String inputPort, @NonNull String displayUniqueId) { |
| 1426 |
try { |
| 1427 |
mIm.addUniqueIdAssociation(inputPort, displayUniqueId); |
| 1428 |
} catch (RemoteException e) { |
| 1429 |
throw e.rethrowFromSystemServer(); |
| 1430 |
} |
| 1431 |
} |
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
public void removeUniqueIdAssociation(@NonNull String inputPort) { |
| 1442 |
try { |
| 1443 |
mIm.removeUniqueIdAssociation(inputPort); |
| 1444 |
} catch (RemoteException e) { |
| 1445 |
throw e.rethrowFromSystemServer(); |
| 1446 |
} |
| 1447 |
} |
| 1448 |
|
| 1449 |
private void populateInputDevicesLocked() { |
| 1450 |
if (mInputDevicesChangedListener == null) { |
| 1451 |
final InputDevicesChangedListener listener = new InputDevicesChangedListener(); |
| 1452 |
try { |
| 1453 |
mIm.registerInputDevicesChangedListener(listener); |
| 1454 |
} catch (RemoteException ex) { |
| 1455 |
throw ex.rethrowFromSystemServer(); |
| 1456 |
} |
| 1457 |
mInputDevicesChangedListener = listener; |
| 1458 |
} |
| 1459 |
|
| 1460 |
if (mInputDevices == null) { |
| 1461 |
final int[] ids; |
| 1462 |
try { |
| 1463 |
ids = mIm.getInputDeviceIds(); |
| 1464 |
} catch (RemoteException ex) { |
| 1465 |
throw ex.rethrowFromSystemServer(); |
| 1466 |
} |
| 1467 |
|
| 1468 |
mInputDevices = new SparseArray<InputDevice>(); |
| 1469 |
|
| 1470 |
if (ids != null) { |
| 1471 |
for (int i = 0; i < ids.length; i++) { |
| 1472 |
mInputDevices.put(ids[i], null); |
| 1473 |
} |
| 1474 |
} |
| 1475 |
} |
| 1476 |
} |
| 1477 |
|
| 1478 |
private void onInputDevicesChanged(int[] deviceIdAndGeneration) { |
| 1479 |
if (DEBUG) { |
| 1480 |
Log.d(TAG, "Received input devices changed."); |
| 1481 |
} |
| 1482 |
|
| 1483 |
synchronized (mInputDevicesLock) { |
| 1484 |
for (int i = mInputDevices.size(); --i > 0; ) { |
| 1485 |
final int deviceId = mInputDevices.keyAt(i); |
| 1486 |
if (!containsDeviceId(deviceIdAndGeneration, deviceId)) { |
| 1487 |
if (DEBUG) { |
| 1488 |
Log.d(TAG, "Device removed: " + deviceId); |
| 1489 |
} |
| 1490 |
mInputDevices.removeAt(i); |
| 1491 |
sendMessageToInputDeviceListenersLocked(MSG_DEVICE_REMOVED, deviceId); |
| 1492 |
} |
| 1493 |
} |
| 1494 |
|
| 1495 |
for (int i = 0; i < deviceIdAndGeneration.length; i += 2) { |
| 1496 |
final int deviceId = deviceIdAndGeneration[i]; |
| 1497 |
int index = mInputDevices.indexOfKey(deviceId); |
| 1498 |
if (index >= 0) { |
| 1499 |
final InputDevice device = mInputDevices.valueAt(index); |
| 1500 |
if (device != null) { |
| 1501 |
final int generation = deviceIdAndGeneration[i + 1]; |
| 1502 |
if (device.getGeneration() != generation) { |
| 1503 |
if (DEBUG) { |
| 1504 |
Log.d(TAG, "Device changed: " + deviceId); |
| 1505 |
} |
| 1506 |
mInputDevices.setValueAt(index, null); |
| 1507 |
sendMessageToInputDeviceListenersLocked(MSG_DEVICE_CHANGED, deviceId); |
| 1508 |
} |
| 1509 |
} |
| 1510 |
} else { |
| 1511 |
if (DEBUG) { |
| 1512 |
Log.d(TAG, "Device added: " + deviceId); |
| 1513 |
} |
| 1514 |
mInputDevices.put(deviceId, null); |
| 1515 |
sendMessageToInputDeviceListenersLocked(MSG_DEVICE_ADDED, deviceId); |
| 1516 |
} |
| 1517 |
} |
| 1518 |
} |
| 1519 |
} |
| 1520 |
|
| 1521 |
private void sendMessageToInputDeviceListenersLocked(int what, int deviceId) { |
| 1522 |
final int numListeners = mInputDeviceListeners.size(); |
| 1523 |
for (int i = 0; i < numListeners; i++) { |
| 1524 |
InputDeviceListenerDelegate listener = mInputDeviceListeners.get(i); |
| 1525 |
listener.sendMessage(listener.obtainMessage(what, deviceId, 0)); |
| 1526 |
} |
| 1527 |
} |
| 1528 |
|
| 1529 |
private static boolean containsDeviceId(int[] deviceIdAndGeneration, int deviceId) { |
| 1530 |
for (int i = 0; i < deviceIdAndGeneration.length; i += 2) { |
| 1531 |
if (deviceIdAndGeneration[i] == deviceId) { |
| 1532 |
return true; |
| 1533 |
} |
| 1534 |
} |
| 1535 |
return false; |
| 1536 |
} |
| 1537 |
|
| 1538 |
|
| 1539 |
private void onTabletModeChanged(long whenNanos, boolean inTabletMode) { |
| 1540 |
if (DEBUG) { |
| 1541 |
Log.d(TAG, "Received tablet mode changed: " |
| 1542 |
+ "whenNanos=" + whenNanos + ", inTabletMode=" + inTabletMode); |
| 1543 |
} |
| 1544 |
synchronized (mTabletModeLock) { |
| 1545 |
final int N = mOnTabletModeChangedListeners.size(); |
| 1546 |
for (int i = 0; i < N; i++) { |
| 1547 |
OnTabletModeChangedListenerDelegate listener = |
| 1548 |
mOnTabletModeChangedListeners.get(i); |
| 1549 |
listener.sendTabletModeChanged(whenNanos, inTabletMode); |
| 1550 |
} |
| 1551 |
} |
| 1552 |
} |
| 1553 |
|
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
public Vibrator getInputDeviceVibrator(int deviceId, int vibratorId) { |
| 1560 |
return new InputDeviceVibrator(this, deviceId, vibratorId); |
| 1561 |
} |
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
@NonNull |
| 1570 |
public VibratorManager getInputDeviceVibratorManager(int deviceId) { |
| 1571 |
return new InputDeviceVibratorManager(InputManager.this, deviceId); |
| 1572 |
} |
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
int[] getVibratorIds(int deviceId) { |
| 1579 |
try { |
| 1580 |
return mIm.getVibratorIds(deviceId); |
| 1581 |
} catch (RemoteException ex) { |
| 1582 |
throw ex.rethrowFromSystemServer(); |
| 1583 |
} |
| 1584 |
} |
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
void vibrate(int deviceId, VibrationEffect effect, IBinder token) { |
| 1590 |
try { |
| 1591 |
mIm.vibrate(deviceId, effect, token); |
| 1592 |
} catch (RemoteException ex) { |
| 1593 |
throw ex.rethrowFromSystemServer(); |
| 1594 |
} |
| 1595 |
} |
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
void vibrate(int deviceId, CombinedVibration effect, IBinder token) { |
| 1601 |
try { |
| 1602 |
mIm.vibrateCombined(deviceId, effect, token); |
| 1603 |
} catch (RemoteException ex) { |
| 1604 |
throw ex.rethrowFromSystemServer(); |
| 1605 |
} |
| 1606 |
} |
| 1607 |
|
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
void cancelVibrate(int deviceId, IBinder token) { |
| 1612 |
try { |
| 1613 |
mIm.cancelVibrate(deviceId, token); |
| 1614 |
} catch (RemoteException ex) { |
| 1615 |
throw ex.rethrowFromSystemServer(); |
| 1616 |
} |
| 1617 |
} |
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
boolean isVibrating(int deviceId) { |
| 1623 |
try { |
| 1624 |
return mIm.isVibrating(deviceId); |
| 1625 |
} catch (RemoteException ex) { |
| 1626 |
throw ex.rethrowFromSystemServer(); |
| 1627 |
} |
| 1628 |
} |
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
boolean registerVibratorStateListener(int deviceId, IVibratorStateListener listener) { |
| 1634 |
try { |
| 1635 |
return mIm.registerVibratorStateListener(deviceId, listener); |
| 1636 |
} catch (RemoteException ex) { |
| 1637 |
throw ex.rethrowFromSystemServer(); |
| 1638 |
} |
| 1639 |
} |
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
boolean unregisterVibratorStateListener(int deviceId, IVibratorStateListener listener) { |
| 1645 |
try { |
| 1646 |
return mIm.unregisterVibratorStateListener(deviceId, listener); |
| 1647 |
} catch (RemoteException ex) { |
| 1648 |
throw ex.rethrowFromSystemServer(); |
| 1649 |
} |
| 1650 |
} |
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 1657 |
@NonNull |
| 1658 |
public SensorManager getInputDeviceSensorManager(int deviceId) { |
| 1659 |
if (mInputDeviceSensorManager == null) { |
| 1660 |
mInputDeviceSensorManager = new InputDeviceSensorManager(this); |
| 1661 |
} |
| 1662 |
return mInputDeviceSensorManager.getSensorManager(deviceId); |
| 1663 |
} |
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
public InputDeviceBatteryState getInputDeviceBatteryState(int deviceId, boolean hasBattery) { |
| 1671 |
return new InputDeviceBatteryState(this, deviceId, hasBattery); |
| 1672 |
} |
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
@NonNull |
| 1680 |
public LightsManager getInputDeviceLightsManager(int deviceId) { |
| 1681 |
return new InputDeviceLightsManager(InputManager.this, deviceId); |
| 1682 |
} |
| 1683 |
|
| 1684 |
|
| 1685 |
|
| 1686 |
|
| 1687 |
|
| 1688 |
@NonNull List<Light> getLights(int deviceId) { |
| 1689 |
try { |
| 1690 |
return mIm.getLights(deviceId); |
| 1691 |
} catch (RemoteException e) { |
| 1692 |
throw e.rethrowFromSystemServer(); |
| 1693 |
} |
| 1694 |
} |
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
@NonNull LightState getLightState(int deviceId, @NonNull Light light) { |
| 1701 |
try { |
| 1702 |
return mIm.getLightState(deviceId, light.getId()); |
| 1703 |
} catch (RemoteException e) { |
| 1704 |
throw e.rethrowFromSystemServer(); |
| 1705 |
} |
| 1706 |
} |
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
void requestLights(int deviceId, @NonNull LightsRequest request, IBinder token) { |
| 1714 |
try { |
| 1715 |
List<Integer> lightIdList = request.getLights(); |
| 1716 |
int[] lightIds = new int[lightIdList.size()]; |
| 1717 |
for (int i = 0; i < lightIds.length; i++) { |
| 1718 |
lightIds[i] = lightIdList.get(i); |
| 1719 |
} |
| 1720 |
List<LightState> lightStateList = request.getLightStates(); |
| 1721 |
mIm.setLightStates(deviceId, lightIds, |
| 1722 |
lightStateList.toArray(new LightState[lightStateList.size()]), |
| 1723 |
token); |
| 1724 |
} catch (RemoteException e) { |
| 1725 |
throw e.rethrowFromSystemServer(); |
| 1726 |
} |
| 1727 |
} |
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
void openLightSession(int deviceId, String opPkg, @NonNull IBinder token) { |
| 1735 |
try { |
| 1736 |
mIm.openLightSession(deviceId, opPkg, token); |
| 1737 |
} catch (RemoteException e) { |
| 1738 |
throw e.rethrowFromSystemServer(); |
| 1739 |
} |
| 1740 |
} |
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
void closeLightSession(int deviceId, @NonNull IBinder token) { |
| 1747 |
try { |
| 1748 |
mIm.closeLightSession(deviceId, token); |
| 1749 |
} catch (RemoteException e) { |
| 1750 |
throw e.rethrowFromSystemServer(); |
| 1751 |
} |
| 1752 |
} |
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
public void cancelCurrentTouch() { |
| 1759 |
try { |
| 1760 |
mIm.cancelCurrentTouch(); |
| 1761 |
} catch (RemoteException e) { |
| 1762 |
throw e.rethrowFromSystemServer(); |
| 1763 |
} |
| 1764 |
} |
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
public void addInputDeviceBatteryListener(int deviceId, @NonNull Executor executor, |
| 1778 |
@NonNull InputDeviceBatteryListener listener) { |
| 1779 |
Objects.requireNonNull(executor, "executor should not be null"); |
| 1780 |
Objects.requireNonNull(listener, "listener should not be null"); |
| 1781 |
|
| 1782 |
synchronized (mBatteryListenersLock) { |
| 1783 |
if (mBatteryListeners == null) { |
| 1784 |
mBatteryListeners = new SparseArray<>(); |
| 1785 |
mInputDeviceBatteryListener = new LocalInputDeviceBatteryListener(); |
| 1786 |
} |
| 1787 |
RegisteredBatteryListeners listenersForDevice = mBatteryListeners.get(deviceId); |
| 1788 |
if (listenersForDevice == null) { |
| 1789 |
|
| 1790 |
|
| 1791 |
listenersForDevice = new RegisteredBatteryListeners(); |
| 1792 |
mBatteryListeners.put(deviceId, listenersForDevice); |
| 1793 |
try { |
| 1794 |
mIm.registerBatteryListener(deviceId, mInputDeviceBatteryListener); |
| 1795 |
} catch (RemoteException e) { |
| 1796 |
throw e.rethrowFromSystemServer(); |
| 1797 |
} |
| 1798 |
} else { |
| 1799 |
|
| 1800 |
|
| 1801 |
for (InputDeviceBatteryListenerDelegate delegate : listenersForDevice.mDelegates) { |
| 1802 |
if (Objects.equals(listener, delegate.mListener)) { |
| 1803 |
throw new IllegalArgumentException( |
| 1804 |
"Attempting to register an InputDeviceBatteryListener that has " |
| 1805 |
+ "already been registered for deviceId: " |
| 1806 |
+ deviceId); |
| 1807 |
} |
| 1808 |
} |
| 1809 |
} |
| 1810 |
final InputDeviceBatteryListenerDelegate delegate = |
| 1811 |
new InputDeviceBatteryListenerDelegate(listener, executor); |
| 1812 |
listenersForDevice.mDelegates.add(delegate); |
| 1813 |
|
| 1814 |
|
| 1815 |
if (listenersForDevice.mLatestBatteryState != null) { |
| 1816 |
delegate.notifyBatteryStateChanged(listenersForDevice.mLatestBatteryState); |
| 1817 |
} |
| 1818 |
} |
| 1819 |
} |
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
|
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
|
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
|
| 1838 |
|
| 1839 |
|
| 1840 |
@RequiresPermission(Manifest.permission.MONITOR_INPUT) |
| 1841 |
public void pilferPointers(IBinder inputChannelToken) { |
| 1842 |
try { |
| 1843 |
mIm.pilferPointers(inputChannelToken); |
| 1844 |
} catch (RemoteException e) { |
| 1845 |
throw e.rethrowFromSystemServer(); |
| 1846 |
} |
| 1847 |
} |
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
public void removeInputDeviceBatteryListener(int deviceId, |
| 1855 |
@NonNull InputDeviceBatteryListener listener) { |
| 1856 |
Objects.requireNonNull(listener, "listener should not be null"); |
| 1857 |
|
| 1858 |
synchronized (mBatteryListenersLock) { |
| 1859 |
if (mBatteryListeners == null) { |
| 1860 |
return; |
| 1861 |
} |
| 1862 |
RegisteredBatteryListeners listenersForDevice = mBatteryListeners.get(deviceId); |
| 1863 |
if (listenersForDevice == null) { |
| 1864 |
|
| 1865 |
return; |
| 1866 |
} |
| 1867 |
final List<InputDeviceBatteryListenerDelegate> delegates = |
| 1868 |
listenersForDevice.mDelegates; |
| 1869 |
for (int i = 0; i < delegates.size();) { |
| 1870 |
if (Objects.equals(listener, delegates.get(i).mListener)) { |
| 1871 |
delegates.remove(i); |
| 1872 |
continue; |
| 1873 |
} |
| 1874 |
i++; |
| 1875 |
} |
| 1876 |
if (!delegates.isEmpty()) { |
| 1877 |
return; |
| 1878 |
} |
| 1879 |
|
| 1880 |
|
| 1881 |
mBatteryListeners.remove(deviceId); |
| 1882 |
try { |
| 1883 |
mIm.unregisterBatteryListener(deviceId, mInputDeviceBatteryListener); |
| 1884 |
} catch (RemoteException e) { |
| 1885 |
throw e.rethrowFromSystemServer(); |
| 1886 |
} |
| 1887 |
if (mBatteryListeners.size() == 0) { |
| 1888 |
|
| 1889 |
|
| 1890 |
mBatteryListeners = null; |
| 1891 |
mInputDeviceBatteryListener = null; |
| 1892 |
} |
| 1893 |
} |
| 1894 |
} |
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
public boolean isStylusEverUsed(@NonNull Context context) { |
| 1901 |
return Settings.Global.getInt(context.getContentResolver(), |
| 1902 |
Settings.Global.STYLUS_EVER_USED, 0) == 1; |
| 1903 |
} |
| 1904 |
|
| 1905 |
|
| 1906 |
|
| 1907 |
|
| 1908 |
|
| 1909 |
|
| 1910 |
@RequiresPermission(Manifest.permission.WRITE_SECURE_SETTINGS) |
| 1911 |
public void setStylusEverUsed(@NonNull Context context, boolean stylusEverUsed) { |
| 1912 |
Settings.Global.putInt(context.getContentResolver(), |
| 1913 |
Settings.Global.STYLUS_EVER_USED, stylusEverUsed ? 1 : 0); |
| 1914 |
} |
| 1915 |
|
| 1916 |
|
| 1917 |
|
| 1918 |
|
| 1919 |
|
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
public interface InputDeviceBatteryListener { |
| 1926 |
|
| 1927 |
|
| 1928 |
|
| 1929 |
|
| 1930 |
|
| 1931 |
|
| 1932 |
|
| 1933 |
void onBatteryStateChanged( |
| 1934 |
int deviceId, long eventTimeMillis, @NonNull BatteryState batteryState); |
| 1935 |
} |
| 1936 |
|
| 1937 |
|
| 1938 |
|
| 1939 |
|
| 1940 |
public interface InputDeviceListener { |
| 1941 |
|
| 1942 |
|
| 1943 |
|
| 1944 |
|
| 1945 |
|
| 1946 |
|
| 1947 |
void onInputDeviceAdded(int deviceId); |
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
void onInputDeviceRemoved(int deviceId); |
| 1955 |
|
| 1956 |
|
| 1957 |
|
| 1958 |
|
| 1959 |
|
| 1960 |
|
| 1961 |
|
| 1962 |
|
| 1963 |
void onInputDeviceChanged(int deviceId); |
| 1964 |
} |
| 1965 |
|
| 1966 |
private final class InputDevicesChangedListener extends IInputDevicesChangedListener.Stub { |
| 1967 |
@Override |
| 1968 |
public void onInputDevicesChanged(int[] deviceIdAndGeneration) throws RemoteException { |
| 1969 |
InputManager.this.onInputDevicesChanged(deviceIdAndGeneration); |
| 1970 |
} |
| 1971 |
} |
| 1972 |
|
| 1973 |
private static final class InputDeviceListenerDelegate extends Handler { |
| 1974 |
public final InputDeviceListener mListener; |
| 1975 |
|
| 1976 |
public InputDeviceListenerDelegate(InputDeviceListener listener, Handler handler) { |
| 1977 |
super(handler != null ? handler.getLooper() : Looper.myLooper()); |
| 1978 |
mListener = listener; |
| 1979 |
} |
| 1980 |
|
| 1981 |
@Override |
| 1982 |
public void handleMessage(Message msg) { |
| 1983 |
switch (msg.what) { |
| 1984 |
case MSG_DEVICE_ADDED: |
| 1985 |
mListener.onInputDeviceAdded(msg.arg1); |
| 1986 |
break; |
| 1987 |
case MSG_DEVICE_REMOVED: |
| 1988 |
mListener.onInputDeviceRemoved(msg.arg1); |
| 1989 |
break; |
| 1990 |
case MSG_DEVICE_CHANGED: |
| 1991 |
mListener.onInputDeviceChanged(msg.arg1); |
| 1992 |
break; |
| 1993 |
} |
| 1994 |
} |
| 1995 |
} |
| 1996 |
|
| 1997 |
|
| 1998 |
public interface OnTabletModeChangedListener { |
| 1999 |
|
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
|
| 2004 |
|
| 2005 |
|
| 2006 |
void onTabletModeChanged(long whenNanos, boolean inTabletMode); |
| 2007 |
} |
| 2008 |
|
| 2009 |
private final class TabletModeChangedListener extends ITabletModeChangedListener.Stub { |
| 2010 |
@Override |
| 2011 |
public void onTabletModeChanged(long whenNanos, boolean inTabletMode) { |
| 2012 |
InputManager.this.onTabletModeChanged(whenNanos, inTabletMode); |
| 2013 |
} |
| 2014 |
} |
| 2015 |
|
| 2016 |
private static final class OnTabletModeChangedListenerDelegate extends Handler { |
| 2017 |
private static final int MSG_TABLET_MODE_CHANGED = 0; |
| 2018 |
|
| 2019 |
public final OnTabletModeChangedListener mListener; |
| 2020 |
|
| 2021 |
public OnTabletModeChangedListenerDelegate( |
| 2022 |
OnTabletModeChangedListener listener, Handler handler) { |
| 2023 |
super(handler != null ? handler.getLooper() : Looper.myLooper()); |
| 2024 |
mListener = listener; |
| 2025 |
} |
| 2026 |
|
| 2027 |
public void sendTabletModeChanged(long whenNanos, boolean inTabletMode) { |
| 2028 |
SomeArgs args = SomeArgs.obtain(); |
| 2029 |
args.argi1 = (int) (whenNanos & 0xFFFFFFFF); |
| 2030 |
args.argi2 = (int) (whenNanos >> 32); |
| 2031 |
args.arg1 = (Boolean) inTabletMode; |
| 2032 |
obtainMessage(MSG_TABLET_MODE_CHANGED, args).sendToTarget(); |
| 2033 |
} |
| 2034 |
|
| 2035 |
@Override |
| 2036 |
public void handleMessage(Message msg) { |
| 2037 |
switch (msg.what) { |
| 2038 |
case MSG_TABLET_MODE_CHANGED: |
| 2039 |
SomeArgs args = (SomeArgs) msg.obj; |
| 2040 |
long whenNanos = (args.argi1 & 0xFFFFFFFFl) | ((long) args.argi2 << 32); |
| 2041 |
boolean inTabletMode = (boolean) args.arg1; |
| 2042 |
mListener.onTabletModeChanged(whenNanos, inTabletMode); |
| 2043 |
break; |
| 2044 |
} |
| 2045 |
} |
| 2046 |
} |
| 2047 |
|
| 2048 |
private static final class LocalBatteryState extends BatteryState { |
| 2049 |
final int mDeviceId; |
| 2050 |
final boolean mIsPresent; |
| 2051 |
final int mStatus; |
| 2052 |
final float mCapacity; |
| 2053 |
final long mEventTime; |
| 2054 |
|
| 2055 |
LocalBatteryState(int deviceId, boolean isPresent, int status, float capacity, |
| 2056 |
long eventTime) { |
| 2057 |
mDeviceId = deviceId; |
| 2058 |
mIsPresent = isPresent; |
| 2059 |
mStatus = status; |
| 2060 |
mCapacity = capacity; |
| 2061 |
mEventTime = eventTime; |
| 2062 |
} |
| 2063 |
|
| 2064 |
@Override |
| 2065 |
public boolean isPresent() { |
| 2066 |
return mIsPresent; |
| 2067 |
} |
| 2068 |
|
| 2069 |
@Override |
| 2070 |
public int getStatus() { |
| 2071 |
return mStatus; |
| 2072 |
} |
| 2073 |
|
| 2074 |
@Override |
| 2075 |
public float getCapacity() { |
| 2076 |
return mCapacity; |
| 2077 |
} |
| 2078 |
} |
| 2079 |
|
| 2080 |
private static final class RegisteredBatteryListeners { |
| 2081 |
final List<InputDeviceBatteryListenerDelegate> mDelegates = new ArrayList<>(); |
| 2082 |
LocalBatteryState mLatestBatteryState; |
| 2083 |
} |
| 2084 |
|
| 2085 |
private static final class InputDeviceBatteryListenerDelegate { |
| 2086 |
final InputDeviceBatteryListener mListener; |
| 2087 |
final Executor mExecutor; |
| 2088 |
|
| 2089 |
InputDeviceBatteryListenerDelegate(InputDeviceBatteryListener listener, Executor executor) { |
| 2090 |
mListener = listener; |
| 2091 |
mExecutor = executor; |
| 2092 |
} |
| 2093 |
|
| 2094 |
void notifyBatteryStateChanged(LocalBatteryState batteryState) { |
| 2095 |
mExecutor.execute(() -> |
| 2096 |
mListener.onBatteryStateChanged(batteryState.mDeviceId, batteryState.mEventTime, |
| 2097 |
batteryState)); |
| 2098 |
} |
| 2099 |
} |
| 2100 |
|
| 2101 |
private class LocalInputDeviceBatteryListener extends IInputDeviceBatteryListener.Stub { |
| 2102 |
@Override |
| 2103 |
public void onBatteryStateChanged(int deviceId, boolean isBatteryPresent, int status, |
| 2104 |
float capacity, long eventTime) { |
| 2105 |
synchronized (mBatteryListenersLock) { |
| 2106 |
if (mBatteryListeners == null) return; |
| 2107 |
final RegisteredBatteryListeners entry = mBatteryListeners.get(deviceId); |
| 2108 |
if (entry == null) return; |
| 2109 |
|
| 2110 |
entry.mLatestBatteryState = |
| 2111 |
new LocalBatteryState( |
| 2112 |
deviceId, isBatteryPresent, status, capacity, eventTime); |
| 2113 |
for (InputDeviceBatteryListenerDelegate delegate : entry.mDelegates) { |
| 2114 |
delegate.notifyBatteryStateChanged(entry.mLatestBatteryState); |
| 2115 |
} |
| 2116 |
} |
| 2117 |
} |
| 2118 |
} |
| 2119 |
} |