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

330 lines
12 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.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.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.util.DialogUtils;
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.common.util.TokenUtils;
import com.gh.download.DownloadManager;
import com.gh.gamecenter.manager.FilterManager;
import com.gh.gamecenter.room.AppDatabase;
import com.gh.gamecenter.user.UserRepository;
import com.lightgame.download.FileUtils;
import com.lightgame.utils.AppManager;
import com.lightgame.utils.Utils;
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(getApplicationContext()), true);
super.onCreate(savedInstanceState);
UserRepository.getInstance(AppDatabase.getInstance(getApplication()), 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);
} else {
setTheme(R.style.AppFullScreenTheme);
}
if (isNewFirstLaunch) {
mContentView.setPadding(0, 0, 0, 0);
ViewPager guideLayout = (ViewPager) 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() {
Bundle bundle = getIntent().getExtras();
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
if (bundle != null) intent.putExtras(bundle);
startActivity(intent);
finish();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus && isFirst) {
isFirst = false;
// 第一次启动把package.txt文件内容加载进数据库
FilterManager filterManager = new FilterManager(getApplicationContext());
if (!mSharedPreferences.getBoolean("isLoadFilterV2d4", false)) {
filterManager.loadFilter();
}
// 获取过滤包
filterManager.getFilterFromServer(0);
// 检查是否存在旧版本光环助手包
checkOldGhFile();
// 更新过滤表获取自动刷新的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();
}
// 更新本地时间
TokenUtils.getTime(this);
// // 上传数据---基于上报信息涉及用户权限 防止由于未授予权限导致的闪退 移动MainActivity上报
// DataCollectionManager.getInstance(getApplicationContext()).upload();
// 解决助手奔溃后导致的下载状态保留问题
DownloadManager.getInstance(this).checkAll();
// 检查权限
if (EasyPermissions.hasPermissions(this, mPermissions)) {
if (!isNewFirstLaunch) {
long end = System.currentTimeMillis() - start;
if (end < 2000) {
getWindow().getDecorView().postDelayed(new Runnable() {
@Override
public void run() {
launch();
}
}, 2000 - end);
} else {
launch();
}
}
} else {
checkAndRequestPermission();
}
}
}
@AfterPermissionGranted(REQUEST_PERMISSION_TAG)
private void checkAndRequestPermission() {
if (EasyPermissions.hasPermissions(this, mPermissions)) {
launch();
} 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请点击\"设置\"-\"应用\"-\"光环助手\"-\"权限\"-打开所需权限", "去设置", "退出",
new DialogUtils.ConfirmListener() {
@Override
public void onConfirm() {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
}
}, new DialogUtils.CancelListener() {
@Override
public void onCancel() {
finish();
}
});
} else {
DialogUtils.showPermissionDialog(this, "重要提醒",
"请授予光环助手权限,否则将无法使用", "重试", "退出",
new DialogUtils.ConfirmListener() {
@Override
public void onConfirm() {
checkAndRequestPermission();
}
}, new DialogUtils.CancelListener() {
@Override
public void onCancel() {
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) {
float version = Float.valueOf(name.substring(name.indexOf("V") + 1, index));
float currentVersion = Float.valueOf(PackageUtils.getVersionName(getApplicationContext()));
if (version <= currentVersion && file.delete()) {
Utils.log(file.getName() + " file delete success.");
}
}
}
}
}
}
private class GuidePagerAdapter extends PagerAdapter {
private int[] mPics = {R.drawable.splash_01};
@Override
public int getCount() {
return mPics.length;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = View.inflate(container.getContext(), R.layout.splash_guide_item, null);
ImageView ivImage = (ImageView) view.findViewById(R.id.splsh_guide_iv_image);
ivImage.setImageResource(mPics[position]);
if (position == mPics.length - 1) {
TextView tvSkip = (TextView) view.findViewById(R.id.splsh_guide_tv_skip);
tvSkip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (EasyPermissions.hasPermissions(SplashScreenActivity.this, mPermissions)) {
launch();
}
}
});
}
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
}