├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── CacheUtilsLibrary.iml ├── LICENSE ├── README.md ├── README_zhCN.md ├── build.gradle ├── cacheutilslibrary ├── .gitignore ├── build.gradle ├── cacheutilslibrary.iml ├── proguard-rules.pro └── src │ ├── main │ ├── AndroidManifest.xml │ └── java │ │ └── com │ │ └── lifeofcoding │ │ └── cacheutlislibrary │ │ ├── CacheUtils.java │ │ └── GsonHelper.java │ └── test │ └── java │ └── com │ └── lifeofcoding │ └── cacheutlislibrary │ └── ExampleUnitTest.java ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── sample ├── .gitignore ├── build.gradle ├── proguard-rules.pro ├── sample.iml └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── lifeofcoding │ │ └── cacheutilslibrary_sample │ │ ├── MainActivity.java │ │ ├── MyApplication.java │ │ └── MyClass.java │ └── res │ ├── layout │ └── activity_main.xml │ ├── menu │ └── menu_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | 19 | # Local configuration file (sdk path, etc) 20 | local.properties 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # Log Files 26 | *.log 27 | 28 | /local.properties 29 | /.idea/workspace.xml 30 | /.idea/libraries 31 | .DS_Store 32 | /captures 33 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | CacheUtilsLibrary -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 25 | 26 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CacheUtilsLibrary.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # CacheUtilsLibrary 4 | 5 | [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-CacheUtilsLibrary-green.svg?style=flat)](https://android-arsenal.com/details/1/2478) 6 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.lifeofcoding/cacheutilslibrary/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.lifeofcoding/cacheutilslibrary) 7 | [![Android Gems](http://www.android-gems.com/badge/westlinkin/CacheUtilsLibrary.svg?branch=master)](http://www.android-gems.com/lib/westlinkin/CacheUtilsLibrary) 8 | 9 | This is a simple Android utils library to write any type of data into cache files and then read them later, using `Gson` to serialize and deserialize these data. 10 | 11 | 中文版请看[这里](https://github.com/westlinkin/CacheUtilsLibrary/blob/master/README_zhCN.md)。 12 | 13 | ##Gradle 14 | ```Groovy 15 | compile 'com.lifeofcoding:cacheutilslibrary:1.1.0@aar' 16 | compile 'com.google.code.gson:gson:2.2.2' 17 | compile 'commons-io:commons-io:2.4' 18 | ``` 19 | 20 | If you have errors like: 21 | 22 | ``` 23 | duplication file during packaging of APK ... 24 | Path in archive: META-INF/LICENSE.txt 25 | ... 26 | ``` 27 | Please add such code in your `android` entry of your `build.gradle` file: 28 | 29 | ```Groovy 30 | packagingOptions { 31 | exclude 'META-INF/DEPENDENCIES' 32 | exclude 'META-INF/NOTICE' 33 | exclude 'META-INF/LICENSE' 34 | exclude 'META-INF/LICENSE.txt' 35 | exclude 'META-INF/NOTICE.txt' 36 | exclude 'META-INF/ASL2.0' 37 | exclude 'META-INF/MANIFEST.MF' 38 | } 39 | ``` 40 | 41 | ##Configuration 42 | You need to configure `CacheUtilsLibrary` in your `Application` class. 43 | 44 | ```Java 45 | public class MyApplication extends Application { 46 | 47 | @Override 48 | public void onCreate() { 49 | super.onCreate(); 50 | // configure CacheUtilsLibrary 51 | CacheUtils.configureCache(this); 52 | } 53 | } 54 | ``` 55 | 56 | Don't forget to declare the `MyApplication` class in your `AndroidManifest.xml` file. 57 | 58 | 59 | ##Usage 60 | All sample code can be found in the [MainActivity](https://github.com/westlinkin/CacheUtilsLibrary/blob/master/sample/src/main/java/com/lifeofcoding/cacheutilslibrary_sample/MainActivity.java) file. 61 | #####Cache `String` File 62 | ```Java 63 | // write 64 | CacheUtils.writeFile(CACHE_FILE_STRING, CACHE_FILE_CONTENT_STRING); 65 | 66 | // read 67 | String fileContent = CacheUtils.readFile(CACHE_FILE_STRING); 68 | ``` 69 | 70 | #####Cache `Map` File 71 | ```Java 72 | // write 73 | CacheUtils.writeDataMapFile(CACHE_FILE_MAP, getCacheFileContentMap()); 74 | 75 | // read 76 | Map mapData = CacheUtils.readDataMapFile(CACHE_FILE_MAP); 77 | 78 | // get mapData 79 | private static Map getCacheFileContentMap() { 80 | Map mapData = new HashMap<>(); 81 | mapData.put("firstItem", "item0"); 82 | mapData.put("secondItem", 1); 83 | mapData.put("thirdItem", false); 84 | return mapData; 85 | } 86 | ``` 87 | 88 | #####Cache `List` File 89 | ```Java 90 | // write 91 | CacheUtils.writeDataMapsFile(CACHE_FILE_LIST_MAP, getCacheFileContentListMap()); 92 | 93 | // read 94 | List> listMapData = CacheUtils.readDataMapsFile(CACHE_FILE_LIST_MAP); 95 | 96 | // get listMapData 97 | private static List> getCacheFileContentListMap() { 98 | List> listMapData = new ArrayList>(); 99 | 100 | for (int i = 0; i < 4; i++) { 101 | Map item = new HashMap<>(); 102 | item.put("firstItemAt" + i, "item0At" + i); 103 | item.put("secondItemAt" + i, 1 + i); 104 | item.put("thirdItemAt" + i, i % 2 == 0); 105 | listMapData.add(item); 106 | } 107 | return listMapData; 108 | } 109 | ``` 110 | #####Cache `Object` File 111 | ```Java 112 | // write 113 | CacheUtils.writeObjectFile(CACHE_FILE_OBJECT, MyClass.SAMPLE_MYCLASS_1); 114 | 115 | // read 116 | MyClass myClassSample = CacheUtils.readObjectFile(CACHE_FILE_OBJECT, new TypeToken(){}.getType()); 117 | ``` 118 | You can see `MyClass` and `MyClass.SAMPLE_MYCLASS_1` [here](https://github.com/westlinkin/CacheUtilsLibrary/blob/master/sample/src/main/java/com/lifeofcoding/cacheutilslibrary_sample/MyClass.java) and [here](https://github.com/westlinkin/CacheUtilsLibrary/blob/master/sample/src/main/java/com/lifeofcoding/cacheutilslibrary_sample/MyClass.java#L46). 119 | 120 | #####Cache `List` File 121 | ```Java 122 | // write 123 | CacheUtils.writeObjectFile(CACHE_FILE_LIST_OBJECT, getCacheFileContentListObject()); 124 | 125 | // read 126 | List myClassList = CacheUtils.readObjectFile(CACHE_FILE_LIST_OBJECT, new TypeToken>(){}.getType()); 127 | 128 | // get List data 129 | private static List getCacheFileContentListObject() { 130 | List listObject = new ArrayList<>(); 131 | listObject.add(MyClass.SAMPLE_MYCLASS_1); 132 | listObject.add(MyClass.SAMPLE_MYCLASS_2); 133 | listObject.add(MyClass.SAMPLE_MYCLASS_3); 134 | return listObject; 135 | } 136 | 137 | ``` 138 | 139 | ##License 140 | 141 | Copyright 2015-2016 Wesley Lin 142 | 143 | Licensed under the Apache License, Version 2.0 (the "License"); 144 | you may not use this file except in compliance with the License. 145 | You may obtain a copy of the License at 146 | 147 | http://www.apache.org/licenses/LICENSE-2.0 148 | 149 | Unless required by applicable law or agreed to in writing, software 150 | distributed under the License is distributed on an "AS IS" BASIS, 151 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 152 | See the License for the specific language governing permissions and 153 | limitations under the License. 154 | 155 | -------------------------------------------------------------------------------- /README_zhCN.md: -------------------------------------------------------------------------------- 1 | # CacheUtilsLibrary 中文版 2 | 3 | [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-CacheUtilsLibrary-green.svg?style=flat)](https://android-arsenal.com/details/1/2478) 4 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.lifeofcoding/cacheutilslibrary/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.lifeofcoding/cacheutilslibrary) 5 | [![Android Gems](http://www.android-gems.com/badge/westlinkin/CacheUtilsLibrary.svg?branch=master)](http://www.android-gems.com/lib/westlinkin/CacheUtilsLibrary) 6 | 7 | 这是一个将任何Java Object类型的数据序列化后写入缓存文件,将来使用时读取缓存文件并反序列化成对应Java Object的库。 8 | 9 | ##Gradle 10 | ```Groovy 11 | compile 'com.lifeofcoding:cacheutilslibrary:1.1.0@aar' 12 | compile 'com.google.code.gson:gson:2.2.2' 13 | compile 'commons-io:commons-io:2.4' 14 | ``` 15 | 16 | 如果编辑时有下面的错误: 17 | 18 | ``` 19 | duplication file during packaging of APK ... 20 | Path in archive: META-INF/LICENSE.txt 21 | ... 22 | ``` 23 | 请在 `build.gradle` 文件的 `android` 节点添加如下代码: 24 | 25 | ```Groovy 26 | packagingOptions { 27 | exclude 'META-INF/DEPENDENCIES' 28 | exclude 'META-INF/NOTICE' 29 | exclude 'META-INF/LICENSE' 30 | exclude 'META-INF/LICENSE.txt' 31 | exclude 'META-INF/NOTICE.txt' 32 | exclude 'META-INF/ASL2.0' 33 | exclude 'META-INF/MANIFEST.MF' 34 | } 35 | ``` 36 | 37 | ##配置 38 | 你需要在项目的 `Application` 类中配置一下 `CacheUtilsLibrary` 库。 39 | 40 | ```Java 41 | public class MyApplication extends Application { 42 | 43 | @Override 44 | public void onCreate() { 45 | super.onCreate(); 46 | // configure CacheUtilsLibrary 47 | CacheUtils.configureCache(this); 48 | } 49 | } 50 | ``` 51 | 别忘了在 `AndroidManifest.xml` 文件中申明这个 `MyApplication` 类。 52 | 53 | 54 | ##使用 55 | 所有样例代码都能在 [MainActivity](https://github.com/westlinkin/CacheUtilsLibrary/blob/master/sample/src/main/java/com/lifeofcoding/cacheutilslibrary_sample/MainActivity.java) 类中找到。 56 | 57 | #####缓存文件内容为 `String` 58 | ```Java 59 | // write 60 | CacheUtils.writeFile(CACHE_FILE_STRING, CACHE_FILE_CONTENT_STRING); 61 | 62 | // read 63 | String fileContent = CacheUtils.readFile(CACHE_FILE_STRING); 64 | ``` 65 | 66 | #####缓存文件内容为 `Map` 67 | ```Java 68 | // write 69 | CacheUtils.writeDataMapFile(CACHE_FILE_MAP, getCacheFileContentMap()); 70 | 71 | // read 72 | Map mapData = CacheUtils.readDataMapFile(CACHE_FILE_MAP); 73 | 74 | // get mapData 75 | private static Map getCacheFileContentMap() { 76 | Map mapData = new HashMap<>(); 77 | mapData.put("firstItem", "item0"); 78 | mapData.put("secondItem", 1); 79 | mapData.put("thirdItem", false); 80 | return mapData; 81 | } 82 | ``` 83 | 84 | #####缓存文件内容为 `List` 85 | ```Java 86 | // write 87 | CacheUtils.writeDataMapsFile(CACHE_FILE_LIST_MAP, getCacheFileContentListMap()); 88 | 89 | // read 90 | List> listMapData = CacheUtils.readDataMapsFile(CACHE_FILE_LIST_MAP); 91 | 92 | // get listMapData 93 | private static List> getCacheFileContentListMap() { 94 | List> listMapData = new ArrayList>(); 95 | 96 | for (int i = 0; i < 4; i++) { 97 | Map item = new HashMap<>(); 98 | item.put("firstItemAt" + i, "item0At" + i); 99 | item.put("secondItemAt" + i, 1 + i); 100 | item.put("thirdItemAt" + i, i % 2 == 0); 101 | listMapData.add(item); 102 | } 103 | return listMapData; 104 | } 105 | ``` 106 | #####缓存文件内容为 `Object` 107 | ```Java 108 | // write 109 | CacheUtils.writeObjectFile(CACHE_FILE_OBJECT, MyClass.SAMPLE_MYCLASS_1); 110 | 111 | // read 112 | MyClass myClassSample = CacheUtils.readObjectFile(CACHE_FILE_OBJECT, new TypeToken(){}.getType()); 113 | ``` 114 | 你能在[这里](https://github.com/westlinkin/CacheUtilsLibrary/blob/master/sample/src/main/java/com/lifeofcoding/cacheutilslibrary_sample/MyClass.java) and [这里](https://github.com/westlinkin/CacheUtilsLibrary/blob/master/sample/src/main/java/com/lifeofcoding/cacheutilslibrary_sample/MyClass.java#L46)分别找到`MyClass` 和 `MyClass.SAMPLE_MYCLASS_1` 。 115 | 116 | #####缓存文件内容为 `List` 117 | ```Java 118 | // write 119 | CacheUtils.writeObjectFile(CACHE_FILE_LIST_OBJECT, getCacheFileContentListObject()); 120 | 121 | // read 122 | List myClassList = CacheUtils.readObjectFile(CACHE_FILE_LIST_OBJECT, new TypeToken>(){}.getType()); 123 | 124 | // get List data 125 | private static List getCacheFileContentListObject() { 126 | List listObject = new ArrayList<>(); 127 | listObject.add(MyClass.SAMPLE_MYCLASS_1); 128 | listObject.add(MyClass.SAMPLE_MYCLASS_2); 129 | listObject.add(MyClass.SAMPLE_MYCLASS_3); 130 | return listObject; 131 | } 132 | 133 | ``` 134 | 135 | ##开源协议 136 | 137 | Copyright 2015-2016 Wesley Lin 138 | 139 | Licensed under the Apache License, Version 2.0 (the "License"); 140 | you may not use this file except in compliance with the License. 141 | You may obtain a copy of the License at 142 | 143 | http://www.apache.org/licenses/LICENSE-2.0 144 | 145 | Unless required by applicable law or agreed to in writing, software 146 | distributed under the License is distributed on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 148 | See the License for the specific language governing permissions and 149 | limitations under the License. 150 | 151 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:1.3.0' 9 | 10 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' 11 | classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.3.1" 12 | 13 | // NOTE: Do not place your application dependencies here; they belong 14 | // in the individual module build.gradle files 15 | } 16 | } 17 | 18 | allprojects { 19 | repositories { 20 | jcenter() 21 | } 22 | } 23 | 24 | task clean(type: Delete) { 25 | delete rootProject.buildDir 26 | } 27 | -------------------------------------------------------------------------------- /cacheutilslibrary/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /cacheutilslibrary/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | apply plugin: 'com.github.dcendents.android-maven' 4 | apply plugin: "com.jfrog.bintray" 5 | 6 | // This is the library version used when deploying the artifact 7 | version = "1.1.0" 8 | 9 | android { 10 | compileSdkVersion 23 11 | buildToolsVersion "22.0.1" 12 | 13 | defaultConfig { 14 | minSdkVersion 15 15 | targetSdkVersion 23 16 | versionCode 1 17 | versionName "1.1.0" 18 | } 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | } 26 | 27 | dependencies { 28 | compile 'com.google.code.gson:gson:2.2.2' 29 | compile 'commons-io:commons-io:2.4' 30 | } 31 | 32 | def siteUrl = 'https://github.com/westlinkin/CacheUtilsLibrary' // Homepage URL of the library 33 | def gitUrl = 'https://github.com/westlinkin/CacheUtilsLibrary.git' // Git repository URL 34 | group = "com.lifeofcoding" // Maven Group ID for the artifact 35 | 36 | install { 37 | repositories.mavenInstaller { 38 | // This generates POM.xml with proper parameters 39 | pom { 40 | project { 41 | packaging 'aar' 42 | 43 | // Add your description here 44 | name 'CacheUtilsLibrary' 45 | description = 'This is a simple Android util library to write any type of data into cache files and then read them later, using Gson to serialize and deserialize these data.' 46 | url siteUrl 47 | 48 | // Set your license 49 | licenses { 50 | license { 51 | name 'The Apache Software License, Version 2.0' 52 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt' 53 | } 54 | } 55 | developers { 56 | developer { 57 | id 'westlinkin' 58 | name 'Wesley Lin' 59 | email 'westlinkin@gmail.com' 60 | } 61 | } 62 | scm { 63 | connection gitUrl 64 | developerConnection gitUrl 65 | url siteUrl 66 | 67 | } 68 | } 69 | } 70 | } 71 | } 72 | 73 | task sourcesJar(type: Jar) { 74 | from android.sourceSets.main.java.srcDirs 75 | classifier = 'sources' 76 | } 77 | 78 | task javadoc(type: Javadoc) { 79 | source = android.sourceSets.main.java.srcDirs 80 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 81 | } 82 | 83 | task javadocJar(type: Jar, dependsOn: javadoc) { 84 | classifier = 'javadoc' 85 | from javadoc.destinationDir 86 | } 87 | artifacts { 88 | archives javadocJar 89 | archives sourcesJar 90 | } 91 | 92 | Properties properties = new Properties() 93 | properties.load(project.rootProject.file('local.properties').newDataInputStream()) 94 | 95 | // https://github.com/bintray/gradle-bintray-plugin 96 | bintray { 97 | user = properties.getProperty("bintray.user") 98 | key = properties.getProperty("bintray.apikey") 99 | 100 | configurations = ['archives'] 101 | pkg { 102 | repo = "maven" 103 | // it is the name that appears in bintray when logged 104 | name = "CacheUtilsLibrary" 105 | websiteUrl = siteUrl 106 | vcsUrl = gitUrl 107 | licenses = ["Apache-2.0"] 108 | publish = true 109 | version { 110 | gpg { 111 | sign = true //Determines whether to GPG sign the files. The default is false 112 | passphrase = properties.getProperty("bintray.gpg.password") //Optional. The passphrase for GPG signing' 113 | } 114 | // mavenCentralSync { 115 | // sync = true //Optional (true by default). Determines whether to sync the version to Maven Central. 116 | // user = properties.getProperty("bintray.oss.user") //OSS user token 117 | // password = properties.getProperty("bintray.oss.password") //OSS user password 118 | // close = '1' //Optional property. By default the staging repository is closed and artifacts are released to Maven Central. You can optionally turn this behaviour off (by puting 0 as value) and release the version manually. 119 | // } 120 | } 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /cacheutilslibrary/cacheutilslibrary.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | -------------------------------------------------------------------------------- /cacheutilslibrary/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 /Users/Linkin/Study/Android/android-sdk-macosx/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 | -------------------------------------------------------------------------------- /cacheutilslibrary/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /cacheutilslibrary/src/main/java/com/lifeofcoding/cacheutlislibrary/CacheUtils.java: -------------------------------------------------------------------------------- 1 | package com.lifeofcoding.cacheutlislibrary; 2 | 3 | import android.content.Context; 4 | import android.text.TextUtils; 5 | import android.util.Log; 6 | 7 | import com.google.gson.reflect.TypeToken; 8 | 9 | import org.apache.commons.io.FileUtils; 10 | import org.apache.commons.io.IOUtils; 11 | 12 | import java.io.File; 13 | import java.io.FileInputStream; 14 | import java.io.FileOutputStream; 15 | import java.io.IOException; 16 | import java.lang.reflect.Type; 17 | import java.util.ArrayList; 18 | import java.util.HashMap; 19 | import java.util.List; 20 | import java.util.Map; 21 | 22 | /** 23 | * Created by Wesley Lin on 9/5/15. 24 | */ 25 | public class CacheUtils { 26 | 27 | private static final String ENCODING = "utf8"; 28 | private static final String FILE_SUFFIX = ".txt"; 29 | public static String BASE_CACHE_PATH; 30 | 31 | private static final String TAG = "CACHE_UTILS"; 32 | 33 | public static void configureCache(Context context) { 34 | BASE_CACHE_PATH = context.getApplicationInfo().dataDir + File.separator + "files" + File.separator + "CacheUtils"; 35 | 36 | if (new File(BASE_CACHE_PATH).mkdirs()) { 37 | if (BuildConfig.DEBUG) 38 | Log.d(TAG, BASE_CACHE_PATH + " created."); 39 | } 40 | } 41 | 42 | private static String pathForCacheEntry(String name) { 43 | return BASE_CACHE_PATH + File.separator + name + FILE_SUFFIX; 44 | } 45 | 46 | private static List> dataMapsFromJson(String dataString) { 47 | if (TextUtils.isEmpty(dataString)) 48 | return new ArrayList>(); 49 | 50 | try { 51 | Type listType = new TypeToken>>(){}.getType(); 52 | return GsonHelper.buildGson().fromJson(dataString, listType); 53 | } catch (Exception e) { 54 | if (BuildConfig.DEBUG) 55 | Log.d(TAG, "failed to read json" + e.toString()); 56 | return new ArrayList>(); 57 | } 58 | } 59 | 60 | private static String dataMapstoJson(List> dataMaps) { 61 | try { 62 | return GsonHelper.buildGson().toJson(dataMaps); 63 | } catch (Exception e) { 64 | if (BuildConfig.DEBUG) 65 | Log.d(TAG, "failed to write json" + e.toString()); 66 | return "[]"; 67 | } 68 | } 69 | 70 | /** 71 | * @param fileName the name of the file 72 | * @return the content of the file, null if there is no such file 73 | */ 74 | public static String readFile(String fileName) { 75 | try { 76 | return IOUtils.toString(new FileInputStream(pathForCacheEntry(fileName)), ENCODING); 77 | } catch (IOException e) { 78 | if (BuildConfig.DEBUG) 79 | Log.d(TAG, "read cache file failure" + e.toString()); 80 | return null; 81 | } 82 | } 83 | 84 | /** 85 | * @param fileName the name of the file 86 | * @param fileContent the content of the file 87 | */ 88 | public static void writeFile(String fileName, String fileContent) { 89 | try { 90 | IOUtils.write(fileContent, new FileOutputStream(pathForCacheEntry(fileName)), ENCODING); 91 | } catch (IOException e) { 92 | if (BuildConfig.DEBUG) 93 | Log.d(TAG, "write cache file failure" + e.toString()); 94 | } 95 | } 96 | 97 | /** 98 | * @param fileName the name of the file 99 | * @param dataMaps the map list you want to store 100 | */ 101 | public static void writeDataMapsFile(String fileName, List> dataMaps) { 102 | writeFile(fileName, dataMapstoJson(dataMaps)); 103 | } 104 | 105 | /** 106 | * @param fileName the name of the file 107 | * @return the map list you previous stored, an empty {@link List} will be returned if there is no such file 108 | */ 109 | public static List> readDataMapsFile(String fileName) { 110 | return dataMapsFromJson(readFile(fileName)); 111 | } 112 | 113 | private static T objectFromJson(String dataString, Type t) { 114 | try { 115 | return GsonHelper.buildGson().fromJson(dataString, t); 116 | } catch (Exception e) { 117 | if (BuildConfig.DEBUG) 118 | Log.v(TAG, "failed to read json" + e.toString()); 119 | return null; 120 | } 121 | } 122 | 123 | private static String objectToJson(T o) { 124 | try { 125 | return GsonHelper.buildGson().toJson(o); 126 | } catch (Exception e) { 127 | if (BuildConfig.DEBUG) 128 | Log.v(TAG, "failed to write json" + e.toString()); 129 | return null; 130 | } 131 | } 132 | 133 | /** 134 | * @param fileName the name of the file 135 | * @param object the object you want to store 136 | * @param a class extends from {@link Object} 137 | */ 138 | public static void writeObjectFile(String fileName, T object) { 139 | writeFile(fileName, objectToJson(object)); 140 | } 141 | 142 | /** 143 | * @param fileName the name of the file 144 | * @param t the type of the object you previous stored 145 | * @return the {@link T} type object you previous stored 146 | */ 147 | public static T readObjectFile(String fileName, Type t) { 148 | return objectFromJson(readFile(fileName), t); 149 | } 150 | 151 | private static Map dataMapFromJson(String dataString) { 152 | if (TextUtils.isEmpty(dataString)) 153 | return new HashMap(); 154 | 155 | try { 156 | Type t = new TypeToken>(){}.getType(); 157 | return GsonHelper.buildGson().fromJson(dataString, t); 158 | } catch (Exception e) { 159 | if (BuildConfig.DEBUG) 160 | Log.v(TAG, "failed to read json" + e.toString()); 161 | return new HashMap(); 162 | } 163 | } 164 | 165 | private static String dataMaptoJson(Map dataMap) { 166 | try { 167 | return GsonHelper.buildGson().toJson(dataMap); 168 | } catch (Exception e) { 169 | if (BuildConfig.DEBUG) 170 | Log.v(TAG, "failed to write json" + e.toString()); 171 | return "{}"; 172 | } 173 | } 174 | 175 | /** 176 | * @param fileName the name of the file 177 | * @param dataMap the map data you want to store 178 | */ 179 | public static void writeDataMapFile(String fileName, Map dataMap) { 180 | writeFile(fileName, dataMaptoJson(dataMap)); 181 | } 182 | 183 | /** 184 | * @param fileName the name of the file 185 | * @return the map data you previous stored 186 | */ 187 | public static Map readDataMapFile(String fileName) { 188 | return dataMapFromJson(readFile(fileName)); 189 | } 190 | 191 | /** 192 | * delete the file with fileName 193 | * @param fileName the name of the file 194 | */ 195 | public static void deleteFile(String fileName) { 196 | FileUtils.deleteQuietly(new File(pathForCacheEntry(fileName))); 197 | } 198 | 199 | 200 | /** 201 | * check if there is a cache file with fileName 202 | * @param fileName the name of the file 203 | * @return true if the file exits, false otherwise 204 | */ 205 | public static boolean hasCache(String fileName) { 206 | return new File(pathForCacheEntry(fileName)).exists(); 207 | } 208 | } 209 | -------------------------------------------------------------------------------- /cacheutilslibrary/src/main/java/com/lifeofcoding/cacheutlislibrary/GsonHelper.java: -------------------------------------------------------------------------------- 1 | package com.lifeofcoding.cacheutlislibrary; 2 | 3 | import com.google.gson.Gson; 4 | import com.google.gson.GsonBuilder; 5 | import com.google.gson.TypeAdapter; 6 | import com.google.gson.stream.JsonToken; 7 | 8 | import java.io.IOException; 9 | import java.util.Date; 10 | 11 | /** 12 | * Created by Wesley Lin on 9/7/15. 13 | */ 14 | class GsonHelper { 15 | 16 | static Gson buildGson() { 17 | GsonBuilder b = new GsonBuilder(); 18 | b.registerTypeAdapter(Date.class, new TypeAdapter() { 19 | 20 | @Override 21 | public void write(com.google.gson.stream.JsonWriter writer, Date value) throws IOException { 22 | if (value == null) { 23 | writer.nullValue(); 24 | return; 25 | } 26 | 27 | long num = value.getTime(); 28 | num /= 1000; 29 | writer.value(num); 30 | } 31 | 32 | @Override 33 | public Date read(com.google.gson.stream.JsonReader reader) throws IOException { 34 | if (reader.peek() == JsonToken.NULL) { 35 | reader.nextNull(); 36 | return null; 37 | } 38 | 39 | long value = reader.nextLong(); 40 | return new Date(value * 1000); 41 | } 42 | 43 | }); 44 | return b.create(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /cacheutilslibrary/src/test/java/com/lifeofcoding/cacheutlislibrary/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.lifeofcoding.cacheutlislibrary; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/westlinkin/CacheUtilsLibrary/39f9d51add101a6f2140644f769c5a8a44ae7b7e/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Sep 05 20:34:00 CST 2015 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.4-all.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 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /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 Windowz 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 | -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "22.0.1" 6 | 7 | defaultConfig { 8 | applicationId "com.lifeofcoding.cacheutilslibrary_sample" 9 | minSdkVersion 15 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | 21 | packagingOptions { 22 | exclude 'META-INF/DEPENDENCIES' 23 | exclude 'META-INF/NOTICE' 24 | exclude 'META-INF/LICENSE' 25 | exclude 'META-INF/LICENSE.txt' 26 | exclude 'META-INF/NOTICE.txt' 27 | exclude 'META-INF/ASL2.0' 28 | exclude 'META-INF/MANIFEST.MF' 29 | } 30 | } 31 | 32 | dependencies { 33 | compile 'com.android.support:appcompat-v7:23.0.0' 34 | compile project(':cacheutilslibrary') 35 | compile 'com.android.support:design:23.0.0' 36 | } 37 | -------------------------------------------------------------------------------- /sample/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 /Users/Linkin/Study/Android/android-sdk-macosx/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 | -------------------------------------------------------------------------------- /sample/sample.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /sample/src/main/java/com/lifeofcoding/cacheutilslibrary_sample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.lifeofcoding.cacheutilslibrary_sample; 2 | 3 | import android.os.Bundle; 4 | import android.support.design.widget.Snackbar; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.Menu; 7 | import android.view.MenuItem; 8 | import android.view.View; 9 | import android.widget.Button; 10 | import android.widget.RadioGroup; 11 | import android.widget.RelativeLayout; 12 | import android.widget.TextView; 13 | 14 | import com.google.gson.reflect.TypeToken; 15 | import com.lifeofcoding.cacheutlislibrary.CacheUtils; 16 | 17 | import java.util.ArrayList; 18 | import java.util.HashMap; 19 | import java.util.List; 20 | import java.util.Map; 21 | 22 | public class MainActivity extends AppCompatActivity { 23 | 24 | private static final String CACHE_FILE_STRING = "cache_file_string"; 25 | private static final String CACHE_FILE_MAP = "cache_file_map"; 26 | private static final String CACHE_FILE_LIST_MAP = "cache_file_list_map"; 27 | private static final String CACHE_FILE_OBJECT = "cache_file_object"; 28 | private static final String CACHE_FILE_LIST_OBJECT = "cache_file_list_object"; 29 | 30 | private static final String CACHE_FILE_CONTENT_STRING = "CacheUtilsLibrary is a simple util library to write and read cache files in Android."; 31 | private static Map getCacheFileContentMap() { 32 | Map mapData = new HashMap<>(); 33 | mapData.put("firstItem", "item0"); 34 | mapData.put("secondItem", 1); 35 | mapData.put("thirdItem", false); 36 | return mapData; 37 | } 38 | private static List> getCacheFileContentListMap() { 39 | List> listMapData = new ArrayList>(); 40 | 41 | for (int i = 0; i < 4; i++) { 42 | Map item = new HashMap<>(); 43 | item.put("firstItemAt" + i, "item0At" + i); 44 | item.put("secondItemAt" + i, 1 + i); 45 | item.put("thirdItemAt" + i, i % 2 == 0); 46 | listMapData.add(item); 47 | } 48 | return listMapData; 49 | } 50 | private static List getCacheFileContentListObject() { 51 | List listObject = new ArrayList<>(); 52 | listObject.add(MyClass.SAMPLE_MYCLASS_1); 53 | listObject.add(MyClass.SAMPLE_MYCLASS_2); 54 | listObject.add(MyClass.SAMPLE_MYCLASS_3); 55 | return listObject; 56 | } 57 | 58 | 59 | private RelativeLayout rootLayout; 60 | private int radioCheckId = R.id.string; 61 | private Snackbar snackbar; 62 | 63 | @Override 64 | protected void onCreate(Bundle savedInstanceState) { 65 | super.onCreate(savedInstanceState); 66 | setContentView(R.layout.activity_main); 67 | 68 | rootLayout = (RelativeLayout) findViewById(R.id.root); 69 | final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup); 70 | Button writeButton = (Button) findViewById(R.id.write); 71 | Button readButton = (Button) findViewById(R.id.read); 72 | 73 | radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 74 | @Override 75 | public void onCheckedChanged(RadioGroup group, int checkedId) { 76 | radioCheckId = checkedId; 77 | } 78 | }); 79 | writeButton.setOnClickListener(writeOnClickListener); 80 | readButton.setOnClickListener(readOnClickListener); 81 | } 82 | 83 | private View.OnClickListener writeOnClickListener = new View.OnClickListener() { 84 | @Override 85 | public void onClick(View v) { 86 | if (radioCheckId == R.id.string) { 87 | CacheUtils.writeFile(CACHE_FILE_STRING, CACHE_FILE_CONTENT_STRING); 88 | Snackbar.make(rootLayout, "Write a String into cache file", Snackbar.LENGTH_SHORT).show(); 89 | 90 | } else if (radioCheckId == R.id.map) { 91 | CacheUtils.writeDataMapFile(CACHE_FILE_MAP, getCacheFileContentMap()); 92 | Snackbar.make(rootLayout, "Write a Map into cache file", Snackbar.LENGTH_SHORT).show(); 93 | 94 | } else if (radioCheckId == R.id.listmap) { 95 | CacheUtils.writeDataMapsFile(CACHE_FILE_LIST_MAP, getCacheFileContentListMap()); 96 | Snackbar.make(rootLayout, "Write a List> into cache file", Snackbar.LENGTH_SHORT).show(); 97 | 98 | } else if (radioCheckId == R.id.object) { 99 | CacheUtils.writeObjectFile(CACHE_FILE_OBJECT, MyClass.SAMPLE_MYCLASS_1); 100 | Snackbar.make(rootLayout, "Write a MyClass into cache file", Snackbar.LENGTH_SHORT).show(); 101 | 102 | } else if (radioCheckId == R.id.listobject) { 103 | CacheUtils.writeObjectFile(CACHE_FILE_LIST_OBJECT, getCacheFileContentListObject()); 104 | Snackbar.make(rootLayout, "Write a List into cache file", Snackbar.LENGTH_SHORT).show(); 105 | 106 | } 107 | } 108 | }; 109 | 110 | private View.OnClickListener readOnClickListener = new View.OnClickListener() { 111 | @Override 112 | public void onClick(View v) { 113 | if (radioCheckId == R.id.string) { 114 | String fileContent = CacheUtils.readFile(CACHE_FILE_STRING); 115 | snackbar = Snackbar.make(rootLayout, fileContent == null ? "No file" : fileContent, Snackbar.LENGTH_INDEFINITE); 116 | } else if (radioCheckId == R.id.map) { 117 | Map mapData = CacheUtils.readDataMapFile(CACHE_FILE_MAP); 118 | snackbar = Snackbar.make(rootLayout, mapData.isEmpty() ? "No file or empty map" : mapData.toString(), Snackbar.LENGTH_INDEFINITE); 119 | } else if (radioCheckId == R.id.listmap) { 120 | List> listMapData = CacheUtils.readDataMapsFile(CACHE_FILE_LIST_MAP); 121 | snackbar = Snackbar.make(rootLayout, listMapData.isEmpty() ? "No file or empty map" : listMapData.toString(), Snackbar.LENGTH_INDEFINITE); 122 | } else if (radioCheckId == R.id.object) { 123 | MyClass myClassSample = CacheUtils.readObjectFile(CACHE_FILE_OBJECT, new TypeToken(){}.getType()); 124 | snackbar = Snackbar.make(rootLayout, myClassSample == null ? "No file" : myClassSample.toString(), Snackbar.LENGTH_INDEFINITE); 125 | 126 | } else if (radioCheckId == R.id.listobject) { 127 | List myClassList = CacheUtils.readObjectFile(CACHE_FILE_LIST_OBJECT, new TypeToken>(){}.getType()); 128 | snackbar = Snackbar.make(rootLayout, myClassList == null ? "No file" : myClassList.toString(), Snackbar.LENGTH_INDEFINITE); 129 | 130 | } 131 | 132 | if (snackbar == null) 133 | return; 134 | TextView textView = (TextView) snackbar.getView().findViewById(android.support.design.R.id.snackbar_text); 135 | textView.setMaxLines(8); 136 | snackbar.setAction("Dismiss", new View.OnClickListener() { 137 | @Override 138 | public void onClick(View v) { 139 | snackbar.dismiss(); 140 | } 141 | }); 142 | snackbar.show(); 143 | } 144 | }; 145 | 146 | @Override 147 | public boolean onCreateOptionsMenu(Menu menu) { 148 | // Inflate the menu; this adds items to the action bar if it is present. 149 | getMenuInflater().inflate(R.menu.menu_main, menu); 150 | return true; 151 | } 152 | 153 | @Override 154 | public boolean onOptionsItemSelected(MenuItem item) { 155 | // Handle action bar item clicks here. The action bar will 156 | // automatically handle clicks on the Home/Up button, so long 157 | // as you specify a parent activity in AndroidManifest.xml. 158 | int id = item.getItemId(); 159 | 160 | //noinspection SimplifiableIfStatement 161 | if (id == R.id.action_settings) { 162 | return true; 163 | } 164 | 165 | return super.onOptionsItemSelected(item); 166 | } 167 | } 168 | -------------------------------------------------------------------------------- /sample/src/main/java/com/lifeofcoding/cacheutilslibrary_sample/MyApplication.java: -------------------------------------------------------------------------------- 1 | package com.lifeofcoding.cacheutilslibrary_sample; 2 | 3 | import android.app.Application; 4 | 5 | import com.lifeofcoding.cacheutlislibrary.CacheUtils; 6 | 7 | /** 8 | * Created by Wesley Lin on 9/5/15. 9 | */ 10 | public class MyApplication extends Application { 11 | 12 | @Override 13 | public void onCreate() { 14 | super.onCreate(); 15 | // configure CacheUtilsLibrary 16 | CacheUtils.configureCache(this); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /sample/src/main/java/com/lifeofcoding/cacheutilslibrary_sample/MyClass.java: -------------------------------------------------------------------------------- 1 | package com.lifeofcoding.cacheutilslibrary_sample; 2 | 3 | /** 4 | * Created by Wesley Lin on 9/6/15. 5 | */ 6 | public class MyClass { 7 | private String text; 8 | private int id; 9 | private boolean isIdZero; 10 | 11 | public MyClass(String text, int id) { 12 | this.text = text; 13 | this.id = id; 14 | this.isIdZero = id == 0; 15 | } 16 | 17 | public String getText() { 18 | return text; 19 | } 20 | 21 | public int getId() { 22 | return id; 23 | } 24 | 25 | public boolean isIdZero() { 26 | return isIdZero; 27 | } 28 | 29 | public void setText(String text) { 30 | this.text = text; 31 | } 32 | 33 | public void setId(int id) { 34 | this.id = id; 35 | } 36 | 37 | public void setIsIdZero(boolean isIdZero) { 38 | this.isIdZero = isIdZero; 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return "{text: " + text + ", id: " + id + ", isIdZero: " + isIdZero + "}"; 44 | } 45 | 46 | public static MyClass SAMPLE_MYCLASS_1 = new MyClass("MyClass Sample1", 0); 47 | public static MyClass SAMPLE_MYCLASS_2 = new MyClass("MyClass Sample2", 1); 48 | public static MyClass SAMPLE_MYCLASS_3 = new MyClass("MyClass Sample3", 1); 49 | } 50 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 11 | 12 | 18 | 19 | 27 | 28 | 34 | 35 | 40 | 41 | 46 | 47 | 48 | 53 | 54 | 59 | 60 | 61 | 62 | 68 | 69 |