pipa: peripheralmanager: Implement smart cover toggle

Change-Id: Id3e70c6128995c7e8a1bf1ed2adf054d3afb7d2c
This commit is contained in:
Roman Lubij
2025-07-01 00:16:30 +02:00
committed by gensis01
parent 230d4e6696
commit 1990629baf
5 changed files with 187 additions and 0 deletions

View File

@@ -43,5 +43,18 @@
android:resource="@string/stylus_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

@@ -15,4 +15,11 @@
<string name="force_stylus_switch_title">Force recognize stylus</string>
<string name="force_stylus_switch_summary">Enable this settings to allow using third party styluses</string>
<string name="stylus_more_info">Stylus Mode limits display\'s max refresh rate to 120Hz</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()) + "] ";
}
}