Java Code Examples for android.content.SharedPreferences.Editor#putStringSet()

The following examples show how to use android.content.SharedPreferences.Editor#putStringSet() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GeoPackageDatabases.java    From geopackage-mapcache-android with MIT License 6 votes vote down vote up
/**
 * Remove the database from the saved preferences
 *
 * @param database
 * @param preserveOverlays
 */
private void removeDatabaseFromPreferences(String database, boolean preserveOverlays) {
    Editor editor = settings.edit();

    Set<String> databases = settings.getStringSet(databasePreference,
            new HashSet<String>());
    if (databases != null && databases.contains(database)) {
        Set<String> newDatabases = new HashSet<String>();
        newDatabases.addAll(databases);
        newDatabases.remove(database);
        editor.putStringSet(databasePreference, newDatabases);
    }
    editor.remove(getTileTablesPreferenceKey(database));
    editor.remove(getFeatureTablesPreferenceKey(database));
    if (!preserveOverlays) {
        editor.remove(getFeatureOverlayTablesPreferenceKey(database));
        deleteTableFiles(database);
    }

    editor.commit();
}
 
Example 2
Source File: EventPreferencesBattery.java    From PhoneProfilesPlus with Apache License 2.0 6 votes vote down vote up
void loadSharedPreferences(SharedPreferences preferences)
{
    Editor editor = preferences.edit();
    editor.putBoolean(PREF_EVENT_BATTERY_ENABLED, _enabled);
    editor.putString(PREF_EVENT_BATTERY_LEVEL_LOW, String.valueOf(this._levelLow));
    editor.putString(PREF_EVENT_BATTERY_LEVEL_HIGHT, String.valueOf(this._levelHight));
    //editor.putBoolean(PREF_EVENT_BATTERY_CHARGING, this._charging);
    editor.putString(PREF_EVENT_BATTERY_CHARGING, String.valueOf(this._charging));

    String[] splits;
    if (this._plugged != null)
        splits = this._plugged.split("\\|");
    else
        splits = new String[]{};
    Set<String> set = new HashSet<>(Arrays.asList(splits));
    editor.putStringSet(PREF_EVENT_BATTERY_PLUGGED, set);

    editor.putBoolean(PREF_EVENT_BATTERY_POWER_SAVE_MODE, this._powerSaveMode);
    editor.apply();
}
 
Example 3
Source File: EventPreferencesOrientation.java    From PhoneProfilesPlus with Apache License 2.0 6 votes vote down vote up
void loadSharedPreferences(SharedPreferences preferences)
{
    Editor editor = preferences.edit();
    editor.putBoolean(PREF_EVENT_ORIENTATION_ENABLED, _enabled);

    String[] splits = this._display.split("\\|");
    Set<String> set = new HashSet<>(Arrays.asList(splits));
    editor.putStringSet(PREF_EVENT_ORIENTATION_DISPLAY, set);

    splits = this._sides.split("\\|");
    set = new HashSet<>(Arrays.asList(splits));
    editor.putStringSet(PREF_EVENT_ORIENTATION_SIDES, set);

    editor.putString(PREF_EVENT_ORIENTATION_DISTANCE, String.valueOf(this._distance));

    editor.putBoolean(PREF_EVENT_ORIENTATION_CHECK_LIGHT, this._checkLight);
    editor.putString(PREF_EVENT_ORIENTATION_LIGHT_MIN, this._lightMin);
    editor.putString(PREF_EVENT_ORIENTATION_LIGHT_MAX, this._lightMax);

    editor.putString(PREF_EVENT_ORIENTATION_IGNORED_APPLICATIONS, this._ignoredApplications);

    editor.apply();
}
 
Example 4
Source File: SPUtil.java    From UIWidget with Apache License 2.0 6 votes vote down vote up
/**
 * 存放object
 *
 * @param context
 * @param fileName
 * @param key
 * @param object
 * @return
 */
public static boolean put(Context context, String fileName, String key, Object object) {
    SharedPreferences sp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
    Editor editor = sp.edit();
    if (object instanceof String) {
        editor.putString(key, (String) object);
    } else if (object instanceof Integer) {
        editor.putInt(key, ((Integer) object).intValue());
    } else if (object instanceof Boolean) {
        editor.putBoolean(key, ((Boolean) object).booleanValue());
    } else if (object instanceof Float) {
        editor.putFloat(key, ((Float) object).floatValue());
    } else if (object instanceof Long) {
        editor.putLong(key, ((Long) object).longValue());
    } else if (object instanceof Set) {
        editor.putStringSet(key, (Set<String>) object);
    } else {
        editor.putStringSet(key, (Set<String>) object);
    }
    return editor.commit();
}
 
Example 5
Source File: SPUtil.java    From FastLib with Apache License 2.0 6 votes vote down vote up
/**
 * 存放object
 *
 * @param context
 * @param fileName
 * @param key
 * @param object
 * @return
 */
public static boolean put(Context context, String fileName, String key, Object object) {
    SharedPreferences sp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
    Editor editor = sp.edit();
    if (object instanceof String) {
        editor.putString(key, (String) object);
    } else if (object instanceof Integer) {
        editor.putInt(key, ((Integer) object).intValue());
    } else if (object instanceof Boolean) {
        editor.putBoolean(key, ((Boolean) object).booleanValue());
    } else if (object instanceof Float) {
        editor.putFloat(key, ((Float) object).floatValue());
    } else if (object instanceof Long) {
        editor.putLong(key, ((Long) object).longValue());
    } else if (object instanceof Set) {
        editor.putStringSet(key, (Set<String>) object);
    } else {
        editor.putStringSet(key, (Set<String>) object);
    }
    return editor.commit();
}
 
Example 6
Source File: PreferVisitor.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
@SuppressLint("NewApi")
private void editorPutValues(Editor editor, String preferKey, Object valueData) {
    if (editor == null) {
        return;
    }
    if (valueData == null) {
        editor.putString(preferKey, null);
    } else {
        if (valueData instanceof String) {
            editor.putString(preferKey, (String) valueData);
        } else if (valueData instanceof Integer) {
            editor.putInt(preferKey, (Integer) valueData);
        } else if (valueData instanceof Boolean) {
            editor.putBoolean(preferKey, (Boolean) valueData);
        } else if (valueData instanceof Float) {
            editor.putFloat(preferKey, (Float) valueData);
        } else if (valueData instanceof Set) {
            if (Util.isCompateApi(11))
                editor.putStringSet(preferKey, (Set<String>) valueData);
        } else if (valueData instanceof Long) {
            editor.putLong(preferKey, (Long) valueData);
        }
    }
}
 
Example 7
Source File: PreferVisitor.java    From BaseProject with Apache License 2.0 5 votes vote down vote up
/**
 * 存储一条数据到首选项文件中
 * @param preferFileName 要保存的SharedPreference 文件名
 * @param key
 * @param value
 */
@SuppressLint("NewApi")
public boolean saveValue(String preferFileName, String key, Object value) {
    if (Util.isEmpty(preferFileName)) {
        return false;
    }
    Editor mEditor = getSpByName(preferFileName).edit();
    if (value == null) {
        mEditor.putString(key, null);
    }
    else{
        String valueType = value.getClass().getSimpleName();
        if ("String".equals(valueType)) {
            mEditor.putString(key, (String) value);
        } else if ("Integer".equals(valueType)) {
            mEditor.putInt(key, (Integer) value);
        } else if ("Boolean".equals(valueType)) {
            mEditor.putBoolean(key, (Boolean) value);
        } else if ("Float".equals(valueType)) {
            mEditor.putFloat(key, (Float) value);
        } else if ("Long".equals(valueType)) {
            mEditor.putLong(key, (Long) value);
        } else if ("Set".equals(valueType)) {
            if (Util.isCompateApi(11)) {
                mEditor.putStringSet(key, (Set<String>) value);
            }
        }
    }
    return mEditor.commit();
}
 
Example 8
Source File: GeoPackageDatabases.java    From geopackage-mapcache-android with MIT License 5 votes vote down vote up
/**
 * Remove all databases from the preferences file
 */
private void removeAllDatabasesFromPreferences(){
    Editor editor = settings.edit();
    Set<String> emptyDatabases = new HashSet<String>();
    editor.putStringSet(databasePreference, emptyDatabases);
    editor.commit();
}
 
Example 9
Source File: ProfileManager.java    From android with GNU General Public License v3.0 5 votes vote down vote up
public void saveProfileList(Context context) {
    SharedPreferences sharedprefs = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
    Editor editor = sharedprefs.edit();
    editor.putStringSet("vpnlist", profiles.keySet());

    // For reasing I do not understand at all
    // Android saves my prefs file only one time
    // if I remove the debug code below :(
    int counter = sharedprefs.getInt("counter", 0);
    editor.putInt("counter", counter + 1);
    editor.apply();

}
 
Example 10
Source File: MultiChoiceListPreference.java    From AndroidMaterialPreferences with Apache License 2.0 5 votes vote down vote up
/**
 * Persists a specific set in the shared preferences by using the preference's key.
 *
 * @param set
 *         The set, which should be persisted, as an instance of the type {@link Set}
 * @return True, if the given set has been persisted, false otherwise
 */
private boolean persistSet(@Nullable final Set<String> set) {
    if (set != null && shouldPersist()) {
        if (set.equals(getPersistedSet(null))) {
            return true;
        }

        Editor editor = getPreferenceManager().getSharedPreferences().edit();
        editor.putStringSet(getKey(), set);
        editor.apply();
        return true;
    }

    return false;
}
 
Example 11
Source File: ProfileManager.java    From Cake-VPN with GNU General Public License v2.0 5 votes vote down vote up
public void saveProfileList(Context context) {
    SharedPreferences sharedprefs = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
    Editor editor = sharedprefs.edit();
    editor.putStringSet("vpnlist", profiles.keySet());
    // For reasing I do not understand at all
    // Android saves my prefs file only one time
    // if I remove the debug code below :(
    int counter = sharedprefs.getInt("counter", 0);
    editor.putInt("counter", counter + 1);
    editor.apply();

    Log.d("TEST", "saveProfileList: ------------------- "+counter);
}
 
Example 12
Source File: ProfileManager.java    From EasyVPN-Free with GNU General Public License v3.0 5 votes vote down vote up
public void saveProfileList(Context context) {
    SharedPreferences sharedprefs = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
    Editor editor = sharedprefs.edit();
    editor.putStringSet("vpnlist", profiles.keySet());

    // For reasing I do not understand at all
    // Android saves my prefs file only one time
    // if I remove the debug code below :(
    int counter = sharedprefs.getInt("counter", 0);
    editor.putInt("counter", counter + 1);
    editor.apply();

}
 
Example 13
Source File: ProfileManager.java    From Cybernet-VPN with GNU General Public License v3.0 5 votes vote down vote up
public void saveProfileList(Context context) {
    SharedPreferences sharedprefs = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
    Editor editor = sharedprefs.edit();
    editor.putStringSet("vpnlist", profiles.keySet());
    // For reasing I do not understand at all
    // Android saves my prefs file only one time
    // if I remove the debug code below :(
    int counter = sharedprefs.getInt("counter", 0);
    editor.putInt("counter", counter + 1);
    editor.apply();
}
 
Example 14
Source File: Prefs.java    From PS4-Payload-Sender-Android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param key   The name of the preference to modify.
 * @param value The new value for the preference.
 * @see Editor#putStringSet(String, Set)
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static void putStringSet(final String key, final Set<String> value) {
    final Editor editor = getPreferences().edit();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        editor.putStringSet(key, value);
    } else {
        // Workaround for pre-HC's lack of StringSets
        int stringSetLength = 0;
        if (mPrefs.contains(key + LENGTH)) {
            // First read what the value was
            stringSetLength = mPrefs.getInt(key + LENGTH, -1);
        }
        editor.putInt(key + LENGTH, value.size());
        int i = 0;
        for (String aValue : value) {
            editor.putString(key + "[" + i + "]", aValue);
            i++;
        }
        for (; i < stringSetLength; i++) {
            // Remove any remaining values
            editor.remove(key + "[" + i + "]");
        }
    }
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
        editor.commit();
    } else {
        editor.apply();
    }
}
 
Example 15
Source File: SettingsObject.java    From EasySettings with Apache License 2.0 5 votes vote down vote up
/**
 * convenience method for setting the given value to this {@link SettingsObject}
 * and also saving the value in the apps' settings.<br><br/>
 * VERY IMPORTANT!!! <br><br/>
 * this method does NOT perform validity checks on the given "value" and
 * assumes that it is a valid value to be saved as a setting!
 * for example, if this method is called from {@link SeekBarSettingsObject},
 * it is assumed that "value" is between {@link SeekBarSettingsObject#getMinValue()}
 * and {@link SeekBarSettingsObject#getMaxValue()}
 * @param context the context to be used to get the app settings
 * @param value the VALID value to be saved in the apps' settings
 * @throws IllegalArgumentException if the given value is of a type which cannot be saved
 *                                  to SharedPreferences
 */
public void setValueAndSaveSetting(Context context, V value)
		throws IllegalArgumentException
{
	setValue(value);
	Editor editor = EasySettings.retrieveSettingsSharedPrefs(context).edit();

	switch (getType())
	{
		case VOID:
			//no actual value to save
			break;
		case BOOLEAN:
			editor.putBoolean(getKey(), (Boolean) value);
			break;
		case FLOAT:
			editor.putFloat(getKey(), (Float) value);
			break;
		case INTEGER:
			editor.putInt(getKey(), (Integer) value);
			break;
		case LONG:
			editor.putLong(getKey(), (Long) value);
			break;
		case STRING:
			editor.putString(getKey(), (String) value);
			break;
		case STRING_SET:
			editor.putStringSet(getKey(), getStringSetToSave((Set<?>) value));
			break;
		default:
			throw new IllegalArgumentException("parameter \"value\" must be of a type that " +
											   "can be saved in SharedPreferences. given type was "
											   + value.getClass().getName());
	}

	editor.apply();
}
 
Example 16
Source File: SpUtils.java    From Last-Launcher with GNU General Public License v3.0 5 votes vote down vote up
void putStringSet(String key, Set<String> value) {
    if (mPref != null) {
        Editor editor = mPref.edit();
        editor.putStringSet(key, value);
        editor.apply();
    } else throw new RuntimeException("First Initialize context");
}
 
Example 17
Source File: ProfileManager.java    From SimpleOpenVpn-Android with Apache License 2.0 5 votes vote down vote up
public void saveProfileList(Context context) {
    SharedPreferences sharedprefs = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
    Editor editor = sharedprefs.edit();
    editor.putStringSet("vpnlist", profiles.keySet());

    // For reasing I do not understand at all
    // Android saves my prefs file only one time
    // if I remove the debug code below :(
    int counter = sharedprefs.getInt("counter", 0);
    editor.putInt("counter", counter + 1);
    editor.apply();

}
 
Example 18
Source File: Repository.java    From WiFiAnalyzer with GNU General Public License v3.0 4 votes vote down vote up
private void save(@NonNull String key, @NonNull Set<String> values) {
    Editor editor = getSharedPreferences().edit();
    editor.putStringSet(key, values);
    editor.apply();
}
 
Example 19
Source File: ExternalAppDatabase.java    From android with GNU General Public License v3.0 4 votes vote down vote up
private void saveExtAppList(Set<String> allowedapps) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    Editor prefedit = prefs.edit();
    prefedit.putStringSet(PREFERENCES_KEY, allowedapps);
    prefedit.apply();
}
 
Example 20
Source File: PreferVisitor.java    From BaseProject with Apache License 2.0 4 votes vote down vote up
/**
 * 批量存储数据 参数的长度一定要一致 并且keys中的key 与 values中同序号的元素要一一对应,以保证数据存储的正确性
 * @param preferFileName
 * @param keys
 * @param vTypes keys中键key要存储的对应的 values中的元素的数据类型 eg.: keys[0]= "name"; vTypes[0] = V_TYPE_STR;values[0]="ZhangSan"
 * @param values 要存储的值
 */
@Deprecated
@SuppressLint("NewApi")
public boolean saveValue(String preferFileName, String[] keys, byte[] vTypes, Object... values) {
    if (keys == null || vTypes == null || values == null) {
        return false;
    }
    int keysLen = keys.length;
    if (keysLen == 0)
        return false;
    int vTypesLen = vTypes.length;
    int valuesLen = values.length;
    if (vTypesLen == 0 || valuesLen == 0) {
        return false;
    }
    //三者长度取最小的,以保证任何一个数组中不出现异常
    int canOptLen = Math.min(keysLen, Math.min(vTypesLen,valuesLen));
    Editor mEditor = getSpByName(preferFileName).edit();
    for (int i = 0; i < canOptLen; i++) {
        byte vType = vTypes[i];
        String key = keys[i];
        Object v = values[i];
        switch (vType) {
            case V_TYPE_STR:
                mEditor.putString(key, (String) v);
                break;
            case V_TYPE_INT:
                mEditor.putInt(key, (Integer) v);
                break;
            case V_TYPE_BOOL:
                mEditor.putBoolean(key, (Boolean) v);
                break;
            case V_TYPE_LONG:
                mEditor.putLong(key, (Long) v);
                break;
            case V_TYPE_FLOAT:
                mEditor.putFloat(key, (Float) v);
                break;
            case V_TYPE_SET_STR:
                if (Util.isCompateApi(11)) {
                    mEditor.putStringSet(key, (Set<String>) v);
                }
                break;
        }
    }
    return mEditor.commit();
}