pipa: enhance PenUtils with improved logging and reliability

- Add robust exception handling for device operations
- Implement consistent logging with timestamps
- Use system properties for debug flag configuration
- Standardize naming conventions for constants
- Improve documentation with JavaDoc comments
- Update copyright year to 2023-2025

Signed-off-by: Abdulwahab Isam <abdoi94.iq@gmail.com>
This commit is contained in:
Abdulwahab Isam
2025-03-24 05:50:27 +03:00
parent 54616309b8
commit d42cfa9e8d

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023 The LineageOS Project
* Copyright (C) 2023-2025 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -16,67 +16,151 @@ import android.view.InputDevice;
import android.preference.PreferenceManager;
import android.content.SharedPreferences;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* Utility class for handling Xiaomi pen operations and mode switching
*/
public class PenUtils {
private static final String TAG = "XiaomiPeripheralManagerPenUtils";
private static final boolean DEBUG = false;
private static final String TAG = "XiaomiPen";
private static boolean DEBUG = SystemProperties.getBoolean("persist.xiaomi.pen.debug", false);
private static final int penVendorId = 6421;
private static final int penProductId = 19841;
// Xiaomi pen identifiers
private static final int PEN_VENDOR_ID = 6421;
private static final int PEN_PRODUCT_ID = 19841;
private static InputManager mInputManager;
private static SharedPreferences mPreferences;
private static final String STYLUS_KEY = "stylus_switch_key";
private static SharedPreferences preferences;
/**
* Initialize the pen utilities and register for input device events
* @param context Application context
*/
public static void setup(Context context) {
mInputManager = (InputManager) context.getSystemService(Context.INPUT_SERVICE);
mInputManager.registerInputDeviceListener(mInputDeviceListener, null);
preferences = PreferenceManager.getDefaultSharedPreferences(context);
refreshPenMode();
}
public static void enablePenMode() {
Log.d(TAG, "enablePenMode: Enable Pen Mode");
SystemProperties.set("persist.vendor.parts.pen", "18");
}
public static void disablePenMode() {
Log.d(TAG, "disablePenMode: Disable Pen Mode");
SystemProperties.set("persist.vendor.parts.pen", "2");
}
private static void refreshPenMode() {
for (int id : mInputManager.getInputDeviceIds()) {
if (isDeviceXiaomiPen(id) || preferences.getBoolean(STYLUS_KEY, false)) {
if (DEBUG) Log.d(TAG, "refreshPenMode: Found Xiaomi Pen");
enablePenMode();
return;
}
logInfo("Initializing Xiaomi pen framework integration");
try {
mInputManager = (InputManager) context.getSystemService(Context.INPUT_SERVICE);
mInputManager.registerInputDeviceListener(mInputDeviceListener, null);
mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
refreshPenMode();
} catch (Exception e) {
logError("Error setting up pen utils: " + e.getMessage());
}
if (DEBUG) Log.d(TAG, "refreshPenMode: No Xiaomi Pen found");
disablePenMode();
}
/**
* Enable pen mode by setting system property
*/
public static void enablePenMode() {
logInfo("Enabling pen mode");
try {
SystemProperties.set("persist.vendor.parts.pen", "18");
} catch (Exception e) {
logError("Failed to enable pen mode: " + e.getMessage());
}
}
/**
* Disable pen mode by setting system property
*/
public static void disablePenMode() {
logInfo("Disabling pen mode");
try {
SystemProperties.set("persist.vendor.parts.pen", "2");
} catch (Exception e) {
logError("Failed to disable pen mode: " + e.getMessage());
}
}
/**
* Check for pen presence and update mode accordingly
*/
private static void refreshPenMode() {
try {
boolean penFound = false;
for (int id : mInputManager.getInputDeviceIds()) {
if (isDeviceXiaomiPen(id)) {
logDebug("Found Xiaomi Pen with id: " + id);
penFound = true;
break;
}
}
// Also check preference override
boolean preferenceEnabled = mPreferences.getBoolean(STYLUS_KEY, false);
if (penFound || preferenceEnabled) {
logInfo("Pen detected or enabled in preferences");
enablePenMode();
} else {
logInfo("No pen detected and not enabled in preferences");
disablePenMode();
}
} catch (Exception e) {
logError("Error refreshing pen mode: " + e.getMessage());
}
}
/**
* Check if an input device is the Xiaomi pen
* @param id Device ID to check
* @return true if the device is the Xiaomi pen
*/
private static boolean isDeviceXiaomiPen(int id) {
InputDevice inputDevice = mInputManager.getInputDevice(id);
return inputDevice.getVendorId() == penVendorId &&
inputDevice.getProductId() == penProductId;
try {
InputDevice inputDevice = mInputManager.getInputDevice(id);
if (inputDevice == null) return false;
return inputDevice.getVendorId() == PEN_VENDOR_ID &&
inputDevice.getProductId() == PEN_PRODUCT_ID;
} catch (Exception e) {
logError("Error checking pen device: " + e.getMessage());
return false;
}
}
/**
* Input device listener for pen connection/disconnection events
*/
private static InputDeviceListener mInputDeviceListener = new InputDeviceListener() {
@Override
public void onInputDeviceAdded(int id) {
refreshPenMode();
}
@Override
public void onInputDeviceRemoved(int id) {
refreshPenMode();
}
@Override
public void onInputDeviceChanged(int id) {
refreshPenMode();
}
};
@Override
public void onInputDeviceAdded(int id) {
logDebug("Input device added: " + id);
refreshPenMode();
}
@Override
public void onInputDeviceRemoved(int id) {
logDebug("Input device removed: " + id);
refreshPenMode();
}
@Override
public void onInputDeviceChanged(int id) {
logDebug("Input device changed: " + id);
refreshPenMode();
}
};
// Enhanced logging helpers to match other classes
private static void logDebug(String message) {
if (DEBUG) Log.d(TAG, getTimestamp() + message);
}
private static void logInfo(String message) {
Log.i(TAG, getTimestamp() + message);
}
private static void logError(String message) {
Log.e(TAG, getTimestamp() + message);
}
private static String getTimestamp() {
return "[" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US).format(new Date()) + "] ";
}
}