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

386 lines
15 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.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.g00fy2.versioncompare.Version;
import com.gh.base.BaseActivity;
import com.gh.common.AppExecutor;
import com.gh.common.constant.Config;
import com.gh.common.constant.Constants;
import com.gh.common.util.DataUtils;
import com.gh.common.util.DeviceTokenUtils;
import com.gh.common.util.DeviceUtils;
import com.gh.common.util.DialogUtils;
import com.gh.common.util.EmptyCallback;
import com.gh.common.util.GameSubstituteRepositoryHelper;
import com.gh.common.util.GdtHelper;
import com.gh.common.util.GsonUtils;
import com.gh.common.util.MtaHelper;
import com.gh.common.util.PackageUtils;
import com.gh.common.util.PlatformUtils;
import com.gh.common.util.SPUtils;
import com.gh.common.util.TagUtils;
import com.gh.common.util.UsageStatsHelper;
import com.gh.download.DownloadManager;
import com.gh.gamecenter.entity.AuthDialogEntity;
import com.gh.gamecenter.entity.PrivacyPolicyEntity;
import com.gh.gamecenter.manager.FilterManager;
import com.gh.gamecenter.retrofit.BiResponse;
import com.gh.gamecenter.retrofit.Response;
import com.gh.gamecenter.retrofit.RetrofitManager;
import com.halo.assistant.HaloApp;
import com.lightgame.download.DownloadEntity;
import com.lightgame.download.FileUtils;
import com.qq.gdt.action.ActionType;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;
/**
* 引导页面
*/
public class SplashScreenActivity extends BaseActivity {
private SharedPreferences mSharedPreferences;
private boolean isNewFirstLaunch;
private static final int REQUEST_PERMISSION_TAG = 30001;
private static final String SP_BRAND_NEW_USER = "brand_new_user"; // 用于标记是否为新用户,应用更新再打开的不算是新用户
private String[] mPermissions = {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.READ_PHONE_STATE};
public static Intent getSplashScreenIntent(Context context, Bundle bundle) {
Intent intent = new Intent(context, SplashScreenActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtras(bundle);
return intent;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
isNewFirstLaunch = mSharedPreferences.getBoolean("isNewFirstLaunchV" + PackageUtils.getVersionName(), true);
super.onCreate(savedInstanceState);
// 处理助手已经在后台运行导致的再次启动助手
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
finish();
return;
}
// 判断是不是这个版本的新用户
if (isNewFirstLaunch) {
// 删除更新后的光环助手包
List<DownloadEntity> all = DownloadManager.getInstance(this).getAll();
for (DownloadEntity downloadEntity : all) {
if (downloadEntity.getPackageName().equals(getPackageName())) {
DownloadManager.getInstance(this).cancel(downloadEntity.getUrl(), true, true);
break;
}
}
mContentView.setPadding(0, 0, 0, 0);
ViewPager guideLayout = findViewById(R.id.splash_intro_vp_guide);
guideLayout.setAdapter(new GuidePagerAdapter());
// 判断是不是光环的新用户
if (SPUtils.getBoolean(SP_BRAND_NEW_USER, true)) {
SPUtils.setLong(Constants.SP_INITIAL_USAGE_TIME, System.currentTimeMillis());
HaloApp.getInstance().isBrandNewInstall = true;
showPrivacyPolicy(() -> {
// Dialog dismiss 后的回调
guideLayout.setVisibility(View.VISIBLE);
SPUtils.setBoolean(SP_BRAND_NEW_USER, false);
requestPermission();
});
} else {
guideLayout.setVisibility(View.VISIBLE);
requestPermission();
}
} else {
launchMainActivity();
}
AppExecutor.getIoExecutor().execute(() -> {
Config.getGhzsSettings();
deviceDialogSetting();
getFilterDetailTags();
getAuthDialog();
UsageStatsHelper.checkAndPostUsageStats();
GameSubstituteRepositoryHelper.updateGameSubstituteRepository();
// 第一次启动把package.txt文件内容加载进数据库
FilterManager filterManager = new FilterManager(getApplicationContext());
if (!mSharedPreferences.getBoolean("isLoadFilterV2d4", false)) {
filterManager.loadFilter();
}
// 获取过滤包
filterManager.getFilterFromServer(0);
// 更新过滤表获取自动刷新的cd获取版本对应表
String time = mSharedPreferences.getString("refresh_time", null);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
String today = format.format(new Date());
if (!today.equals(time)) {
// 获取版本代码、名称
PlatformUtils.getInstance(getApplicationContext()).getPlatform();
TagUtils.getInstance(getApplicationContext()).getTag();
}
// 更新本地时间
DeviceTokenUtils.syncServerTime(this);
});
}
private void requestPermission() {
if (EasyPermissions.hasPermissions(this, mPermissions)) {
GdtHelper.INSTANCE.logAction(ActionType.START_APP, GdtHelper.NETWORK_TYPE, DeviceUtils.getNetwork(this));
} else {
checkAndRequestPermission();
}
}
@SuppressLint("CheckResult")
private void deviceDialogSetting() {
RetrofitManager.getInstance(HaloApp.getInstance().getApplication())
.getApi().deviceDialogs()
.subscribe((entities, throwable) -> {
if (entities.size() > 0) {
SPUtils.setString(Constants.SP_DEVICE_REMIND, GsonUtils.toJson(entities));
}
});
}
private void getFilterDetailTags() {
RetrofitManager.getInstance(HaloApp.getInstance().getApplication())
.getApi().getFilterDetailTags()
.subscribe(new Response<ArrayList<String>>() {
@Override
public void onResponse(@Nullable ArrayList<String> response) {
super.onResponse(response);
SPUtils.setString(Constants.SP_FILTER_TAGS, GsonUtils.toJson(response));
}
});
}
private void getAuthDialog() {
RetrofitManager.getInstance(HaloApp.getInstance().getApplication())
.getApi()
.authDialog()
.subscribe(new Response<List<AuthDialogEntity>>() {
@Override
public void onResponse(@Nullable List<AuthDialogEntity> response) {
super.onResponse(response);
SPUtils.setString(Constants.SP_AUTH_DIALOG, GsonUtils.toJson(response));
}
});
}
@SuppressLint("CheckResult")
private void showPrivacyPolicy(EmptyCallback callback) {
RetrofitManager.getInstance(this).getApi()
.getPrivacyPolicy()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new BiResponse<PrivacyPolicyEntity>() {
@Override
public void onSuccess(PrivacyPolicyEntity data) {
DialogUtils.showPrivacyPolicyDialog(
SplashScreenActivity.this,
data, callback);
}
@Override
public void onFailure(@NotNull Exception exception) {
DialogUtils.showPrivacyPolicyDialog(
SplashScreenActivity.this,
PrivacyPolicyEntity.createDefaultData(), callback);
}
});
}
@Override
protected int getLayoutId() {
if (isNewFirstLaunch) {
return R.layout.activity_splash_intro;
} else {
return 0;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
if (isNewFirstLaunch && EasyPermissions.hasPermissions(this, mPermissions)) {
launchMainActivity();
} else {
return true;
}
}
return super.onKeyDown(keyCode, event);
}
@Override
protected boolean useEventBus() {
return false;
}
@Override
protected boolean useButterKnife() {
return false;
}
// 跳转到主界面
private void launchMainActivity() {
getUniqueId();
Bundle bundle = getIntent().getExtras();
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
if (bundle != null) intent.putExtras(bundle);
startActivity(intent);
finish();
}
private void getUniqueId() {
DataUtils.getGid();
}
@AfterPermissionGranted(REQUEST_PERMISSION_TAG)
private void checkAndRequestPermission() {
if (EasyPermissions.hasPermissions(this, mPermissions)) {
MtaHelper.onEvent("授权情况", "启动授权", "都授权");
// 检查是否有旧版本光环,有就删掉
AppExecutor.getIoExecutor().execute(this::deleteOutdatedUpdatePackage);
} else {
ActivityCompat.requestPermissions(this, mPermissions, REQUEST_PERMISSION_TAG);
}
}
@Override
public void onPermissionsDenied(int requestCode, List<String> perms) {
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) { // 设置"不要再询问",必须手动到系统设置授权
DialogUtils.showPermissionDialog(this, "权限申请",
"光环助手需要获取(存储空间权限)和(手机信息),以保证游戏的正常下载以及您的账号安全", "去设置", "放弃",
() -> {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
}, () -> logGrantedPermission(perms));
} else {
DialogUtils.showPermissionDialog(this, "权限申请",
"在设置-应用-光环助手-权限中开启存储和手机信息权限,以保证能正常使用相关功能", "重试", "放弃",
this::checkAndRequestPermission,
() -> logGrantedPermission(perms));
}
}
private void logGrantedPermission(List<String> perms) {
if (perms.size() == 1) {
MtaHelper.onEvent("授权情况", "启动授权", "只授权存储");
AppExecutor.getIoExecutor().execute(this::deleteOutdatedUpdatePackage);
} else {
if (perms.contains(Manifest.permission.READ_PHONE_STATE)) {
MtaHelper.onEvent("授权情况", "启动授权", "都不授权");
} else {
MtaHelper.onEvent("授权情况", "启动授权", "只授权IMEI");
}
}
}
// 检查下载文件夹下是否有旧版本的光环助手的包,有则删除
@SuppressWarnings("ResultOfMethodCallIgnored")
private void deleteOutdatedUpdatePackage() {
File folder = new File(FileUtils.getDownloadDir(this) + File.separator);
if (folder.isDirectory()) {
for (File file : folder.listFiles()) {
if (!file.isDirectory() && file.getName().startsWith("光环助手V")) {
String name = file.getName();
int index = name.indexOf("_");
if (index != -1) {
try {
String versionString = name.substring(name.indexOf("V") + 1, index);
Version currentVersion = new Version(PackageUtils.getVersionName());
if (currentVersion.isHigherThan(versionString) || currentVersion.isEqual(versionString)) {
file.delete();
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
}
}
}
private class GuidePagerAdapter extends PagerAdapter {
private int[] mPics = {R.drawable.splash_01};
@Override
public int getCount() {
return mPics.length;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
View view = View.inflate(container.getContext(), R.layout.splash_guide_item, null);
ImageView ivImage = view.findViewById(R.id.splsh_guide_iv_image);
ivImage.setImageResource(mPics[position]);
if (position == mPics.length - 1) {
TextView tvSkip = view.findViewById(R.id.splsh_guide_tv_skip);
tvSkip.setOnClickListener(v -> launchMainActivity());
}
container.addView(view);
return view;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
}
}