1087 lines
49 KiB
Java
1087 lines
49 KiB
Java
package com.gh.gamecenter;
|
||
|
||
import android.content.Context;
|
||
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.provider.Settings;
|
||
import android.support.annotation.NonNull;
|
||
import android.support.v4.util.ArrayMap;
|
||
import android.text.Html;
|
||
import android.text.Spanned;
|
||
import android.text.TextUtils;
|
||
import android.view.KeyEvent;
|
||
|
||
import com.gh.base.AppController;
|
||
import com.gh.base.BaseActivity;
|
||
import com.gh.common.constant.Config;
|
||
import com.gh.common.util.ClassUtils;
|
||
import com.gh.common.util.DataCollectionUtils;
|
||
import com.gh.common.util.DataLogUtils;
|
||
import com.gh.common.util.DataUtils;
|
||
import com.gh.common.util.DialogUtils;
|
||
import com.gh.common.util.EntranceUtils;
|
||
import com.gh.common.util.FileUtils;
|
||
import com.gh.common.util.GameUtils;
|
||
import com.gh.common.util.LibaoUtils;
|
||
import com.gh.common.util.LoginUtils;
|
||
import com.gh.common.util.PackageUtils;
|
||
import com.gh.common.util.PlatformUtils;
|
||
import com.gh.common.util.TokenUtils;
|
||
import com.gh.common.util.TrafficUtils;
|
||
import com.gh.common.util.Utils;
|
||
import com.gh.download.DataWatcher;
|
||
import com.gh.download.DownloadEntity;
|
||
import com.gh.download.DownloadManager;
|
||
import com.gh.download.DownloadStatus;
|
||
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.EBPackage;
|
||
import com.gh.gamecenter.eventbus.EBShowDialog;
|
||
import com.gh.gamecenter.eventbus.EBUISwitch;
|
||
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.JSONObjectResponse;
|
||
import com.gh.gamecenter.retrofit.ObservableUtil;
|
||
import com.gh.gamecenter.retrofit.Response;
|
||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||
import com.tencent.connect.common.Constants;
|
||
import com.tencent.tauth.Tencent;
|
||
|
||
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.util.ArrayList;
|
||
import java.util.Enumeration;
|
||
import java.util.HashMap;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
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.functions.Func1;
|
||
import rx.schedulers.Schedulers;
|
||
|
||
/**
|
||
* 项目的主Activity,3个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 boolean isSkipped;
|
||
Runnable skipRun = new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
if (getIntent() != null && getIntent().getExtras() != null && !isSkipped) {
|
||
isSkipped = true;
|
||
Bundle bundle = getIntent().getBundleExtra("data");
|
||
if (bundle != null) {
|
||
String to = bundle.getString("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("currentItem", 1);
|
||
}
|
||
|
||
skipIntent.putExtra("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("解析包出错(可能被误删了),请重新下载");
|
||
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.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 == Constants.REQUEST_LOGIN) { // QQ Login callback
|
||
Tencent.onActivityResultData(requestCode,resultCode,data, LoginUtils.getInstance(this).QqLoginListener);
|
||
} else if (requestCode == 32973 && LoginUtils.getInstance(this).mSsoHandler != null) {
|
||
LoginUtils.getInstance(this).mSsoHandler.authorizeCallBack(requestCode, resultCode, data);
|
||
} else if (requestCode == 0x123) {
|
||
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.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.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() != 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() {
|
||
AppController.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);
|
||
}
|
||
}
|
||
|
||
// 更新用户已安装游戏
|
||
TokenUtils.getToken(MainActivity.this, true)
|
||
.flatMap(new Func1<String, Observable<ResponseBody>>() {
|
||
@Override
|
||
public Observable<ResponseBody> call(String token) {
|
||
RequestBody body = RequestBody.create(MediaType.parse("application/json"),
|
||
new JSONArray(installed).toString());
|
||
return RetrofitManager.getUser().putPackage(token, 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.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.getApi().getGameUpdate(packageName);
|
||
} else {
|
||
observable = RetrofitManager.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);
|
||
|
||
// 初始化gameMap
|
||
DownloadManager.getInstance(this).initGameMap();
|
||
|
||
sp = getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
|
||
boolean isNewFirstLaunch = sp.getBoolean("isNewFirstLaunchV" + PackageUtils.getVersionName(this), true);
|
||
|
||
isSkipped = false;
|
||
|
||
// initViews();
|
||
if (savedInstanceState != null) {
|
||
// currentTab = savedInstanceState.getInt("currentTab");
|
||
isSkipped = savedInstanceState.getBoolean("isSkipped");
|
||
// } else {
|
||
// currentTab = 0;
|
||
}
|
||
|
||
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();
|
||
}
|
||
|
||
// 初始化PlatformUtils
|
||
PlatformUtils.getInstance(getApplicationContext());
|
||
|
||
// 添加观察者
|
||
DownloadManager.getInstance(this).addObserver(dataWatcher);
|
||
|
||
// 检查是否有权限读取应用列表
|
||
checkPermission();
|
||
|
||
if (isNewFirstLaunch || TokenUtils.getDeviceId(this) == null) {
|
||
AppController.MAIN_EXECUTOR.execute(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
// 注册设备
|
||
TokenUtils.register(MainActivity.this.getApplicationContext());
|
||
TokenUtils.getToken(MainActivity.this.getApplicationContext(), false)
|
||
.subscribeOn(Schedulers.io())
|
||
.observeOn(AndroidSchedulers.mainThread())
|
||
.subscribe(new Response<String>() {
|
||
@Override
|
||
public void onResponse(String token) {
|
||
TokenUtils.checkDeviceInfo(MainActivity.this.getApplicationContext(), token);
|
||
}
|
||
});
|
||
updateUserGhzs();
|
||
initConcern(); // 初始化关注
|
||
LibaoUtils.getCunHaoXiang(MainActivity.this, true);
|
||
}
|
||
});
|
||
} else {
|
||
updateUserGhzs();
|
||
}
|
||
|
||
// 执行跳转事件
|
||
handler.postDelayed(skipRun, 500);
|
||
|
||
if (sp.getBoolean("changeDefaultIcon", false) && sp.getInt("default_user_icon", 0) != 0) {
|
||
postDefaultIcon(true, sp.getInt("default_user_icon", 0), 1);
|
||
}
|
||
|
||
getSuggestionType();
|
||
|
||
}
|
||
|
||
@Override
|
||
protected void onDestroy() {
|
||
super.onDestroy();
|
||
DownloadManager.getInstance(this).removeObserver(dataWatcher);
|
||
|
||
if (sp.getBoolean("changeDefaultIcon", false) && sp.getInt("default_user_icon", 0) != 0) {
|
||
postDefaultIcon(true, sp.getInt("default_user_icon", 0), 1);
|
||
}
|
||
|
||
handler.removeCallbacksAndMessages(null);
|
||
}
|
||
|
||
private void getSuggestionType() {
|
||
RetrofitManager
|
||
.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 updateUserGhzs() {
|
||
String version_code = sp.getString("version_code", null);
|
||
String version_name = sp.getString("version_name", null);
|
||
String channel = sp.getString("channel", null);
|
||
|
||
final String versionCode = PackageUtils.getVersionCode(MainActivity.this);
|
||
final String versionName = PackageUtils.getVersionName(MainActivity.this);
|
||
final String packageChannel = AppController.getInstance().getChannel();
|
||
if ((TextUtils.isEmpty(version_code) || TextUtils.isEmpty(version_name) || TextUtils.isEmpty(channel))
|
||
|| (!version_code.equals(versionCode) || !version_name.equals(versionName) || !channel.equals(packageChannel))) {
|
||
TokenUtils.getToken(MainActivity.this, true)
|
||
.flatMap(new Func1<String, Observable<ResponseBody>>() {
|
||
@Override
|
||
public Observable<ResponseBody> call(String token) {
|
||
Map<String, String> params = new ArrayMap<>();
|
||
params.put("version_code", versionCode);
|
||
params.put("version_name", versionName);
|
||
params.put("channel", packageChannel);
|
||
RequestBody body = RequestBody.create(MediaType.parse("application/json"),
|
||
new JSONObject(params).toString());
|
||
return RetrofitManager.getUser().postGhzs(token, body);
|
||
}
|
||
}).subscribeOn(Schedulers.io())
|
||
.observeOn(Schedulers.io())
|
||
.subscribe(new Response<ResponseBody>() {
|
||
@Override
|
||
public void onResponse(ResponseBody response) {
|
||
SharedPreferences.Editor editor = sp.edit();
|
||
editor.putString("version_code", versionCode);
|
||
editor.putString("version_name", versionName);
|
||
editor.putString("channel", packageChannel);
|
||
editor.apply();
|
||
}
|
||
});
|
||
}
|
||
}
|
||
|
||
// 初始化关注
|
||
private void initConcern() {
|
||
String versionName = PackageUtils.getVersionName(MainActivity.this);
|
||
if ("2.3".equals(versionName) && !sp.getBoolean("isSwitchConcern", false)) {
|
||
// 针对助手2.3版本关注数据的迁移
|
||
RetrofitManager.getApi().getConcern(TokenUtils.getDeviceId(MainActivity.this))
|
||
.subscribeOn(Schedulers.io())
|
||
.observeOn(AndroidSchedulers.mainThread())
|
||
.subscribe(new Response<List<String>>() {
|
||
@Override
|
||
public void onResponse(final List<String> response) {
|
||
if (response.size() != 0) {
|
||
TokenUtils.getToken(MainActivity.this, true)
|
||
.flatMap(new Func1<String, Observable<ResponseBody>>() {
|
||
@Override
|
||
public Observable<ResponseBody> call(String token) {
|
||
RequestBody body = RequestBody.create(MediaType.parse("application/json"),
|
||
new JSONArray(response).toString());
|
||
return RetrofitManager.getUser().putConcern(token, body);
|
||
}
|
||
}).subscribeOn(Schedulers.io())
|
||
.observeOn(Schedulers.io())
|
||
.subscribe(new Response<ResponseBody>());
|
||
}
|
||
sp.edit().putBoolean("isSwitchConcern", true).apply();
|
||
getConcernDigest(response);
|
||
}
|
||
});
|
||
} else {
|
||
TokenUtils.getToken(MainActivity.this, true)
|
||
.flatMap(new Func1<String, Observable<List<String>>>() {
|
||
@Override
|
||
public Observable<List<String>> call(String token) {
|
||
return RetrofitManager.getUser().getConcern(token);
|
||
}
|
||
}).subscribeOn(Schedulers.io())
|
||
.observeOn(Schedulers.io())
|
||
.subscribe(new Response<List<String>>() {
|
||
@Override
|
||
public void onResponse(List<String> response) {
|
||
getConcernDigest(response);
|
||
}
|
||
});
|
||
}
|
||
}
|
||
|
||
private void getConcernDigest(final List<String> arrGameId) {
|
||
final ArrayList<GameEntity> concernDigest = new ArrayList<>();
|
||
|
||
if (arrGameId.size() == 0) {
|
||
EventBus.getDefault().post(new EBUISwitch("NewsWrapperFragment", -2)); // 没有关注
|
||
}
|
||
|
||
List<Observable<GameEntity>> list = new ArrayList<>();
|
||
for (String gameId : arrGameId) {
|
||
list.add(RetrofitManager.getApi().getGameDigest(gameId));
|
||
}
|
||
Observable.merge(list)
|
||
.subscribeOn(Schedulers.io())
|
||
.observeOn(AndroidSchedulers.mainThread())
|
||
.subscribe(new Response<GameEntity>() {
|
||
@Override
|
||
public void onCompleted() {
|
||
concernManager.addByList(concernDigest);
|
||
} @Override
|
||
public void onNext(GameEntity response) {
|
||
concernDigest.add(response);
|
||
}
|
||
|
||
|
||
});
|
||
}
|
||
|
||
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, 0x123);
|
||
}
|
||
}, 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")) {
|
||
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(EBPackage busFour) {
|
||
final String packageName = busFour.getPackageName();
|
||
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())) {
|
||
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.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);
|
||
concernGame(gameDigestEntity.getId(), packageName);
|
||
if (PackageUtils.getMetaData(getApplicationContext(),
|
||
packageName, "gh_version") != null) {
|
||
checkGameUpdate(packageName);
|
||
}
|
||
}
|
||
// 更新已安装游戏
|
||
TokenUtils.getToken(MainActivity.this, true)
|
||
.flatMap(new Func1<String, Observable<ResponseBody>>() {
|
||
@Override
|
||
public Observable<ResponseBody> call(String token) {
|
||
JSONArray params = new JSONArray();
|
||
params.put(packageName);
|
||
RequestBody body = RequestBody.create(
|
||
MediaType.parse("application/json"), params.toString());
|
||
return RetrofitManager.getUser().postPackage(token, body);
|
||
}
|
||
}).subscribeOn(Schedulers.io())
|
||
.observeOn(Schedulers.io())
|
||
.subscribe(new Response<ResponseBody>());
|
||
}
|
||
});
|
||
}
|
||
|
||
if ("卸载".equals(busFour.getType())) {
|
||
// 更新已安装游戏
|
||
TokenUtils.getToken(MainActivity.this, true)
|
||
.flatMap(new Func1<String, Observable<ResponseBody>>() {
|
||
@Override
|
||
public Observable<ResponseBody> call(String token) {
|
||
return RetrofitManager.getUser().deletePackage(token, 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.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 (!concernManager.isConcern(id)) {
|
||
concernManager.addByEntity(response);
|
||
}
|
||
// 检查是否能插件化
|
||
if (response.getTag() != null && response.getTag().size() != 0
|
||
&& response.getApk() != null) {
|
||
for (ApkEntity apkEntity : response.getApk()) {
|
||
if (apkEntity.getPackageName().equals(packageName)
|
||
&& !TextUtils.isEmpty(apkEntity.getGhVersion())
|
||
&& !PackageUtils.isSignature(getApplicationContext(), apkEntity.getPackageName())) {
|
||
PackageManager.addUpdate(GameUtils.getGameUpdateEntity(response, apkEntity));
|
||
EventBus.getDefault().post(new EBDownloadStatus("plugin"));
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
// 提交默认头像
|
||
private void postDefaultIcon(boolean isCheck, final int i, final int count) {
|
||
TokenUtils.getToken(this, isCheck)
|
||
.flatMap(new Func1<String, Observable<ResponseBody>>() {
|
||
@Override
|
||
public Observable<ResponseBody> call(String token) {
|
||
Map<String, String> params = new HashMap<>();
|
||
params.put("icon_key", "icon" + i);
|
||
RequestBody body = RequestBody.create(MediaType.parse("application/json"),
|
||
new JSONObject(params).toString());
|
||
return RetrofitManager.getUser().postDefaultIcon(token, body);
|
||
}
|
||
})
|
||
.subscribeOn(Schedulers.io())
|
||
.observeOn(AndroidSchedulers.mainThread())
|
||
.subscribe(new JSONObjectResponse() {
|
||
@Override
|
||
public void onResponse(JSONObject response) {
|
||
sp.edit().putBoolean("changeDefaultIcon", false).apply();
|
||
}
|
||
|
||
@Override
|
||
public void onFailure(HttpException e) {
|
||
if (e != null) {
|
||
if (e.code() == 401) {
|
||
postDefaultIcon(true, i, count - 1);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
);
|
||
}
|
||
|
||
}
|