/templates/mvp-arms/arms/src/main/java/com/jess/arms/utils/DataHelper.java

https://github.com/androidstarters/androidstarters.com · Java · 290 lines · 161 code · 26 blank · 103 comment · 39 complexity · 9e1db04013d4ef5198de129fa853e2f8 MD5 · raw file

  1. /**
  2. * Copyright 2017 JessYan
  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. package <%= appPackage %>.utils;
  17. import android.content.Context;
  18. import android.content.SharedPreferences;
  19. import android.os.Environment;
  20. import android.util.Base64;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.ObjectInputStream;
  27. import java.io.ObjectOutputStream;
  28. /**
  29. * ================================================
  30. * 处理数据或本地文件的工具类
  31. * <p>
  32. * Created by JessYan on 2016/3/15
  33. * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
  34. * <a href="https://github.com/JessYanCoding">Follow me</a>
  35. * ================================================
  36. */
  37. public class DataHelper {
  38. private static SharedPreferences mSharedPreferences;
  39. public static final String SP_NAME = "config";
  40. private DataHelper() {
  41. throw new IllegalStateException("you can't instantiate me!");
  42. }
  43. /**
  44. * 存储重要信息到sharedPreferences;
  45. *
  46. * @param key
  47. * @param value
  48. */
  49. public static void setStringSF(Context context, String key, String value) {
  50. if (mSharedPreferences == null) {
  51. mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
  52. }
  53. mSharedPreferences.edit().putString(key, value).apply();
  54. }
  55. /**
  56. * 返回存在sharedPreferences的信息
  57. *
  58. * @param key
  59. * @return
  60. */
  61. public static String getStringSF(Context context, String key) {
  62. if (mSharedPreferences == null) {
  63. mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
  64. }
  65. return mSharedPreferences.getString(key, null);
  66. }
  67. /**
  68. * 存储重要信息到sharedPreferences;
  69. *
  70. * @param key
  71. * @param value
  72. */
  73. public static void setIntergerSF(Context context, String key, int value) {
  74. if (mSharedPreferences == null) {
  75. mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
  76. }
  77. mSharedPreferences.edit().putInt(key, value).apply();
  78. }
  79. /**
  80. * 返回存在sharedPreferences的信息
  81. *
  82. * @param key
  83. * @return
  84. */
  85. public static int getIntergerSF(Context context, String key) {
  86. if (mSharedPreferences == null) {
  87. mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
  88. }
  89. return mSharedPreferences.getInt(key, -1);
  90. }
  91. /**
  92. * 清除某个内容
  93. */
  94. public static void removeSF(Context context, String key) {
  95. if (mSharedPreferences == null) {
  96. mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
  97. }
  98. mSharedPreferences.edit().remove(key).apply();
  99. }
  100. /**
  101. * 清除Shareprefrence
  102. */
  103. public static void clearShareprefrence(Context context) {
  104. if (mSharedPreferences == null) {
  105. mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
  106. }
  107. mSharedPreferences.edit().clear().apply();
  108. }
  109. /**
  110. * 将对象储存到sharepreference
  111. *
  112. * @param key
  113. * @param device
  114. * @param <T>
  115. */
  116. public static <T> boolean saveDeviceData(Context context, String key, T device) {
  117. if (mSharedPreferences == null) {
  118. mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
  119. }
  120. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  121. try { //Device为自定义类
  122. // 创建对象输出流,并封装字节流
  123. ObjectOutputStream oos = new ObjectOutputStream(baos);
  124. // 将对象写入字节流
  125. oos.writeObject(device);
  126. // 将字节流编码成base64的字符串
  127. String oAuth_Base64 = new String(Base64.encode(baos
  128. .toByteArray(), Base64.DEFAULT));
  129. mSharedPreferences.edit().putString(key, oAuth_Base64).apply();
  130. return true;
  131. } catch (Exception e) {
  132. e.printStackTrace();
  133. return false;
  134. }
  135. }
  136. /**
  137. * 将对象从shareprerence中取出来
  138. *
  139. * @param key
  140. * @param <T>
  141. * @return
  142. */
  143. public static <T> T getDeviceData(Context context, String key) {
  144. if (mSharedPreferences == null) {
  145. mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
  146. }
  147. T device = null;
  148. String productBase64 = mSharedPreferences.getString(key, null);
  149. if (productBase64 == null) {
  150. return null;
  151. }
  152. // 读取字节
  153. byte[] base64 = Base64.decode(productBase64.getBytes(), Base64.DEFAULT);
  154. // 封装到字节流
  155. ByteArrayInputStream bais = new ByteArrayInputStream(base64);
  156. try {
  157. // 再次封装
  158. ObjectInputStream bis = new ObjectInputStream(bais);
  159. // 读取对象
  160. device = (T) bis.readObject();
  161. } catch (Exception e) {
  162. // TODO Auto-generated catch block
  163. e.printStackTrace();
  164. }
  165. return device;
  166. }
  167. /**
  168. * 返回缓存文件夹
  169. */
  170. public static File getCacheFile(Context context) {
  171. if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
  172. File file = null;
  173. file = context.getExternalCacheDir();//获取系统管理的sd卡缓存文件
  174. if (file == null) {//如果获取的文件为空,就使用自己定义的缓存文件夹做缓存路径
  175. file = new File(getCacheFilePath(context));
  176. makeDirs(file);
  177. }
  178. return file;
  179. } else {
  180. return context.getCacheDir();
  181. }
  182. }
  183. /**
  184. * 获取自定义缓存文件地址
  185. *
  186. * @param context
  187. * @return
  188. */
  189. public static String getCacheFilePath(Context context) {
  190. String packageName = context.getPackageName();
  191. return "/mnt/sdcard/" + packageName;
  192. }
  193. /**
  194. * 创建未存在的文件夹
  195. *
  196. * @param file
  197. * @return
  198. */
  199. public static File makeDirs(File file) {
  200. if (!file.exists()) {
  201. file.mkdirs();
  202. }
  203. return file;
  204. }
  205. /**
  206. * 使用递归获取目录文件大小
  207. *
  208. * @param dir
  209. * @return
  210. */
  211. public static long getDirSize(File dir) {
  212. if (dir == null) {
  213. return 0;
  214. }
  215. if (!dir.isDirectory()) {
  216. return 0;
  217. }
  218. long dirSize = 0;
  219. File[] files = dir.listFiles();
  220. for (File file : files) {
  221. if (file.isFile()) {
  222. dirSize += file.length();
  223. } else if (file.isDirectory()) {
  224. dirSize += file.length();
  225. dirSize += getDirSize(file); // 递归调用继续统计
  226. }
  227. }
  228. return dirSize;
  229. }
  230. /**
  231. * 使用递归删除文件夹
  232. *
  233. * @param dir
  234. * @return
  235. */
  236. public static boolean deleteDir(File dir) {
  237. if (dir == null) {
  238. return false;
  239. }
  240. if (!dir.isDirectory()) {
  241. return false;
  242. }
  243. File[] files = dir.listFiles();
  244. for (File file : files) {
  245. if (file.isFile()) {
  246. file.delete();
  247. } else if (file.isDirectory()) {
  248. deleteDir(file); // 递归调用继续删除
  249. }
  250. }
  251. return true;
  252. }
  253. public static String bytyToString(InputStream in) throws IOException {
  254. ByteArrayOutputStream out = new ByteArrayOutputStream();
  255. byte[] buf = new byte[1024];
  256. int num = 0;
  257. while ((num = in.read(buf)) != -1) {
  258. out.write(buf, 0, buf.length);
  259. }
  260. String result = out.toString();
  261. out.close();
  262. return result;
  263. }
  264. }