Files
assistant-android/app/src/main/java/com/gh/gamecenter/MainActivity.java
2017-11-13 15:34:24 +08:00

1069 lines
47 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.gh.gamecenter;
import android.app.Dialog;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import com.gh.base.AppUncaughtHandler;
import com.gh.base.BaseActivity;
import com.gh.common.constant.Config;
import com.gh.common.util.ClassUtils;
import com.gh.common.util.ConcernUtils;
import com.gh.common.util.DataCollectionUtils;
import com.gh.common.util.DataLogUtils;
import com.gh.common.util.DataUtils;
import com.gh.common.util.DeviceUtils;
import com.gh.common.util.DialogUtils;
import com.gh.common.util.EntranceUtils;
import com.gh.common.util.GameUtils;
import com.gh.common.util.LoginUtils;
import com.gh.common.util.NetworkUtils;
import com.gh.common.util.PackageUtils;
import com.gh.common.util.PlatformUtils;
import com.gh.common.util.TrafficUtils;
import com.gh.download.DownloadManager;
import com.gh.gamecenter.db.info.ConcernInfo;
import com.gh.gamecenter.db.info.GameInfo;
import com.gh.gamecenter.entity.ApkEntity;
import com.gh.gamecenter.entity.GameDigestEntity;
import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.entity.GameUpdateEntity;
import com.gh.gamecenter.eventbus.EBDownloadStatus;
import com.gh.gamecenter.eventbus.EBNetworkState;
import com.gh.gamecenter.eventbus.EBPackage;
import com.gh.gamecenter.eventbus.EBShowDialog;
import com.gh.gamecenter.eventbus.EBSkip;
import com.gh.gamecenter.fragment.MainFragment;
import com.gh.gamecenter.manager.ConcernManager;
import com.gh.gamecenter.manager.DataCollectionManager;
import com.gh.gamecenter.manager.FilterManager;
import com.gh.gamecenter.manager.GameManager;
import com.gh.gamecenter.manager.PackageManager;
import com.gh.gamecenter.manager.UpdateManager;
import com.gh.gamecenter.retrofit.ObservableUtil;
import com.gh.gamecenter.retrofit.Response;
import com.gh.gamecenter.retrofit.RetrofitManager;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.halo.assistant.HaloApp;
import com.lightgame.config.CommonDebug;
import com.lightgame.download.DataWatcher;
import com.lightgame.download.DownloadEntity;
import com.lightgame.download.DownloadStatus;
import com.lightgame.download.FileUtils;
import com.lightgame.utils.Utils;
import com.tencent.bugly.beta.tinker.TinkerManager;
import com.tencent.bugly.crashreport.CrashReport;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.HttpException;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import static com.gh.common.util.EntranceUtils.KEY_DATA;
import static com.gh.common.util.EntranceUtils.KEY_TO;
/**
* 项目的主Activity3个Fragment都嵌入在这里。
*/
public class MainActivity extends BaseActivity {
public final static String EB_MAINACTIVITY_TAG = "MainActivity";
public final static String EB_SKIP_GAMEFRAGMENT = "GameFragment";
private SharedPreferences sp;
private ConcernManager concernManager;
private static final int SKIP_SETTING_REQUEST = 14;
private boolean isSkipped;
public static boolean isNewFirstLaunch;
Runnable skipRun = new Runnable() {
@Override
public void run() {
if (getIntent() != null && getIntent().getExtras() != null && !isSkipped) {
isSkipped = true;
Bundle bundle = getIntent().getBundleExtra(KEY_DATA);
if (bundle != null) {
String to = bundle.getString(KEY_TO);
if (!TextUtils.isEmpty(to)) {
Class<?> clazz = ClassUtils.forName(to);
if (clazz != null) {
Intent skipIntent = new Intent(MainActivity.this, clazz);
String entrance = bundle.getString(EntranceUtils.KEY_ENTRANCE);
if (TextUtils.isEmpty(entrance) || !entrance.startsWith("(") || !entrance.endsWith(")")) {
bundle.putString(EntranceUtils.KEY_ENTRANCE, "(插件跳转)");
}
String packageName = bundle.getString("packageName");
if (packageName != null) {
bundle.putInt(EntranceUtils.KEY_CURRENTITEM, 1);
}
skipIntent.putExtra(KEY_DATA, bundle);
startActivity(skipIntent);
}
}
}
}
}
};
private Handler handler = new Handler();
// 黄壮华 添加观察者 修改2015/8/15
private DataWatcher dataWatcher = new DataWatcher() {
@Override
public void onDataChanged(DownloadEntity downloadEntity) {
if (DownloadStatus.hijack.equals(downloadEntity.getStatus())) {
// 链接被劫持
processHijack(downloadEntity);
return;
} else if (DownloadStatus.notfound.equals(downloadEntity.getStatus())) {
// 404 Not Found
// 删除任务
downloadEntity.setStatus(DownloadStatus.cancel);
DownloadManager.getInstance(getApplicationContext()).cancel(downloadEntity.getUrl());
toast("该链接已失效!请联系管理员。");
return;
} else if (DownloadStatus.neterror.equals(downloadEntity.getStatus())
|| DownloadStatus.timeout.equals(downloadEntity.getStatus())) {
toast("网络不稳定,下载任务已暂停");
DataLogUtils.uploadNeterrorLog(MainActivity.this, downloadEntity);
}
if (DownloadStatus.done.equals(downloadEntity.getStatus())) {
if (downloadEntity.getName().contains("光环助手")) {
DataUtils.onEvent(MainActivity.this, "软件更新", "下载完成");
startActivity(PackageUtils.getInstallIntent(MainActivity.this, downloadEntity.getPath()));
DataLogUtils.uploadUpgradeLog(MainActivity.this, "install"); //上传更新安装数据
} else {
statDoneEvent(downloadEntity);
String platform = PlatformUtils.getInstance(getApplicationContext())
.getPlatformName(downloadEntity.getPlatform());
if (platform != null) {
if (downloadEntity.isPluggable()) {
// 弹出插件化提示框
EventBus.getDefault().post(new EBShowDialog("plugin", downloadEntity.getPath()));
} else if (downloadEntity.isPlugin()) {
toast(downloadEntity.getName() + " - " + platform + " - 下载完成");
} else {
toast(downloadEntity.getName() + " - 下载完成");
}
} else {
toast(downloadEntity.getName() + " - 下载完成");
}
if (!downloadEntity.isPluggable()) {
// 是否是自动安装
if (sp.getBoolean("autoinstall", true)) {
if (FileUtils.isEmptyFile(downloadEntity.getPath())) {
toast(R.string.install_failure_hint);
DownloadManager.getInstance(MainActivity.this).cancel(downloadEntity.getUrl());
} else {
if (PackageUtils.isCanLaunchSetup(getApplicationContext(), downloadEntity.getPath())) {
startActivity(PackageUtils.getInstallIntent(MainActivity.this, downloadEntity.getPath()));
} else {
// 弹出卸载提示框
EventBus.getDefault().post(new EBShowDialog("plugin", downloadEntity.getPath()));
}
}
}
}
// 统计下载完成
uploadData(downloadEntity.getGameId(), downloadEntity.getPlatform());
}
}
}
};
/*
* 黄壮华 按连续按返回键两次才退出应用
*/
private long[] mHits = new long[2];
@NonNull
public static Intent getMainIntent(Context context) {
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
return intent;
}
// 统计下载
private void uploadData(String id, String platform) {
Map<String, String> params = new HashMap<>();
params.put("game", id);
params.put("platform", platform);
RequestBody body = RequestBody.create(MediaType.parse("application/json"),
new JSONObject(params).toString());
RetrofitManager.getInstance(this).getApi().postDownload(body)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<ResponseBody>());
}
// 统计下载完成事件
private void statDoneEvent(DownloadEntity downloadEntity) {
Map<String, Object> kv1 = new HashMap<>();
kv1.put("版本", downloadEntity.getPlatform());
kv1.put("状态", "下载完成");
if (downloadEntity.isUpdate()) {
DataUtils.onEvent(MainActivity.this, "游戏更新", downloadEntity.getName(), kv1);
} else {
DataUtils.onEvent(MainActivity.this, "游戏下载", downloadEntity.getName(), kv1);
}
Map<String, Object> kv2 = new HashMap<>();
kv2.put("版本", downloadEntity.getPlatform());
kv2.put("状态", "下载完成");
kv2.put("位置", downloadEntity.getEntrance());
DataUtils.onEvent(MainActivity.this, "游戏下载位置", downloadEntity.getName(), kv2);
if (downloadEntity.isPluggable()) {
Map<String, Object> kv3 = new HashMap<>();
kv3.put("下载", "下载完成");
kv3.put("版本", downloadEntity.getPlatform());
kv3.put("位置", downloadEntity.getEntrance());
DataUtils.onEvent(MainActivity.this, "插件化", downloadEntity.getName(), kv3);
}
DataCollectionUtils.uploadDownload(this, downloadEntity, "完成");
}
private void processHijack(DownloadEntity downloadEntity) {
// 删除任务
downloadEntity.setStatus(DownloadStatus.cancel);
DownloadManager.getInstance(getApplicationContext()).cancel(downloadEntity.getUrl());
// 弹出提示框
EventBus.getDefault().post(new EBShowDialog("hijack"));
// 记录链接被劫持
DataCollectionUtils.uploadHijack(this, downloadEntity);
// 上传劫持log
DataLogUtils.uploadHijack(this, downloadEntity);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
DownloadEntity downloadEntity = null;
for (DownloadEntity entity : DownloadManager.getInstance(getApplicationContext()).getAll()) {
if (entity.getStatus().equals(DownloadStatus.done)) {
if (PackageUtils.isInstalled(getApplicationContext(), entity.getPackageName())
&& (!entity.isPlugin()
|| PackageUtils.isSignature(getApplicationContext(), entity.getPackageName()))) {
continue;
}
if (downloadEntity == null) {
downloadEntity = entity;
} else if (entity.getEnd() > downloadEntity.getEnd()) {
downloadEntity = entity;
}
}
}
if (downloadEntity != null) {
String msg;
if (downloadEntity.isPlugin()) {
msg = "" + downloadEntity.getName() + "-"
+ PlatformUtils.getInstance(getApplicationContext()).getPlatformName(downloadEntity.getPlatform())
+ "》已下载完但还未安装,是否立即安装?";
} else {
msg = "" + downloadEntity.getName() + "》已下载完但还未安装,是否立即安装?";
}
final String path = downloadEntity.getPath();
DialogUtils.showWarningDialog(this, "提示", msg, "直接退出", "立即安装",
new DialogUtils.ConfirmListener() {
@Override
public void onConfirm() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
PackageUtils.launchSetup(MainActivity.this, path);
}
}, 200);
}
}, new DialogUtils.CancelListener() {
@Override
public void onCancel() {
finish();
}
});
return true;
}
System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
mHits[mHits.length - 1] = SystemClock.uptimeMillis();
if (mHits[0] >= (SystemClock.uptimeMillis() - 1000)) {
finish();
} else {
toast("再按一次就退出光环助手了哦");
return true;
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public void finish() {
// 上传数据
DataCollectionManager.getInstance(getApplicationContext()).statClickData();
super.finish();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("isSkipped", isSkipped);
}
@Override
protected int getLayoutId() {
return R.layout.layout_wrapper_activity;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SKIP_SETTING_REQUEST) {
List<PackageInfo> installedList = getPackageManager().getInstalledPackages(0);
if (installedList.isEmpty()) {
toast("读取应用列表失败");
} else {
getInstalledListFromServer();
}
}
}
// 获取已安装游戏
private void getInstalledListFromServer() {
ArrayList<String> list = PackageUtils.getAllPackageName(getApplicationContext());
PackageManager.init(list);
//应用上报
uploadAppList();
// 过滤助手收录的包
ArrayList<String> localList = new ArrayList<>();
FilterManager filterManager = new FilterManager(this);
for (int i = 0, size = list.size(); i < size; i++) {
if (filterManager.isFilter(list.get(i))) {
localList.add(list.get(i));
}
}
final CountDownLatch latch = ObservableUtil.latch(localList.size(), new Action1<Object>() {
@Override
public void call(Object o) {
updateConcern();
}
});
for (int i = 0, size = localList.size(); i < size; i++) {
final String packageName = localList.get(i);
RetrofitManager.getInstance(this).getApi().getGameDigestByPackageName(packageName)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<List<GameDigestEntity>>() {
@Override
public void onResponse(List<GameDigestEntity> response) {
for (GameDigestEntity gameDigestEntity : response) {
GameInfo gameInfo = new GameInfo();
gameInfo.setId(gameDigestEntity.getId());
gameInfo.setPackageName(packageName);
gameInfo.setGameName(gameDigestEntity.getName());
concernManager.updateByEntity(gameInfo);
}
latch.countDown();
}
@Override
public void onFailure(HttpException e) {
latch.countDown();
}
});
}
}
private void uploadAppList() {
//检查是否符合应用上报周期
long time = sp.getLong("last_upload_applist_time", 0);
if (Utils.getTime(this) - time >= 604800L) {//一周为一个周期
DataCollectionUtils.uploadAppList(this, PackageUtils.getAppList(this));
}
}
private void updateConcern() {
ArrayList<String> concernIdList = new ArrayList<>();
for (ConcernInfo entity : concernManager.getAllConcern()) {
concernIdList.add(entity.getId());
}
if (concernIdList.isEmpty()) {
update();
} else {
List<Observable<GameEntity>> sequences = new ArrayList<>();
for (String id : concernIdList) {
sequences.add(RetrofitManager.getInstance(this).getApi().getGameDigest(id));
}
Observable.merge(sequences)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<GameEntity>() {
@Override
public void onNext(GameEntity response) {
ConcernInfo concernInfo = concernManager.findConcernById(response.getId());
if (concernInfo != null && response.getApk().size() != 0) {
HashMap<String, Boolean> packageNames = new HashMap<>();
for (ApkEntity apkEntity : response.getApk()) {
if (PackageManager.isInstalled(apkEntity.getPackageName())) {
packageNames.put(apkEntity.getPackageName(), true);
} else {
packageNames.put(apkEntity.getPackageName(), false);
}
}
concernInfo.setTime(System.currentTimeMillis());
concernInfo.setPackageNames(packageNames);
concernManager.updateByConcern(concernInfo);
}
}
@Override
public void onCompleted() {
update();
}
});
}
}
/*
* 更新关注列表数据
*/
private void update() {
HaloApp.MAIN_EXECUTOR.execute(new Runnable() {
@Override
public void run() {
final List<String> installed = new ArrayList<>();
TrafficUtils spy = TrafficUtils.getInstance(getApplicationContext(), true);
List<ConcernInfo> concernList = concernManager.getAllConcern();
for (ConcernInfo concernEntity : concernList) {
concernEntity.setTime(System.currentTimeMillis());
int quantity = 0;
HashMap<String, Boolean> packageNames = new HashMap<>();
for (String packageName : concernEntity.getPackageNames().keySet()) {
if (PackageManager.isInstalled(packageName)) {
quantity++;
packageNames.put(packageName, true);
installed.add(packageName);
} else {
packageNames.put(packageName, false);
}
}
concernEntity.setPackageNames(packageNames);
concernEntity.setInstalled(quantity > 0);
concernEntity.setInstalledQuantity(quantity);
String tag = null;
int weight = 0;
if (concernEntity.isConcern() && concernEntity.isInstalled()) {
tag = "已关注,已安装";
weight = 2;
} else if (concernEntity.isConcern()
&& !concernEntity.isInstalled()) {
tag = "已关注";
weight = 1;
} else if (!concernEntity.isConcern()
&& concernEntity.isInstalled()) {
tag = "已安装";
weight = 1;
}
concernEntity.setTag(tag);
long traffic = spy.getTraffic(new ArrayList<>(concernEntity.getPackageNames().keySet()));
concernEntity.setTraffic(traffic);
if (traffic > 0) {
weight++;
}
concernEntity.setWeight(weight);
if (weight == 0) {
concernManager.deleteConcern(concernEntity.getId());
} else {
concernManager.updateByConcern(concernEntity);
}
}
// 更新用户已安装游戏
RequestBody body = RequestBody.create(MediaType.parse("application/json"),
new JSONArray(installed).toString());
RetrofitManager.getInstance(MainActivity.this).getApi().putPackage(body)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe(new Response<ResponseBody>());
handler.postDelayed(new Runnable() {
@Override
public void run() {
DataCollectionUtils.uploadUser(MainActivity.this);
}
}, 1000);
// 检查游戏更新
checkGameUpdate();
// 检查可插件化游戏
checkGamePlugin();
}
});
}
private void checkGameUpdate() {
List<ConcernInfo> infos = concernManager.getInstalledGame();
for (ConcernInfo info : infos) {
for (String packageName : info.getPackageNames().keySet()) {
if (info.getPackageNames().get(packageName)
&& PackageUtils.getMetaData(this, packageName, "gh_version") != null) {
checkGameUpdate(packageName);
}
}
}
}
private void checkGamePlugin() {
final List<GameEntity> list = new ArrayList<>();
List<Observable<GameEntity>> sequences = new ArrayList<>();
for (ConcernInfo info : concernManager.getInstalledGame()) {
sequences.add(RetrofitManager.getInstance(this).getApi().getGameDigest(info.getId()));
}
Observable.merge(sequences)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<GameEntity>() {
@Override
public void onNext(GameEntity response) {
list.add(response);
}
@Override
public void onCompleted() {
processPluginData(list);
}
});
}
private void checkGameUpdate(String packageName) {
Object gh_id = PackageUtils.getMetaData(this, packageName, "gh_id");
Observable<GameUpdateEntity> observable;
if (gh_id == null) {
observable = RetrofitManager.getInstance(this).getApi().getGameUpdate(packageName);
} else {
observable = RetrofitManager.getInstance(this).getApi().getGameUpdate((String) gh_id, packageName);
}
observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<GameUpdateEntity>() {
@Override
public void onResponse(GameUpdateEntity response) {
if (PackageUtils.isCanUpdate(MainActivity.this, response)) {
PackageManager.addUpdate(response);
EventBus.getDefault().post(new EBDownloadStatus("update"));
}
}
});
}
private void processPluginData(List<GameEntity> list) {
if (list == null || list.isEmpty()) {
return;
}
List<ConcernInfo> infos = concernManager.getInstalledGame();
HashMap<String, Boolean> map;
for (ConcernInfo info : infos) {
for (GameEntity gameEntity : list) {
if (gameEntity.getId().equals(info.getId())
&& gameEntity.getTag() != null && gameEntity.getTag().size() != 0
&& gameEntity.getApk() != null) {
map = info.getPackageNames();
for (String key : map.keySet()) {
if (map.get(key)) {
for (ApkEntity apkEntity : gameEntity.getApk()) {
if (apkEntity.getPackageName().equals(key)
&& !TextUtils.isEmpty(apkEntity.getGhVersion())
&& !PackageUtils.isSignature(this, apkEntity.getPackageName())) {
PackageManager.addUpdate(GameUtils.getGameUpdateEntity(gameEntity, apkEntity));
break;
}
}
GameManager manager = new GameManager(this);
manager.addOrUpdate(gameEntity.getApk(), gameEntity.getId(), gameEntity.getName());
}
}
break;
}
}
}
EventBus.getDefault().post(new EBDownloadStatus("plugin"));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 上传数据
DataCollectionManager.getInstance(getApplicationContext()).upload();
sp = PreferenceManager.getDefaultSharedPreferences(this);
isNewFirstLaunch = sp.getBoolean("isNewFirstLaunchV" + PackageUtils.getVersionName(this), true);
isSkipped = false;
if (savedInstanceState != null) {
isSkipped = savedInstanceState.getBoolean("isSkipped");
}
replaceFragment(new MainFragment());
concernManager = new ConcernManager(getApplicationContext());
if (!isNewFirstLaunch && sp.getBoolean("autoupdate", true)) {
// 检查助手更新
UpdateManager.getInstance(this).checkUpdate(true, null);
}
if (isNewFirstLaunch) {
getPluginUpdate();
sp.edit().putBoolean("isNewFirstLaunchV" + PackageUtils.getVersionName(this), false).apply();
checkDevice(); // 根据设备信息判断用户是否是新用户
}
// 初始化PlatformUtils
PlatformUtils.getInstance(getApplicationContext());
// 添加观察者
DownloadManager.getInstance(this).addObserver(dataWatcher);
// 检查是否有权限读取应用列表
checkPermission();
getSuggestionType();
final String message = Config.getExceptionMsg(this);
if (LoginUtils.getUserInfo(this) != null && !TextUtils.isEmpty(message)) {
Config.setExceptionMsg(this, null);
AppUncaughtHandler.reportException(this, new Throwable(message));
DialogUtils.showWarningDialog(this, "发生闪退", "光环助手刚刚发生了闪退,马上反馈以帮助我们更好地修复问题?(只需简单描述你刚才的操作)"
, "暂不", "马上反馈", new DialogUtils.ConfirmListener() {
@Override
public void onConfirm() {
SuggestionActivity.startSuggestionActivity(MainActivity.this, 3, null, null);
}
}, null);
}
checkTinkerPath();
checkRetryDownload();
// 执行跳转事件
handler.postDelayed(skipRun, 500);
}
private void checkRetryDownload() {
if (!NetworkUtils.isWifiConnected(this)) return;
for (DownloadEntity downloadEntity : DownloadManager.getInstance(this).getAll()) {
if (DownloadStatus.neterror.equals(downloadEntity.getStatus()) ||
DownloadStatus.timeout.equals(downloadEntity.getStatus())) {
DownloadManager.getInstance(this).add(downloadEntity);
}
Utils.log("checkRetryDownload::" + downloadEntity.getStatus());
}
}
private void checkTinkerPath() {
CommonDebug.logMethodWithParams(this, TinkerManager.getTinkerId(), TinkerManager.getNewTinkerId());
CommonDebug.logMethodWithParams(this, CrashReport.getAppVer(), CrashReport.getAppID(), CrashReport.getAppChannel(), CrashReport.getSdkExtraData());
// 1.母包首次启动tinkerId不为空newTinkerId(patchTinkerId)应该是空的
// 2. 补丁包启动tinkerId和之前一致但newTinkerId不一致
// 2.1 显示更新信息弹窗记录tinkerId+newTinkerId
// 2.1.1 若tinkerId一致但newTinkerId不一致才能认为是一次补丁更新
String tinkerId = TinkerManager.getTinkerId();
String newTinkerId = TinkerManager.getNewTinkerId();
if (TextUtils.isEmpty(newTinkerId)) {
//这是一个新的母包,没有任何补丁信息
return;
}
final Gson gson = new Gson();
Type type = new TypeToken<HashMap<String, Set<String>>>() {
}.getType();
final Map<String, Set<String>> tinkerIdMap;
final String tinkerMapJson = sp.getString(Config.PATCHES, "");
if (!TextUtils.isEmpty(tinkerMapJson)) {
tinkerIdMap = gson.fromJson(tinkerMapJson, type);
} else {
tinkerIdMap = new HashMap<>();
}
Set<String> tinkerIdSet = tinkerIdMap.get(tinkerId);
if (tinkerIdSet == null) {
tinkerIdSet = new HashSet<>();
tinkerIdMap.put(tinkerId, tinkerIdSet);
}
// 有新的补丁,没有显示过
if (!tinkerIdSet.contains(newTinkerId)) {
tinkerIdSet.add(newTinkerId);
final Dialog dialog = new Dialog(this, R.style.GhAlertDialog);
View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_alert, null);
TextView contentTv = contentView.findViewById(R.id.dialog_content);
TextView titleTv = contentView.findViewById(R.id.dialog_title);
TextView negativeTv = contentView.findViewById(R.id.dialog_negative);
TextView positiveTv = contentView.findViewById(R.id.dialog_positive);
titleTv.setText("补丁更新成功");
contentTv.setText("光环助手已成功更新到V" + PackageUtils.getPatchVersionName());
positiveTv.setText("知道了");
negativeTv.setText(null);
negativeTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
positiveTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
sp.edit().putString(Config.PATCHES, gson.toJson(tinkerIdMap)).apply();
}
});
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(contentView);
dialog.show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
DownloadManager.getInstance(this).removeObserver(dataWatcher);
handler.removeCallbacksAndMessages(null);
}
private void checkDevice() {
String content = DeviceUtils.getUserDevice(MainActivity.this).toString();
RequestBody body = RequestBody.create(MediaType.parse("application/json"), content);
RetrofitManager.getInstance(this).getApi().checkDevice(body)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<ResponseBody>() {
@Override
public void onResponse(ResponseBody response) {
super.onResponse(response);
try {
String string = response.string();
JSONObject object = new JSONObject(string);
String deviceId = object.getString("device_id");
if (!TextUtils.isEmpty(deviceId)) {
oldUserSkip(deviceId);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private void oldUserSkip(String deviceId) {
sp.edit().putString("syncDeviceID", deviceId).apply();
DialogUtils.showForceDialog(MainActivity.this, "数据同步提醒"
, "系统检测到你是光环助手的老用户,为了避免数据丢失,请尽快登录"
, "前往登录", "残忍拒绝", new DialogUtils.ConfirmListener() {
@Override
public void onConfirm() {
EventBus.getDefault().post(new EBSkip(MainActivity.EB_SKIP_GAMEFRAGMENT, 2));
}
}, null);
}
private void getSuggestionType() {
RetrofitManager.getInstance(this).getApi()
.getSuggestionType()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<ResponseBody>() {
@Override
public void onResponse(ResponseBody response) {
super.onResponse(response);
try {
String string = response.string();
SharedPreferences.Editor edit = sp.edit();
edit.putString("SuggestionType", string);
edit.apply();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onError(Throwable e) {
super.onError(e);
Utils.log("反馈的问题选项::ERROR::" + e.toString());
}
});
}
private void checkPermission() {
List<PackageInfo> installedList = getPackageManager().getInstalledPackages(0);
if (installedList.isEmpty()) {
// 无权限获取应用列表
Spanned content = Html.fromHtml("请前往系统权限设置,把“" +
"<font color='#ff0000'>读取应用列表</font>”选择为打开" +
"“<font color='#ff0000'>允许</font>”,否则光环助手将无法帮您安装插件加速版的游戏");
DialogUtils.showWarningDialog(this, "提示", content, "忽略", "去开启",
new DialogUtils.ConfirmListener() {
@Override
public void onConfirm() {
// 跳转应用信息界面
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, SKIP_SETTING_REQUEST);
}
}, null);
} else {
getInstalledListFromServer();
}
}
// 获取META-INF中的plugin_update 文件判断是否从游戏插件中下载的app是则获取游戏id启动游戏更新下载该游戏
private void getPluginUpdate() {
ApplicationInfo appinfo = getApplicationInfo();
String sourceDir = appinfo.sourceDir;
ZipFile zipfile = null;
try {
zipfile = new ZipFile(sourceDir);
Enumeration<?> entries = zipfile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
String entryName = entry.getName();
if (entryName.contains("gh_assist")) { // TODO: 20/09/17 统一入口
String packageName = entryName.substring(entryName.lastIndexOf("_") + 1);
Intent intent = new Intent(MainActivity.this, DownloadManagerActivity.class);
intent.putExtra("currentItem", 1);
intent.putExtra("packageName", packageName);
intent.putExtra(EntranceUtils.KEY_ENTRANCE, "(游戏插件)");
startActivity(intent);
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (zipfile != null) {
try {
zipfile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 连接上网络事件
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(EBNetworkState busNetworkState) {
if (busNetworkState.isNetworkConnected()) {
checkRetryDownload();
}
}
// TODO: 30/10/17 整理这个方法
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(EBPackage busFour) {
final String packageName = busFour.getPackageName();
if ("安装".equals(busFour.getType())) { // 删除下载完成 弹窗
NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nManager.cancel(packageName.hashCode());
}
if ("安装".equals(busFour.getType())) {
// 添加进已安装map中
PackageManager.addInstalled(packageName);
} else if ("卸载".equals(busFour.getType())) {
// 删除map中数据
PackageManager.removeInstalled(packageName);
}
DownloadEntity mDownloadEntity = null;
for (DownloadEntity downloadEntity : DownloadManager.getInstance(getApplicationContext()).getAll()) {
if (packageName.equals(downloadEntity.getPackageName())) {
mDownloadEntity = downloadEntity;
break;
}
}
if (mDownloadEntity != null) {
if ("安装".equals(busFour.getType())) {
if (mDownloadEntity.isPluggable()) {
Map<String, Object> kv6 = new HashMap<>();
kv6.put("安装或卸载", "安装完成");
DataUtils.onEvent(this, "插件化", mDownloadEntity.getName(), kv6);
}
// if (!mDownloadEntity.isPlugin() // 不是插件游戏,自己删除数据库数据和安装包
// || PackageUtils.isSignature(this, packageName)) {// 是插件游戏,判断签名是否相同,是才删除数据库数据和安装包
DownloadManager.getInstance(getApplicationContext()).cancel(
mDownloadEntity.getUrl(), sp.getBoolean("autodelete", true));
// }
} else if ("卸载".equals(busFour.getType())) {
Map<String, Object> kv6 = new HashMap<>();
kv6.put("安装或卸载", "卸载完成");
DataUtils.onEvent(this, "插件化", mDownloadEntity.getName(), kv6);
startActivity(PackageUtils.getInstallIntent(this, mDownloadEntity.getPath()));
}
}
if ("安装".equals(busFour.getType()) && sp.getBoolean("concerngame", true)) {
GameManager gameManager = new GameManager(getApplicationContext());
GameInfo gameInfo = gameManager.findGame(packageName);
if (gameInfo != null) {
concernManager.updateByEntity(gameInfo);
}
} else if ("卸载".equals(busFour.getType())) {
FilterManager filterManager = new FilterManager(getApplicationContext());
if (filterManager.isFilter(packageName)) {
concernManager.updateByPackageName(packageName);
}
}
if ("卸载".equals(busFour.getType()) || "安装".equals(busFour.getType())) {
PackageManager.removeUpdate(packageName);
}
if ("安装".equals(busFour.getType()) && sp.getBoolean("concerngame", true)) {//设置页面控制是否安装后自动关注
// 安装后关注游戏
RetrofitManager.getInstance(this).getApi().getGameDigestByPackageName(packageName)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<List<GameDigestEntity>>() {
@Override
public void onResponse(List<GameDigestEntity> response) {
for (GameDigestEntity gameDigestEntity : response) {
GameInfo gameInfo = new GameInfo();
gameInfo.setId(gameDigestEntity.getId());
gameInfo.setPackageName(packageName);
gameInfo.setGameName(gameDigestEntity.getName());
concernGame(gameDigestEntity.getId(), packageName);
if (PackageUtils.getMetaData(getApplicationContext(),
packageName, "gh_version") != null) {
checkGameUpdate(packageName);
}
if (!TextUtils.isEmpty(gameDigestEntity.getId())) { // 关注游戏
ConcernUtils.INSTANCE.postConcernGameId(MainActivity.this, gameDigestEntity.getId(), null);
}
}
// 更新已安装游戏
JSONArray params = new JSONArray();
params.put(packageName);
RequestBody body = RequestBody.create(
MediaType.parse("application/json"), params.toString());
RetrofitManager.getInstance(MainActivity.this).getApi()
.postPackage(body)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe(new Response<ResponseBody>());
}
});
}
if ("卸载".equals(busFour.getType())) {
// 更新已安装游戏
RetrofitManager.getInstance(this).getApi()
.deletePackage(packageName)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe(new Response<ResponseBody>());
}
DataCollectionUtils.uploadInorunstall(this, busFour.getType(), busFour.getPackageName());
}
private void concernGame(final String id, final String packageName) {
RetrofitManager.getInstance(this).getApi().getGameDigest(id)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<GameEntity>() {
@Override
public void onResponse(GameEntity response) {
GameManager manager = new GameManager(getApplicationContext());
manager.addOrUpdate(response.getApk(), response.getId(), response.getName());
// 检查是否能插件化
if (response.getTag() != null && response.getTag().size() != 0) {
for (ApkEntity apkEntity : response.getApk()) {
if (packageName.equals(apkEntity.getPackageName())
&& !TextUtils.isEmpty(apkEntity.getGhVersion())
&& !PackageUtils.isSignature(getApplicationContext(), apkEntity.getPackageName())) {
PackageManager.addUpdate(GameUtils.getGameUpdateEntity(response, apkEntity));
EventBus.getDefault().post(new EBDownloadStatus("plugin"));
break;
}
}
}
}
});
}
}