├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE.txt ├── README.md ├── build.gradle ├── examples └── proguard │ ├── README.md │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── net │ │ └── orange_box │ │ └── storebox │ │ └── example │ │ └── proguard │ │ ├── MainActivity.java │ │ └── Preferences.java │ └── res │ ├── layout │ └── activity_main.xml │ └── values │ └── values.xml ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle ├── storebox-harness ├── README.md ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ ├── AndroidManifest.xml │ └── java │ │ └── net │ │ └── orange_box │ │ └── storebox │ │ └── harness │ │ ├── ChainingMethodsTestCase.java │ │ ├── ClearMethodTestCase.kt │ │ ├── CustomInstrumentationTestRunner.java │ │ ├── Extensions.kt │ │ ├── ForwardingMethodsTestCase.java │ │ ├── KeysAndDefaultValuesTestCase.java │ │ ├── PreferencesTypeAndModeTestCase.java │ │ ├── RemoveMethodTestCase.java │ │ ├── SaveModeTestCase.java │ │ ├── base │ │ └── PreferencesTestCase.java │ │ ├── changes │ │ └── ChangesListenersTestCase.java │ │ └── types │ │ ├── CustomTypesTestCase.java │ │ └── ValueTypesTestCase.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── net │ │ └── orange_box │ │ └── storebox │ │ └── harness │ │ ├── activities │ │ └── TestActivity.java │ │ ├── interfaces │ │ ├── ChainingMethodsInterface.java │ │ ├── ForwardingMethodsInterface.java │ │ ├── KeysAndDefaultValuesInterface.java │ │ ├── PreferencesModeMultiProcessInterface.java │ │ ├── PreferencesModePrivateInterface.java │ │ ├── PreferencesModeWordWriteableInterface.java │ │ ├── PreferencesModeWorldReadableInterface.java │ │ ├── PreferencesTypeActivityInterface.java │ │ ├── PreferencesTypeDefaultSharedInterface.java │ │ ├── PreferencesTypeFileInterface.java │ │ ├── RemoveMethodInterface.java │ │ ├── SaveModeApplyInterface.java │ │ ├── SaveModeCommitInterface.java │ │ ├── changes │ │ │ └── ChangeListenersInterface.java │ │ └── types │ │ │ ├── CustomTypesInterface.java │ │ │ └── ValueTypesInterface.java │ │ └── types │ │ ├── CustomClass.java │ │ ├── CustomEnum.java │ │ └── adapters │ │ ├── CustomClassListTypeAdapter.java │ │ └── CustomClassTypeAdapter.java │ ├── kotlin │ └── net │ │ └── orange_box │ │ └── storebox │ │ └── harness │ │ └── interfaces │ │ └── ClearMethodInterface.kt │ └── res │ └── values │ └── values.xml └── storebox-lib ├── .gitignore ├── README.md ├── build.gradle └── src └── main ├── AndroidManifest.xml └── java └── net └── orange_box └── storebox ├── StoreBox.java ├── StoreBoxInvocationHandler.java ├── adapters ├── StoreBoxTypeAdapter.java ├── StoreType.java ├── base │ ├── BaseBooleanTypeAdapter.java │ ├── BaseFloatTypeAdapter.java │ ├── BaseIntegerTypeAdapter.java │ ├── BaseLongTypeAdapter.java │ ├── BaseStringSetTypeAdapter.java │ └── BaseStringTypeAdapter.java ├── extra │ ├── DateTypeAdapter.java │ ├── DoubleTypeAdapter.java │ ├── EnumTypeAdapter.java │ └── UriTypeAdapter.java └── standard │ ├── BooleanTypeAdapter.java │ ├── FloatTypeAdapter.java │ ├── IntegerTypeAdapter.java │ ├── LongTypeAdapter.java │ ├── StringSetTypeAdapter.java │ └── StringTypeAdapter.java ├── annotations ├── method │ ├── ClearMethod.java │ ├── DefaultValue.java │ ├── KeyByResource.java │ ├── KeyByString.java │ ├── RegisterChangeListenerMethod.java │ ├── RemoveMethod.java │ ├── TypeAdapter.java │ └── UnregisterChangeListenerMethod.java ├── option │ └── SaveOption.java └── type │ ├── ActivityPreferences.java │ ├── DefaultSharedPreferences.java │ └── FilePreferences.java ├── enums ├── PreferencesMode.java ├── PreferencesType.java └── SaveMode.java ├── handlers ├── ChangeListenerMethodHandler.java └── MethodHandler.java ├── listeners └── OnPreferenceValueChangedListener.java └── utils ├── MethodUtils.java ├── PreferenceUtils.java └── TypeUtils.java /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.iml 3 | 4 | local.properties 5 | publishing.properties 6 | 7 | .gradle 8 | build/ 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: android 2 | jdk: oraclejdk8 3 | sudo : false 4 | 5 | env: 6 | global: 7 | - ADB_INSTALL_TIMEOUT=5 8 | matrix: 9 | - ANDROID_SDKS=sys-img-armeabi-android-10 ANDROID_TARGET=android-10 ANDROID_ABI=armeabi 10 | - ANDROID_SDKS=sys-img-armeabi-v7a-android-23 ANDROID_TARGET=android-23 ANDROID_ABI=armeabi-v7a 11 | - ANDROID_SDKS=sys-img-armeabi-v7a-android-19 ANDROID_TARGET=android-19 ANDROID_ABI=armeabi-v7a 12 | 13 | android: 14 | components: 15 | - platform-tools 16 | - tools 17 | - build-tools-23.0.3 18 | - android-23 19 | - extra-android-m2repository 20 | - $ANDROID_SDKS 21 | 22 | before_script: 23 | - echo no | android create avd --force -n test -t $ANDROID_TARGET --abi $ANDROID_ABI 24 | - emulator -avd test -no-skin -no-audio -no-window & 25 | - android-wait-for-emulator 26 | - adb shell input keyevent 82 & 27 | 28 | script: 29 | - ./gradlew clean build connectedCheck 30 | 31 | before_cache: 32 | - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock 33 | cache: 34 | directories: 35 | - $HOME/.gradle/caches/ 36 | - $HOME/.gradle/wrapper/ 37 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 1.4.0 (2016-04-17) 2 | - added clear method annotation #15 3 | 4 | ## 1.3.0 (2015-06-21) 5 | - added support for preference change listeners #9 6 | - added support for double/Double types #10 7 | 8 | ## 1.2.1 (2015-06-15) 9 | - added support to set nulls for Date values 10 | - fixed NPE when using Date values 11 | 12 | ## 1.2.0 (2015-05-31) 13 | - added support for custom types #3 14 | - removed default value modes 15 | 16 | ## 1.1.0 (2015-05-16) 17 | - added support for remove methods #8 18 | 19 | ## 1.0.1 (2015-05-15) 20 | - fixed exception when passing null for string setters #8 21 | 22 | ## 1.0.0 (2015-04-11) 23 | - initial release 24 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | jcenter() 4 | } 5 | 6 | dependencies { 7 | classpath 'com.android.tools.build:gradle:2.0.0' 8 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' 9 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6' 10 | } 11 | } 12 | 13 | allprojects { 14 | repositories { 15 | jcenter() 16 | } 17 | } 18 | 19 | ext { 20 | versionName = '1.4.0' 21 | 22 | compileSdkVersion = 23 23 | buildToolsVersion = '23.0.3' 24 | 25 | minSdkVersion = 10 26 | targetSdkVersion = 23 27 | 28 | sourceCompatibilityVersion = JavaVersion.VERSION_1_6 29 | targetCompatibilityVersion = JavaVersion.VERSION_1_6 30 | } 31 | 32 | task wrapper(type: Wrapper) { 33 | gradleVersion = '2.12' 34 | } 35 | -------------------------------------------------------------------------------- /examples/proguard/README.md: -------------------------------------------------------------------------------- 1 | ### StoreBox Harness ### 2 | The harness module for StoreBox which uses the ```lib``` module for instrumenting the library and running automated tests. 3 | -------------------------------------------------------------------------------- /examples/proguard/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion project.compileSdkVersion 5 | buildToolsVersion project.buildToolsVersion 6 | 7 | defaultConfig { 8 | applicationId 'net.orange_box.storebox.example.proguard' 9 | 10 | minSdkVersion project.minSdkVersion 11 | targetSdkVersion project.targetSdkVersion 12 | 13 | versionCode 1 14 | versionName project.versionName 15 | } 16 | 17 | compileOptions { 18 | sourceCompatibility project.sourceCompatibilityVersion 19 | targetCompatibility project.targetCompatibilityVersion 20 | } 21 | 22 | signingConfigs { 23 | release.initWith(debug) 24 | } 25 | 26 | buildTypes { 27 | release { 28 | minifyEnabled true 29 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 30 | 31 | signingConfig signingConfigs.release 32 | } 33 | 34 | releaseOptimize { 35 | minifyEnabled true 36 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 37 | 38 | signingConfig signingConfigs.release 39 | } 40 | } 41 | } 42 | 43 | dependencies { 44 | compile project(':storebox-lib') 45 | } 46 | -------------------------------------------------------------------------------- /examples/proguard/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /usr/local/src/android-sdk-linux_x86/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | -dontwarn net.jodah.typetools.TypeResolver 20 | -keep class net.orange_box.storebox.** { *; } 21 | -keepattributes *Annotation*,Exceptions,InnerClasses,Signature 22 | -------------------------------------------------------------------------------- /examples/proguard/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /examples/proguard/src/main/java/net/orange_box/storebox/example/proguard/MainActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.example.proguard; 18 | 19 | import android.app.Activity; 20 | import android.os.Bundle; 21 | 22 | import net.orange_box.storebox.StoreBox; 23 | import net.orange_box.storebox.listeners.OnPreferenceValueChangedListener; 24 | 25 | import java.util.concurrent.atomic.AtomicInteger; 26 | 27 | public final class MainActivity extends Activity { 28 | 29 | @Override 30 | protected void onCreate(Bundle savedInstanceState) { 31 | super.onCreate(savedInstanceState); 32 | 33 | final Preferences prefs = StoreBox.create(this, Preferences.class); 34 | prefs.clear(); 35 | 36 | final AtomicInteger listenerValue = new AtomicInteger(-1); 37 | final OnPreferenceValueChangedListener listener = 38 | new OnPreferenceValueChangedListener() { 39 | @Override 40 | public void onChanged(Integer newValue) { 41 | listenerValue.set(newValue); 42 | } 43 | }; 44 | 45 | prefs.regIntListener(listener); 46 | 47 | if (prefs.getInt() != 1) { 48 | throw new RuntimeException("Failed at getInt() default value"); 49 | } 50 | 51 | prefs.setInt(2); 52 | 53 | if (prefs.getInt() != 2) { 54 | throw new RuntimeException("Failed at getInt() after setInt()"); 55 | } 56 | 57 | if (listenerValue.get() != 2) { 58 | throw new RuntimeException("Listener was not invoked"); 59 | } 60 | 61 | prefs.unregIntListener(listener); 62 | prefs.setInt(3); 63 | 64 | if (listenerValue.get() != 2) { 65 | throw new RuntimeException("Listener shouldn't have been invoked"); 66 | } 67 | 68 | if (getPreferences(MODE_PRIVATE).getAll().isEmpty()) { 69 | throw new RuntimeException("Not saved to activity preferences"); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /examples/proguard/src/main/java/net/orange_box/storebox/example/proguard/Preferences.java: -------------------------------------------------------------------------------- 1 | package net.orange_box.storebox.example.proguard; 2 | 3 | import net.orange_box.storebox.annotations.method.ClearMethod; 4 | import net.orange_box.storebox.annotations.method.DefaultValue; 5 | import net.orange_box.storebox.annotations.method.KeyByResource; 6 | import net.orange_box.storebox.annotations.method.KeyByString; 7 | import net.orange_box.storebox.annotations.method.RegisterChangeListenerMethod; 8 | import net.orange_box.storebox.annotations.method.UnregisterChangeListenerMethod; 9 | import net.orange_box.storebox.annotations.type.ActivityPreferences; 10 | import net.orange_box.storebox.listeners.OnPreferenceValueChangedListener; 11 | 12 | @ActivityPreferences 13 | interface Preferences { 14 | 15 | @KeyByString("int") 16 | @DefaultValue(R.integer.default_int) 17 | int getInt(); 18 | 19 | @KeyByResource(R.string.key_int) 20 | void setInt(int value); 21 | 22 | @RegisterChangeListenerMethod 23 | @KeyByString("int") 24 | void regIntListener(OnPreferenceValueChangedListener listener); 25 | 26 | @UnregisterChangeListenerMethod 27 | @KeyByResource(R.string.key_int) 28 | void unregIntListener(OnPreferenceValueChangedListener listener); 29 | 30 | @ClearMethod 31 | void clear(); 32 | } 33 | -------------------------------------------------------------------------------- /examples/proguard/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | -------------------------------------------------------------------------------- /examples/proguard/src/main/res/values/values.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | int 4 | 5 | 1 6 | 7 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true 19 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martino2k6/StoreBox/232ca7a7eea4e5428b3fc35663764086649943c1/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Apr 05 12:20:02 BST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-bin.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':storebox-lib' 2 | include ':storebox-harness' 3 | include ':example-proguard' 4 | project(':example-proguard').projectDir = file('examples/proguard') 5 | -------------------------------------------------------------------------------- /storebox-harness/README.md: -------------------------------------------------------------------------------- 1 | ### StoreBox Harness ### 2 | The harness module for StoreBox which uses the ```lib``` module for instrumenting the library and running automated tests. 3 | -------------------------------------------------------------------------------- /storebox-harness/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion project.compileSdkVersion 7 | buildToolsVersion project.buildToolsVersion 8 | 9 | defaultConfig { 10 | applicationId 'net.orange_box.storebox.harness' 11 | 12 | minSdkVersion project.minSdkVersion 13 | targetSdkVersion project.targetSdkVersion 14 | 15 | versionCode 1 16 | versionName project.versionName 17 | 18 | testInstrumentationRunner 'net.orange_box.storebox.harness.CustomInstrumentationTestRunner' 19 | } 20 | 21 | sourceSets { 22 | main { 23 | java.srcDirs += 'src/main/kotlin' 24 | } 25 | 26 | androidTest { 27 | java.srcDirs += 'androidTest/main/kotlin' 28 | } 29 | } 30 | 31 | buildTypes { 32 | debug { 33 | testCoverageEnabled true 34 | } 35 | 36 | release { 37 | minifyEnabled false 38 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 39 | } 40 | } 41 | 42 | adbOptions { 43 | timeOutInMs 5 * 60 * 1000 44 | } 45 | } 46 | 47 | dependencies { 48 | compile project(':storebox-lib') 49 | 50 | compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 51 | compile 'com.google.code.gson:gson:2.6.2' 52 | 53 | androidTestCompile 'com.google.dexmaker:dexmaker:1.2' 54 | androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2' 55 | androidTestCompile 'org.mockito:mockito-core:1.10.19' 56 | } 57 | 58 | // https://github.com/dam5s/android-example-kotlin/issues/1 59 | buildscript { 60 | ext.kotlin_version = '1.0.1' 61 | 62 | repositories { 63 | jcenter() 64 | } 65 | 66 | dependencies { 67 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 68 | classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /storebox-harness/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /usr/local/src/android-sdk-linux_x86/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /storebox-harness/src/androidTest/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /storebox-harness/src/androidTest/java/net/orange_box/storebox/harness/ChainingMethodsTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness; 18 | 19 | import android.annotation.SuppressLint; 20 | import android.content.SharedPreferences; 21 | import android.preference.PreferenceManager; 22 | import android.test.InstrumentationTestCase; 23 | import android.test.suitebuilder.annotation.SmallTest; 24 | 25 | import net.orange_box.storebox.StoreBox; 26 | import net.orange_box.storebox.harness.interfaces.ChainingMethodsInterface; 27 | 28 | public class ChainingMethodsTestCase extends InstrumentationTestCase { 29 | 30 | private ChainingMethodsInterface uut; 31 | private SharedPreferences prefs; 32 | 33 | @Override 34 | protected void setUp() throws Exception { 35 | super.setUp(); 36 | 37 | uut = StoreBox.create( 38 | getInstrumentation().getTargetContext(), 39 | ChainingMethodsInterface.class); 40 | 41 | prefs = PreferenceManager.getDefaultSharedPreferences( 42 | getInstrumentation().getTargetContext()); 43 | } 44 | 45 | @SuppressLint("CommitPrefEdits") 46 | @Override 47 | protected void tearDown() throws Exception { 48 | uut = null; 49 | 50 | // we are saving to the actual preferences so let's clear them 51 | prefs.edit().clear().commit(); 52 | prefs = null; 53 | 54 | super.tearDown(); 55 | } 56 | 57 | @SmallTest 58 | public void testChainingSetMethods() { 59 | assertTrue(uut.setFirstValue("one") instanceof SharedPreferences.Editor); 60 | assertSame(uut, uut.setSecondValue("two")); 61 | } 62 | 63 | @SmallTest 64 | public void testChainingRemoveMethods() { 65 | assertTrue(uut.removeFirstValue() instanceof SharedPreferences.Editor); 66 | assertSame(uut, uut.removeSecondValue()); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /storebox-harness/src/androidTest/java/net/orange_box/storebox/harness/ClearMethodTestCase.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015-2016 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness 18 | 19 | import android.test.InstrumentationTestCase 20 | import android.test.suitebuilder.annotation.SmallTest 21 | import net.orange_box.storebox.harness.interfaces.ClearMethodInterface 22 | 23 | class ClearMethodTestCase : InstrumentationTestCase() { 24 | 25 | @SmallTest 26 | fun testClear() { 27 | with(prefs()) { 28 | edit().putInt("int", 1).apply() 29 | 30 | with(storeBox()) { 31 | clear() 32 | assertTrue(all.isEmpty()) 33 | } 34 | 35 | edit().clear().apply() 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /storebox-harness/src/androidTest/java/net/orange_box/storebox/harness/CustomInstrumentationTestRunner.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness; 18 | 19 | import android.os.Bundle; 20 | import android.test.InstrumentationTestRunner; 21 | 22 | public class CustomInstrumentationTestRunner extends InstrumentationTestRunner { 23 | 24 | @Override 25 | public void onCreate(Bundle arguments) { 26 | super.onCreate(arguments); 27 | 28 | System.setProperty( 29 | "dexmaker.dexcache", 30 | getTargetContext().getCacheDir().toString()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /storebox-harness/src/androidTest/java/net/orange_box/storebox/harness/Extensions.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015-2016 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness 18 | 19 | import android.content.SharedPreferences 20 | import android.preference.PreferenceManager 21 | import android.test.InstrumentationTestCase 22 | import net.orange_box.storebox.StoreBox 23 | 24 | fun InstrumentationTestCase.prefs(): SharedPreferences { 25 | return PreferenceManager.getDefaultSharedPreferences( 26 | instrumentation.targetContext) 27 | } 28 | 29 | inline fun InstrumentationTestCase.storeBox(): T { 30 | return StoreBox.create(instrumentation.targetContext, T::class.java) 31 | } 32 | -------------------------------------------------------------------------------- /storebox-harness/src/androidTest/java/net/orange_box/storebox/harness/ForwardingMethodsTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness; 18 | 19 | import android.annotation.SuppressLint; 20 | import android.app.Instrumentation; 21 | import android.content.ContextWrapper; 22 | import android.content.Intent; 23 | import android.content.SharedPreferences; 24 | import android.test.ActivityUnitTestCase; 25 | import android.test.suitebuilder.annotation.SmallTest; 26 | 27 | import net.orange_box.storebox.StoreBox; 28 | import net.orange_box.storebox.harness.activities.TestActivity; 29 | import net.orange_box.storebox.harness.interfaces.ForwardingMethodsInterface; 30 | 31 | import static org.mockito.Matchers.eq; 32 | import static org.mockito.Mockito.mock; 33 | import static org.mockito.Mockito.verify; 34 | import static org.mockito.Mockito.when; 35 | 36 | public class ForwardingMethodsTestCase extends ActivityUnitTestCase { 37 | 38 | private SharedPreferences prefs; 39 | private SharedPreferences.Editor editor; 40 | private ForwardingMethodsInterface uut; 41 | 42 | public ForwardingMethodsTestCase() { 43 | super(TestActivity.class); 44 | } 45 | 46 | @SuppressLint("CommitPrefEdits") 47 | @Override 48 | protected void setUp() throws Exception { 49 | super.setUp(); 50 | 51 | prefs = mock(SharedPreferences.class); 52 | editor = mock(SharedPreferences.Editor.class); 53 | 54 | when(prefs.edit()).thenReturn(editor); 55 | 56 | setActivityContext(new InjectedContext(getInstrumentation(), prefs)); 57 | startActivity( 58 | new Intent( 59 | getInstrumentation().getTargetContext(), 60 | TestActivity.class), 61 | null, 62 | null); 63 | 64 | uut = StoreBox.create(getActivity(), ForwardingMethodsInterface.class); 65 | } 66 | 67 | @Override 68 | protected void tearDown() throws Exception { 69 | prefs = null; 70 | editor = null; 71 | uut = null; 72 | 73 | super.tearDown(); 74 | } 75 | 76 | @SmallTest 77 | public void testSharedPreferencesMethodsForwarded() { 78 | uut.getString("key", "default"); 79 | 80 | verify(prefs).getString(eq("key"), eq("default")); 81 | } 82 | 83 | @SmallTest 84 | public void testEditorMethodsForwarded() { 85 | uut.putString("key", "value"); 86 | uut.apply(); 87 | 88 | verify(editor).putString(eq("key"), eq("value")); 89 | verify(editor).apply(); 90 | } 91 | 92 | private static class InjectedContext extends ContextWrapper { 93 | 94 | private final SharedPreferences prefs; 95 | 96 | public InjectedContext( 97 | Instrumentation instr, SharedPreferences prefs) { 98 | 99 | super(instr.getTargetContext()); 100 | 101 | this.prefs = prefs; 102 | } 103 | 104 | @Override 105 | public SharedPreferences getSharedPreferences(String name, int mode) { 106 | return prefs; 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /storebox-harness/src/androidTest/java/net/orange_box/storebox/harness/KeysAndDefaultValuesTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness; 18 | 19 | import android.annotation.SuppressLint; 20 | import android.app.Instrumentation; 21 | import android.content.ContextWrapper; 22 | import android.content.SharedPreferences; 23 | import android.test.InstrumentationTestCase; 24 | import android.test.suitebuilder.annotation.SmallTest; 25 | 26 | import net.orange_box.storebox.StoreBox; 27 | import net.orange_box.storebox.harness.interfaces.KeysAndDefaultValuesInterface; 28 | 29 | import static org.mockito.Matchers.any; 30 | import static org.mockito.Matchers.eq; 31 | import static org.mockito.Mockito.mock; 32 | import static org.mockito.Mockito.times; 33 | import static org.mockito.Mockito.verify; 34 | import static org.mockito.Mockito.when; 35 | 36 | public class KeysAndDefaultValuesTestCase extends InstrumentationTestCase { 37 | 38 | private SharedPreferences prefs; 39 | private SharedPreferences.Editor editor; 40 | 41 | private KeysAndDefaultValuesInterface uut; 42 | 43 | @SuppressLint("CommitPrefEdits") 44 | @Override 45 | protected void setUp() throws Exception { 46 | super.setUp(); 47 | 48 | prefs = mock(SharedPreferences.class); 49 | editor = mock(SharedPreferences.Editor.class); 50 | 51 | when(prefs.edit()).thenReturn(editor); 52 | 53 | uut = StoreBox.create( 54 | new InjectedContext(getInstrumentation(), prefs), 55 | KeysAndDefaultValuesInterface.class); 56 | } 57 | 58 | @Override 59 | protected void tearDown() throws Exception { 60 | uut = null; 61 | 62 | prefs = null; 63 | editor = null; 64 | 65 | super.tearDown(); 66 | } 67 | 68 | @SmallTest 69 | public void testKeyByString() { 70 | uut.getValue(); 71 | verify(prefs).getString(eq("string"), any(String.class)); 72 | 73 | uut.setValue("value"); 74 | verify(editor).putString(eq("string"), any(String.class)); 75 | } 76 | 77 | @SmallTest 78 | public void testKeyByResource() { 79 | uut.getValueAlt(); 80 | verify(prefs).getString(eq("string"), any(String.class)); 81 | 82 | uut.setValueAlt("value"); 83 | verify(editor).putString(eq("string"), any(String.class)); 84 | } 85 | 86 | @SmallTest 87 | public void testGetWithDefault() { 88 | uut.getValueWithDefault(); 89 | uut.getValueWithDefault("default"); 90 | 91 | verify(prefs, times(2)).getString(eq("string"), eq("default")); 92 | } 93 | 94 | @SmallTest 95 | public void testGetWithDefaultPrecedence() { 96 | uut.getValueWithDefaultPrecedence("test"); 97 | verify(prefs).getString(eq("string"), eq("test")); 98 | } 99 | 100 | @SmallTest 101 | public void testGetWithBadDefault() { 102 | try { 103 | uut.getValueWithBadDefault(); 104 | 105 | fail("Exception not thrown"); 106 | } catch (UnsupportedOperationException e) { 107 | // pass 108 | } 109 | 110 | try { 111 | uut.getValueWithBadDefault(true); 112 | 113 | fail("Exception not thrown"); 114 | } catch (UnsupportedOperationException e) { 115 | // pass 116 | } 117 | } 118 | 119 | private static class InjectedContext extends ContextWrapper { 120 | 121 | private final SharedPreferences prefs; 122 | 123 | public InjectedContext(Instrumentation instr, SharedPreferences prefs) { 124 | super(instr.getTargetContext()); 125 | 126 | this.prefs = prefs; 127 | } 128 | 129 | @Override 130 | public SharedPreferences getSharedPreferences(String name, int mode) { 131 | return prefs; 132 | } 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /storebox-harness/src/androidTest/java/net/orange_box/storebox/harness/PreferencesTypeAndModeTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness; 18 | 19 | import android.annotation.TargetApi; 20 | import android.app.Instrumentation; 21 | import android.content.Context; 22 | import android.content.ContextWrapper; 23 | import android.content.Intent; 24 | import android.content.SharedPreferences; 25 | import android.os.Build; 26 | import android.test.ActivityUnitTestCase; 27 | import android.test.suitebuilder.annotation.SmallTest; 28 | 29 | import net.orange_box.storebox.StoreBox; 30 | import net.orange_box.storebox.harness.activities.TestActivity; 31 | import net.orange_box.storebox.harness.interfaces.PreferencesModeMultiProcessInterface; 32 | import net.orange_box.storebox.harness.interfaces.PreferencesModePrivateInterface; 33 | import net.orange_box.storebox.harness.interfaces.PreferencesModeWordWriteableInterface; 34 | import net.orange_box.storebox.harness.interfaces.PreferencesModeWorldReadableInterface; 35 | import net.orange_box.storebox.harness.interfaces.PreferencesTypeActivityInterface; 36 | import net.orange_box.storebox.harness.interfaces.PreferencesTypeDefaultSharedInterface; 37 | import net.orange_box.storebox.harness.interfaces.PreferencesTypeFileInterface; 38 | 39 | import java.util.concurrent.atomic.AtomicInteger; 40 | 41 | public class PreferencesTypeAndModeTestCase extends ActivityUnitTestCase { 42 | 43 | public PreferencesTypeAndModeTestCase() { 44 | super(TestActivity.class); 45 | } 46 | 47 | @SmallTest 48 | public void testTypeDefaultShared() { 49 | final AtomicInteger count = new AtomicInteger(1); 50 | setActivityContext(new InjectedContext( 51 | getInstrumentation(), 52 | getInstrumentation().getTargetContext().getPackageName() + "_preferences", 53 | null, 54 | count)); 55 | startActivity(); 56 | 57 | StoreBox.create(getActivity(), PreferencesTypeDefaultSharedInterface.class); 58 | 59 | assertEquals(0, count.get()); 60 | } 61 | 62 | @SmallTest 63 | public void testTypeActivity() { 64 | final AtomicInteger count = new AtomicInteger(1); 65 | setActivityContext(new InjectedContext( 66 | getInstrumentation(), 67 | TestActivity.class.getName().substring(getInstrumentation() 68 | .getTargetContext().getPackageName().length() + 1), 69 | null, 70 | count)); 71 | startActivity(); 72 | 73 | StoreBox.create(getActivity(), PreferencesTypeActivityInterface.class); 74 | 75 | assertEquals(0, count.get()); 76 | } 77 | 78 | @SmallTest 79 | public void testTypeFile() { 80 | final AtomicInteger count = new AtomicInteger(1); 81 | setActivityContext(new InjectedContext( 82 | getInstrumentation(), "test", null, count)); 83 | startActivity(); 84 | 85 | StoreBox.create(getActivity(), PreferencesTypeFileInterface.class); 86 | 87 | assertEquals(0, count.get()); 88 | } 89 | 90 | @SmallTest 91 | public void testModePrivate() { 92 | final AtomicInteger count = new AtomicInteger(1); 93 | setActivityContext(new InjectedContext( 94 | getInstrumentation(), null, Context.MODE_PRIVATE, count)); 95 | startActivity(); 96 | 97 | StoreBox.create(getActivity(), PreferencesModePrivateInterface.class); 98 | 99 | assertEquals(0, count.get()); 100 | } 101 | 102 | @TargetApi(Build.VERSION_CODES.HONEYCOMB) 103 | @SmallTest 104 | public void testModeMultiProcess() { 105 | final AtomicInteger count = new AtomicInteger(1); 106 | setActivityContext(new InjectedContext( 107 | getInstrumentation(), null, Context.MODE_MULTI_PROCESS, count)); 108 | startActivity(); 109 | 110 | StoreBox.create(getActivity(), PreferencesModeMultiProcessInterface.class); 111 | 112 | assertEquals(0, count.get()); 113 | } 114 | 115 | @SuppressWarnings("deprecation") 116 | @SmallTest 117 | public void testModeWorldReadable() { 118 | final AtomicInteger count = new AtomicInteger(1); 119 | setActivityContext(new InjectedContext( 120 | getInstrumentation(), null, Context.MODE_WORLD_READABLE, count)); 121 | startActivity(); 122 | 123 | StoreBox.create(getActivity(), PreferencesModeWorldReadableInterface.class); 124 | 125 | assertEquals(0, count.get()); 126 | } 127 | 128 | @SuppressWarnings("deprecation") 129 | @SmallTest 130 | public void testModeWorldWriteable() { 131 | final AtomicInteger count = new AtomicInteger(1); 132 | setActivityContext(new InjectedContext( 133 | getInstrumentation(), null, Context.MODE_WORLD_WRITEABLE, count)); 134 | startActivity(); 135 | 136 | StoreBox.create(getActivity(), PreferencesModeWordWriteableInterface.class); 137 | 138 | assertEquals(0, count.get()); 139 | } 140 | 141 | private void startActivity() { 142 | startActivity( 143 | new Intent( 144 | getInstrumentation().getTargetContext(), 145 | TestActivity.class), 146 | null, 147 | null); 148 | } 149 | 150 | private static class InjectedContext extends ContextWrapper { 151 | 152 | private final String expectedName; 153 | private final Integer expectedMode; 154 | private final AtomicInteger count; 155 | 156 | public InjectedContext( 157 | Instrumentation instr, 158 | String expectedName, 159 | Integer expectedMode, 160 | AtomicInteger count) { 161 | 162 | super(instr.getTargetContext()); 163 | 164 | this.expectedName = expectedName; 165 | this.expectedMode = expectedMode; 166 | this.count = count; 167 | } 168 | 169 | @Override 170 | public SharedPreferences getSharedPreferences(String name, int mode) { 171 | // FIXME better way to only check the call we're interested in 172 | if (name != null && name.equals("net.orange_box.storebox.versions")) { 173 | return super.getSharedPreferences(name, mode); 174 | } 175 | 176 | if (expectedName != null) { 177 | assertEquals(expectedName, name); 178 | } 179 | if (expectedMode != null) { 180 | assertEquals(expectedMode.intValue(), mode); 181 | } 182 | count.decrementAndGet(); 183 | 184 | return super.getSharedPreferences(name, mode); 185 | } 186 | } 187 | } 188 | -------------------------------------------------------------------------------- /storebox-harness/src/androidTest/java/net/orange_box/storebox/harness/RemoveMethodTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness; 18 | 19 | import android.annotation.SuppressLint; 20 | import android.content.SharedPreferences; 21 | import android.preference.PreferenceManager; 22 | import android.test.InstrumentationTestCase; 23 | import android.test.suitebuilder.annotation.SmallTest; 24 | 25 | import net.orange_box.storebox.StoreBox; 26 | import net.orange_box.storebox.harness.interfaces.RemoveMethodInterface; 27 | 28 | public class RemoveMethodTestCase extends InstrumentationTestCase { 29 | 30 | private static final String KEY = "int"; 31 | private static final int VALUE = 1; 32 | 33 | private RemoveMethodInterface uut; 34 | private SharedPreferences prefs; 35 | 36 | @SuppressLint("CommitPrefEdits") 37 | @Override 38 | protected void setUp() throws Exception { 39 | super.setUp(); 40 | 41 | uut = StoreBox.create( 42 | getInstrumentation().getTargetContext(), RemoveMethodInterface.class); 43 | 44 | prefs = PreferenceManager.getDefaultSharedPreferences( 45 | getInstrumentation().getTargetContext()); 46 | 47 | prefs.edit().putInt(KEY, VALUE).commit(); 48 | } 49 | 50 | @SuppressLint("CommitPrefEdits") 51 | @Override 52 | protected void tearDown() throws Exception { 53 | uut = null; 54 | 55 | // we are saving to the actual preferences so let's clear them 56 | prefs.edit().clear().commit(); 57 | prefs = null; 58 | 59 | super.tearDown(); 60 | } 61 | 62 | @SmallTest 63 | public void testRemoveUsingStringKey() { 64 | uut.removeUsingStringKey(KEY); 65 | assertFalse(prefs.contains(KEY)); 66 | } 67 | 68 | @SmallTest 69 | public void testRemoveUsingIntKey() { 70 | uut.removeUsingIntKey(R.string.key_int); 71 | assertFalse(prefs.contains(KEY)); 72 | } 73 | 74 | @SmallTest 75 | public void testRemoveWithStringAnnotation() { 76 | uut.removeWithStringAnnotation(); 77 | assertFalse(prefs.contains(KEY)); 78 | } 79 | 80 | @SmallTest 81 | public void testRemoveWithResourceAnnotation() { 82 | uut.removeWithResourceAnnotation(); 83 | assertFalse(prefs.contains(KEY)); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /storebox-harness/src/androidTest/java/net/orange_box/storebox/harness/SaveModeTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness; 18 | 19 | import android.annotation.SuppressLint; 20 | import android.app.Instrumentation; 21 | import android.content.ContextWrapper; 22 | import android.content.Intent; 23 | import android.content.SharedPreferences; 24 | import android.test.ActivityUnitTestCase; 25 | import android.test.suitebuilder.annotation.SmallTest; 26 | 27 | import net.orange_box.storebox.StoreBox; 28 | import net.orange_box.storebox.harness.activities.TestActivity; 29 | import net.orange_box.storebox.harness.interfaces.SaveModeApplyInterface; 30 | import net.orange_box.storebox.harness.interfaces.SaveModeCommitInterface; 31 | 32 | import static org.mockito.Mockito.mock; 33 | import static org.mockito.Mockito.never; 34 | import static org.mockito.Mockito.verify; 35 | import static org.mockito.Mockito.when; 36 | 37 | public class SaveModeTestCase extends ActivityUnitTestCase { 38 | 39 | private SharedPreferences prefs; 40 | private SharedPreferences.Editor editor; 41 | 42 | public SaveModeTestCase() { 43 | super(TestActivity.class); 44 | } 45 | 46 | @SuppressLint("CommitPrefEdits") 47 | @Override 48 | protected void setUp() throws Exception { 49 | super.setUp(); 50 | 51 | prefs = mock(SharedPreferences.class); 52 | editor = mock(SharedPreferences.Editor.class); 53 | 54 | when(prefs.edit()).thenReturn(editor); 55 | 56 | setActivityContext(new InjectedContext(getInstrumentation(), prefs)); 57 | startActivity( 58 | new Intent( 59 | getInstrumentation().getTargetContext(), 60 | TestActivity.class), 61 | null, 62 | null); 63 | } 64 | 65 | @Override 66 | protected void tearDown() throws Exception { 67 | prefs = null; 68 | editor = null; 69 | 70 | super.tearDown(); 71 | } 72 | 73 | @SmallTest 74 | public void testApply() { 75 | final SaveModeApplyInterface uut = StoreBox.create( 76 | getActivity(), 77 | SaveModeApplyInterface.class); 78 | 79 | uut.setValue("value"); 80 | 81 | verify(editor).apply(); 82 | verify(editor, never()).commit(); 83 | } 84 | 85 | @SmallTest 86 | public void testApplyUsingMethodPrecedence() { 87 | final SaveModeApplyInterface uut = StoreBox.create( 88 | getActivity(), 89 | SaveModeApplyInterface.class); 90 | 91 | uut.setValueWithMethodAnnotation("value"); 92 | 93 | verify(editor).commit(); 94 | verify(editor, never()).apply(); 95 | } 96 | 97 | @SmallTest 98 | public void testApplyWithoutSaving() { 99 | final SaveModeApplyInterface uut = StoreBox.create( 100 | getActivity(), 101 | SaveModeApplyInterface.class); 102 | 103 | uut.setValueWithoutApplying("value"); 104 | 105 | verify(editor, never()).apply(); 106 | verify(editor, never()).commit(); 107 | } 108 | 109 | @SmallTest 110 | public void testCommit() { 111 | final SaveModeCommitInterface uut = StoreBox.create( 112 | getActivity(), 113 | SaveModeCommitInterface.class); 114 | 115 | uut.setValue("value"); 116 | 117 | verify(editor).commit(); 118 | verify(editor, never()).apply(); 119 | } 120 | 121 | @SmallTest 122 | public void testCommitUsingMethodPrecedence() { 123 | final SaveModeCommitInterface uut = StoreBox.create( 124 | getActivity(), 125 | SaveModeCommitInterface.class); 126 | 127 | uut.setValueWithMethodAnnotation("value"); 128 | 129 | verify(editor).apply(); 130 | verify(editor, never()).commit(); 131 | } 132 | 133 | @SmallTest 134 | public void testCommitWithoutSaving() { 135 | final SaveModeCommitInterface uut = StoreBox.create( 136 | getActivity(), 137 | SaveModeCommitInterface.class); 138 | 139 | uut.setValueWithoutApplying("value"); 140 | 141 | verify(editor, never()).commit(); 142 | verify(editor, never()).apply(); 143 | } 144 | 145 | private static class InjectedContext extends ContextWrapper { 146 | 147 | private final SharedPreferences prefs; 148 | 149 | public InjectedContext( 150 | Instrumentation instr, SharedPreferences prefs) { 151 | 152 | super(instr.getTargetContext()); 153 | 154 | this.prefs = prefs; 155 | } 156 | 157 | @Override 158 | public SharedPreferences getSharedPreferences(String name, int mode) { 159 | return prefs; 160 | } 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /storebox-harness/src/androidTest/java/net/orange_box/storebox/harness/base/PreferencesTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.base; 18 | 19 | import android.annotation.SuppressLint; 20 | import android.content.SharedPreferences; 21 | import android.preference.PreferenceManager; 22 | import android.test.InstrumentationTestCase; 23 | 24 | import net.orange_box.storebox.StoreBox; 25 | 26 | public abstract class PreferencesTestCase extends InstrumentationTestCase { 27 | 28 | protected T uut; 29 | private SharedPreferences prefs; 30 | 31 | protected abstract Class getInterface(); 32 | 33 | @Override 34 | protected void setUp() throws Exception { 35 | super.setUp(); 36 | 37 | uut = StoreBox.create( 38 | getInstrumentation().getTargetContext(), 39 | getInterface()); 40 | 41 | prefs = PreferenceManager.getDefaultSharedPreferences( 42 | getInstrumentation().getTargetContext()); 43 | } 44 | 45 | @SuppressLint("CommitPrefEdits") 46 | @Override 47 | protected void tearDown() throws Exception { 48 | uut = null; 49 | 50 | // we are saving to the actual preferences so let's clear them 51 | prefs.edit().clear().commit(); 52 | prefs = null; 53 | 54 | super.tearDown(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /storebox-harness/src/androidTest/java/net/orange_box/storebox/harness/changes/ChangesListenersTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.changes; 18 | 19 | import android.test.UiThreadTest; 20 | import android.test.suitebuilder.annotation.SmallTest; 21 | 22 | import net.orange_box.storebox.harness.base.PreferencesTestCase; 23 | import net.orange_box.storebox.harness.interfaces.changes.ChangeListenersInterface; 24 | import net.orange_box.storebox.harness.types.CustomClass; 25 | import net.orange_box.storebox.listeners.OnPreferenceValueChangedListener; 26 | 27 | import java.util.concurrent.atomic.AtomicBoolean; 28 | import java.util.concurrent.atomic.AtomicInteger; 29 | import java.util.concurrent.atomic.AtomicReference; 30 | 31 | public class ChangesListenersTestCase extends 32 | PreferencesTestCase { 33 | 34 | @Override 35 | protected Class getInterface() { 36 | return ChangeListenersInterface.class; 37 | } 38 | 39 | @UiThreadTest 40 | @SmallTest 41 | public void testIntChanged() { 42 | final AtomicInteger value = new AtomicInteger(-1); 43 | final OnPreferenceValueChangedListener listener = 44 | new OnPreferenceValueChangedListener() { 45 | @Override 46 | public void onChanged(Integer newValue) { 47 | value.set(newValue); 48 | } 49 | }; 50 | 51 | uut.registerIntChangeListener(listener); 52 | uut.setInt(1); 53 | 54 | assertEquals(1, value.get()); 55 | } 56 | 57 | @UiThreadTest 58 | @SmallTest 59 | public void testIntChangedMultiple() { 60 | final AtomicInteger count = new AtomicInteger(2); 61 | final OnPreferenceValueChangedListener one = 62 | new OnPreferenceValueChangedListener() { 63 | @Override 64 | public void onChanged(Integer newValue) { 65 | assertEquals(1, newValue.intValue()); 66 | 67 | count.decrementAndGet(); 68 | } 69 | }; 70 | final OnPreferenceValueChangedListener two = 71 | new OnPreferenceValueChangedListener() { 72 | @Override 73 | public void onChanged(Integer newValue) { 74 | assertEquals(1, newValue.intValue()); 75 | 76 | count.decrementAndGet(); 77 | } 78 | }; 79 | 80 | uut.registerIntChangeListener(one); 81 | uut.registerIntChangeListener(two); 82 | uut.setInt(1); 83 | 84 | assertEquals(0, count.get()); 85 | } 86 | 87 | @UiThreadTest 88 | @SmallTest 89 | public void testIntChangedVarArgs() { 90 | final AtomicInteger count = new AtomicInteger(2); 91 | final OnPreferenceValueChangedListener one = 92 | new OnPreferenceValueChangedListener() { 93 | @Override 94 | public void onChanged(Integer newValue) { 95 | assertEquals(1, newValue.intValue()); 96 | 97 | count.decrementAndGet(); 98 | } 99 | }; 100 | final OnPreferenceValueChangedListener two = 101 | new OnPreferenceValueChangedListener() { 102 | @Override 103 | public void onChanged(Integer newValue) { 104 | assertEquals(1, newValue.intValue()); 105 | 106 | count.decrementAndGet(); 107 | } 108 | }; 109 | 110 | uut.registerIntChangeListenerVarArgs(one, two); 111 | uut.setInt(1); 112 | 113 | assertEquals(0, count.get()); 114 | } 115 | 116 | @UiThreadTest 117 | @SmallTest 118 | public void testIntUnregistered() { 119 | final AtomicBoolean called = new AtomicBoolean(); 120 | final OnPreferenceValueChangedListener listener = 121 | new OnPreferenceValueChangedListener() { 122 | @Override 123 | public void onChanged(Integer newValue) { 124 | called.set(true); 125 | } 126 | }; 127 | 128 | uut.registerIntChangeListener(listener); 129 | uut.unregisterIntChangeListener(listener); 130 | uut.setInt(1); 131 | 132 | assertFalse(called.get()); 133 | } 134 | 135 | @UiThreadTest 136 | @SmallTest 137 | public void testIntUnregisteredVarArgs() { 138 | final AtomicInteger count = new AtomicInteger(2); 139 | final OnPreferenceValueChangedListener one = 140 | new OnPreferenceValueChangedListener() { 141 | @Override 142 | public void onChanged(Integer newValue) { 143 | count.decrementAndGet(); 144 | } 145 | }; 146 | final OnPreferenceValueChangedListener two = 147 | new OnPreferenceValueChangedListener() { 148 | @Override 149 | public void onChanged(Integer newValue) { 150 | count.decrementAndGet(); 151 | } 152 | }; 153 | 154 | uut.registerIntChangeListenerVarArgs(one, two); 155 | uut.unregisterIntChangeListenerVarArgs(one, two); 156 | uut.setInt(1); 157 | 158 | assertEquals(2, count.get()); 159 | } 160 | 161 | @UiThreadTest 162 | @SmallTest 163 | public void testCustomClassChanged() { 164 | final AtomicReference value = new AtomicReference<>(); 165 | final OnPreferenceValueChangedListener listener = 166 | new OnPreferenceValueChangedListener() { 167 | @Override 168 | public void onChanged(CustomClass newValue) { 169 | value.set(newValue); 170 | } 171 | }; 172 | 173 | uut.registerCustomClassChangeListener(listener); 174 | uut.setCustomClass(new CustomClass("a", "b")); 175 | 176 | assertEquals(new CustomClass("a", "b"), value.get()); 177 | } 178 | 179 | @UiThreadTest 180 | @SmallTest 181 | public void testCustomClassChangedNull() { 182 | final AtomicReference value = new AtomicReference<>(); 183 | final OnPreferenceValueChangedListener listener = 184 | new OnPreferenceValueChangedListener() { 185 | @Override 186 | public void onChanged(CustomClass newValue) { 187 | value.set(newValue); 188 | } 189 | }; 190 | 191 | uut.setCustomClass(new CustomClass("a", "b")); 192 | uut.registerCustomClassChangeListener(listener); 193 | uut.setCustomClass(null); 194 | 195 | assertNull(value.get()); 196 | } 197 | 198 | @UiThreadTest 199 | @SmallTest 200 | public void testCustomClassChangedMultiple() { 201 | final AtomicInteger count = new AtomicInteger(2); 202 | final OnPreferenceValueChangedListener one = 203 | new OnPreferenceValueChangedListener() { 204 | @Override 205 | public void onChanged(CustomClass newValue) { 206 | assertEquals(new CustomClass("a", "b"), newValue); 207 | 208 | count.decrementAndGet(); 209 | } 210 | }; 211 | final OnPreferenceValueChangedListener two = 212 | new OnPreferenceValueChangedListener() { 213 | @Override 214 | public void onChanged(CustomClass newValue) { 215 | assertEquals(new CustomClass("a", "b"), newValue); 216 | 217 | count.decrementAndGet(); 218 | } 219 | }; 220 | 221 | uut.registerCustomClassChangeListener(one); 222 | uut.registerCustomClassChangeListener(two); 223 | uut.setCustomClass(new CustomClass("a", "b")); 224 | 225 | assertEquals(0, count.get()); 226 | } 227 | 228 | @UiThreadTest 229 | @SmallTest 230 | public void testCustomClassUnregistered() { 231 | final AtomicBoolean called = new AtomicBoolean(); 232 | final OnPreferenceValueChangedListener listener = 233 | new OnPreferenceValueChangedListener() { 234 | @Override 235 | public void onChanged(CustomClass newValue) { 236 | called.set(true); 237 | } 238 | }; 239 | 240 | uut.registerCustomClassChangeListener(listener); 241 | uut.unregisterCustomClassChangeListener(listener); 242 | uut.setCustomClass(new CustomClass("a", "b")); 243 | 244 | assertFalse(called.get()); 245 | } 246 | 247 | @UiThreadTest 248 | @SmallTest 249 | public void testListenerGarbageCollected() throws Exception { 250 | final AtomicBoolean called = new AtomicBoolean(); 251 | 252 | uut.registerIntChangeListener(new OnPreferenceValueChangedListener() { 253 | @Override 254 | public void onChanged(Integer newValue) { 255 | called.set(true); 256 | } 257 | }); 258 | // nasty, but it does force collection of soft references... 259 | // TODO is there a better way to do this? 260 | try { 261 | Object[] ignored = new Object[(int) Runtime.getRuntime().maxMemory()]; 262 | } catch (OutOfMemoryError e) { 263 | // NOP 264 | } 265 | uut.setInt(1); 266 | 267 | assertFalse(called.get()); 268 | } 269 | } 270 | -------------------------------------------------------------------------------- /storebox-harness/src/androidTest/java/net/orange_box/storebox/harness/types/CustomTypesTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.types; 18 | 19 | import android.annotation.SuppressLint; 20 | import android.content.SharedPreferences; 21 | import android.net.Uri; 22 | import android.preference.PreferenceManager; 23 | import android.test.InstrumentationTestCase; 24 | import android.test.suitebuilder.annotation.SmallTest; 25 | 26 | import net.orange_box.storebox.StoreBox; 27 | import net.orange_box.storebox.harness.interfaces.types.CustomTypesInterface; 28 | 29 | import java.util.ArrayList; 30 | import java.util.Arrays; 31 | import java.util.Collections; 32 | import java.util.Date; 33 | import java.util.List; 34 | 35 | public class CustomTypesTestCase extends InstrumentationTestCase { 36 | 37 | private CustomTypesInterface uut; 38 | private SharedPreferences prefs; 39 | 40 | @Override 41 | protected void setUp() throws Exception { 42 | super.setUp(); 43 | 44 | uut = StoreBox.create( 45 | getInstrumentation().getTargetContext(), 46 | CustomTypesInterface.class); 47 | 48 | prefs = PreferenceManager.getDefaultSharedPreferences( 49 | getInstrumentation().getTargetContext()); 50 | } 51 | 52 | @SuppressLint("CommitPrefEdits") 53 | @Override 54 | protected void tearDown() throws Exception { 55 | uut = null; 56 | 57 | // we are saving to the actual preferences so let's clear them 58 | prefs.edit().clear().commit(); 59 | prefs = null; 60 | 61 | super.tearDown(); 62 | } 63 | 64 | @SmallTest 65 | public void testCustomEnumDefaults() { 66 | assertNull(uut.getCustomEnum()); 67 | assertSame(CustomEnum.ONE, uut.getCustomEnum(CustomEnum.ONE)); 68 | } 69 | 70 | @SmallTest 71 | public void testCustomEnum() { 72 | uut.setCustomEnum(CustomEnum.TWO); 73 | assertSame(CustomEnum.TWO, uut.getCustomEnum()); 74 | assertSame(CustomEnum.TWO, uut.getCustomEnum(CustomEnum.ONE)); 75 | } 76 | 77 | @SmallTest 78 | public void testCustomEnumNull() { 79 | uut.setCustomEnum(CustomEnum.TWO); 80 | uut.setCustomEnum(null); 81 | 82 | assertNull(uut.getCustomEnum()); 83 | } 84 | 85 | @SmallTest 86 | public void testDateDefaults() { 87 | assertNull(uut.getDate()); 88 | assertEquals(new Date(0), uut.getDate(new Date(0))); 89 | } 90 | 91 | @SmallTest 92 | public void testDate() { 93 | final Date date = new Date(); 94 | 95 | uut.setDate(date); 96 | assertEquals(date, uut.getDate()); 97 | assertEquals(date, uut.getDate(new Date(0))); 98 | } 99 | 100 | @SmallTest 101 | public void testDateNull() { 102 | uut.setDate(new Date()); 103 | uut.setDate(null); 104 | 105 | assertNull(uut.getDate()); 106 | } 107 | 108 | @SmallTest 109 | public void testDoubleDefaults() { 110 | assertEquals(0D, uut.getDouble()); 111 | assertEquals(1D, uut.getDouble(1D)); 112 | } 113 | 114 | @SmallTest 115 | public void testDouble() { 116 | final double value = 1; 117 | 118 | uut.setDouble(value); 119 | assertEquals(value, uut.getDouble()); 120 | assertEquals(value, uut.getDouble(0D)); 121 | } 122 | 123 | @SmallTest 124 | public void testDoubleBoundaries() { 125 | uut.setDouble(Double.MIN_VALUE); 126 | assertEquals(Double.MIN_VALUE, uut.getDouble()); 127 | 128 | uut.setDouble(Double.MAX_VALUE); 129 | assertEquals(Double.MAX_VALUE, uut.getDouble()); 130 | 131 | uut.setDouble(Double.MIN_NORMAL); 132 | assertEquals(Double.MIN_NORMAL, uut.getDouble()); 133 | } 134 | 135 | @SmallTest 136 | public void testUriDefaults() { 137 | assertNull(uut.getUri()); 138 | assertEquals(Uri.EMPTY, uut.getUri(Uri.EMPTY)); 139 | } 140 | 141 | @SmallTest 142 | public void testUri() { 143 | final Uri value = Uri.parse("http://www.google.com"); 144 | 145 | uut.setUri(value); 146 | assertEquals(value, uut.getUri()); 147 | assertEquals(value, uut.getUri(Uri.EMPTY)); 148 | } 149 | 150 | @SmallTest 151 | public void testUriNull() { 152 | uut.setUri(Uri.parse("http://www.google.com")); 153 | uut.setUri(null); 154 | 155 | assertNull(uut.getUri()); 156 | } 157 | 158 | @SmallTest 159 | public void testCustomClassDefaults() { 160 | assertNull(uut.getCustomClass()); 161 | assertEquals(new CustomClass(), uut.getCustomClass(new CustomClass())); 162 | } 163 | 164 | @SmallTest 165 | public void testCustomClass() { 166 | final CustomClass value = new CustomClass("one", "two"); 167 | 168 | uut.setCustomClass(value); 169 | assertEquals(value, uut.getCustomClass()); 170 | assertEquals(value, uut.getCustomClass(new CustomClass())); 171 | } 172 | 173 | @SmallTest 174 | public void testCustomClassNull() { 175 | uut.setCustomClass(new CustomClass("one", "two")); 176 | uut.setCustomClass(null); 177 | 178 | assertNull(uut.getCustomClass()); 179 | } 180 | 181 | @SmallTest 182 | public void testUriListDefaults() { 183 | assertNull(uut.getCustomClassList()); 184 | assertEquals( 185 | new ArrayList(), 186 | uut.getCustomClassList(new ArrayList())); 187 | } 188 | 189 | @SmallTest 190 | public void testUriList() { 191 | final List value = Arrays.asList( 192 | new CustomClass("a", "b"), 193 | new CustomClass("b", "c")); 194 | 195 | uut.setCustomClassList(value); 196 | assertEquals(value, uut.getCustomClassList()); 197 | } 198 | 199 | @SmallTest 200 | public void testUriListNull() { 201 | uut.setCustomClassList(Collections.singletonList(new CustomClass())); 202 | uut.setCustomClassList(null); 203 | 204 | assertNull(uut.getCustomClassList()); 205 | } 206 | } 207 | -------------------------------------------------------------------------------- /storebox-harness/src/androidTest/java/net/orange_box/storebox/harness/types/ValueTypesTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.types; 18 | 19 | import android.annotation.SuppressLint; 20 | import android.content.SharedPreferences; 21 | import android.os.Build; 22 | import android.preference.PreferenceManager; 23 | import android.test.InstrumentationTestCase; 24 | import android.test.suitebuilder.annotation.SmallTest; 25 | 26 | import net.orange_box.storebox.StoreBox; 27 | import net.orange_box.storebox.harness.interfaces.types.ValueTypesInterface; 28 | 29 | import java.util.Collections; 30 | import java.util.HashSet; 31 | import java.util.Set; 32 | 33 | public class ValueTypesTestCase extends InstrumentationTestCase { 34 | 35 | private ValueTypesInterface uut; 36 | private SharedPreferences prefs; 37 | 38 | @Override 39 | protected void setUp() throws Exception { 40 | super.setUp(); 41 | 42 | uut = StoreBox.create( 43 | getInstrumentation().getTargetContext(), ValueTypesInterface.class); 44 | 45 | prefs = PreferenceManager.getDefaultSharedPreferences( 46 | getInstrumentation().getTargetContext()); 47 | } 48 | 49 | @SuppressLint("CommitPrefEdits") 50 | @Override 51 | protected void tearDown() throws Exception { 52 | uut = null; 53 | 54 | // we are saving to the actual preferences so let's clear them 55 | prefs.edit().clear().commit(); 56 | prefs = null; 57 | 58 | super.tearDown(); 59 | } 60 | 61 | @SmallTest 62 | public void testBoolean() { 63 | assertFalse(uut.getBoolean()); 64 | 65 | uut.setBoolean(true); 66 | assertTrue(uut.getBoolean()); 67 | } 68 | 69 | @SmallTest 70 | public void testFloat() { 71 | assertEquals(0F, uut.getFloat()); 72 | 73 | uut.setFloat(1.0F); 74 | assertEquals(1.0F, uut.getFloat()); 75 | } 76 | 77 | @SmallTest 78 | public void testInt() { 79 | assertEquals(0, uut.getInt()); 80 | 81 | uut.setInt(1); 82 | assertEquals(1, uut.getInt()); 83 | } 84 | 85 | @SmallTest 86 | public void testLong() { 87 | assertEquals(0L, uut.getLong()); 88 | 89 | uut.setLong(1L); 90 | assertEquals(1L, uut.getLong()); 91 | } 92 | 93 | @SmallTest 94 | public void testString() { 95 | assertNull(uut.getString()); 96 | 97 | uut.setString("string"); 98 | assertEquals("string", uut.getString()); 99 | } 100 | 101 | @SmallTest 102 | public void testStringNull() { 103 | uut.setString("string"); 104 | uut.setString(null); 105 | 106 | assertEquals(null, uut.getString()); 107 | } 108 | 109 | @SmallTest 110 | public void testStringSet() { 111 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 112 | assertNull(uut.getStringSet()); 113 | 114 | final Set value = 115 | new HashSet<>(Collections.singletonList("one")); 116 | uut.setStringSet(value); 117 | assertEquals(value, uut.getStringSet()); 118 | } else { 119 | try { 120 | uut.setStringSet(Collections.emptySet()); 121 | 122 | fail("Exception not thrown"); 123 | } catch (UnsupportedOperationException e) { 124 | // pass 125 | } 126 | 127 | try { 128 | uut.getStringSet(); 129 | 130 | fail("Exception not thrown"); 131 | } catch (UnsupportedOperationException e) { 132 | // pass 133 | } 134 | } 135 | } 136 | 137 | @SmallTest 138 | public void testStringSetNull() { 139 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 140 | uut.setStringSet(new HashSet<>(Collections.singletonList("one"))); 141 | uut.setStringSet(null); 142 | 143 | assertEquals(null, uut.getStringSet()); 144 | } else { 145 | // testStringSet() covers this scenario 146 | } 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /storebox-harness/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /storebox-harness/src/main/java/net/orange_box/storebox/harness/activities/TestActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.activities; 18 | 19 | import android.app.Activity; 20 | 21 | public class TestActivity extends Activity { 22 | 23 | // NOP 24 | } 25 | -------------------------------------------------------------------------------- /storebox-harness/src/main/java/net/orange_box/storebox/harness/interfaces/ChainingMethodsInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.interfaces; 18 | 19 | import android.content.SharedPreferences; 20 | 21 | import net.orange_box.storebox.annotations.method.KeyByString; 22 | import net.orange_box.storebox.annotations.method.RemoveMethod; 23 | 24 | public interface ChainingMethodsInterface { 25 | 26 | @KeyByString("first") 27 | SharedPreferences.Editor setFirstValue(String value); 28 | 29 | @KeyByString("second") 30 | ChainingMethodsInterface setSecondValue(String value); 31 | 32 | @KeyByString("first") 33 | @RemoveMethod 34 | SharedPreferences.Editor removeFirstValue(); 35 | 36 | @KeyByString("second") 37 | @RemoveMethod 38 | ChainingMethodsInterface removeSecondValue(); 39 | } 40 | -------------------------------------------------------------------------------- /storebox-harness/src/main/java/net/orange_box/storebox/harness/interfaces/ForwardingMethodsInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.interfaces; 18 | 19 | import android.content.SharedPreferences; 20 | 21 | public interface ForwardingMethodsInterface 22 | extends SharedPreferences, SharedPreferences.Editor { 23 | } 24 | -------------------------------------------------------------------------------- /storebox-harness/src/main/java/net/orange_box/storebox/harness/interfaces/KeysAndDefaultValuesInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.interfaces; 18 | 19 | import net.orange_box.storebox.annotations.method.DefaultValue; 20 | import net.orange_box.storebox.annotations.method.KeyByResource; 21 | import net.orange_box.storebox.annotations.method.KeyByString; 22 | import net.orange_box.storebox.harness.R; 23 | 24 | public interface KeysAndDefaultValuesInterface { 25 | 26 | @KeyByString("string") 27 | String getValue(); 28 | 29 | @KeyByString("string") 30 | void setValue(String value); 31 | 32 | 33 | @KeyByResource(R.string.key_string) 34 | String getValueAlt(); 35 | 36 | @KeyByResource(R.string.key_string) 37 | void setValueAlt(String value); 38 | 39 | 40 | @KeyByResource(R.string.key_string) 41 | @DefaultValue(R.string.default_string) 42 | String getValueWithDefault(); 43 | 44 | @KeyByResource(R.string.key_string) 45 | String getValueWithDefault(String defValue); 46 | 47 | 48 | @KeyByResource(R.string.key_string) 49 | @DefaultValue(R.string.default_string) 50 | String getValueWithDefaultPrecedence(String defValue); 51 | 52 | 53 | @KeyByResource(R.string.key_string) 54 | @DefaultValue(R.bool.default_boolean) 55 | String getValueWithBadDefault(); 56 | 57 | @KeyByResource(R.string.key_string) 58 | String getValueWithBadDefault(boolean defValue); 59 | } 60 | -------------------------------------------------------------------------------- /storebox-harness/src/main/java/net/orange_box/storebox/harness/interfaces/PreferencesModeMultiProcessInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.interfaces; 18 | 19 | import net.orange_box.storebox.annotations.type.ActivityPreferences; 20 | import net.orange_box.storebox.enums.PreferencesMode; 21 | 22 | @ActivityPreferences(mode = PreferencesMode.MODE_MULTI_PROCESS) 23 | public interface PreferencesModeMultiProcessInterface { 24 | 25 | // NOP 26 | } 27 | -------------------------------------------------------------------------------- /storebox-harness/src/main/java/net/orange_box/storebox/harness/interfaces/PreferencesModePrivateInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.interfaces; 18 | 19 | import net.orange_box.storebox.annotations.type.FilePreferences; 20 | import net.orange_box.storebox.enums.PreferencesMode; 21 | 22 | @FilePreferences(value = "test", mode = PreferencesMode.MODE_PRIVATE) 23 | public interface PreferencesModePrivateInterface { 24 | 25 | // NOP 26 | } 27 | -------------------------------------------------------------------------------- /storebox-harness/src/main/java/net/orange_box/storebox/harness/interfaces/PreferencesModeWordWriteableInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.interfaces; 18 | 19 | import net.orange_box.storebox.annotations.type.ActivityPreferences; 20 | import net.orange_box.storebox.enums.PreferencesMode; 21 | 22 | @SuppressWarnings("deprecation") 23 | @ActivityPreferences(mode = PreferencesMode.MODE_WORLD_WRITEABLE) 24 | public interface PreferencesModeWordWriteableInterface { 25 | 26 | // NOP 27 | } 28 | -------------------------------------------------------------------------------- /storebox-harness/src/main/java/net/orange_box/storebox/harness/interfaces/PreferencesModeWorldReadableInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.interfaces; 18 | 19 | import net.orange_box.storebox.annotations.type.FilePreferences; 20 | import net.orange_box.storebox.enums.PreferencesMode; 21 | 22 | @SuppressWarnings("deprecation") 23 | @FilePreferences(value = "test", mode = PreferencesMode.MODE_WORLD_READABLE) 24 | public interface PreferencesModeWorldReadableInterface { 25 | 26 | // NOP 27 | } 28 | -------------------------------------------------------------------------------- /storebox-harness/src/main/java/net/orange_box/storebox/harness/interfaces/PreferencesTypeActivityInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.interfaces; 18 | 19 | import net.orange_box.storebox.annotations.type.ActivityPreferences; 20 | 21 | @ActivityPreferences 22 | public interface PreferencesTypeActivityInterface { 23 | 24 | // NOP 25 | } 26 | -------------------------------------------------------------------------------- /storebox-harness/src/main/java/net/orange_box/storebox/harness/interfaces/PreferencesTypeDefaultSharedInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.interfaces; 18 | 19 | import net.orange_box.storebox.annotations.type.DefaultSharedPreferences; 20 | 21 | @DefaultSharedPreferences 22 | public interface PreferencesTypeDefaultSharedInterface { 23 | 24 | // NOP 25 | } 26 | -------------------------------------------------------------------------------- /storebox-harness/src/main/java/net/orange_box/storebox/harness/interfaces/PreferencesTypeFileInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.interfaces; 18 | 19 | import net.orange_box.storebox.annotations.type.FilePreferences; 20 | 21 | @FilePreferences("test") 22 | public interface PreferencesTypeFileInterface { 23 | 24 | // NOP 25 | } 26 | -------------------------------------------------------------------------------- /storebox-harness/src/main/java/net/orange_box/storebox/harness/interfaces/RemoveMethodInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.interfaces; 18 | 19 | import net.orange_box.storebox.annotations.method.KeyByResource; 20 | import net.orange_box.storebox.annotations.method.KeyByString; 21 | import net.orange_box.storebox.annotations.method.RemoveMethod; 22 | import net.orange_box.storebox.harness.R; 23 | 24 | public interface RemoveMethodInterface { 25 | 26 | @RemoveMethod 27 | void removeUsingStringKey(String key); 28 | 29 | @RemoveMethod 30 | void removeUsingIntKey(int key); 31 | 32 | @KeyByString("int") 33 | @RemoveMethod 34 | void removeWithStringAnnotation(); 35 | 36 | @KeyByResource(R.string.key_int) 37 | @RemoveMethod 38 | void removeWithResourceAnnotation(); 39 | } 40 | -------------------------------------------------------------------------------- /storebox-harness/src/main/java/net/orange_box/storebox/harness/interfaces/SaveModeApplyInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.interfaces; 18 | 19 | import net.orange_box.storebox.annotations.method.KeyByResource; 20 | import net.orange_box.storebox.annotations.option.SaveOption; 21 | import net.orange_box.storebox.annotations.type.DefaultSharedPreferences; 22 | import net.orange_box.storebox.enums.SaveMode; 23 | import net.orange_box.storebox.harness.R; 24 | 25 | @DefaultSharedPreferences 26 | @SaveOption(SaveMode.APPLY) 27 | public interface SaveModeApplyInterface { 28 | 29 | @KeyByResource(R.string.key_string) 30 | void setValue(String value); 31 | 32 | @KeyByResource(R.string.key_string) 33 | @SaveOption(SaveMode.COMMIT) 34 | void setValueWithMethodAnnotation(String value); 35 | 36 | @KeyByResource(R.string.key_string) 37 | @SaveOption(SaveMode.NOME) 38 | void setValueWithoutApplying(String value); 39 | } 40 | -------------------------------------------------------------------------------- /storebox-harness/src/main/java/net/orange_box/storebox/harness/interfaces/SaveModeCommitInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.interfaces; 18 | 19 | import net.orange_box.storebox.annotations.method.KeyByResource; 20 | import net.orange_box.storebox.annotations.option.SaveOption; 21 | import net.orange_box.storebox.annotations.type.DefaultSharedPreferences; 22 | import net.orange_box.storebox.enums.SaveMode; 23 | import net.orange_box.storebox.harness.R; 24 | 25 | @DefaultSharedPreferences 26 | @SaveOption(SaveMode.COMMIT) 27 | public interface SaveModeCommitInterface { 28 | 29 | @KeyByResource(R.string.key_string) 30 | void setValue(String value); 31 | 32 | @KeyByResource(R.string.key_string) 33 | @SaveOption(SaveMode.APPLY) 34 | void setValueWithMethodAnnotation(String value); 35 | 36 | @KeyByResource(R.string.key_string) 37 | @SaveOption(SaveMode.NOME) 38 | void setValueWithoutApplying(String value); 39 | } 40 | -------------------------------------------------------------------------------- /storebox-harness/src/main/java/net/orange_box/storebox/harness/interfaces/changes/ChangeListenersInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.interfaces.changes; 18 | 19 | import net.orange_box.storebox.annotations.method.KeyByString; 20 | import net.orange_box.storebox.annotations.method.TypeAdapter; 21 | import net.orange_box.storebox.annotations.method.RegisterChangeListenerMethod; 22 | import net.orange_box.storebox.annotations.method.UnregisterChangeListenerMethod; 23 | import net.orange_box.storebox.harness.types.CustomClass; 24 | import net.orange_box.storebox.harness.types.adapters.CustomClassTypeAdapter; 25 | import net.orange_box.storebox.listeners.OnPreferenceValueChangedListener; 26 | 27 | public interface ChangeListenersInterface { 28 | 29 | @KeyByString("key_int") 30 | void setInt(int value); 31 | 32 | @KeyByString("key_int") 33 | @RegisterChangeListenerMethod 34 | void registerIntChangeListener( 35 | OnPreferenceValueChangedListener listener); 36 | 37 | @KeyByString("key_int") 38 | @RegisterChangeListenerMethod 39 | void registerIntChangeListenerVarArgs( 40 | OnPreferenceValueChangedListener... listeners); 41 | 42 | @KeyByString("key_int") 43 | @UnregisterChangeListenerMethod 44 | void unregisterIntChangeListener( 45 | OnPreferenceValueChangedListener listener); 46 | 47 | @KeyByString("key_int") 48 | @UnregisterChangeListenerMethod 49 | void unregisterIntChangeListenerVarArgs( 50 | OnPreferenceValueChangedListener... listeners); 51 | 52 | 53 | @KeyByString("key_custom_class") 54 | @TypeAdapter(CustomClassTypeAdapter.class) 55 | void setCustomClass(CustomClass value); 56 | 57 | @KeyByString("key_custom_class") 58 | @RegisterChangeListenerMethod 59 | @TypeAdapter(CustomClassTypeAdapter.class) 60 | void registerCustomClassChangeListener( 61 | OnPreferenceValueChangedListener listener); 62 | 63 | @KeyByString("key_custom_class") 64 | @UnregisterChangeListenerMethod 65 | @TypeAdapter(CustomClassTypeAdapter.class) 66 | void unregisterCustomClassChangeListener( 67 | OnPreferenceValueChangedListener listener); 68 | } 69 | -------------------------------------------------------------------------------- /storebox-harness/src/main/java/net/orange_box/storebox/harness/interfaces/types/CustomTypesInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.interfaces.types; 18 | 19 | import android.net.Uri; 20 | 21 | import net.orange_box.storebox.annotations.method.KeyByString; 22 | import net.orange_box.storebox.annotations.method.TypeAdapter; 23 | import net.orange_box.storebox.harness.types.CustomClass; 24 | import net.orange_box.storebox.harness.types.CustomEnum; 25 | import net.orange_box.storebox.harness.types.adapters.CustomClassListTypeAdapter; 26 | import net.orange_box.storebox.harness.types.adapters.CustomClassTypeAdapter; 27 | 28 | import java.util.Date; 29 | import java.util.List; 30 | 31 | public interface CustomTypesInterface { 32 | 33 | @KeyByString("key_custom_enum") 34 | CustomEnum getCustomEnum(); 35 | 36 | @KeyByString("key_custom_enum") 37 | CustomEnum getCustomEnum(CustomEnum defValue); 38 | 39 | @KeyByString("key_custom_enum") 40 | void setCustomEnum(CustomEnum value); 41 | 42 | 43 | @KeyByString("key_date") 44 | Date getDate(); 45 | 46 | @KeyByString("key_date") 47 | Date getDate(Date defValue); 48 | 49 | @KeyByString("key_date") 50 | void setDate(Date value); 51 | 52 | 53 | @KeyByString("key_double") 54 | double getDouble(); 55 | 56 | @KeyByString("key_double") 57 | double getDouble(double defValue); 58 | 59 | @KeyByString("key_double") 60 | void setDouble(double value); 61 | 62 | 63 | @KeyByString("key_uri") 64 | Uri getUri(); 65 | 66 | @KeyByString("key_uri") 67 | Uri getUri(Uri defValue); 68 | 69 | @KeyByString("key_uri") 70 | void setUri(Uri value); 71 | 72 | 73 | @TypeAdapter(CustomClassTypeAdapter.class) 74 | @KeyByString("key_custom_class") 75 | CustomClass getCustomClass(); 76 | 77 | @TypeAdapter(CustomClassTypeAdapter.class) 78 | @KeyByString("key_custom_class") 79 | CustomClass getCustomClass(CustomClass defValue); 80 | 81 | @TypeAdapter(CustomClassTypeAdapter.class) 82 | @KeyByString("key_custom_class") 83 | void setCustomClass(CustomClass value); 84 | 85 | 86 | @TypeAdapter(CustomClassListTypeAdapter.class) 87 | @KeyByString("key_custom_class_list") 88 | List getCustomClassList(); 89 | 90 | @TypeAdapter(CustomClassListTypeAdapter.class) 91 | @KeyByString("key_custom_class_list") 92 | List getCustomClassList(List defValue); 93 | 94 | @TypeAdapter(CustomClassListTypeAdapter.class) 95 | @KeyByString("key_custom_class_list") 96 | void setCustomClassList(List value); 97 | } 98 | -------------------------------------------------------------------------------- /storebox-harness/src/main/java/net/orange_box/storebox/harness/interfaces/types/ValueTypesInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.interfaces.types; 18 | 19 | import net.orange_box.storebox.annotations.method.KeyByResource; 20 | import net.orange_box.storebox.harness.R; 21 | 22 | import java.util.Set; 23 | 24 | public interface ValueTypesInterface { 25 | 26 | @KeyByResource(R.string.key_boolean) 27 | boolean getBoolean(); 28 | 29 | @KeyByResource(R.string.key_boolean) 30 | void setBoolean(boolean value); 31 | 32 | 33 | @KeyByResource(R.string.key_float) 34 | float getFloat(); 35 | 36 | @KeyByResource(R.string.key_float) 37 | void setFloat(float value); 38 | 39 | 40 | @KeyByResource(R.string.key_int) 41 | int getInt(); 42 | 43 | @KeyByResource(R.string.key_int) 44 | void setInt(int value); 45 | 46 | 47 | @KeyByResource(R.string.key_long) 48 | long getLong(); 49 | 50 | @KeyByResource(R.string.key_long) 51 | void setLong(long value); 52 | 53 | 54 | @KeyByResource(R.string.key_string) 55 | String getString(); 56 | 57 | @KeyByResource(R.string.key_string) 58 | void setString(String value); 59 | 60 | 61 | @KeyByResource(R.string.key_string_set) 62 | Set getStringSet(); 63 | 64 | @KeyByResource(R.string.key_string_set) 65 | void setStringSet(Set value); 66 | } 67 | -------------------------------------------------------------------------------- /storebox-harness/src/main/java/net/orange_box/storebox/harness/types/CustomClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.types; 18 | 19 | import java.util.Arrays; 20 | 21 | public class CustomClass { 22 | 23 | private int hashCode; 24 | 25 | private final String one; 26 | private final String two; 27 | 28 | public CustomClass() { 29 | this("", ""); 30 | } 31 | 32 | public CustomClass(String one, String two) { 33 | this.one = one; 34 | this.two = two; 35 | } 36 | 37 | public String getOne() { 38 | return one; 39 | } 40 | 41 | public String getTwo() { 42 | return two; 43 | } 44 | 45 | @Override 46 | public boolean equals(Object o) { 47 | if (o == null || !(o instanceof CustomClass)) { 48 | return false; 49 | } 50 | 51 | final CustomClass other = (CustomClass) o; 52 | return (one.equals(other.one) && two.equals(other.two)); 53 | } 54 | 55 | @Override 56 | public int hashCode() { 57 | if (hashCode == 0) { 58 | hashCode = Arrays.hashCode(new String[] {one, two}); 59 | } 60 | 61 | return hashCode; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /storebox-harness/src/main/java/net/orange_box/storebox/harness/types/CustomEnum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.types; 18 | 19 | public enum CustomEnum { 20 | 21 | ONE, 22 | TWO 23 | } 24 | -------------------------------------------------------------------------------- /storebox-harness/src/main/java/net/orange_box/storebox/harness/types/adapters/CustomClassListTypeAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.types.adapters; 18 | 19 | import android.support.annotation.Nullable; 20 | 21 | import com.google.gson.Gson; 22 | import com.google.gson.reflect.TypeToken; 23 | 24 | import net.orange_box.storebox.adapters.base.BaseStringTypeAdapter; 25 | import net.orange_box.storebox.harness.types.CustomClass; 26 | 27 | import java.util.List; 28 | 29 | public class CustomClassListTypeAdapter extends 30 | BaseStringTypeAdapter> { 31 | 32 | private static final Gson GSON = new Gson(); 33 | 34 | @Nullable 35 | @Override 36 | public String adaptForPreferences(@Nullable List value) { 37 | return GSON.toJson(value); 38 | } 39 | 40 | @Nullable 41 | @Override 42 | public List adaptFromPreferences(@Nullable String value) { 43 | return GSON.fromJson(value, new TypeToken>(){}.getType()); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /storebox-harness/src/main/java/net/orange_box/storebox/harness/types/adapters/CustomClassTypeAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.types.adapters; 18 | 19 | import android.support.annotation.Nullable; 20 | 21 | import net.orange_box.storebox.adapters.base.BaseStringTypeAdapter; 22 | import net.orange_box.storebox.harness.types.CustomClass; 23 | 24 | public class CustomClassTypeAdapter extends BaseStringTypeAdapter { 25 | 26 | @Nullable 27 | @Override 28 | public String adaptForPreferences(@Nullable CustomClass value) { 29 | if (value == null) { 30 | return null; 31 | } 32 | 33 | return value.getOne() + "|" + value.getTwo(); 34 | } 35 | 36 | @Nullable 37 | @Override 38 | public CustomClass adaptFromPreferences(@Nullable String value) { 39 | if (value == null) { 40 | return null; 41 | } 42 | 43 | if (value.length() == 1) { 44 | return new CustomClass(); 45 | } else { 46 | final String split[] = value.split("\\|"); 47 | 48 | return new CustomClass(split[0], split[1]); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /storebox-harness/src/main/kotlin/net/orange_box/storebox/harness/interfaces/ClearMethodInterface.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015-2016 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.harness.interfaces 18 | 19 | import net.orange_box.storebox.annotations.method.ClearMethod 20 | 21 | interface ClearMethodInterface { 22 | 23 | @ClearMethod 24 | fun clear() 25 | } 26 | -------------------------------------------------------------------------------- /storebox-harness/src/main/res/values/values.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | boolean 4 | float 5 | int 6 | long 7 | string 8 | string_set 9 | 10 | true 11 | 0.5 12 | 1 13 | default 14 | 15 | -------------------------------------------------------------------------------- /storebox-lib/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /storebox-lib/README.md: -------------------------------------------------------------------------------- 1 | ### StoreBox Lib ### 2 | The library module for StoreBox that gets published to Maven. 3 | -------------------------------------------------------------------------------- /storebox-lib/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'findbugs' 3 | apply plugin: 'com.github.dcendents.android-maven' 4 | apply plugin: 'com.jfrog.bintray' 5 | 6 | android { 7 | compileSdkVersion project.compileSdkVersion 8 | buildToolsVersion project.buildToolsVersion 9 | 10 | defaultConfig { 11 | minSdkVersion project.minSdkVersion 12 | targetSdkVersion project.targetSdkVersion 13 | 14 | versionName project.versionName 15 | } 16 | 17 | buildTypes { 18 | debug { 19 | testCoverageEnabled true 20 | } 21 | } 22 | } 23 | 24 | dependencies { 25 | compile 'com.android.support:support-annotations:23.3.0' 26 | compile 'net.jodah:typetools:0.4.6' 27 | } 28 | 29 | // FindBugs 30 | 31 | findbugs { 32 | sourceSets = [] 33 | ignoreFailures = true 34 | } 35 | 36 | task findbugs(type: FindBugs, dependsOn: assembleDebug) { 37 | description 'Run findbugs' 38 | group 'verification' 39 | 40 | classes = fileTree('build/intermediates/classes/debug/') 41 | source = fileTree('src/main/java') 42 | classpath = files() 43 | 44 | effort = 'max' 45 | reportLevel = "medium" 46 | 47 | reports { 48 | xml.enabled = false 49 | html.enabled = true 50 | } 51 | } 52 | 53 | check.doLast { 54 | project.tasks.getByName("findbugs").execute() 55 | } 56 | 57 | // BinTray/Maven 58 | 59 | // https://github.com/danielemaddaluno/gradle-jcenter-publish 60 | Properties properties = new Properties() 61 | File propertiesFile = project.rootProject.file('publishing.properties') 62 | if (propertiesFile.exists()) { 63 | properties.load(propertiesFile.newDataInputStream()) 64 | } 65 | 66 | version = project.versionName 67 | group = 'net.orange-box.storebox' 68 | 69 | install { 70 | repositories.mavenInstaller { 71 | pom { 72 | project { 73 | packaging 'jar' 74 | 75 | name 'StroreBox Android Library' 76 | description 'Android library for streamlining SharedPreferences.' 77 | url 'http://martino2k6.github.io/StoreBox' 78 | 79 | scm { 80 | url 'https://github.com/martino2k6/StoreBox' 81 | connection 'https://github.com/martino2k6/StoreBox.git' 82 | developerConnection 'https://github.com/martino2k6/StoreBox.git' 83 | } 84 | 85 | licenses { 86 | license { 87 | name 'The Apache Software License, Version 2.0' 88 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt' 89 | } 90 | } 91 | 92 | developers { 93 | developer { 94 | id properties.getProperty('owner.id') 95 | name properties.getProperty('owner.name') 96 | } 97 | } 98 | } 99 | } 100 | } 101 | } 102 | 103 | task javadoc(type: Javadoc) { 104 | failOnError false 105 | source = android.sourceSets.main.java.sourceFiles 106 | } 107 | 108 | task javadocJar(type: Jar, dependsOn: javadoc) { 109 | classifier = 'javadoc' 110 | from javadoc.destinationDir 111 | } 112 | 113 | task sourcesJar(type: Jar) { 114 | classifier = 'sources' 115 | from android.sourceSets.main.java.sourceFiles 116 | } 117 | 118 | artifacts { 119 | archives javadocJar 120 | archives sourcesJar 121 | } 122 | 123 | // https://github.com/bintray/gradle-bintray-plugin 124 | bintray { 125 | user = properties.getProperty('bintray.user') 126 | key = properties.getProperty('bintray.apikey') 127 | 128 | configurations = ['archives'] 129 | publish = true 130 | 131 | pkg { 132 | repo = 'maven' 133 | name = 'storebox' 134 | desc = 'Android library for streamlining SharedPreferences.' 135 | 136 | websiteUrl = 'http://martino2k6.github.io/StoreBox' 137 | issueTrackerUrl = 'https://github.com/martino2k6/StoreBox/issues' 138 | vcsUrl = 'https://github.com/martino2k6/StoreBox.git' 139 | 140 | licenses = ['Apache-2.0'] 141 | labels = ['android', 'preferences', 'library', 'jar'] 142 | publicDownloadNumbers = true 143 | 144 | version { 145 | name = project.versionName 146 | vcsTag = project.versionName 147 | 148 | gpg { 149 | sign = true 150 | passphrase = properties.getProperty('bintray.gpg.password') 151 | } 152 | 153 | // mavenCentralSync { 154 | // sync = true 155 | // user = properties.getProperty('bintray.oss.user') 156 | // password = properties.getProperty('bintray.oss.password') 157 | // close = '1' 158 | // } 159 | } 160 | } 161 | } 162 | 163 | // http://stackoverflow.com/a/19484146/1795559 164 | android.libraryVariants.all { variant -> 165 | def task = project.tasks.create "jar${name.capitalize()}", Jar 166 | task.dependsOn variant.javaCompile 167 | task.from variant.javaCompile.destinationDir 168 | artifacts.add('archives', task); 169 | } 170 | -------------------------------------------------------------------------------- /storebox-lib/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/StoreBox.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox; 18 | 19 | import android.app.Activity; 20 | import android.content.Context; 21 | import android.text.TextUtils; 22 | 23 | import net.orange_box.storebox.annotations.option.SaveOption; 24 | import net.orange_box.storebox.annotations.type.ActivityPreferences; 25 | import net.orange_box.storebox.annotations.type.DefaultSharedPreferences; 26 | import net.orange_box.storebox.annotations.type.FilePreferences; 27 | import net.orange_box.storebox.enums.PreferencesMode; 28 | import net.orange_box.storebox.enums.PreferencesType; 29 | import net.orange_box.storebox.enums.SaveMode; 30 | 31 | import java.lang.reflect.Proxy; 32 | import java.util.Locale; 33 | 34 | /** 35 | * Creates a no-thrills instance of the supplied interface, by reading any 36 | * options provided through interface-level annotations. 37 | *

38 | * If you'd like to provide options dynamically at run-time, take a look at 39 | * {@link Builder}. 40 | */ 41 | public final class StoreBox { 42 | 43 | /** 44 | * @param context - the context under which the 45 | * {@link android.content.SharedPreferences} should be opened 46 | * @param cls - the interface class which should be instantiated 47 | * @return new instance of class {@code cls} using {@code context} 48 | */ 49 | public static T create(Context context, Class cls) { 50 | return new Builder<>(context, cls).build(); 51 | } 52 | 53 | private StoreBox() {} 54 | 55 | /** 56 | * Can be used to provide a customised instance of the supplied interface, 57 | * by setting custom options through builder methods. 58 | * 59 | * @param 60 | */ 61 | public static final class Builder { 62 | 63 | private final Context context; 64 | private final Class cls; 65 | 66 | private PreferencesType preferencesType = PreferencesType.DEFAULT_SHARED; 67 | private String preferencesName = ""; 68 | private PreferencesMode preferencesMode = PreferencesMode.MODE_PRIVATE; 69 | private SaveMode saveMode = SaveMode.APPLY; 70 | 71 | public Builder(Context context, Class cls) { 72 | this.context = context; 73 | this.cls = cls; 74 | 75 | readAnnotations(); 76 | } 77 | 78 | public Builder preferencesType(PreferencesType value) { 79 | preferencesType = value; 80 | return this; 81 | } 82 | 83 | public Builder preferencesType( 84 | PreferencesType value, String name) { 85 | 86 | preferencesType = value; 87 | preferencesName = name; 88 | return this; 89 | } 90 | 91 | public Builder preferencesMode(PreferencesMode value) { 92 | preferencesMode = value; 93 | return this; 94 | } 95 | 96 | public Builder saveMode(SaveMode value) { 97 | saveMode = value; 98 | return this; 99 | } 100 | 101 | /** 102 | * @return new instance of class {@code cls} using {@code context} 103 | */ 104 | @SuppressWarnings("unchecked") 105 | public T build() { 106 | validate(); 107 | 108 | return (T) Proxy.newProxyInstance( 109 | cls.getClassLoader(), 110 | new Class[]{cls}, 111 | new StoreBoxInvocationHandler( 112 | context, 113 | preferencesType, 114 | preferencesName, 115 | preferencesMode, 116 | saveMode)); 117 | } 118 | 119 | private void readAnnotations() { 120 | // type/mode option 121 | if (cls.isAnnotationPresent(DefaultSharedPreferences.class)) { 122 | preferencesType(PreferencesType.DEFAULT_SHARED); 123 | } else if (cls.isAnnotationPresent(ActivityPreferences.class)) { 124 | final ActivityPreferences annotation = 125 | cls.getAnnotation(ActivityPreferences.class); 126 | 127 | preferencesType(PreferencesType.ACTIVITY); 128 | preferencesMode(annotation.mode()); 129 | } else if (cls.isAnnotationPresent(FilePreferences.class)) { 130 | final FilePreferences annotation = 131 | cls.getAnnotation(FilePreferences.class); 132 | 133 | preferencesType(PreferencesType.FILE, annotation.value()); 134 | preferencesMode(annotation.mode()); 135 | } 136 | // save option 137 | if (cls.isAnnotationPresent(SaveOption.class)) { 138 | saveMode(cls.getAnnotation(SaveOption.class).value()); 139 | } 140 | } 141 | 142 | private void validate() { 143 | if (context == null) { 144 | throw new IllegalArgumentException( 145 | "Context cannot be null"); 146 | } 147 | if (cls == null) { 148 | throw new IllegalArgumentException( 149 | "Class cannot be null"); 150 | } else if (!cls.isInterface()) { 151 | throw new IllegalArgumentException( 152 | "Class needs to be an interface"); 153 | } 154 | 155 | if (preferencesType == PreferencesType.ACTIVITY) { 156 | if (!(context instanceof Activity)) { 157 | throw new IllegalArgumentException(String.format( 158 | Locale.ENGLISH, 159 | "Cannot use %1$s without an Activity context", 160 | PreferencesType.ACTIVITY.name())); 161 | } 162 | } else if (preferencesType == PreferencesType.FILE) { 163 | if (TextUtils.isEmpty(preferencesName)) { 164 | throw new IllegalArgumentException(String.format( 165 | Locale.ENGLISH, 166 | "Cannot use %1$s with an empty file name", 167 | PreferencesType.FILE.name())); 168 | } 169 | } 170 | } 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/adapters/StoreBoxTypeAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.adapters; 18 | 19 | /** 20 | * Interface defining a type adapter for adapting type {@link F} to {@link T}, 21 | * which can be natively stored in the preferences. 22 | *

23 | * This interface should not be implemented directly, but instead one of the 24 | * following classes should be extended to provide an adapter implementation 25 | * for storing {@link F}: 26 | *

    27 | *
  • {@link net.orange_box.storebox.adapters.base.BaseBooleanTypeAdapter} 28 | * as a {@link Boolean}
  • 29 | *
  • {@link net.orange_box.storebox.adapters.base.BaseFloatTypeAdapter} 30 | * as a {@link Float}
  • 31 | *
  • {@link net.orange_box.storebox.adapters.base.BaseIntegerTypeAdapter} 32 | * as an {@link Integer}
  • 33 | *
  • {@link net.orange_box.storebox.adapters.base.BaseLongTypeAdapter} 34 | * as a {@link Long}
  • 35 | *
  • {@link net.orange_box.storebox.adapters.base.BaseStringTypeAdapter} 36 | * as a {@link String}
  • 37 | *
  • {@link net.orange_box.storebox.adapters.base.BaseStringSetTypeAdapter} 38 | * as a {@link java.util.Set} of {@link String}s
  • 39 | *
40 | * 41 | * @param type which needs to be adapted 42 | * @param type which goes into the preferences 43 | * 44 | * @see net.orange_box.storebox.annotations.method.TypeAdapter 45 | */ 46 | public interface StoreBoxTypeAdapter { 47 | 48 | StoreType getStoreType(); 49 | 50 | T getDefaultValue(); 51 | 52 | T adaptForPreferences(F value); 53 | 54 | F adaptFromPreferences(T value); 55 | } 56 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/adapters/StoreType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.adapters; 18 | 19 | public enum StoreType { 20 | 21 | BOOLEAN, 22 | FLOAT, 23 | INTEGER, 24 | LONG, 25 | STRING, 26 | STRING_SET 27 | } 28 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/adapters/base/BaseBooleanTypeAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.adapters.base; 18 | 19 | import net.orange_box.storebox.adapters.StoreBoxTypeAdapter; 20 | import net.orange_box.storebox.adapters.StoreType; 21 | 22 | /** 23 | * A {@link StoreBoxTypeAdapter} which should be extended in order to provide 24 | * an adapter implementation for storing {@link T} as a {@link Boolean}. 25 | * 26 | * @param type which needs to be adapted 27 | */ 28 | public abstract class BaseBooleanTypeAdapter implements 29 | StoreBoxTypeAdapter { 30 | 31 | protected boolean DEFAULT; 32 | 33 | @Override 34 | public final StoreType getStoreType() { 35 | return StoreType.BOOLEAN; 36 | } 37 | 38 | @Override 39 | public Boolean getDefaultValue() { 40 | return DEFAULT; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/adapters/base/BaseFloatTypeAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.adapters.base; 18 | 19 | import net.orange_box.storebox.adapters.StoreBoxTypeAdapter; 20 | import net.orange_box.storebox.adapters.StoreType; 21 | 22 | /** 23 | * A {@link StoreBoxTypeAdapter} which should be extended in order to provide 24 | * an adapter implementation for storing {@link T} as a {@link Float}. 25 | * 26 | * @param type which needs to be adapted 27 | */ 28 | public abstract class BaseFloatTypeAdapter implements 29 | StoreBoxTypeAdapter { 30 | 31 | protected float DEFAULT; 32 | 33 | @Override 34 | public final StoreType getStoreType() { 35 | return StoreType.FLOAT; 36 | } 37 | 38 | @Override 39 | public Float getDefaultValue() { 40 | return DEFAULT; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/adapters/base/BaseIntegerTypeAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.adapters.base; 18 | 19 | import net.orange_box.storebox.adapters.StoreBoxTypeAdapter; 20 | import net.orange_box.storebox.adapters.StoreType; 21 | 22 | /** 23 | * A {@link StoreBoxTypeAdapter} which should be extended in order to provide 24 | * an adapter implementation for storing {@link T} as an {@link Integer}. 25 | * 26 | * @param type which needs to be adapted 27 | */ 28 | public abstract class BaseIntegerTypeAdapter implements 29 | StoreBoxTypeAdapter { 30 | 31 | protected int DEFAULT; 32 | 33 | @Override 34 | public final StoreType getStoreType() { 35 | return StoreType.INTEGER; 36 | } 37 | 38 | @Override 39 | public Integer getDefaultValue() { 40 | return DEFAULT; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/adapters/base/BaseLongTypeAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.adapters.base; 18 | 19 | import net.orange_box.storebox.adapters.StoreBoxTypeAdapter; 20 | import net.orange_box.storebox.adapters.StoreType; 21 | 22 | /** 23 | * A {@link StoreBoxTypeAdapter} which should be extended in order to provide 24 | * an adapter implementation for storing {@link T} as a {@link Long}. 25 | * 26 | * @param type which needs to be adapted 27 | */ 28 | public abstract class BaseLongTypeAdapter implements 29 | StoreBoxTypeAdapter { 30 | 31 | protected long DEFAULT; 32 | 33 | @Override 34 | public final StoreType getStoreType() { 35 | return StoreType.LONG; 36 | } 37 | 38 | @Override 39 | public Long getDefaultValue() { 40 | return DEFAULT; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/adapters/base/BaseStringSetTypeAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.adapters.base; 18 | 19 | import android.annotation.TargetApi; 20 | import android.os.Build; 21 | import android.support.annotation.Nullable; 22 | 23 | import net.orange_box.storebox.adapters.StoreBoxTypeAdapter; 24 | import net.orange_box.storebox.adapters.StoreType; 25 | 26 | import java.util.Set; 27 | 28 | /** 29 | * A {@link StoreBoxTypeAdapter} which should be extended in order to provide 30 | * an adapter implementation for storing {@link T} as a {@link Set} of 31 | * {@link String}s. 32 | * 33 | * @param type which needs to be adapted 34 | */ 35 | @TargetApi(Build.VERSION_CODES.HONEYCOMB) 36 | public abstract class BaseStringSetTypeAdapter implements 37 | StoreBoxTypeAdapter> { 38 | 39 | @Override 40 | public final StoreType getStoreType() { 41 | return StoreType.STRING_SET; 42 | } 43 | 44 | @Nullable 45 | @Override 46 | public Set getDefaultValue() { 47 | return null; 48 | } 49 | 50 | @Nullable 51 | @Override 52 | public abstract Set adaptForPreferences(@Nullable T value); 53 | 54 | @Nullable 55 | @Override 56 | public abstract T adaptFromPreferences(@Nullable Set value); 57 | } 58 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/adapters/base/BaseStringTypeAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.adapters.base; 18 | 19 | import android.support.annotation.Nullable; 20 | 21 | import net.orange_box.storebox.adapters.StoreBoxTypeAdapter; 22 | import net.orange_box.storebox.adapters.StoreType; 23 | 24 | /** 25 | * A {@link StoreBoxTypeAdapter} which should be extended in order to provide 26 | * an adapter implementation for storing {@link T} as a {@link String}. 27 | * 28 | * @param type which needs to be adapted 29 | */ 30 | public abstract class BaseStringTypeAdapter implements 31 | StoreBoxTypeAdapter { 32 | 33 | @Override 34 | public final StoreType getStoreType() { 35 | return StoreType.STRING; 36 | } 37 | 38 | @Nullable 39 | @Override 40 | public String getDefaultValue() { 41 | return null; 42 | } 43 | 44 | @Nullable 45 | @Override 46 | public abstract String adaptForPreferences(@Nullable T value); 47 | 48 | @Nullable 49 | @Override 50 | public abstract T adaptFromPreferences(@Nullable String value); 51 | } 52 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/adapters/extra/DateTypeAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.adapters.extra; 18 | 19 | import android.support.annotation.Nullable; 20 | 21 | import net.orange_box.storebox.adapters.base.BaseLongTypeAdapter; 22 | 23 | import java.util.Date; 24 | 25 | /** 26 | * A {@link net.orange_box.storebox.adapters.StoreBoxTypeAdapter} for 27 | * {@link Date} objects which uses {@link Long#MIN_VALUE} to represent 28 | * an absent/null preference value internally. 29 | */ 30 | public class DateTypeAdapter extends BaseLongTypeAdapter { 31 | 32 | @Override 33 | public Long getDefaultValue() { 34 | return Long.MIN_VALUE; 35 | } 36 | 37 | @Override 38 | public Long adaptForPreferences(@Nullable Date value) { 39 | if (value == null) { 40 | return Long.MIN_VALUE; 41 | } 42 | 43 | return value.getTime(); 44 | } 45 | 46 | @Nullable 47 | @Override 48 | public Date adaptFromPreferences(Long value) { 49 | if (value == Long.MIN_VALUE) { 50 | return null; 51 | } 52 | 53 | return new Date(value); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/adapters/extra/DoubleTypeAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.adapters.extra; 18 | 19 | import net.orange_box.storebox.adapters.base.BaseLongTypeAdapter; 20 | 21 | public class DoubleTypeAdapter extends BaseLongTypeAdapter { 22 | 23 | @Override 24 | public Long adaptForPreferences(Double value) { 25 | return Double.doubleToRawLongBits(value); 26 | } 27 | 28 | @Override 29 | public Double adaptFromPreferences(Long value) { 30 | return Double.longBitsToDouble(value); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/adapters/extra/EnumTypeAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.adapters.extra; 18 | 19 | import android.support.annotation.Nullable; 20 | 21 | import net.orange_box.storebox.adapters.base.BaseStringTypeAdapter; 22 | 23 | public class EnumTypeAdapter extends BaseStringTypeAdapter { 24 | 25 | private final Class type; 26 | 27 | public EnumTypeAdapter(Class type) { 28 | this.type = type; 29 | } 30 | 31 | @Nullable 32 | @Override 33 | public String adaptForPreferences(@Nullable Enum value) { 34 | if (value == null) { 35 | return null; 36 | } 37 | 38 | return value.name(); 39 | } 40 | 41 | @Nullable 42 | @Override 43 | public Enum adaptFromPreferences(@Nullable String value) { 44 | if (value == null) { 45 | return null; 46 | } 47 | 48 | return Enum.valueOf(type, value); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/adapters/extra/UriTypeAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.adapters.extra; 18 | 19 | import android.net.Uri; 20 | import android.support.annotation.Nullable; 21 | 22 | import net.orange_box.storebox.adapters.base.BaseStringTypeAdapter; 23 | 24 | public class UriTypeAdapter extends BaseStringTypeAdapter { 25 | 26 | @Nullable 27 | @Override 28 | public String adaptForPreferences(@Nullable Uri value) { 29 | if (value == null) { 30 | return null; 31 | } 32 | 33 | return value.toString(); 34 | } 35 | 36 | @Nullable 37 | @Override 38 | public Uri adaptFromPreferences(@Nullable String value) { 39 | if (value == null) { 40 | return null; 41 | } 42 | 43 | return Uri.parse(value); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/adapters/standard/BooleanTypeAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.adapters.standard; 18 | 19 | import net.orange_box.storebox.adapters.base.BaseBooleanTypeAdapter; 20 | 21 | public class BooleanTypeAdapter extends BaseBooleanTypeAdapter { 22 | 23 | @Override 24 | public Boolean adaptForPreferences(Boolean value) { 25 | return value; 26 | } 27 | 28 | @Override 29 | public Boolean adaptFromPreferences(Boolean value) { 30 | return value; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/adapters/standard/FloatTypeAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.adapters.standard; 18 | 19 | import net.orange_box.storebox.adapters.base.BaseFloatTypeAdapter; 20 | 21 | public class FloatTypeAdapter extends BaseFloatTypeAdapter { 22 | 23 | @Override 24 | public Float adaptForPreferences(Float value) { 25 | return value; 26 | } 27 | 28 | @Override 29 | public Float adaptFromPreferences(Float value) { 30 | return value; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/adapters/standard/IntegerTypeAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.adapters.standard; 18 | 19 | import net.orange_box.storebox.adapters.base.BaseIntegerTypeAdapter; 20 | 21 | public class IntegerTypeAdapter extends BaseIntegerTypeAdapter { 22 | 23 | @Override 24 | public Integer adaptForPreferences(Integer value) { 25 | return value; 26 | } 27 | 28 | @Override 29 | public Integer adaptFromPreferences(Integer value) { 30 | return value; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/adapters/standard/LongTypeAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.adapters.standard; 18 | 19 | import net.orange_box.storebox.adapters.base.BaseLongTypeAdapter; 20 | 21 | public class LongTypeAdapter extends BaseLongTypeAdapter { 22 | 23 | @Override 24 | public Long adaptForPreferences(Long value) { 25 | return value; 26 | } 27 | 28 | @Override 29 | public Long adaptFromPreferences(Long value) { 30 | return value; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/adapters/standard/StringSetTypeAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.adapters.standard; 18 | 19 | import net.orange_box.storebox.adapters.base.BaseStringSetTypeAdapter; 20 | 21 | import java.util.Set; 22 | 23 | public class StringSetTypeAdapter extends 24 | BaseStringSetTypeAdapter> { 25 | 26 | @Override 27 | public Set adaptForPreferences(Set value) { 28 | return value; 29 | } 30 | 31 | @Override 32 | public Set adaptFromPreferences(Set value) { 33 | return value; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/adapters/standard/StringTypeAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.adapters.standard; 18 | 19 | import net.orange_box.storebox.adapters.base.BaseStringTypeAdapter; 20 | 21 | public class StringTypeAdapter extends BaseStringTypeAdapter { 22 | 23 | @Override 24 | public String adaptForPreferences(String value) { 25 | return value; 26 | } 27 | 28 | @Override 29 | public String adaptFromPreferences(String value) { 30 | return value; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/annotations/method/ClearMethod.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015-2016 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.annotations.method; 18 | 19 | import android.content.SharedPreferences; 20 | 21 | import java.lang.annotation.ElementType; 22 | import java.lang.annotation.Retention; 23 | import java.lang.annotation.RetentionPolicy; 24 | import java.lang.annotation.Target; 25 | 26 | /** 27 | * Annotation which should be used for a clear method, for removing all values 28 | * from the preferences. 29 | * 30 | * @see SharedPreferences.Editor#clear() 31 | */ 32 | @Retention(RetentionPolicy.RUNTIME) 33 | @Target(ElementType.METHOD) 34 | public @interface ClearMethod {} 35 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/annotations/method/DefaultValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.annotations.method; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | /** 25 | * Annotation which should be used on set methods to declare the resource for 26 | * the default value which should be returned when a preference with the 27 | * requested key does not exist. 28 | *

29 | * A parameter default takes precedence over this annotation. 30 | */ 31 | @Retention(RetentionPolicy.RUNTIME) 32 | @Target(ElementType.METHOD) 33 | public @interface DefaultValue { 34 | 35 | int value(); 36 | } 37 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/annotations/method/KeyByResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.annotations.method; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | /** 25 | * Can be used when it is more convenient for keys to be declared using an 26 | * integer resource, rather than a String. For example, if the key names are 27 | * stored in an XML resource file. 28 | * 29 | * @see KeyByString 30 | */ 31 | @Retention(RetentionPolicy.RUNTIME) 32 | @Target(ElementType.METHOD) 33 | public @interface KeyByResource { 34 | 35 | int value(); 36 | } 37 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/annotations/method/KeyByString.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.annotations.method; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | /** 25 | * Annotation which should be used on set/get methods to declare the key under 26 | * which the value should be stored/retrieved. 27 | * 28 | * @see KeyByResource 29 | */ 30 | @Retention(RetentionPolicy.RUNTIME) 31 | @Target(ElementType.METHOD) 32 | public @interface KeyByString { 33 | 34 | String value(); 35 | } 36 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/annotations/method/RegisterChangeListenerMethod.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.annotations.method; 18 | 19 | import net.orange_box.storebox.listeners.OnPreferenceValueChangedListener; 20 | 21 | import java.lang.annotation.ElementType; 22 | import java.lang.annotation.Retention; 23 | import java.lang.annotation.RetentionPolicy; 24 | import java.lang.annotation.Target; 25 | 26 | /** 27 | * Annotation which should be used for registering a 28 | * {@link OnPreferenceValueChangedListener}. 29 | * The key for the value that should be listened to can be provided through 30 | * a {@link KeyByString} or {@link KeyByResource} annotation. 31 | * 32 | * @see UnregisterChangeListenerMethod 33 | * @see OnPreferenceValueChangedListener 34 | */ 35 | @Retention(RetentionPolicy.RUNTIME) 36 | @Target(ElementType.METHOD) 37 | public @interface RegisterChangeListenerMethod {} 38 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/annotations/method/RemoveMethod.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.annotations.method; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | /** 25 | * Annotation which should be used for a remove method. The key for the value 26 | * which should be removed can be provided in two ways: 27 | *

    28 | *
  • 29 | * Using just this annotation and adding either a {@link String} or {@code int} 30 | * type parameter to the method. The latter case can be used if the key name 31 | * will be provided from an XML resource. 32 | *
  • 33 | *
  • 34 | * Adding a {@link KeyByString} or {@link KeyByResource} annotation to the 35 | * method. 36 | *
  • 37 | *
38 | * 39 | * @see android.content.SharedPreferences.Editor#remove(String) 40 | */ 41 | @Retention(RetentionPolicy.RUNTIME) 42 | @Target(ElementType.METHOD) 43 | public @interface RemoveMethod {} 44 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/annotations/method/TypeAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.annotations.method; 18 | 19 | import net.orange_box.storebox.adapters.StoreBoxTypeAdapter; 20 | 21 | import java.lang.annotation.ElementType; 22 | import java.lang.annotation.Retention; 23 | import java.lang.annotation.RetentionPolicy; 24 | import java.lang.annotation.Target; 25 | 26 | /** 27 | * Annotation which should be used on set and get methods to declare the 28 | * {@link StoreBoxTypeAdapter} to be used for adapting the type for the 29 | * preferences. 30 | *

31 | * Type adapters for {@link java.util.Date}, {@link Enum}, and 32 | * {@link android.net.Uri} are already supported and as such there is no need 33 | * to provide a type adapter for them. 34 | */ 35 | @Retention(RetentionPolicy.RUNTIME) 36 | @Target(ElementType.METHOD) 37 | public @interface TypeAdapter { 38 | 39 | Class value(); 40 | } 41 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/annotations/method/UnregisterChangeListenerMethod.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.annotations.method; 18 | 19 | import net.orange_box.storebox.listeners.OnPreferenceValueChangedListener; 20 | 21 | import java.lang.annotation.ElementType; 22 | import java.lang.annotation.Retention; 23 | import java.lang.annotation.RetentionPolicy; 24 | import java.lang.annotation.Target; 25 | 26 | /** 27 | * Annotation which should be used for unregistering a 28 | * {@link OnPreferenceValueChangedListener}. 29 | * The key for the value that should stop being listened to can be provided 30 | * through a {@link KeyByString} or {@link KeyByResource} annotation. 31 | * 32 | * @see RegisterChangeListenerMethod 33 | * @see OnPreferenceValueChangedListener 34 | */ 35 | @Retention(RetentionPolicy.RUNTIME) 36 | @Target(ElementType.METHOD) 37 | public @interface UnregisterChangeListenerMethod {} 38 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/annotations/option/SaveOption.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.annotations.option; 18 | 19 | import net.orange_box.storebox.enums.SaveMode; 20 | 21 | import java.lang.annotation.ElementType; 22 | import java.lang.annotation.Retention; 23 | import java.lang.annotation.RetentionPolicy; 24 | import java.lang.annotation.Target; 25 | 26 | /** 27 | * Annotation which should be used to define what {@link SaveMode} will be 28 | * applied for get methods which don't specify a default value. 29 | *

30 | * Annotation can be used at interface and method-level, however any 31 | * method-level annotations will take precedence over interface-level 32 | * annotations. 33 | */ 34 | @Retention(RetentionPolicy.RUNTIME) 35 | @Target({ElementType.TYPE, ElementType.METHOD}) 36 | public @interface SaveOption { 37 | 38 | SaveMode value(); 39 | } 40 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/annotations/type/ActivityPreferences.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.annotations.type; 18 | 19 | import net.orange_box.storebox.enums.PreferencesMode; 20 | 21 | import java.lang.annotation.ElementType; 22 | import java.lang.annotation.Retention; 23 | import java.lang.annotation.RetentionPolicy; 24 | import java.lang.annotation.Target; 25 | 26 | /** 27 | * Annotation which should be used at interface-level to define that the 28 | * preferences private to the Activity should be used. 29 | *

30 | * When this annotation is used an {@link android.app.Activity} context needs 31 | * to be passed in when instantiating the interface using 32 | * {@link net.orange_box.storebox.StoreBox}. 33 | * 34 | * @see net.orange_box.storebox.enums.PreferencesType#ACTIVITY 35 | * @see android.app.Activity#getPreferences(int) 36 | */ 37 | @Retention(RetentionPolicy.RUNTIME) 38 | @Target(ElementType.TYPE) 39 | public @interface ActivityPreferences { 40 | 41 | PreferencesMode mode() default PreferencesMode.MODE_PRIVATE; 42 | } 43 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/annotations/type/DefaultSharedPreferences.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.annotations.type; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | /** 25 | * Annotation which should be used at interface-level to define that the 26 | * default shared preferences should be used. 27 | *

28 | * This is the default type used when no annotation has been specified for an 29 | * interface. 30 | * 31 | * @see net.orange_box.storebox.enums.PreferencesType#ACTIVITY 32 | * @see android.preference.PreferenceManager#getDefaultSharedPreferences( 33 | * android.content.Context) 34 | */ 35 | @Retention(RetentionPolicy.RUNTIME) 36 | @Target(ElementType.TYPE) 37 | public @interface DefaultSharedPreferences { 38 | 39 | // NOP 40 | } 41 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/annotations/type/FilePreferences.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.annotations.type; 18 | 19 | import net.orange_box.storebox.enums.PreferencesMode; 20 | 21 | import java.lang.annotation.ElementType; 22 | import java.lang.annotation.Retention; 23 | import java.lang.annotation.RetentionPolicy; 24 | import java.lang.annotation.Target; 25 | 26 | /** 27 | * Annotation which should be used at interface-level to define that the 28 | * preferences should be opened from a file name. 29 | *

30 | * When this annotation is used a file name needs to be specified using 31 | * {@link #value()}. 32 | * 33 | * @see net.orange_box.storebox.enums.PreferencesType#FILE 34 | * @see android.content.Context#getSharedPreferences(String, int) 35 | */ 36 | @Retention(RetentionPolicy.RUNTIME) 37 | @Target(ElementType.TYPE) 38 | public @interface FilePreferences { 39 | 40 | String value(); 41 | 42 | PreferencesMode mode() default PreferencesMode.MODE_PRIVATE; 43 | } 44 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/enums/PreferencesMode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.enums; 18 | 19 | import android.content.Context; 20 | 21 | public enum PreferencesMode { 22 | 23 | /** 24 | * Default. 25 | * 26 | * @see android.content.Context#MODE_PRIVATE 27 | */ 28 | MODE_PRIVATE(Context.MODE_PRIVATE), 29 | 30 | /** 31 | * @see android.content.Context#MODE_MULTI_PROCESS 32 | */ 33 | MODE_MULTI_PROCESS(Context.MODE_MULTI_PROCESS), 34 | 35 | /** 36 | * @see android.content.Context#MODE_WORLD_READABLE 37 | */ 38 | @Deprecated 39 | MODE_WORLD_READABLE(Context.MODE_WORLD_READABLE), 40 | 41 | /** 42 | * @see android.content.Context#MODE_WORLD_WRITEABLE 43 | */ 44 | @Deprecated 45 | MODE_WORLD_WRITEABLE(Context.MODE_WORLD_WRITEABLE); 46 | 47 | private final int value; 48 | 49 | private PreferencesMode(int value) { 50 | this.value = value; 51 | } 52 | 53 | public int value() { 54 | return value; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/enums/PreferencesType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.enums; 18 | 19 | public enum PreferencesType { 20 | 21 | /** 22 | * Default. 23 | * 24 | * @see net.orange_box.storebox.annotations.type.DefaultSharedPreferences 25 | * @see android.preference.PreferenceManager#getDefaultSharedPreferences( 26 | * android.content.Context) 27 | */ 28 | DEFAULT_SHARED, 29 | 30 | /** 31 | * @see net.orange_box.storebox.annotations.type.ActivityPreferences 32 | * @see android.app.Activity#getPreferences(int) 33 | */ 34 | ACTIVITY, 35 | 36 | /** 37 | * @see net.orange_box.storebox.annotations.type.FilePreferences 38 | * @see android.content.Context#getSharedPreferences(String, int) 39 | */ 40 | FILE 41 | } 42 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/enums/SaveMode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.enums; 18 | 19 | public enum SaveMode { 20 | 21 | /** 22 | * Default. 23 | * 24 | * @see android.content.SharedPreferences.Editor#apply() 25 | */ 26 | APPLY, 27 | 28 | /** 29 | * @see android.content.SharedPreferences.Editor#commit() 30 | */ 31 | COMMIT, 32 | 33 | /** 34 | * {@link android.content.SharedPreferences.Editor#apply()} or 35 | * {@link android.content.SharedPreferences.Editor#commit()} will have 36 | * to be called explicitly. 37 | */ 38 | NOME 39 | } 40 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/handlers/ChangeListenerMethodHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.handlers; 18 | 19 | import android.content.SharedPreferences; 20 | import android.support.annotation.Nullable; 21 | 22 | import net.jodah.typetools.TypeResolver; 23 | import net.orange_box.storebox.adapters.StoreBoxTypeAdapter; 24 | import net.orange_box.storebox.annotations.method.RegisterChangeListenerMethod; 25 | import net.orange_box.storebox.annotations.method.UnregisterChangeListenerMethod; 26 | import net.orange_box.storebox.annotations.method.TypeAdapter; 27 | import net.orange_box.storebox.listeners.OnPreferenceValueChangedListener; 28 | import net.orange_box.storebox.utils.PreferenceUtils; 29 | import net.orange_box.storebox.utils.TypeUtils; 30 | 31 | import java.lang.ref.WeakReference; 32 | import java.lang.reflect.Method; 33 | import java.util.Arrays; 34 | import java.util.HashSet; 35 | import java.util.Iterator; 36 | import java.util.Locale; 37 | import java.util.Map; 38 | import java.util.Set; 39 | import java.util.concurrent.ConcurrentHashMap; 40 | 41 | /** 42 | * TODO should use a ReferenceQueue for cleaning up 43 | */ 44 | public class ChangeListenerMethodHandler implements 45 | MethodHandler, 46 | SharedPreferences.OnSharedPreferenceChangeListener { 47 | 48 | private final SharedPreferences prefs; 49 | private final Map> listeners; 50 | 51 | public ChangeListenerMethodHandler(SharedPreferences prefs) { 52 | this.prefs = prefs; 53 | 54 | listeners = new ConcurrentHashMap<>(); 55 | 56 | prefs.registerOnSharedPreferenceChangeListener(this); 57 | } 58 | 59 | @Override 60 | public Object handleInvocation( 61 | String key, 62 | Object proxy, 63 | Method method, 64 | Object... args) 65 | throws Throwable { 66 | 67 | if (args.length == 0) { 68 | throw new IllegalArgumentException( 69 | "At least one argument must be supplied for" + 70 | "register/unregister listener method"); 71 | } 72 | 73 | final Set listenerInfos = new HashSet<>(); 74 | for (final Object arg : args) { 75 | if (arg instanceof OnPreferenceValueChangedListener) { 76 | listenerInfos.add(ListenerInfo.create( 77 | (OnPreferenceValueChangedListener) arg, 78 | method.getAnnotation(TypeAdapter.class))); 79 | } else if (arg instanceof OnPreferenceValueChangedListener[]) { 80 | listenerInfos.addAll(Arrays.asList(ListenerInfo.create( 81 | (OnPreferenceValueChangedListener[]) arg, 82 | method.getAnnotation(TypeAdapter.class)))); 83 | } else { 84 | throw new IllegalArgumentException(String.format( 85 | Locale.ENGLISH, 86 | "Argument for register/unregister" + 87 | " listener method must be of %s type", 88 | OnPreferenceValueChangedListener.class.getSimpleName())); 89 | } 90 | } 91 | 92 | if (method.isAnnotationPresent( 93 | RegisterChangeListenerMethod.class)) { 94 | 95 | if (listeners.containsKey(key)) { 96 | listeners.get(key).addAll(listenerInfos); 97 | } else { 98 | listeners.put(key, listenerInfos); 99 | } 100 | } else if (method.isAnnotationPresent( 101 | UnregisterChangeListenerMethod.class)) { 102 | 103 | if (listeners.containsKey(key)) { 104 | listeners.get(key).removeAll(listenerInfos); 105 | } 106 | } 107 | 108 | return null; 109 | } 110 | 111 | @Override 112 | public void onSharedPreferenceChanged( 113 | SharedPreferences sharedPreferences, 114 | String key) { 115 | 116 | final Set listeners = this.listeners.get(key); 117 | if (listeners != null) { 118 | final Iterator it = listeners.iterator(); 119 | while (it.hasNext()) { 120 | final ListenerInfo listenerInfo = it.next(); 121 | final StoreBoxTypeAdapter adapter = listenerInfo.getAdapter(); 122 | 123 | final Object newValue = PreferenceUtils.getValue( 124 | prefs, 125 | key, 126 | adapter.getStoreType(), 127 | adapter.getDefaultValue()); 128 | 129 | final OnPreferenceValueChangedListener listener = 130 | listenerInfo.getListener(); 131 | 132 | if (listener != null) { 133 | listener.onChanged(adapter.adaptFromPreferences(newValue)); 134 | } else { 135 | it.remove(); 136 | } 137 | } 138 | } 139 | } 140 | 141 | private static class ListenerInfo { 142 | 143 | private final WeakReference listener; 144 | private final StoreBoxTypeAdapter adapter; 145 | 146 | private final int hashCode; 147 | 148 | public ListenerInfo( 149 | OnPreferenceValueChangedListener listener, 150 | StoreBoxTypeAdapter adapter) { 151 | 152 | this.listener = new WeakReference<>(listener); 153 | this.adapter = adapter; 154 | 155 | hashCode = listener.hashCode(); 156 | } 157 | 158 | @Nullable 159 | public OnPreferenceValueChangedListener getListener() { 160 | return listener.get(); 161 | } 162 | 163 | public StoreBoxTypeAdapter getAdapter() { 164 | return adapter; 165 | } 166 | 167 | @Override 168 | public boolean equals(Object o) { 169 | if (o == null || !(o instanceof ListenerInfo)) { 170 | return false; 171 | } else if (this == o) { 172 | return true; 173 | } 174 | 175 | final ListenerInfo other = (ListenerInfo) o; 176 | 177 | return (getListener() == null) 178 | ? (other.getListener() == null) 179 | : getListener().equals(other.getListener()); 180 | } 181 | 182 | @Override 183 | public int hashCode() { 184 | return hashCode; 185 | } 186 | 187 | private static ListenerInfo create( 188 | OnPreferenceValueChangedListener listener, 189 | TypeAdapter adapterAnnotation) { 190 | 191 | return new ListenerInfo( 192 | listener, 193 | TypeUtils.getTypeAdapter( 194 | TypeResolver.resolveRawArguments( 195 | OnPreferenceValueChangedListener.class, 196 | listener.getClass())[0], 197 | adapterAnnotation)); 198 | } 199 | 200 | private static ListenerInfo[] create( 201 | OnPreferenceValueChangedListener[] listeners, 202 | TypeAdapter adapterAnnotation) { 203 | 204 | final ListenerInfo[] result = new ListenerInfo[listeners.length]; 205 | 206 | for (int i = 0; i < listeners.length; i++) { 207 | result[i] = create(listeners[i], adapterAnnotation); 208 | } 209 | 210 | return result; 211 | } 212 | } 213 | } 214 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/handlers/MethodHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.handlers; 18 | 19 | import java.lang.reflect.Method; 20 | 21 | public interface MethodHandler { 22 | 23 | Object handleInvocation( 24 | String key, 25 | Object proxy, 26 | Method method, 27 | Object... args) 28 | throws Throwable; 29 | } 30 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/listeners/OnPreferenceValueChangedListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.listeners; 18 | 19 | /** 20 | * Interface definition for a callback to be invoked when a preference value 21 | * is changed. 22 | * 23 | * @param type of the value 24 | * 25 | * @see net.orange_box.storebox.annotations.method.RegisterChangeListenerMethod 26 | * @see net.orange_box.storebox.annotations.method.UnregisterChangeListenerMethod 27 | */ 28 | public interface OnPreferenceValueChangedListener { 29 | 30 | void onChanged(T newValue); 31 | } 32 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/utils/MethodUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.utils; 18 | 19 | import android.content.res.Resources; 20 | 21 | import java.lang.annotation.Annotation; 22 | import java.lang.reflect.Method; 23 | 24 | public final class MethodUtils { 25 | 26 | @SafeVarargs 27 | public static > boolean areAnyAnnotationsPresent( 28 | Method method, T... types) { 29 | 30 | for (final T type : types) { 31 | if (method.isAnnotationPresent(type)) { 32 | return true; 33 | } 34 | } 35 | return false; 36 | } 37 | 38 | public static String getKeyForRemove(Resources res, Object... args) { 39 | if (args != null && args.length > 0) { 40 | final Object value = args[0]; 41 | final Class type = TypeUtils.wrapToBoxedType(value.getClass()); 42 | 43 | if (type == String.class) { 44 | return (String) value; 45 | } else if (type == Integer.class) { 46 | return res.getString((int) value); 47 | } else { 48 | throw new UnsupportedOperationException( 49 | "Only String or int supported for remove method"); 50 | } 51 | } else { 52 | throw new UnsupportedOperationException( 53 | "String or int key argument not found for remove method"); 54 | } 55 | } 56 | 57 | public static Object getValueArg(Object... args) { 58 | if (args != null && args.length > 0) { 59 | return args[0]; 60 | } else { 61 | throw new UnsupportedOperationException( 62 | "Value argument not found"); 63 | } 64 | } 65 | 66 | public static Class getValueParameterType(Method method) { 67 | final Class[] types = method.getParameterTypes(); 68 | if (types != null && types.length > 0) { 69 | return TypeUtils.wrapToBoxedType(types[0]); 70 | } else { 71 | throw new UnsupportedOperationException( 72 | "Value parameter type not found"); 73 | } 74 | } 75 | 76 | public static Method getObjectMethod(String name, Class... types) { 77 | try { 78 | return Object.class.getMethod(name, types); 79 | } catch (NoSuchMethodException e) { 80 | throw new IllegalArgumentException(e); 81 | } 82 | } 83 | 84 | private MethodUtils() {} 85 | } 86 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/utils/PreferenceUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.utils; 18 | 19 | import android.annotation.TargetApi; 20 | import android.content.SharedPreferences; 21 | import android.os.Build; 22 | 23 | import net.orange_box.storebox.adapters.StoreType; 24 | import net.orange_box.storebox.enums.SaveMode; 25 | 26 | import java.util.Locale; 27 | import java.util.Set; 28 | 29 | public final class PreferenceUtils { 30 | 31 | public static Object getValue( 32 | SharedPreferences prefs, 33 | String key, 34 | StoreType type, 35 | Object defValue) { 36 | 37 | switch (type) { 38 | case BOOLEAN: 39 | return prefs.getBoolean(key, (Boolean) defValue); 40 | 41 | case FLOAT: 42 | return prefs.getFloat(key, (Float) defValue); 43 | 44 | case INTEGER: 45 | return prefs.getInt(key, (Integer) defValue); 46 | 47 | case LONG: 48 | return prefs.getLong(key, (Long) defValue); 49 | 50 | case STRING: 51 | return prefs.getString(key, (String) defValue); 52 | 53 | case STRING_SET: 54 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 55 | return getStringSetApi11( 56 | prefs, key, (Set) defValue); 57 | } 58 | 59 | default: 60 | throw new UnsupportedOperationException(String.format( 61 | Locale.ENGLISH, 62 | "Retrieving type %1$s from the preferences is not supported", 63 | type.name())); 64 | } 65 | } 66 | 67 | public static void putValue( 68 | SharedPreferences.Editor editor, 69 | String key, 70 | StoreType type, 71 | Object value) { 72 | 73 | switch (type) { 74 | case BOOLEAN: 75 | editor.putBoolean(key, (Boolean) value); 76 | break; 77 | 78 | case FLOAT: 79 | editor.putFloat(key, (Float) value); 80 | break; 81 | 82 | case INTEGER: 83 | editor.putInt(key, (Integer) value); 84 | break; 85 | 86 | case LONG: 87 | editor.putLong(key, (Long) value); 88 | break; 89 | 90 | case STRING: 91 | editor.putString(key, (String) value); 92 | break; 93 | 94 | case STRING_SET: 95 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 96 | putStringSetApi11(editor, key, (Set) value); 97 | break; 98 | } 99 | 100 | default: 101 | throw new UnsupportedOperationException(String.format( 102 | Locale.ENGLISH, 103 | "Saving type %1$s into the preferences is not supported", 104 | type.name())); 105 | } 106 | } 107 | 108 | public static void saveChanges( 109 | SharedPreferences.Editor editor, 110 | SaveMode mode) { 111 | 112 | switch (mode) { 113 | case APPLY: 114 | editor.apply(); 115 | break; 116 | 117 | case COMMIT: 118 | editor.commit(); 119 | break; 120 | 121 | case NOME: 122 | default: 123 | // NOP 124 | } 125 | } 126 | 127 | @TargetApi(Build.VERSION_CODES.HONEYCOMB) 128 | private static Set getStringSetApi11( 129 | SharedPreferences prefs, 130 | String key, 131 | Set defValue) { 132 | 133 | return prefs.getStringSet(key, defValue); 134 | } 135 | 136 | @TargetApi(Build.VERSION_CODES.HONEYCOMB) 137 | private static void putStringSetApi11( 138 | SharedPreferences.Editor editor, 139 | String key, 140 | Set value) { 141 | 142 | editor.putStringSet(key, value); 143 | } 144 | 145 | private PreferenceUtils() {} 146 | } 147 | -------------------------------------------------------------------------------- /storebox-lib/src/main/java/net/orange_box/storebox/utils/TypeUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Martin Bella 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.orange_box.storebox.utils; 18 | 19 | import android.net.Uri; 20 | 21 | import net.orange_box.storebox.adapters.extra.DateTypeAdapter; 22 | import net.orange_box.storebox.adapters.extra.DoubleTypeAdapter; 23 | import net.orange_box.storebox.adapters.extra.EnumTypeAdapter; 24 | import net.orange_box.storebox.adapters.StoreBoxTypeAdapter; 25 | import net.orange_box.storebox.adapters.standard.BooleanTypeAdapter; 26 | import net.orange_box.storebox.adapters.standard.FloatTypeAdapter; 27 | import net.orange_box.storebox.adapters.standard.IntegerTypeAdapter; 28 | import net.orange_box.storebox.adapters.standard.LongTypeAdapter; 29 | import net.orange_box.storebox.adapters.standard.StringSetTypeAdapter; 30 | import net.orange_box.storebox.adapters.standard.StringTypeAdapter; 31 | import net.orange_box.storebox.adapters.extra.UriTypeAdapter; 32 | import net.orange_box.storebox.annotations.method.TypeAdapter; 33 | 34 | import java.util.Date; 35 | import java.util.HashMap; 36 | import java.util.Locale; 37 | import java.util.Map; 38 | import java.util.Set; 39 | import java.util.concurrent.ConcurrentHashMap; 40 | 41 | public final class TypeUtils { 42 | 43 | private static final Map, Class> PRIMITIVE_TO_BOXED_MAP; 44 | static { 45 | final Map, Class> map = new HashMap<>(5); 46 | // standard 47 | map.put(boolean.class, Boolean.class); 48 | map.put(float.class, Float.class); 49 | map.put(int.class, Integer.class); 50 | map.put(long.class, Long.class); 51 | // extra 52 | map.put(double.class, Double.class); 53 | 54 | PRIMITIVE_TO_BOXED_MAP = map; 55 | } 56 | 57 | private static final Map, StoreBoxTypeAdapter> ADAPTERS_MAP; 58 | static { 59 | final Map, StoreBoxTypeAdapter> map = 60 | new ConcurrentHashMap<>(9); 61 | 62 | // standard 63 | map.put(Boolean.class, new BooleanTypeAdapter()); 64 | map.put(Float.class, new FloatTypeAdapter()); 65 | map.put(Integer.class, new IntegerTypeAdapter()); 66 | map.put(Long.class, new LongTypeAdapter()); 67 | map.put(String.class, new StringTypeAdapter()); 68 | map.put(Set.class, new StringSetTypeAdapter()); 69 | 70 | // extra 71 | map.put(Date.class, new DateTypeAdapter()); 72 | map.put(Double.class, new DoubleTypeAdapter()); 73 | map.put(Uri.class, new UriTypeAdapter()); 74 | 75 | ADAPTERS_MAP = map; 76 | } 77 | 78 | public static Class wrapToBoxedType(Class type) { 79 | if (type.isPrimitive() && PRIMITIVE_TO_BOXED_MAP.containsKey(type)) { 80 | return PRIMITIVE_TO_BOXED_MAP.get(type); 81 | } else { 82 | return type; 83 | } 84 | } 85 | 86 | @SuppressWarnings("unchecked") 87 | public static StoreBoxTypeAdapter getTypeAdapter( 88 | Class type, 89 | TypeAdapter annotation) { 90 | 91 | if (annotation != null) { 92 | try { 93 | return annotation.value().newInstance(); 94 | } catch (InstantiationException e) { 95 | throw new RuntimeException(String.format( 96 | Locale.ENGLISH, 97 | "Failed to instantiate %1$s, perhaps the no-arguments " + 98 | "constructor is missing?", 99 | annotation.value().getSimpleName()), 100 | e); 101 | } catch (IllegalAccessException e) { 102 | throw new RuntimeException(String.format( 103 | Locale.ENGLISH, 104 | "Failed to instantiate %1$s, perhaps the no-arguments " + 105 | "constructor is not public?", 106 | annotation.value().getSimpleName()), 107 | e); 108 | } 109 | } else { 110 | StoreBoxTypeAdapter adapter = ADAPTERS_MAP.get(type); 111 | 112 | if (adapter == null && type.isEnum()) { 113 | // enums have a special type adapter 114 | adapter = new EnumTypeAdapter((Class) type); 115 | ADAPTERS_MAP.put(type, adapter); 116 | } 117 | 118 | if (adapter != null) { 119 | return adapter; 120 | } 121 | 122 | throw new RuntimeException(String.format( 123 | Locale.ENGLISH, 124 | "Failed to find type adapter for %1$s", 125 | type.getName())); 126 | } 127 | } 128 | 129 | private TypeUtils() {} 130 | } 131 | --------------------------------------------------------------------------------