整理已安装的游戏数据表
This commit is contained in:
127
app/src/main/java/com/gh/gamecenter/manager/InstallManager.java
Normal file
127
app/src/main/java/com/gh/gamecenter/manager/InstallManager.java
Normal file
@ -0,0 +1,127 @@
|
||||
package com.gh.gamecenter.manager;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.gh.common.util.PackageUtils;
|
||||
import com.gh.gamecenter.db.InstallDao;
|
||||
import com.gh.gamecenter.db.info.InstallInfo;
|
||||
import com.gh.gamecenter.eventbus.EBConcernChanged;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class InstallManager {
|
||||
|
||||
private Context context;
|
||||
private InstallDao dao;
|
||||
|
||||
public InstallManager(Context context) {
|
||||
this.context = context;
|
||||
dao = new InstallDao(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个已安装的游戏
|
||||
*/
|
||||
public void addInstall(InstallInfo entity) {
|
||||
dao.add(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取某一个安装的游戏
|
||||
*/
|
||||
public InstallInfo findInstallById(String id) {
|
||||
return dao.find(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新关注列表
|
||||
*/
|
||||
public void updateInstall(InstallInfo entity) {
|
||||
dao.update(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据GameEntity更新已安装的游戏
|
||||
*/
|
||||
public void updateByEntity(com.gh.gamecenter.db.info.GameInfo gameEntity) {
|
||||
String packageName = gameEntity.getPackageName();
|
||||
InstallInfo concernEntity = findInstallById(gameEntity.getId());
|
||||
if (concernEntity != null) {
|
||||
concernEntity.setTime(System.currentTimeMillis());
|
||||
if (gameEntity.getGameName() != null) {
|
||||
concernEntity.setGameName(gameEntity.getGameName());
|
||||
}
|
||||
concernEntity.getPackageNames().put(packageName, true);
|
||||
int quantity = 0;
|
||||
for (String key : concernEntity.getPackageNames().keySet()) {
|
||||
if (concernEntity.getPackageNames().get(key)) {
|
||||
quantity++;
|
||||
}
|
||||
}
|
||||
concernEntity.setInstalledQuantity(quantity);
|
||||
updateInstall(concernEntity);
|
||||
} else {
|
||||
concernEntity = new InstallInfo();
|
||||
concernEntity.setId(gameEntity.getId());
|
||||
concernEntity.setTime(System.currentTimeMillis());
|
||||
HashMap<String, Boolean> packageNames = new HashMap<>();
|
||||
packageNames.put(packageName, true);
|
||||
concernEntity.setPackageNames(packageNames);
|
||||
concernEntity.setWeight(1);
|
||||
concernEntity.setInstalledQuantity(1);
|
||||
if (gameEntity.getGameName() != null) {
|
||||
concernEntity.setGameName(gameEntity.getGameName());
|
||||
} else {
|
||||
concernEntity.setGameName(PackageUtils.getNameByPackageName(
|
||||
context, packageName));
|
||||
}
|
||||
addInstall(concernEntity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据包名 更新关注
|
||||
*/
|
||||
public void updateByPackageName(String packageName) {
|
||||
for (InstallInfo concernEntity : getAllInstall()) {
|
||||
Boolean isInstalled = concernEntity.getPackageNames().get(packageName);
|
||||
if (isInstalled != null && isInstalled) {
|
||||
concernEntity.getPackageNames().put(packageName, false);
|
||||
int quantity = 0;
|
||||
for (String key : concernEntity.getPackageNames().keySet()) {
|
||||
if (concernEntity.getPackageNames().get(key)) {
|
||||
quantity++;
|
||||
}
|
||||
}
|
||||
if (quantity == 0) {
|
||||
deleteInstall(concernEntity.getId());
|
||||
} else {
|
||||
concernEntity.setTime(System.currentTimeMillis());
|
||||
concernEntity.setInstalledQuantity(quantity);
|
||||
concernEntity.getPackageNames().put(packageName, false);
|
||||
updateInstall(concernEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取已安装列表
|
||||
*/
|
||||
public List<InstallInfo> getAllInstall() {
|
||||
return dao.getAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除一个关注
|
||||
*/
|
||||
public void deleteInstall(String id) {
|
||||
dao.delete(id);
|
||||
EventBus.getDefault().post(new EBConcernChanged(id, false));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user