2

Just an example, I can get the Display Timeout setting like this:

int timeout = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT);

I can set the Display Timeout setting like this:

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 10000);

How can I programmatically get and set the Fast Charging and the Super Fast Charging settings?

Edit: Thanks to Veniamin for helping me get the correct variable names, here's what worked for me:

try {
    int isSuperFastChargingEnabled = Settings.System.getInt(getContentResolver(), "super_fast_charging");
    if ( isSuperFastChargingEnabled == 0) {
        Settings.System.putInt(getContentResolver(), "super_fast_charging", 1);
        Settings.System.putInt(getContentResolver(), "adaptive_fast_charging", 1);
        Toast.makeText(this, "Fast charge is set to 1",Toast.LENGTH_LONG).show();
    } else if ( isSuperFastChargingEnabled == 1) {
        Settings.System.putInt(getContentResolver(), "super_fast_charging", 0);
        Settings.System.putInt(getContentResolver(), "adaptive_fast_charging", 0);
        Toast.makeText(this, "Fast charge is set to 0",Toast.LENGTH_LONG).show();
    }
} catch (Settings.SettingNotFoundException e) {
                Toast.makeText(this,"Failed to get fast charge setting",Toast.LENGTH_LONG).show();
}
TuxForLife
  • 219
  • 1
  • 2
  • 15
  • I'm not sure that you can access these settings. From what I found out, they are Samsung-only settings. So it would be up to Samsung to provide an API for get/set them programatically. I don't think that such an API or SDK exists. – muetzenflo Jul 26 '22 at 15:40
  • Thank you, I didn't think to analyze that they are only Samsung settings – TuxForLife Jul 26 '22 at 20:59
  • By the way, I am able to change the fast charging settings programmatically with the Tasker app – TuxForLife Jul 27 '22 at 12:29
  • Maybe the Tasker developer reverse engineered this feature. I would start searching for an official SDK here: https://developer.samsung.com/mobile – muetzenflo Jul 28 '22 at 10:26
  • Hi, did you try to control fast charging using these properties in Android 13? I see these properties still provide actual result when they are read but it is not possible to write them, an error "You cannot keep your settings in the secure settings" is returned when '...putInt' is called. When the app granted to change system properties(android.permission.WRITE_SETTINGS) it is possible to write to these properties in Android 11 and Android 12 – nikolayandr Nov 18 '22 at 20:25
  • Sorry, I've only had the opportunity to test it on Android 12. If you find a solution, I hope you can share it here – TuxForLife Nov 19 '22 at 21:04
  • @TuxForLife I have asked on Samsung Develop Forum https://forum.developer.samsung.com/t/are-adaptive-fast-charging-super-fast-charging-wireless-fast-charging-available-for-writing/23335 Waiting on any info – nikolayandr Nov 24 '22 at 11:32

1 Answers1

2

You can read and update these settings in the Device Care Samsung system application, so I reverse engineered it.

Here is how you can read Fast Charging, Super Fast Charging, and Fast Wireless Charging settings in your application:

val isSuperFastChargingEnabled = Settings.System.getInt(context.contentResolver, "super_fast_charging", 0) == 1

val isFastChargingEnabled = Settings.System.getInt(context.contentResolver, "adaptive_fast_charging", 0) == 1

val isFastWirelessChargingEnabled = Settings.System.getInt(context.contentResolver, "wireless_fast_charging", 0) == 1

Unfortunately, to update these settings programmatically, you need the android.permission.WRITE_SETTINGS permission is only granted to the system applications.

So, if you are not developing a system app, the only way to enable these settings is to ask users to enable them manually. To simplify workflow, you can route users directly to the system Fast Charging Settings Activity, like so:

val intent = Intent()
intent.component = ComponentName(
    "com.samsung.android.lool",
    "com.samsung.android.sm.battery.ui.BatteryAdvancedMenuActivity"
)

// Activity class name may be updated in future versions, so
// the safest way to handle Activity class name updates is to wrap this
// call in the try/catch and add a custom error handling if the activity wasn't found.
try {
    startActivity(intent)
} catch (e: Exception) {
    // Custom error handling
}

Since these settings are vendor-specific, checking the vendor id before reading them is recommended.

I tested it and confirm that it works on the Samsung Galaxy S20 (SM-G980F/DS); Android 12; One UI 4.1.

Veniamin
  • 774
  • 5
  • 12
  • I appreciate the variable names, they worked like a charm. Great idea to recommend an option without WRITE_SETTINGS. How did you reverse engineer? I would like to figure out how to reach the volume settings page via intent just like you did with the Super Fast Charging. Thanks for the help! – TuxForLife Aug 02 '22 at 00:47
  • @TuxForLife I've used the online decompiler for this task, but you can use local tools on your PC, like 'jadx'. Here is a great article on the reverse engineering topic - https://chris-yn-chen.medium.com/apk-reverse-engineering-df7ed8cec191. After fetching APK from the device, you can also use the online APK decompiler - http://www.javadecompilers.com/apk. But I suggest using it only for public applications. And you can use ADB to get the current foreground activity - https://stackoverflow.com/questions/13193592/adb-android-getting-the-name-of-the-current-activity – Veniamin Aug 02 '22 at 09:45
  • Much appreciated, thanks. I just realized that I didn't give you the bounty. I thought I did, but I only gave you the Accepted Answer. Looks like Stack auto-awarded you the bounty, good thing! – TuxForLife Aug 03 '22 at 03:23
  • 1
    Hi, are you still able to control fast charging using these properties in Android 13? I see these properties still provide actual result when they are read but it is not possible to write them, an error "You cannot keep your settings in the secure settings" is returned when '...putInt' is called. When the app granted to change system properties(android.permission.WRITE_SETTINGS) it is possible to write to these properties in Android 11 and Android 12 – nikolayandr Nov 18 '22 at 20:25