FlashControl: Do not tamper with settings

Logic here is since APP can tell if QS panel flash is on, and APP cannot control QS to modify state, just disable turning off flash if QS has turned it on

- Revert 2776ddb454
This commit is contained in:
roynatech2544
2023-05-24 13:07:49 +09:00
parent a2e354f899
commit 8ca9bd12ef
6 changed files with 49 additions and 29 deletions

View File

@@ -12,13 +12,6 @@ android_app {
defaults: ["SettingsLibDefaults"],
required: [
"vendor.samsung_ext.hardware.camera.flashlight-service",
"privapp_whitelist_com.royna.flashcontrol.xml"
],
system_ext_specific: true,
}
prebuilt_etc {
name: "privapp_whitelist_com.royna.flashcontrol.xml",
src: "privapp_whitelist_com.royna.flashcontrol.xml",
sub_dir: "permissions"
}

View File

@@ -4,7 +4,6 @@
android:sharedUserId="android.uid.system">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
<application
android:defaultToDeviceProtectedStorage="true"

View File

@@ -1,17 +0,0 @@
<!--
Copyright (C) 2023 Royna
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.
-->
<permissions>
<privapp-permissions package="com.royna.flashcontrol">
<permission name="android.permission.WRITE_SECURE_SETTINGS"/>
</privapp-permissions>
</permissions>

View File

@@ -19,6 +19,7 @@
<!-- Tests, Infos -->
<string name="flash_current_intesity">Current brightness intesity: %d</string>
<string name="flash_current_on">Flash is currently %s</string>
<string name="disabled_qs">Use the QS panel flash to turn off flashlight</string>
<!-- On Off -->
<string name="on">on</string>

View File

@@ -60,7 +60,9 @@ class FlashFragment : PreferenceFragmentCompat(), OnMainSwitchChangeListener {
switchBar = findPreference<MainSwitchPreference>(PREF_FLASH_ENABLE)!!
switchBar.addOnSwitchChangeListener(this)
val mBrightness = mService?.getCurrentBrightness() ?: 0
val mSettingBrightness = Settings.Secure.getInt(requireContext().contentResolver, Settings.Secure.FLASHLIGHT_ENABLED, 0)
switchBar.isChecked = mBrightness != 0
switchBar.isEnabled = mSettingBrightness == 0
val mSavedIntesity = mSharedPreferences.getInt(PREF_FLASH_INTESITY, 1)
@@ -89,11 +91,18 @@ class FlashFragment : PreferenceFragmentCompat(), OnMainSwitchChangeListener {
val mEnabled = Settings.Secure.getInt(requireContext().contentResolver, Settings.Secure.FLASHLIGHT_ENABLED)
val mMainHandler = Handler(Looper.getMainLooper())
when (mEnabled) {
0 -> mMainHandler.post { switchBar.isChecked = false }
1 -> mMainHandler.post { switchBar.isChecked = true }
0 -> mMainHandler.post {
switchBar.isChecked = false
switchBar.isEnabled = true
}
1 -> mMainHandler.post {
switchBar.isChecked = true
switchBar.isEnabled = false
Toast.makeText(requireContext(), R.string.disabled_qs, Toast.LENGTH_SHORT).show()
}
else -> {}
}
changeRadioButtons(switchBar.isChecked)
changeRadioButtons(mEnabled == 1)
} catch (e: Settings.SettingNotFoundException) {
e.printStackTrace()
}
@@ -115,7 +124,6 @@ class FlashFragment : PreferenceFragmentCompat(), OnMainSwitchChangeListener {
}
mCurrentOn.title = String.format(requireContext().getString(R.string.flash_current_on), requireContext().getString(if (isChecked) R.string.on else R.string.off))
if (isChecked) mCurrentIntesity.title = String.format(requireContext().getString(R.string.flash_current_intesity), mService.getCurrentBrightness() ?: -1)
Settings.Secure.putInt(requireContext().contentResolver, Settings.Secure.FLASHLIGHT_ENABLED, if (isChecked) 1 else 0)
requireContext().contentResolver.notifyChange(mFlashUrl, mSettingsObserver, ContentResolver.NOTIFY_UPDATE)
changeRadioButtons(isChecked)
}

View File

@@ -0,0 +1,36 @@
package com.royna.flashcontrol
import android.app.Service
import android.content.Intent
import android.database.ContentObserver
import android.os.Looper
import android.os.Handler
import android.os.ServiceManager
import android.util.Log
import android.provider.Settings
import androidx.preference.PreferenceManager
import vendor.samsung_ext.hardware.camera.flash.IFlashlight
class FlashService : Service() {
private val mService : IFlashlight? = IFlashlight.Stub.asInterface(ServiceManager.waitForDeclaredService("vendor.samsung_ext.hardware.camera.flashlight.IFlashlight/default"))
private val mMainHandler = Handler(Looper.getMainLooper())
private var mBrightSetting = 0
override fun onStartCommand(i: Intent?, flags: Int, startid : Int) : Int {
mBrightSetting = PreferenceManager.getDefaultSharedPreferences(this).getInt(FlashFragment.PREF_FLASH_INTESITY, 1)
}
private val mFlashObserver = object : ContentObserver(mMainHandler) {
override fun onChange(s: Boolean) {
super.onChange(s)
val isOn = Settings.Secure.getInt(requireContext().contentResolver, Settings.Secure.FLASHLIGHT_ENABLED, 0)
if (isOn == 1) {
mMainHandler.postDelayed({
Log.d("FlashControlSVC", Setting $mBrightSetting")
mService?.setBrightness(mBrightSetting)
}, 500)
}
}
}
}