package com.gh.gamecenter; import android.Manifest; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; import android.provider.Settings; 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.eventbus.EBReuse; import com.gh.gamecenter.manager.FilterManager; import com.gh.gamecenter.retrofit.JSONObjectResponse; import com.gh.gamecenter.retrofit.RetrofitManager; import com.halo.assistant.HaloApp; import com.lightgame.download.FileUtils; import com.lightgame.utils.AppManager; import com.lightgame.utils.Utils; import com.readystatesoftware.systembartint.SystemBarTintManager; import org.greenrobot.eventbus.EventBus; import org.json.JSONException; import org.json.JSONObject; 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.EasyPermissions; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; import static com.gh.common.util.EntranceUtils.KEY_DATA; import static com.taobao.accs.ACCSManager.mContext; /** * 引导页面 */ public class SplashScreenActivity extends BaseActivity { private SharedPreferences mSharedPreferences; private long start; private boolean isFirst; private boolean isNewFirstLaunch; private final static int REQUEST_PERMISSION_TAG = 10012; private String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE}; public static Intent[] getRedirectIntents(Context context, Uri uri) { List 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.putExtra(KEY_DATA, bundle); return intent; } @Override protected void onCreate(Bundle savedInstanceState) { mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); isNewFirstLaunch = mSharedPreferences.getBoolean("isNewFirstLaunchV" + PackageUtils.getVersionName(getApplicationContext()), true); super.onCreate(savedInstanceState); // 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()); SystemBarTintManager tintManager = getTintManager(); if (tintManager != null) { tintManager.setTintAlpha(0); } } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { SystemBarTintManager tintManager = getTintManager(); if (tintManager != null) { tintManager.setStatusBarTintColor(ContextCompat.getColor(getApplicationContext(), R.color.theme)); } } } 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, permissions)) { launch(); } else { return true; } } return super.onKeyDown(keyCode, event); } // 跳转到主界面 private void launch() { Bundle bundle = getIntent().getExtras(); if (getIntent().getBundleExtra(KEY_DATA) != null) { bundle = getIntent().getBundleExtra(KEY_DATA); } Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class); intent.putExtra(KEY_DATA, 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(); } // 获取下载按钮状态、开启or关闭 if (mSharedPreferences.getBoolean("isCheckShow", true)) { getDownloadStatus(); } // 获取界面设置 getUISetting(); // 更新本地时间 TokenUtils.getTime(this); // // 上传数据 TODO 基于上报信息涉及用户权限 防止由于未授予权限导致的闪退 移动MainActivity上报 // DataCollectionManager.getInstance(getApplicationContext()).upload(); // 解决助手奔溃后导致的下载状态保留问题 DownloadManager.getInstance(this).checkAll(); checkPermissions(permissions, null); } } @Override protected void onPermissionsSuccess(String tag) { if (!isNewFirstLaunch) { long end = System.currentTimeMillis() - start; if (end < 3000) { getWindow().getDecorView().postDelayed(new Runnable() { @Override public void run() { launch(); } }, 3000 - end); } else { launch(); } } } @Override public void onPermissionsDenied(int requestCode, List perms) { if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) { // 设置"不要再询问",必须手动到系统设置授权 DialogUtils.showPermissionDialog(this, "重要提醒", "请授予光环助手权限,否则将无法使用\n请点击\"设置\"-\"应用\"-\"光环助手\"-\"权限\"-打开所需权限", "去设置", "退出", new DialogUtils.ConfirmListener() { @Override public void onConfirm() { // TODO 跳转至当前应用设置界面时,当前app自动退出 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() { checkPermissions(permissions, null); } }, 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 void getDownloadStatus() { String channel = HaloApp.getInstance().getChannel(); RetrofitManager.getInstance(mContext).getApi().getDownloadStatus( PackageUtils.getVersionName(getApplicationContext()), channel) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new JSONObjectResponse() { @Override public void onResponse(JSONObject response) { if (response.length() != 0) { try { String status = response.getString("status"); if ("on".equals(status)) { Editor editor = mSharedPreferences.edit(); editor.putBoolean("isShow", true); editor.putBoolean("isCheckShow", false); editor.apply(); } else { mSharedPreferences.edit().putBoolean("isShow", false).apply(); } EventBus.getDefault().post(new EBReuse("Refresh")); } catch (JSONException e) { e.printStackTrace(); } } } }); } // 获取界面设置 private void getUISetting() { RetrofitManager.getInstance(mContext).getApi().getUISetting() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new JSONObjectResponse() { @Override public void onResponse(JSONObject response) { if (response.length() != 0) { try { Editor editor = mSharedPreferences.edit(); editor.putInt("download_box_row", response.getJSONObject("download_box").getInt("row")); editor.putInt("download_box_column", response.getJSONObject("download_box").getInt("column")); editor.putInt("game_detail_news_type_tab_column", response.getJSONObject("game_detail_news_type_tab").getInt("column")); editor.apply(); } catch (JSONException e) { e.printStackTrace(); } } } }); } private class GuidePagerAdapter extends PagerAdapter { private int[] pics = {R.drawable.splash_01}; @Override public int getCount() { return pics.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(pics[position]); if (position == pics.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, permissions)) { 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; } } }