VancedMicroG/play-services-core/src/main/java/org/microg/gms/safetynet/SafetyNetPrefs.java

115 lines
3.8 KiB
Java
Raw Normal View History

2017-02-08 13:13:34 +00:00
/*
* SPDX-FileCopyrightText: 2021, microG Project Team
* SPDX-License-Identifier: Apache-2.0
2017-02-08 13:13:34 +00:00
*/
package org.microg.gms.safetynet;
2017-02-08 13:13:34 +00:00
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import org.microg.gms.common.PackageUtils;
import java.io.File;
2017-02-08 13:13:34 +00:00
public class SafetyNetPrefs implements SharedPreferences.OnSharedPreferenceChangeListener {
2018-09-21 19:37:45 +00:00
private static final String OFFICIAL_ATTEST_BASE_URL = "https://www.googleapis.com/androidcheck/v1/attestations/attest";
2017-02-08 13:13:34 +00:00
public static final String PREF_SNET_DISABLED = "snet_disabled";
public static final String PREF_SNET_OFFICIAL = "snet_official";
public static final String PREF_SNET_THIRD_PARTY = "snet_third_party";
public static final String PREF_SNET_CUSTOM_URL = "snet_custom_url";
2017-02-22 03:20:46 +00:00
public static final String PREF_SNET_SELF_SIGNED = "snet_self_signed";
2017-02-08 13:13:34 +00:00
private static SafetyNetPrefs INSTANCE;
public static SafetyNetPrefs get(Context context) {
if (INSTANCE == null) {
PackageUtils.warnIfNotMainProcess(context, SafetyNetPrefs.class);
2017-02-08 13:13:34 +00:00
if (context == null) return new SafetyNetPrefs(null);
INSTANCE = new SafetyNetPrefs(context.getApplicationContext());
}
return INSTANCE;
}
private boolean disabled;
private boolean official;
2017-02-22 03:20:46 +00:00
private boolean selfSigned;
2017-02-08 13:13:34 +00:00
private boolean thirdParty;
private String customUrl;
private SharedPreferences preferences;
private SharedPreferences systemDefaultPreferences;
2017-02-08 13:13:34 +00:00
private SafetyNetPrefs(Context context) {
if (context != null) {
preferences = PreferenceManager.getDefaultSharedPreferences(context);
preferences.registerOnSharedPreferenceChangeListener(this);
try {
systemDefaultPreferences = (SharedPreferences) Context.class.getDeclaredMethod("getSharedPreferences", File.class, int.class).invoke(context, new File("/system/etc/microg.xml"), Context.MODE_PRIVATE);
} catch (Exception ignored) {
}
2017-02-08 13:13:34 +00:00
update();
}
}
private boolean getSettingsBoolean(String key, boolean def) {
if (systemDefaultPreferences != null) {
def = systemDefaultPreferences.getBoolean(key, def);
}
return preferences.getBoolean(key, def);
}
private String getSettingsString(String key, String def) {
if (systemDefaultPreferences != null) {
def = systemDefaultPreferences.getString(key, def);
}
return preferences.getString(key, def);
}
2017-02-08 13:13:34 +00:00
public void update() {
disabled = getSettingsBoolean(PREF_SNET_DISABLED, true);
official = getSettingsBoolean(PREF_SNET_OFFICIAL, false);
selfSigned = getSettingsBoolean(PREF_SNET_SELF_SIGNED, false);
thirdParty = getSettingsBoolean(PREF_SNET_THIRD_PARTY, false);
customUrl = getSettingsString(PREF_SNET_CUSTOM_URL, null);
2017-02-08 13:13:34 +00:00
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
update();
}
public boolean isEnabled() {
2017-02-22 03:20:46 +00:00
return !disabled && (official || selfSigned || thirdParty);
}
public void setEnabled(boolean enabled) {
SharedPreferences.Editor edit = preferences.edit();
edit.putBoolean(PREF_SNET_DISABLED, !enabled);
2017-02-22 03:20:46 +00:00
if (enabled && !isEnabled()) {
official = true;
edit.putBoolean(PREF_SNET_OFFICIAL, true);
2017-02-22 03:20:46 +00:00
}
edit.commit();
2017-02-22 03:20:46 +00:00
}
public boolean isSelfSigned() {
return selfSigned;
2017-02-08 13:13:34 +00:00
}
public boolean isOfficial() {
return official;
}
public boolean isThirdParty() {
return thirdParty;
}
2018-09-21 19:37:45 +00:00
public String getServiceUrl() {
if (official) return OFFICIAL_ATTEST_BASE_URL;
2017-02-08 13:13:34 +00:00
return customUrl;
}
}