package com.gh.gamecenter; import android.app.ActionBar; import android.app.ActionBar.LayoutParams; import android.content.*; import android.content.SharedPreferences.Editor; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.support.v4.content.ContextCompat; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.*; import android.widget.*; import com.gh.base.BaseActivity; import com.gh.common.constant.Config; import com.gh.common.util.*; import com.gh.download.DownloadManager; import com.gh.download.DownloadService; import com.gh.gamecenter.eventbus.EBReuse; import com.gh.gamecenter.manager.DataCollectionManager; import com.gh.gamecenter.manager.FilterManager; import com.gh.gamecenter.retrofit.JSONObjectResponse; import com.gh.gamecenter.retrofit.RetrofitManager; import com.readystatesoftware.systembartint.SystemBarTintManager; import de.greenrobot.event.EventBus; import org.json.JSONException; import org.json.JSONObject; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; import java.io.File; import java.text.SimpleDateFormat; import java.util.*; import static com.gh.common.util.EntranceUtils.KEY_DATA; /** * 引导页面 */ public class SplashScreenActivity extends BaseActivity { private SharedPreferences sp; private long start; private boolean isFirst; private boolean isNewFirstLaunch; 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()]); } @Override protected void onCreate(Bundle savedInstanceState) { sp = getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE); isNewFirstLaunch = sp.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 || sp.getInt("actionbar_height", 0) != 0) { setTheme(R.style.AppGuideTheme); } else { setTheme(R.style.AppFullScreenTheme); // 自定义ActionBar ActionBar actionBar = getActionBar(); if (actionBar != null) { actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); RelativeLayout relativeLayout = new RelativeLayout(this); relativeLayout.setBackgroundResource(R.color.theme); LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); actionBar.setCustomView(relativeLayout, params);// 自定义ActionBar布局 } } if (isNewFirstLaunch) { ViewPager guideLayout = (ViewPager) findViewById(R.id.splash_intro_vp_guide); guideLayout.setAdapter(new GuidePagerAdapter()); } 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) { launch(); } else { return true; } } return super.onKeyDown(keyCode, event); } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus && isFirst) { isFirst = false; // 第一次启动,把package.txt文件内容加载进数据库 FilterManager filterManager = new FilterManager(getApplicationContext()); if (!sp.getBoolean("isLoadFilterV2d4", false)) { filterManager.loadFilter(); } // 获取过滤包 filterManager.getFilterFromServer(0); // 检查是否存在旧版本光环助手包 checkOldGhFile(); // 更新过滤表,获取自动刷新的cd,获取版本对应表 String time = sp.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 (sp.getBoolean("isCheckShow", true)) { getDownloadStatus(); } // 获取界面设置 getUISetting(); // 更新本地时间 TokenUtils.getTime(this); // 上传数据 DataCollectionManager.getInstance(getApplicationContext()).upload(); // 解决助手奔溃后导致的下载状态保留问题 DownloadManager.getInstance(this).checkAll(); // 开启下载服务 startService(new Intent(this, DownloadService.class)); // 不是第一次启动 if (!isNewFirstLaunch) { int height = sp.getInt("actionbar_height", 0); if (height == 0) { final ActionBar actionBar = getActionBar(); if (actionBar != null) { sp.edit().putInt("actionbar_height", actionBar.getHeight()).apply(); } } long end = System.currentTimeMillis() - start; if (end < 3000) { getWindow().getDecorView().postDelayed(new Runnable() { @Override public void run() { launch(); } }, 3000 - end); } else { launch(); } } } } // 检查下载文件夹下是否有旧版本的光环助手的包,有则删除 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 getUISetting() { RetrofitManager.getApi().getUISetting() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new JSONObjectResponse() { @Override public void onResponse(JSONObject response) { if (response.length() != 0) { try { Editor editor = sp.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 void getDownloadStatus() { String TD_CHANNEL_ID = (String) PackageUtils.getMetaData(this, getPackageName(), "TD_CHANNEL_ID"); RetrofitManager.getApi().getDownloadStatus(PackageUtils.getVersionName(getApplicationContext()), TD_CHANNEL_ID) .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 = sp.edit(); editor.putBoolean("isShow", true); editor.putBoolean("isCheckShow", false); editor.apply(); } else { sp.edit().putBoolean("isShow", false).apply(); } EventBus.getDefault().post(new EBReuse("Refresh")); } catch (JSONException e) { e.printStackTrace(); } } } }); } // 跳转到主界面 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(); } 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) { launch(); } }); } container.addView(view); return view; } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } } }