49 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
			
		
		
	
	
			49 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
| package com.novelbook.android.utils;
 | |
| 
 | |
| 
 | |
| import android.content.Context;
 | |
| import android.content.SharedPreferences;
 | |
| import android.content.SharedPreferences.Editor;
 | |
| import com.google.gson.Gson;
 | |
| import com.google.gson.reflect.TypeToken;
 | |
| import java.util.List;
 | |
| 
 | |
| public class HistoryCache {
 | |
|     public HistoryCache() {
 | |
|     }
 | |
| 
 | |
|     public static void clear(Context context) {
 | |
|         SharedPreferences sp = context.getSharedPreferences("record", 0);
 | |
|         sp.edit().clear().commit();
 | |
|     }
 | |
| 
 | |
|     public static void saveHistory(Context context, String result) {
 | |
|         SharedPreferences sp = context.getSharedPreferences("record", 0);
 | |
|         Editor editor = sp.edit();
 | |
|         editor.putString("record_key", result);
 | |
|         editor.commit();
 | |
|     }
 | |
| 
 | |
|     public static String getHistory(Context context) {
 | |
|         SharedPreferences sp = context.getSharedPreferences("record", 0);
 | |
|         String result = sp.getString("record_key", (String)null);
 | |
|         return result;
 | |
|     }
 | |
| 
 | |
|     public static List<String> toArray(Context context) {
 | |
|         String history = getHistory(context);
 | |
|         Gson gson = new Gson();
 | |
|         List<String> retList = (List)gson.fromJson(history, (new TypeToken<List<String>>() {
 | |
|         }).getType());
 | |
|         if(retList!=null && retList.size()>9){
 | |
|             return retList.subList(0,9);
 | |
|         }
 | |
|         return retList;
 | |
|     }
 | |
| 
 | |
|     public static String toJsonArray(List<String> historyList) {
 | |
|         Gson gson = new Gson();
 | |
|         return gson.toJson(historyList);
 | |
|     }
 | |
| }
 |