AppModel.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package com.ysnows.sultra.model;
  2. import android.content.pm.PackageManager;
  3. import android.graphics.drawable.Drawable;
  4. import android.text.TextUtils;
  5. import com.github.promeg.pinyinhelper.Pinyin;
  6. import com.litesuits.orm.LiteOrm;
  7. import com.litesuits.orm.db.annotation.PrimaryKey;
  8. import com.litesuits.orm.db.annotation.Table;
  9. import com.litesuits.orm.db.annotation.Unique;
  10. import com.litesuits.orm.db.assit.QueryBuilder;
  11. import com.litesuits.orm.db.assit.WhereBuilder;
  12. import com.litesuits.orm.db.enums.AssignType;
  13. import com.ysnows.sultra.App;
  14. import com.ysnows.sultra.utils.PinyinUtil;
  15. import java.util.ArrayList;
  16. @Table("AppModel")
  17. public class AppModel {
  18. // // 指定自增,每个对象需要有一个主键
  19. @PrimaryKey(AssignType.AUTO_INCREMENT)
  20. @Unique
  21. public int id;
  22. public String packageName;
  23. public String name;
  24. public String pinyin;
  25. public String pinyinSimple;
  26. public AppModel(String packageName, String name) {
  27. this.packageName = packageName;
  28. this.name = name;
  29. if (this.name != null) {
  30. this.pinyin = PinyinUtil.getPinyin(this.name);
  31. this.pinyinSimple = PinyinUtil.getPinyinSimple(this.name);
  32. }
  33. }
  34. public Drawable loadIcon() {
  35. try {
  36. PackageManager packageManager = App.getApp().getPackageManager();
  37. return packageManager.getPackageInfo(this.packageName, PackageManager.GET_META_DATA).applicationInfo.loadIcon(packageManager);
  38. } catch (PackageManager.NameNotFoundException e) {
  39. return null;
  40. }
  41. }
  42. public static ArrayList<AppModel> findAppsByName(String name) {
  43. if (TextUtils.isEmpty(name)) {
  44. return null;
  45. }
  46. LiteOrm liteOrm = App.getLiteOrm();
  47. ArrayList<AppModel> res = null;
  48. if (Pinyin.isChinese(name.charAt(0))) {
  49. res = liteOrm.query(new QueryBuilder<>(AppModel.class).where("name LIKE ?", "%" + name + "%"));
  50. } else {
  51. res = liteOrm.query(new QueryBuilder<>(AppModel.class).where("pinyin LIKE ?", "%" + name + "%"));
  52. }
  53. return res;
  54. }
  55. public static ArrayList<AppModel> findAllApps() {
  56. LiteOrm liteOrm = App.getLiteOrm();
  57. return liteOrm.query(new QueryBuilder<>(AppModel.class));
  58. }
  59. public void save() {
  60. App.getLiteOrm().save(this);
  61. }
  62. public static void clear() {
  63. App.getLiteOrm().delete(AppModel.class);
  64. }
  65. public static void delByPackage(String packageName) {
  66. App.getLiteOrm().delete(new WhereBuilder(AppModel.class).where("packageName=?", packageName));
  67. }
  68. }