mirror of
https://github.com/Genymobile/scrcpy.git
synced 2025-10-01 21:58:38 +00:00
Simplify settings access
For Android >= 12, scrcpy executed "settings" commands (in a new process) rather than using the ContentProvider directly, due to permission issues [1]. However, these permission issues were resolved by introducing FakeContext.getContentResolver() [2]. Therefore, remove the use of "settings" commands and use the ContentProvider directly in all cases. Refs [1]cc0902b13c
Refs [2]91373d906b
Refs #6224 comment <https://github.com/Genymobile/scrcpy/issues/6224#issuecomment-3078418268>
This commit is contained in:
parent
eb576c44f8
commit
939c8e7f68
1 changed files with 9 additions and 54 deletions
|
@ -1,13 +1,8 @@
|
||||||
package com.genymobile.scrcpy.util;
|
package com.genymobile.scrcpy.util;
|
||||||
|
|
||||||
import com.genymobile.scrcpy.AndroidVersions;
|
|
||||||
import com.genymobile.scrcpy.wrappers.ContentProvider;
|
import com.genymobile.scrcpy.wrappers.ContentProvider;
|
||||||
import com.genymobile.scrcpy.wrappers.ServiceManager;
|
import com.genymobile.scrcpy.wrappers.ServiceManager;
|
||||||
|
|
||||||
import android.os.Build;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public final class Settings {
|
public final class Settings {
|
||||||
|
|
||||||
public static final String TABLE_SYSTEM = ContentProvider.TABLE_SYSTEM;
|
public static final String TABLE_SYSTEM = ContentProvider.TABLE_SYSTEM;
|
||||||
|
@ -18,66 +13,26 @@ public final class Settings {
|
||||||
/* not instantiable */
|
/* not instantiable */
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void execSettingsPut(String table, String key, String value) throws SettingsException {
|
|
||||||
try {
|
|
||||||
Command.exec("settings", "put", table, key, value);
|
|
||||||
} catch (IOException | InterruptedException e) {
|
|
||||||
throw new SettingsException("put", table, key, value, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String execSettingsGet(String table, String key) throws SettingsException {
|
|
||||||
try {
|
|
||||||
return Command.execReadLine("settings", "get", table, key);
|
|
||||||
} catch (IOException | InterruptedException e) {
|
|
||||||
throw new SettingsException("get", table, key, null, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getValue(String table, String key) throws SettingsException {
|
public static String getValue(String table, String key) throws SettingsException {
|
||||||
if (Build.VERSION.SDK_INT <= AndroidVersions.API_30_ANDROID_11) {
|
try (ContentProvider provider = ServiceManager.getActivityManager().createSettingsProvider()) {
|
||||||
// on Android >= 12, it always fails: <https://github.com/Genymobile/scrcpy/issues/2788>
|
return provider.getValue(table, key);
|
||||||
try (ContentProvider provider = ServiceManager.getActivityManager().createSettingsProvider()) {
|
|
||||||
return provider.getValue(table, key);
|
|
||||||
} catch (SettingsException e) {
|
|
||||||
Ln.w("Could not get settings value via ContentProvider, fallback to settings process", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return execSettingsGet(table, key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void putValue(String table, String key, String value) throws SettingsException {
|
public static void putValue(String table, String key, String value) throws SettingsException {
|
||||||
if (Build.VERSION.SDK_INT <= AndroidVersions.API_30_ANDROID_11) {
|
try (ContentProvider provider = ServiceManager.getActivityManager().createSettingsProvider()) {
|
||||||
// on Android >= 12, it always fails: <https://github.com/Genymobile/scrcpy/issues/2788>
|
provider.putValue(table, key, value);
|
||||||
try (ContentProvider provider = ServiceManager.getActivityManager().createSettingsProvider()) {
|
|
||||||
provider.putValue(table, key, value);
|
|
||||||
} catch (SettingsException e) {
|
|
||||||
Ln.w("Could not put settings value via ContentProvider, fallback to settings process", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
execSettingsPut(table, key, value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getAndPutValue(String table, String key, String value) throws SettingsException {
|
public static String getAndPutValue(String table, String key, String value) throws SettingsException {
|
||||||
if (Build.VERSION.SDK_INT <= AndroidVersions.API_30_ANDROID_11) {
|
try (ContentProvider provider = ServiceManager.getActivityManager().createSettingsProvider()) {
|
||||||
// on Android >= 12, it always fails: <https://github.com/Genymobile/scrcpy/issues/2788>
|
String oldValue = provider.getValue(table, key);
|
||||||
try (ContentProvider provider = ServiceManager.getActivityManager().createSettingsProvider()) {
|
if (!value.equals(oldValue)) {
|
||||||
String oldValue = provider.getValue(table, key);
|
provider.putValue(table, key, value);
|
||||||
if (!value.equals(oldValue)) {
|
|
||||||
provider.putValue(table, key, value);
|
|
||||||
}
|
|
||||||
return oldValue;
|
|
||||||
} catch (SettingsException e) {
|
|
||||||
Ln.w("Could not get and put settings value via ContentProvider, fallback to settings process", e);
|
|
||||||
}
|
}
|
||||||
|
return oldValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
String oldValue = getValue(table, key);
|
|
||||||
if (!value.equals(oldValue)) {
|
|
||||||
putValue(table, key, value);
|
|
||||||
}
|
|
||||||
return oldValue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue