Files
assistant-android/app/src/main/java/com/gh/gamecenter/manager/InstallManager.java

131 lines
4.1 KiB
Java

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.GameInfo;
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(GameInfo gameEntity) {
String packageName = gameEntity.getPackageName();
InstallInfo installInfo = findInstallById(gameEntity.getId());
if (installInfo != null) {
installInfo.setTime(System.currentTimeMillis());
installInfo.setIcon(gameEntity.getGameIcon());
if (gameEntity.getGameName() != null) {
installInfo.setGameName(gameEntity.getGameName());
}
installInfo.getPackageNames().put(packageName, true);
int quantity = 0;
for (String key : installInfo.getPackageNames().keySet()) {
if (installInfo.getPackageNames().get(key)) {
quantity++;
}
}
installInfo.setInstalledQuantity(quantity);
updateInstall(installInfo);
} else {
installInfo = new InstallInfo();
installInfo.setId(gameEntity.getId());
installInfo.setIcon(gameEntity.getGameIcon());
installInfo.setTime(System.currentTimeMillis());
HashMap<String, Boolean> packageNames = new HashMap<>();
packageNames.put(packageName, true);
installInfo.setPackageNames(packageNames);
installInfo.setWeight(1);
installInfo.setInstalledQuantity(1);
if (gameEntity.getGameName() != null) {
installInfo.setGameName(gameEntity.getGameName());
} else {
installInfo.setGameName(PackageUtils.getNameByPackageName(
context, packageName));
}
addInstall(installInfo);
}
}
/**
* 根据包名 更新关注
*/
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));
}
}