AndroidUtils:
A set of utility classes to hopefully help you save your time.
Version 1.x
- November, 2017 - Gradle update, added Codacy.
- August, 2017 - SharedPrefsUtils.
Getting started
The first step is to include AndroidUtils into your project, for example, as a Gradle compile dependency:
implementation 'com.github.guilhe:android-utils:${
LATEST_VERSION
}
'
Sample usage
SharedPrefsUtils
public static boolean putObject(SharedPreferences prefs, String key, Object object) {
}
public static <T> T getObject(SharedPreferences prefs, String key, TypeToken<T> type, T defaultValue) {
}
public static <T> T getObject(SharedPreferences prefs, String key, Class<T> object, T defaultValue) {
}
To save and load primitive types:
SharedPrefsUtils.putObject(prefs, "key", 1);
int a = SharedPrefsUtils.getObject(prefs, "key", int.class, 1);
To save and load object types:
List<T> list = new ArrayList<>();
SharedPrefsUtils.putObject(prefs, "key", list);
list = SharedPrefsUtils.getObject(prefs, "key", new TypeToken<List<T>>(){
}
, new ArrayList<T>()));
When not using primitive types you should use TypeToken
instead of T.class
, for example:
@Test
public void testOne() throws Exception {
List<Integer> list = new ArrayList<>();
list.add(1);
SharedPrefsUtils.putObject(mPrefs, "key", list);
assertEquals(list, SharedPrefsUtils.getObject(mPrefs, "key", new TypeToken<List<Integer>>(){
}
, new ArrayList<Integer>()));
assertNotEquals(list, SharedPrefsUtils.getObject(mPrefs, "key", List.class, new ArrayList<Integer>()));
}
@Test
public void testTwo() throws Exception {
List<MyObjectType> list = new ArrayList<>();
list.add(new MyObjectType("string", 1, true));
SharedPrefsUtils.putObject(mPrefs, "key", list);
assertEquals(list, SharedPrefsUtils.getObject(mPrefs, "key", new TypeToken<List<MyObjectType>>() {
}
, new ArrayList<MyObjectType>()));
assertNotEquals(list, SharedPrefsUtils.getObject(mPrefs, "key", List.class, new ArrayList<MyObjectType>()));
}
private static class MyObjectType implements Parcelable {
private String mFieldString;
private int mFieldInt;
private boolean mFieldBoolean;
...
}
Both tests will ran to completion.
Regarding assertNotEquals(list, SharedPrefsUtils.getObject(mPrefs, "key", List.class, new ArrayList<Integer>()));
being true, I guess it's related with the fact that public <T> T fromJson(JsonReader reader, Type typeOfT){
}
method from Gson.java
(line 886) is type unsafe: "Since Type is not parameterized by T, this method is type unsafe and should be used carefully". That's why I believe I'm getting List<Double>
instead of List<Integer>
.
Also:
SharedPrefsUtils.putObject(prefs, "key", 1);
SharedPrefsUtils.getObject(prefs, "key", boolean.class, false);
Will throw JsonParseException
.
Binaries
Additional binaries and dependency information for can be found at https://search.maven.org.
Dependencies
Bugs and Feedback
For bugs, questions and discussions please use the Github Issues.
LICENSE
Copyright (c) 2017-present, AndroidUtils Contributors.
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.