ViewerPreferences.java 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package com.poqop.document;
  2. import android.content.Context;
  3. import android.content.SharedPreferences;
  4. import android.net.Uri;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7. import java.util.List;
  8. import java.util.TreeMap;
  9. /**
  10. * 最近浏览的文件
  11. * @author Administrator
  12. * 主要用SharedPreferences来进行存储
  13. */
  14. public class ViewerPreferences
  15. {
  16. private SharedPreferences sharedPreferences;
  17. private static final String FULL_SCREEN = "FullScreen";
  18. public ViewerPreferences(Context context)
  19. {
  20. //得到最近浏览的文件参数
  21. sharedPreferences = context.getSharedPreferences("ViewerPreferences", 0);
  22. }
  23. public void setFullScreen(boolean fullscreen)
  24. {
  25. final SharedPreferences.Editor editor = sharedPreferences.edit();
  26. // editor.putBoolean(FULL_SCREEN, fullscreen);
  27. editor.commit();
  28. }
  29. public boolean isFullScreen()
  30. {
  31. return sharedPreferences.getBoolean(FULL_SCREEN, false);
  32. }
  33. public void addRecent(Uri uri)
  34. {
  35. SharedPreferences.Editor editor = sharedPreferences.edit(); //修改配置信息之后,并提交
  36. editor.putString("recent:" + uri.toString(), uri.toString() + "\n" + System.currentTimeMillis());
  37. editor.commit();
  38. }
  39. public List<Uri> getRecent()
  40. {
  41. TreeMap<Long, Uri> treeMap = new TreeMap<Long, Uri>();
  42. for (String key : sharedPreferences.getAll().keySet())
  43. {
  44. if (key.startsWith("recent"))
  45. {
  46. String uriPlusDate = sharedPreferences.getString(key, null);
  47. String[] uriThenDate = uriPlusDate.split("\n");
  48. treeMap.put(Long.parseLong(uriThenDate.length > 1 ? uriThenDate[1] : "0"), Uri.parse(uriThenDate[0]));
  49. }
  50. }
  51. ArrayList<Uri> list = new ArrayList<Uri>(treeMap.values());
  52. Collections.reverse(list);
  53. return list;
  54. }
  55. }