This repository has been archived by the owner on Feb 26, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #962 from WonderCsabo/264_sharedpref_string_set
SharefPref StringSet implementation
- Loading branch information
Showing
14 changed files
with
339 additions
and
22 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
.../src/main/java/org/androidannotations/annotations/sharedpreferences/DefaultStringSet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Copyright (C) 2010-2014 eBusiness Information, Excilys Group | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed To in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package org.androidannotations.annotations.sharedpreferences; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.androidannotations.annotations.ResId; | ||
|
||
/** | ||
* Use on methods in {@link SharedPref} annotated class. The generated method | ||
* will return an empty {@link java.util.Set} of Strings by default. | ||
* <p/> | ||
* The key of the preference will be the method name by default. This can be | ||
* overridden by specifying a string resource with the {@link #keyRes()} | ||
* parameter. | ||
* <p/> | ||
* <b>Implementation note:</b> | ||
* <p/> | ||
* Since {@code SharedPreferences.getStringSet} is only available from API 11, | ||
* the generated method serializes the {@code Set<String>} into a {@code String} | ||
* , and persists it with | ||
* {@link android.content.SharedPreferences.Editor#putString(String, String) | ||
* SharedPreferences.Editor#putString(String, String)} using API 10 and below. | ||
* From API 11 and up, the generated method simply uses the native | ||
* {@link android.content.SharedPreferences SharedPreferences} | ||
* {@code Set<String>} methods. | ||
*/ | ||
@Retention(RetentionPolicy.CLASS) | ||
@Target(ElementType.METHOD) | ||
public @interface DefaultStringSet { | ||
|
||
int keyRes() default ResId.DEFAULT_VALUE; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...ions-api/src/main/java/org/androidannotations/api/sharedpreferences/SetXmlSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* Copyright (C) 2010-2014 eBusiness Information, Excilys Group | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed To in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package org.androidannotations.api.sharedpreferences; | ||
|
||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.io.StringWriter; | ||
import java.util.Collections; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
|
||
import org.xmlpull.v1.XmlPullParser; | ||
import org.xmlpull.v1.XmlPullParserException; | ||
import org.xmlpull.v1.XmlSerializer; | ||
|
||
import android.util.Log; | ||
import android.util.Xml; | ||
|
||
public final class SetXmlSerializer { | ||
|
||
private static final String NAMESPACE = ""; | ||
private static final String STRING_TAG = "AA_string"; | ||
private static final String SET_TAG = "AA_set"; | ||
|
||
private SetXmlSerializer() { | ||
|
||
} | ||
|
||
public static String serialize(Set<String> set) { | ||
if (set == null) { | ||
set = Collections.emptySet(); | ||
} | ||
|
||
StringWriter writer = new StringWriter(); | ||
XmlSerializer serializer = Xml.newSerializer(); | ||
|
||
try { | ||
serializer.setOutput(writer); | ||
serializer.startTag(NAMESPACE, SET_TAG); | ||
|
||
for (String string : set) { | ||
serializer.startTag(NAMESPACE, STRING_TAG) // | ||
.text(string) // | ||
.endTag(NAMESPACE, STRING_TAG); | ||
} | ||
|
||
serializer.endTag(NAMESPACE, SET_TAG) // | ||
.endDocument(); | ||
|
||
} catch (IllegalArgumentException e) { | ||
|
||
} catch (IllegalStateException e) { | ||
|
||
} catch (IOException e) { | ||
|
||
} | ||
|
||
return writer.toString(); | ||
} | ||
|
||
public static Set<String> deserialize(String data) { | ||
Set<String> stringSet = new TreeSet<String>(); | ||
XmlPullParser parser = Xml.newPullParser(); | ||
|
||
try { | ||
parser.setInput(new StringReader(data)); | ||
parser.next(); | ||
parser.require(XmlPullParser.START_TAG, NAMESPACE, SET_TAG); | ||
|
||
while (parser.next() != XmlPullParser.END_TAG) { | ||
parser.require(XmlPullParser.START_TAG, NAMESPACE, STRING_TAG); | ||
|
||
parser.next(); | ||
parser.require(XmlPullParser.TEXT, null, null); | ||
stringSet.add(parser.getText()); | ||
|
||
parser.next(); | ||
parser.require(XmlPullParser.END_TAG, null, STRING_TAG); | ||
} | ||
} catch (XmlPullParserException e) { | ||
Log.w("getStringSet", e); | ||
return null; | ||
} catch (IOException e) { | ||
Log.w("getStringSet", e); | ||
return null; | ||
} | ||
|
||
return stringSet; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
.../src/main/java/org/androidannotations/api/sharedpreferences/StringSetPrefEditorField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Copyright (C) 2010-2014 eBusiness Information, Excilys Group | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed To in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package org.androidannotations.api.sharedpreferences; | ||
|
||
import java.util.Set; | ||
|
||
public final class StringSetPrefEditorField<T extends EditorHelper<T>> extends AbstractPrefEditorField<T> { | ||
|
||
StringSetPrefEditorField(T editorHelper, String key) { | ||
super(editorHelper, key); | ||
} | ||
|
||
public T put(Set<String> value) { | ||
SharedPreferencesCompat.putStringSet(editorHelper.getEditor(), key, value); | ||
return editorHelper; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...ns-api/src/main/java/org/androidannotations/api/sharedpreferences/StringSetPrefField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Copyright (C) 2010-2014 eBusiness Information, Excilys Group | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed To in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package org.androidannotations.api.sharedpreferences; | ||
|
||
import java.util.Set; | ||
|
||
import android.content.SharedPreferences; | ||
|
||
public final class StringSetPrefField extends AbstractPrefField { | ||
|
||
private final Set<String> defaultValue; | ||
|
||
StringSetPrefField(SharedPreferences sharedPreferences, String key, Set<String> defaultValue) { | ||
super(sharedPreferences, key); | ||
this.defaultValue = defaultValue; | ||
} | ||
|
||
public Set<String> get() { | ||
return getOr(defaultValue); | ||
} | ||
|
||
public Set<String> getOr(Set<String> defaultValue) { | ||
return SharedPreferencesCompat.getStringSet(sharedPreferences, key, defaultValue); | ||
} | ||
|
||
public void put(Set<String> value) { | ||
SharedPreferences.Editor editor = sharedPreferences.edit(); | ||
SharedPreferencesCompat.putStringSet(editor, key, value); | ||
apply(editor); | ||
} | ||
} |
Oops, something went wrong.