浏览代码

添加持久化保存信息,防止卸载后消失;修复本地升级bug

zengjiebin 7 年之前
父节点
当前提交
274753ba4d

+ 1 - 1
app/src/main/java/com/kfzs/duanduan/react/ConfigUtil.java

@@ -60,7 +60,7 @@ public class ConfigUtil {
      */
      */
     public boolean isUpgrade() {
     public boolean isUpgrade() {
         String item = getItem(1);
         String item = getItem(1);
-        return TextUtils.equals(item, "1");
+        return item == null || TextUtils.equals(item, "1");
     }
     }
     /**
     /**
      * 是否进入rn调试模式
      * 是否进入rn调试模式

+ 19 - 3
app/src/main/java/com/kfzs/duanduan/react/FileUtil.java

@@ -119,13 +119,13 @@ public class FileUtil {
     /**
     /**
      * 追加文件:使用FileWriter
      * 追加文件:使用FileWriter
      *
      *
-     * @param fileName
+     * @param filePath
      * @param content
      * @param content
      */
      */
-    public static void addToFile(String fileName, String content) {
+    public static void addToFile(String filePath, String content) {
         try {
         try {
             // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
             // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
-            FileWriter writer = new FileWriter(fileName, true);
+            FileWriter writer = new FileWriter(filePath, true);
             writer.write(content);
             writer.write(content);
             writer.close();
             writer.close();
         } catch (IOException e) {
         } catch (IOException e) {
@@ -133,4 +133,20 @@ public class FileUtil {
         }
         }
     }
     }
 
 
+    /**
+     * 保存文件:使用FileWriter
+     *
+     * @param filePath
+     * @param content
+     */
+    public static void saveFile(String filePath, String content) {
+        try {
+            // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
+            FileWriter writer = new FileWriter(filePath, false);
+            writer.write(content);
+            writer.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
 }
 }

+ 103 - 0
app/src/main/java/com/sheep/gamegroup/util/ForeverCacheUtil.java

@@ -0,0 +1,103 @@
+package com.sheep.gamegroup.util;
+
+import android.os.Environment;
+import android.util.Log;
+
+import com.kfzs.duanduan.react.FileUtil;
+import com.kfzs.duanduan.utils.NumberFormatUtils;
+import com.sheep.gamegroup.model.api.BaseMessageConverter;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.util.List;
+import java.util.Locale;
+
+/**
+ * Created by realicing on 2018/5/24.
+ * realicing@sina.com
+ */
+public class ForeverCacheUtil {
+    private static ForeverCacheUtil instance;
+    //.开头隐藏文件和隐藏文件夹
+    private static final String FILE_PATH = "data/cache/a54df5a4s5d";
+    private ForeverCacheUtil(){
+        File file = new File(getCacheFilePath());
+        if(!file.exists()){
+            try {
+                if(file.getParentFile().mkdirs())
+                    if(file.createNewFile()){
+                        LogUtil.println("ForeverCacheUtil", "yes");
+                    } else {
+                        LogUtil.println("ForeverCacheUtil", "no");
+                    }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    public static ForeverCacheUtil getInstance() {
+        if (instance == null)
+            instance = new ForeverCacheUtil();
+        return instance;
+    }
+
+
+    private static String getFileName() {
+        return "a5sdf41a6sdf1asd1fd52145s";
+    }
+
+    private static String getCacheFilePath() {
+        boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
+        if (hasSDCard) { // SD卡根目录
+            return Environment.getExternalStorageDirectory().toString() + File.separator + FILE_PATH + File.separator + getFileName();
+        } else {  // 系统下载缓存根目录
+            return Environment.getDownloadCacheDirectory().toString() + File.separator + FILE_PATH + File.separator + getFileName();
+        }
+    }
+
+
+    private String readLineByPosition(int position) {
+        List<String> list = FileUtil.readLines(getCacheFilePath());
+        return ListUtil.getItem(list, position);
+    }
+
+    /**
+     * 文件第一行保存版本号
+     */
+    public int getVersionCode() {
+        String msg = readLineByPosition(0);
+        return NumberFormatUtils.parseInteger(msg, 0);
+    }
+
+    /**
+     * 保存版本号
+     *
+     * @param cur_version_code
+     */
+    public void saveVersionCode(int cur_version_code) {
+        String line = String.format(Locale.CHINA, "%d", cur_version_code);
+        saveFile(line, 0);
+    }
+
+    private void saveFile(String line, int position) {
+        List<String> list = FileUtil.readLines(getCacheFilePath());
+        if (list == null)
+            list = ListUtil.emptyList();
+        while (list.size() <= position) {
+            list.add("");
+        }
+        list.set(position, line);
+        StringBuilder content = new StringBuilder();
+        for (String item : list) {
+            content.append(item);
+            content.append("\n");
+        }
+        FileUtil.saveFile(getCacheFilePath(), content.toString());
+    }
+}

+ 3 - 3
app/src/main/java/com/sheep/gamegroup/util/SysAppUtil.java

@@ -116,11 +116,11 @@ public class SysAppUtil {
      * @return
      * @return
      */
      */
     public static boolean isNewSmallSheep(boolean isSetNew) {
     public static boolean isNewSmallSheep(boolean isSetNew) {
-        int version_code = PreferenceUtils.getPrefInt(SheepApp.getInstance(), "version_code", 0);
+        int version_code = ForeverCacheUtil.getInstance().getVersionCode();
         int cur_version_code = ApkUtils.getCurrentPkgVersionCode(SheepApp.getInstance());
         int cur_version_code = ApkUtils.getCurrentPkgVersionCode(SheepApp.getInstance());
         if(isSetNew)
         if(isSetNew)
-            PreferenceUtils.setPrefInt(SheepApp.getInstance(), "version_code", cur_version_code);
-        return version_code != cur_version_code
+            ForeverCacheUtil.getInstance().saveVersionCode(cur_version_code);
+        return version_code < cur_version_code
 //                || BuildConfig.DEBUG
 //                || BuildConfig.DEBUG
                 ;
                 ;
     }
     }

+ 3 - 4
app/src/main/res/layout/x_ask_top5_item.xml

@@ -11,13 +11,12 @@
 
 
     <LinearLayout
     <LinearLayout
         android:layout_width="match_parent"
         android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:orientation="horizontal"
-        android:paddingBottom="@dimen/content_padding_10"
-        android:paddingTop="@dimen/content_padding_10">
+        android:layout_height="70dp"
+        android:orientation="horizontal">
 
 
         <RelativeLayout
         <RelativeLayout
             android:layout_width="22dp"
             android:layout_width="22dp"
+            android:paddingTop="@dimen/content_padding_10"
             android:layout_height="wrap_content">
             android:layout_height="wrap_content">
 
 
             <ImageView
             <ImageView