android-utils-lib


Source link: https://github.com/GuilhE/android-utils-lib

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.

Resources

AutoValue extension to implement "with-er" methods for AutoValue objects.

Command design pattern implementation for Android Data Binding.

WebP support for Android API Level 4 and up, includes the native library and a WebPImageView to render webp in your app.

A powerful and simple library to open issues on GitHub directly from your app.

Bottom Navigation component inspired by the Google Material Design Guidelines.

Actual Number Picker is an Android custom-built View for choosing numbers. It's simple, elegant, fits in a small-height constraint box and it can be completely customized in XML. You can easily swipe it right and left, or click on arrow controls to increase/decrease the current value.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes