SettingsHelper.javaAPI DocAndroid 5.1 API10927Thu Mar 12 22:22:42 GMT


public class SettingsHelper extends Object

Fields Summary
private static final String
private android.content.Context
private android.telephony.TelephonyManager
Constructors Summary
public SettingsHelper(android.content.Context context)

        mContext = context;
        mAudioManager = (AudioManager) context
        mTelephonyManager = (TelephonyManager) context
Methods Summary
Informs the audio service of changes to the settings so that they can be re-read and applied.

        AudioManager am = new AudioManager(mContext);
private java.lang.StringgetCanonicalRingtoneValue(java.lang.String value)

        final Uri ringtoneUri = Uri.parse(value);
        final Uri canonicalUri = mContext.getContentResolver().canonicalize(ringtoneUri);
        return canonicalUri == null ? null : canonicalUri.toString();

        Configuration conf = mContext.getResources().getConfiguration();
        final Locale loc = conf.locale;
        String localeString = loc.getLanguage();
        String country = loc.getCountry();
        if (!TextUtils.isEmpty(country)) {
            localeString += "-" + country;
        return localeString.getBytes();
private booleanisAlreadyConfiguredCriticalAccessibilitySetting(java.lang.String name)

        // These are the critical accessibility settings that are required for a
        // blind user to be able to interact with the device. If these settings are
        // already configured, we will not overwrite them. If they are already set,
        // it means that the user has performed a global gesture to enable accessibility
        // and definitely needs these features working after the restore.
        if (Settings.Secure.ACCESSIBILITY_ENABLED.equals(name)
                || Settings.Secure.ACCESSIBILITY_SCRIPT_INJECTION.equals(name)
                || Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD.equals(name)
                || Settings.Secure.TOUCH_EXPLORATION_ENABLED.equals(name)) {
            return Settings.Secure.getInt(mContext.getContentResolver(), name, 0) != 0;
        } else if (Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES.equals(name)
                || Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES.equals(name)) {
            return !TextUtils.isEmpty(Settings.Secure.getString(
                    mContext.getContentResolver(), name));
        return false;
public java.lang.StringonBackupValue(java.lang.String name, java.lang.String value)

        // Special processing for backing up ringtones & notification sounds
        if (Settings.System.RINGTONE.equals(name)
                || Settings.System.NOTIFICATION_SOUND.equals(name)) {
            if (value == null) {
                if (Settings.System.RINGTONE.equals(name)) {
                    // For ringtones, we need to distinguish between non-telephony vs telephony
                    if (mTelephonyManager != null && mTelephonyManager.isVoiceCapable()) {
                        // Backup a null ringtone as silent on voice-capable devices
                        return SILENT_RINGTONE;
                    } else {
                        // Skip backup of ringtone on non-telephony devices.
                        return null;
                } else {
                    // Backup a null notification sound as silent
                    return SILENT_RINGTONE;
            } else {
                return getCanonicalRingtoneValue(value);
        // Return the original value
        return value;
public booleanrestoreValue(java.lang.String name, java.lang.String value)
Sets the property via a call to the appropriate API, if any, and returns whether or not the setting should be saved to the database as well.

name the name of the setting
value the string value of the setting
whether to continue with writing the value to the database. In some cases the data will be written by the call to the appropriate API, and in some cases the property value needs to be modified before setting.

        if (Settings.System.SCREEN_BRIGHTNESS.equals(name)) {
        } else if (Settings.System.SOUND_EFFECTS_ENABLED.equals(name)) {
            setSoundEffects(Integer.parseInt(value) == 1);
        } else if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
            return false;
        } else if (Settings.Secure.BACKUP_AUTO_RESTORE.equals(name)) {
            setAutoRestore(Integer.parseInt(value) == 1);
        } else if (isAlreadyConfiguredCriticalAccessibilitySetting(name)) {
            return false;
        } else if (Settings.System.RINGTONE.equals(name)
                || Settings.System.NOTIFICATION_SOUND.equals(name)) {
            setRingtone(name, value);
            return false;
        return true;
private voidsetAutoRestore(boolean enabled)

        try {
            IBackupManager bm = IBackupManager.Stub.asInterface(
            if (bm != null) {
        } catch (RemoteException e) {}
private voidsetBrightness(int brightness)

        try {
            IPowerManager power = IPowerManager.Stub.asInterface(
            if (power != null) {
        } catch (RemoteException doe) {

private voidsetGpsLocation(java.lang.String value)

        UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
        if (um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION)) {
        final String GPS = LocationManager.GPS_PROVIDER;
        boolean enabled =
                GPS.equals(value) ||
                value.startsWith(GPS + ",") ||
                value.endsWith("," + GPS) ||
                value.contains("," + GPS + ",");
                mContext.getContentResolver(), GPS, enabled);
voidsetLocaleData(byte[] data, int size)
Sets the locale specified. Input data is the byte representation of a BCP-47 language tag. For backwards compatibility, strings of the form {@code ll_CC} are also accepted, where {@code ll} is a two letter language code and {@code CC} is a two letter country code.

data the locale string in bytes.

        // Check if locale was set by the user:
        Configuration conf = mContext.getResources().getConfiguration();
        // TODO: The following is not working as intended because the network is forcing a locale
        // change after registering. Need to find some other way to detect if the user manually
        // changed the locale
        if (conf.userSetLocale) return; // Don't change if user set it in the SetupWizard

        final String[] availableLocales = mContext.getAssets().getLocales();
        // Replace "_" with "-" to deal with older backups.
        String localeCode = new String(data, 0, size).replace('_", '-");
        Locale loc = null;
        for (int i = 0; i < availableLocales.length; i++) {
            if (availableLocales[i].equals(localeCode)) {
                loc = Locale.forLanguageTag(localeCode);
        if (loc == null) return; // Couldn't find the saved locale in this version of the software

        try {
            IActivityManager am = ActivityManagerNative.getDefault();
            Configuration config = am.getConfiguration();
            config.locale = loc;
            // indicate this isn't some passing default - the user wants this remembered
            config.userSetLocale = true;

        } catch (RemoteException e) {
            // Intentionally left blank
private voidsetRingtone(java.lang.String name, java.lang.String value)
Sets the ringtone of type specified by the name.

name should be Settings.System.RINGTONE or Settings.System.NOTIFICATION_SOUND.
value can be a canonicalized uri or "_silent" to indicate a silent (null) ringtone.

        // If it's null, don't change the default
        if (value == null) return;
        Uri ringtoneUri = null;
        if (SILENT_RINGTONE.equals(value)) {
            ringtoneUri = null;
        } else {
            Uri canonicalUri = Uri.parse(value);
            ringtoneUri = mContext.getContentResolver().uncanonicalize(canonicalUri);
            if (ringtoneUri == null) {
                // Unrecognized or invalid Uri, don't restore
        final int ringtoneType = Settings.System.RINGTONE.equals(name)
                ? RingtoneManager.TYPE_RINGTONE : RingtoneManager.TYPE_NOTIFICATION;
        RingtoneManager.setActualDefaultRingtoneUri(mContext, ringtoneType, ringtoneUri);
private voidsetSoundEffects(boolean enable)

        if (enable) {
        } else {