pipa: peripheralmanager: Allow recognizing third party styluses

Change-Id: I3d4ba746126e0cb8d273cd62feb7fce4487c6f3b
This commit is contained in:
luka177
2023-10-20 20:07:54 +02:00
committed by Abdulwahab Isam
parent d7dd8ca379
commit 2ff61846e5
8 changed files with 197 additions and 5 deletions

View File

@@ -6,13 +6,19 @@
android_app {
name: "XiaomiPeripheralManager",
defaults: [
"SettingsLibDefaults",
],
certificate: "platform",
srcs: ["src/**/*.java"],
platform_apis: true,
privileged: true,
system_ext_specific: true,
resource_dirs: ["res"],
static_libs: [
"androidx.core_core",
"androidx.preference_preference",
],
required: [
"xiaomi-pen"

View File

@@ -11,8 +11,8 @@
android:sharedUserId="android.uid.system">
<uses-sdk
android:minSdkVersion="31"
android:targetSdkVersion="31"/>
android:minSdkVersion="24"
android:targetSdkVersion="30"/>
<application
android:label="@string/app_name"
@@ -30,5 +30,18 @@
</intent-filter>
</receiver>
<activity
android:name=".StylusSettingsActivity"
android:label="@string/stylus_title"
android:theme="@style/Theme.SubSettingsBase">
<intent-filter>
<action android:name="com.android.settings.action.IA_SETTINGS" />
</intent-filter>
<meta-data android:name="com.android.settings.category"
android:value="com.android.settings.category.ia.connect" />
<meta-data android:name="com.android.settings.summary"
android:resource="@string/stylus_summary" />
</activity>
</application>
</manifest>

View File

@@ -7,4 +7,11 @@
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<!-- App Name -->
<string name="app_name">Xiaomi Peripheral Manager</string>
<!-- Xiaomi Stylus -->
<string name="stylus_title">Stylus</string>
<string name="stylus_summary">Stylus Settings</string>
<string name="stylus_switch_title">Force recognize stylus</string>
<string name="stylus_switch_summary">Enable this settings to allow using third party styluses</string>
</resources>

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2015-2016 The CyanogenMod Project
2017-2020 The LineageOS Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<style name="TextAppearance.Medium" parent="@android:style/TextAppearance.Material.Medium" />
</resources>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2018 The LineageOS Project
Licensed under the Apache License, Version 2.0 (the "License"
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/stylus_title">
<SwitchPreference
android:key="stylus_switch_key"
android:defaultValue="false"
android:title="@string/stylus_switch_title"
android:summary="@string/stylus_switch_summary"/>
</PreferenceScreen>

View File

@@ -13,6 +13,8 @@ import android.os.SystemProperties;
import android.util.Log;
import android.view.InputDevice;
import android.content.SharedPreferences;
public class PenUtils {
private static final String TAG = "XiaomiPeripheralManagerPenUtils";
@@ -23,25 +25,31 @@ public class PenUtils {
private static InputManager mInputManager;
public static final String SHARED_STYLUS = "shared_stylus_force";
private static SharedPreferences preferences;
public static void setup(Context context) {
mInputManager = (InputManager) context.getSystemService(Context.INPUT_SERVICE);
refreshPenMode();
mInputManager.registerInputDeviceListener(mInputDeviceListener, null);
SharedPreferences preferences = context.getSharedPreferences(SHARED_STYLUS,
Context.MODE_PRIVATE);
}
private static void enablePenMode() {
public static void enablePenMode() {
Log.d(TAG, "enablePenMode: Enable Pen Mode");
SystemProperties.set("persist.sys.parts.pen", "18");
}
private static void disablePenMode() {
public static void disablePenMode() {
Log.d(TAG, "disablePenMode: Disable Pen Mode");
SystemProperties.set("persist.sys.parts.pen", "0");
}
private static void refreshPenMode() {
for (int id : mInputManager.getInputDeviceIds()) {
if (isDeviceXiaomiPen(id)) {
if (isDeviceXiaomiPen(id) || preferences.getInt(SHARED_STYLUS, 0) == 1) {
if (DEBUG) Log.d(TAG, "refreshPenMode: Found Xiaomi Pen");
enablePenMode();
return;

View File

@@ -0,0 +1,35 @@
/*
* Copyright (C) 2023 The LineageOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.lineageos.xiaomiperipheralmanager;
import android.os.Bundle;
import com.android.settingslib.collapsingtoolbar.CollapsingToolbarBaseActivity;
import com.android.settingslib.widget.R;
public class StylusSettingsActivity extends CollapsingToolbarBaseActivity {
private static final String TAG_STYLUS = "stylus";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(R.id.content_frame,
new StylusSettingsFragment(), TAG_STYLUS).commit();
}
}

View File

@@ -0,0 +1,76 @@
/*
* Copyright (C) 2023 The LineageOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.lineageos.xiaomiperipheralmanager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Switch;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.Preference.OnPreferenceChangeListener;
import androidx.preference.PreferenceFragment;
import androidx.preference.SwitchPreference;
import com.android.settingslib.widget.MainSwitchPreference;
import org.lineageos.xiaomiperipheralmanager.PenUtils;
import org.lineageos.xiaomiperipheralmanager.R;
public class StylusSettingsFragment extends PreferenceFragment implements
OnPreferenceChangeListener {
private static final String STYLUS_KEY = "stylus_switch_key";
public static final String SHARED_STYLUS = "shared_stylus_force";
private SwitchPreference mStylusPreference;
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.stylus_settings);
SharedPreferences preferences = getActivity().getSharedPreferences(SHARED_STYLUS,
Context.MODE_PRIVATE);
mStylusPreference = (SwitchPreference) findPreference(STYLUS_KEY);
mStylusPreference.setChecked(preferences.getInt(SHARED_STYLUS, 0) == 1);
mStylusPreference.setEnabled(true);
mStylusPreference.setOnPreferenceChangeListener(this);
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (STYLUS_KEY.equals(preference.getKey())) {
forceStylus((Boolean) newValue ? 1 : 0);
}
return true;
}
private void forceStylus(int status) {
SharedPreferences preferences = getActivity().getSharedPreferences(SHARED_STYLUS,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(SHARED_STYLUS, status);
editor.commit();
if (status == 1)
PenUtils.enablePenMode();
else
PenUtils.disablePenMode();
}
}