352 lines
13 KiB
Java
352 lines
13 KiB
Java
package com.gh.gamecenter;
|
||
|
||
import android.Manifest;
|
||
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.support.annotation.NonNull;
|
||
import android.support.v4.app.ActivityCompat;
|
||
import android.support.v4.content.ContextCompat;
|
||
import android.support.v4.view.PagerAdapter;
|
||
import android.support.v4.view.ViewPager;
|
||
import android.view.KeyEvent;
|
||
import android.view.View;
|
||
import android.view.ViewGroup;
|
||
import android.widget.ImageView;
|
||
import android.widget.TextView;
|
||
|
||
import com.gh.base.BaseActivity;
|
||
import com.gh.common.constant.Constants;
|
||
import com.gh.common.exposure.meta.MetaUtil;
|
||
import com.gh.common.util.DeviceTokenUtils;
|
||
import com.gh.common.util.DeviceUtils;
|
||
import com.gh.common.util.DialogUtils;
|
||
import com.gh.common.util.GameRepositoryHelper;
|
||
import com.gh.common.util.GdtHelper;
|
||
import com.gh.common.util.PackageUtils;
|
||
import com.gh.common.util.PlatformUtils;
|
||
import com.gh.common.util.TagUtils;
|
||
import com.gh.common.util.TimestampUtils;
|
||
import com.gh.download.DownloadManager;
|
||
import com.gh.gamecenter.manager.FilterManager;
|
||
import com.gh.gamecenter.user.UserRepository;
|
||
import com.gh.gid.GidCallback;
|
||
import com.gh.gid.GidHelper;
|
||
import com.halo.assistant.HaloApp;
|
||
import com.lightgame.download.DownloadEntity;
|
||
import com.lightgame.download.FileUtils;
|
||
import com.lightgame.utils.AppManager;
|
||
import com.lightgame.utils.Utils;
|
||
import com.qq.gdt.action.ActionType;
|
||
|
||
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 pub.devrel.easypermissions.AfterPermissionGranted;
|
||
import pub.devrel.easypermissions.EasyPermissions;
|
||
|
||
/**
|
||
* 引导页面
|
||
*/
|
||
public class SplashScreenActivity extends BaseActivity {
|
||
|
||
private SharedPreferences mSharedPreferences;
|
||
|
||
private long start;
|
||
|
||
private boolean isFirst;
|
||
private boolean isNewFirstLaunch;
|
||
|
||
private final static int REQUEST_PERMISSION_TAG = 30001;
|
||
private String[] mPermissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,
|
||
Manifest.permission.READ_PHONE_STATE};
|
||
|
||
public static Intent[] getRedirectIntents(Context context, Uri uri) {
|
||
List<Intent> intentList = new ArrayList<>();
|
||
if (AppManager.getInstance().isEmpty()) {
|
||
intentList.add(BaseActivity.getReorderToFrontIntent(context, SplashScreenActivity.class));
|
||
intentList.add(BaseActivity.getReorderToFrontIntent(context, MainActivity.class));
|
||
}
|
||
|
||
// if (uri != null && !TextUtils.isEmpty(uri.toString())) {
|
||
// intentList.add(NavigationActivity.getNavigateUriIntent(context, uri));
|
||
// }
|
||
|
||
return intentList.toArray(new Intent[intentList.size()]);
|
||
}
|
||
|
||
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);
|
||
|
||
UserRepository.getInstance(getApplication()); // 初始化登录
|
||
|
||
// if (!AppManager.getInstance().isEmpty()) {
|
||
// redirectAndFinish(getRedirectIntents(this, getIntent().getData()));
|
||
// return;
|
||
// }
|
||
|
||
// 处理助手已经在后台运行导致的再次启动助手
|
||
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
|
||
finish();
|
||
return;
|
||
}
|
||
|
||
start = System.currentTimeMillis();
|
||
|
||
TimestampUtils.initMap();
|
||
|
||
isFirst = true;
|
||
|
||
if (isNewFirstLaunch) {
|
||
setTheme(R.style.AppGuideTheme);
|
||
|
||
// 删除更新后的光环助手包
|
||
List<DownloadEntity> all = DownloadManager.getInstance(this).getAll();
|
||
for (DownloadEntity downloadEntity : all) {
|
||
if (downloadEntity.getPackageName().equals(getPackageName())) {
|
||
DownloadManager.getInstance(this).cancel(downloadEntity.getUrl(), true);
|
||
break;
|
||
}
|
||
}
|
||
} else {
|
||
setTheme(R.style.AppFullScreenTheme);
|
||
}
|
||
|
||
if (isNewFirstLaunch) {
|
||
mContentView.setPadding(0, 0, 0, 0);
|
||
ViewPager guideLayout = findViewById(R.id.splash_intro_vp_guide);
|
||
guideLayout.setAdapter(new GuidePagerAdapter());
|
||
}
|
||
|
||
}
|
||
|
||
private void redirectAndFinish(final Intent... intent) {
|
||
if (intent != null && intent.length > 0) {
|
||
ContextCompat.startActivities(this, intent);
|
||
}
|
||
finish();
|
||
}
|
||
|
||
@Override
|
||
protected int getLayoutId() {
|
||
|
||
final int layoutId;
|
||
if (isNewFirstLaunch) {
|
||
layoutId = R.layout.activity_splash_intro;
|
||
} else {
|
||
layoutId = R.layout.activity_splash_normal;
|
||
}
|
||
|
||
return layoutId;
|
||
}
|
||
|
||
@Override
|
||
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
||
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
|
||
if (isNewFirstLaunch && EasyPermissions.hasPermissions(this, mPermissions)) {
|
||
launch();
|
||
} else {
|
||
return true;
|
||
}
|
||
}
|
||
return super.onKeyDown(keyCode, event);
|
||
}
|
||
|
||
// 跳转到主界面
|
||
private void launch() {
|
||
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() {
|
||
GidHelper.getInstance().registerDevice(new GidCallback() {
|
||
@Override
|
||
public void onSuccess(String s) {
|
||
Utils.log("Gid", s);
|
||
mSharedPreferences.edit().putString(Constants.DEVICE_KEY, s).apply();
|
||
|
||
HaloApp.getInstance().setGid(s);
|
||
|
||
// 避免初始化顺序问题导致 MetaUtil 一直持有空的 gid
|
||
MetaUtil.INSTANCE.refreshMeta();
|
||
}
|
||
|
||
@Override
|
||
public void onFailure(String s) {
|
||
Utils.log("Gid", s);
|
||
}
|
||
});
|
||
}
|
||
|
||
@Override
|
||
public void onWindowFocusChanged(boolean hasFocus) {
|
||
super.onWindowFocusChanged(hasFocus);
|
||
if (hasFocus && isFirst) {
|
||
isFirst = false;
|
||
|
||
GameRepositoryHelper.getGameRepository(this);
|
||
|
||
HaloApp.getInstance().getMainExecutor().execute(() -> {
|
||
// 第一次启动,把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);
|
||
|
||
// 解决助手奔溃后导致的下载状态保留问题
|
||
DownloadManager.getInstance(this).checkAll();
|
||
});
|
||
|
||
// 检查权限
|
||
if (EasyPermissions.hasPermissions(this, mPermissions)) {
|
||
GdtHelper.INSTANCE.logAction(ActionType.START_APP,
|
||
GdtHelper.NETWORK_TYPE, DeviceUtils.getNetwork(this));
|
||
if (!isNewFirstLaunch) {
|
||
long end = System.currentTimeMillis() - start;
|
||
if (end < 2000) {
|
||
getWindow().getDecorView().postDelayed(this::launch, 2000 - end);
|
||
} else {
|
||
launch();
|
||
}
|
||
}
|
||
} else {
|
||
checkAndRequestPermission();
|
||
}
|
||
}
|
||
}
|
||
|
||
@AfterPermissionGranted(REQUEST_PERMISSION_TAG)
|
||
private void checkAndRequestPermission() {
|
||
if (EasyPermissions.hasPermissions(this, mPermissions)) {
|
||
launch();
|
||
// 检查是否有旧版本光环,有就删掉
|
||
HaloApp.getInstance().getMainExecutor().execute(this::checkOldGhFile);
|
||
} 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, "重要提醒",
|
||
"请授予光环助手权限,否则将无法使用\n请点击\"设置\"-\"应用\"-\"光环助手\"-\"权限\"-打开所需权限", "去设置", "退出",
|
||
() -> {
|
||
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
||
intent.setData(Uri.parse("package:" + getPackageName()));
|
||
startActivity(intent);
|
||
}, this::finish);
|
||
} else {
|
||
DialogUtils.showPermissionDialog(this, "重要提醒",
|
||
"请授予光环助手权限,否则将无法使用", "重试", "退出",
|
||
this::checkAndRequestPermission, this::finish);
|
||
}
|
||
}
|
||
|
||
// 检查下载文件夹下是否有旧版本的光环助手的包,有则删除
|
||
private void checkOldGhFile() {
|
||
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 {
|
||
float version = Float.valueOf(name.substring(name.indexOf("V") + 1, index));
|
||
float currentVersion = Float.valueOf(PackageUtils.getVersionName());
|
||
if (version <= currentVersion && file.delete()) {
|
||
Utils.log(file.getName() + " file delete success.");
|
||
}
|
||
} catch (Exception ignore) {
|
||
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
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 -> {
|
||
if (EasyPermissions.hasPermissions(SplashScreenActivity.this, mPermissions)) {
|
||
launch();
|
||
}
|
||
});
|
||
|
||
}
|
||
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;
|
||
}
|
||
|
||
}
|
||
}
|