pipa: peripheralmanager: implement a switch for toggling the smart cover

This commit is contained in:
Roman Lubij
2025-06-17 12:00:00 +00:00
committed by gensis01
parent 93c69a9ac4
commit af04be6e03
5 changed files with 187 additions and 1 deletions

View File

@@ -55,6 +55,19 @@
<meta-data android:name="com.android.settings.summary"
android:resource="@string/keyboard_summary" />
</activity>
<activity
android:name=".LidSettingsActivity"
android:label="@string/lid_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/lid_summary" />
</activity>
</application>
</manifest>

View File

@@ -14,10 +14,16 @@
<string name="stylus_switch_title">Force recognize stylus</string>
<string name="stylus_switch_summary">Enable this settings to allow using third party styluses</string>
<!-- Xiaomi Stylus -->
<!-- Xiaomi Keyboard -->
<string name="keyboard_title">Keyboard</string>
<string name="keyboard_summary">Keyboard Settings</string>
<string name="keyboard_switch_title">Toggle angle detection</string>
<string name="keyboard_switch_summary">This toggles the angle detection of the keyboard</string>
<!-- Smart Cover -->
<string name="lid_title">Smart Cover</string>
<string name="lid_summary">Smart Cover Settings</string>
<string name="lid_switch_title">Toggle Smart Cover behaivor</string>
<string name="lid_switch_summary">This toggles the behaivor of the smart lid</string>
</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="lid_switch_key"
android:defaultValue="true"
android:title="@string/lid_switch_title"
android:summary="@string/lid_switch_summary"/>
</PreferenceScreen>

View File

@@ -0,0 +1,33 @@
/*
* Copyright (C) 2023-2025 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.lineageos.xiaomiperipheralmanager;
import android.os.Bundle;
import android.util.Log;
import com.android.settingslib.collapsingtoolbar.CollapsingToolbarBaseActivity;
/**
* Settings activity for stylus/pen configuration
* Hosts the StylusSettingsFragment for user configuration
*/
public class LidSettingsActivity extends CollapsingToolbarBaseActivity {
private static final String TAG = "XiaomiLidSettings";
private static final String TAG_LID = "lid";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "Opening lid settings");
getFragmentManager().beginTransaction().replace(
com.android.settingslib.collapsingtoolbar.R.id.content_frame,
new LidSettingsFragment(), TAG_LID).commit();
}
}

View File

@@ -0,0 +1,107 @@
/*
* Copyright (C) 2023-2025 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.lineageos.xiaomiperipheralmanager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.SystemProperties;
import android.util.Log;
import android.preference.PreferenceManager;
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 java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.provider.Settings;
/**
* Settings fragment for lid configuration
* Allows users to manually enable/disable the smart cover
*/
public class LidSettingsFragment extends PreferenceFragment implements
SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = "XiaomiLidSettings";
private static final String LID_KEY = "lid_switch_key";
private SharedPreferences mLidPreference;
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
try {
addPreferencesFromResource(R.xml.lid_settings);
mLidPreference = PreferenceManager.getDefaultSharedPreferences(getContext());
SwitchPreference switchPreference = (SwitchPreference) findPreference(LID_KEY);
if (switchPreference != null) {
switchPreference.setChecked(mLidPreference.getBoolean(LID_KEY, false));
switchPreference.setEnabled(true);
} else {
logError("Could not find lid switch preference");
}
logInfo("Lid settings fragment created");
} catch (Exception e) {
logError("Error creating lid settings: " + e.getMessage());
}
}
@Override
public void onResume() {
super.onResume();
try {
mLidPreference.registerOnSharedPreferenceChangeListener(this);
} catch (Exception e) {
logError("Error in onResume: " + e.getMessage());
}
}
@Override
public void onPause() {
super.onPause();
try {
mLidPreference.unregisterOnSharedPreferenceChangeListener(this);
} catch (Exception e) {
logError("Error in onPause: " + e.getMessage());
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreference, String key) {
if (LID_KEY.equals(key)) {
try {
boolean newStatus = mLidPreference.getBoolean(key, false);
logInfo("Lid preference changed to: " + newStatus);
Settings.Global.putInt(getActivity().getContentResolver(),
"lid_behavior", newStatus ? 1 : 0);
} catch (Exception e) {
logError("Error handling preference change: " + e.getMessage());
}
}
}
private void logInfo(String message) {
Log.i(TAG, getTimestamp() + message);
}
private void logError(String message) {
Log.e(TAG, getTimestamp() + message);
}
private String getTimestamp() {
return "[" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US).format(new Date()) + "] ";
}
}