12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- package com.ysnows.sultra.model;
- import android.content.pm.PackageManager;
- import android.graphics.drawable.Drawable;
- import android.text.TextUtils;
- import com.github.promeg.pinyinhelper.Pinyin;
- import com.litesuits.orm.LiteOrm;
- import com.litesuits.orm.db.annotation.PrimaryKey;
- import com.litesuits.orm.db.annotation.Table;
- import com.litesuits.orm.db.annotation.Unique;
- import com.litesuits.orm.db.assit.QueryBuilder;
- import com.litesuits.orm.db.assit.WhereBuilder;
- import com.litesuits.orm.db.enums.AssignType;
- import com.ysnows.sultra.App;
- import com.ysnows.sultra.utils.PinyinUtil;
- import java.util.ArrayList;
- @Table("AppModel")
- public class AppModel {
- // // 指定自增,每个对象需要有一个主键
- @PrimaryKey(AssignType.AUTO_INCREMENT)
- @Unique
- public int id;
- public String packageName;
- public String name;
- public String pinyin;
- public String pinyinSimple;
- public AppModel(String packageName, String name) {
- this.packageName = packageName;
- this.name = name;
- if (this.name != null) {
- this.pinyin = PinyinUtil.getPinyin(this.name);
- this.pinyinSimple = PinyinUtil.getPinyinSimple(this.name);
- }
- }
- public Drawable loadIcon() {
- try {
- PackageManager packageManager = App.getApp().getPackageManager();
- return packageManager.getPackageInfo(this.packageName, PackageManager.GET_META_DATA).applicationInfo.loadIcon(packageManager);
- } catch (PackageManager.NameNotFoundException e) {
- return null;
- }
- }
- public static ArrayList<AppModel> findAppsByName(String name) {
- if (TextUtils.isEmpty(name)) {
- return null;
- }
- LiteOrm liteOrm = App.getLiteOrm();
- ArrayList<AppModel> res = null;
- if (Pinyin.isChinese(name.charAt(0))) {
- res = liteOrm.query(new QueryBuilder<>(AppModel.class).where("name LIKE ?", "%" + name + "%"));
- } else {
- res = liteOrm.query(new QueryBuilder<>(AppModel.class).where("pinyin LIKE ?", "%" + name + "%"));
- }
- return res;
- }
- public static ArrayList<AppModel> findAllApps() {
- LiteOrm liteOrm = App.getLiteOrm();
- return liteOrm.query(new QueryBuilder<>(AppModel.class));
- }
- public void save() {
- App.getLiteOrm().save(this);
- }
- public static void clear() {
- App.getLiteOrm().delete(AppModel.class);
- }
- public static void delByPackage(String packageName) {
- App.getLiteOrm().delete(new WhereBuilder(AppModel.class).where("packageName=?", packageName));
- }
- }
|