package com.gh.gamecenter; import android.app.ActionBar; import android.app.ActionBar.LayoutParams; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.os.Handler; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.util.DisplayMetrics; import android.view.KeyEvent; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import com.gh.base.BaseActivity; import com.gh.common.constant.Config; import com.gh.common.util.FileUtils; import com.gh.common.util.PackageUtils; import com.gh.common.util.PlatformUtils; import com.gh.common.util.TimestampUtils; import com.gh.common.util.TokenUtils; import com.gh.common.util.Utils; 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 org.json.JSONException; import org.json.JSONObject; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import de.greenrobot.event.EventBus; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; /** * 引导页面 */ public class SplashScreenActivity extends BaseActivity { private SharedPreferences sp; private long start; private boolean isFirst; private boolean isNewFirstLaunch; private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 处理助手已经在后台运行导致的再次启动助手 if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) { finish(); return; } start = System.currentTimeMillis(); TimestampUtils.initMap(); isFirst = true; sp = getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE); isNewFirstLaunch = sp.getBoolean("isNewFirstLaunchV" + PackageUtils.getVersion(getApplicationContext()), true); if (isNewFirstLaunch || sp.getInt("actionbar_height", 0) != 0) { setTheme(R.style.AppGuideTheme); } else { setTheme(R.style.AppFullScreenTheme); // 自定义ActionBar ActionBar mActionBar = getActionBar(); if (mActionBar != null) { mActionBar.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); mActionBar.setCustomView(relativeLayout, params);// 自定义ActionBar布局 } } if (isNewFirstLaunch) { setContentView(R.layout.activity_splash_intro); ViewPager guideLayout = (ViewPager) findViewById(R.id.splash_intro_vp_guide); guideLayout.setAdapter(new GuidePagerAdapter()); } else { setContentView(R.layout.activity_splash_normal); } } 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) { if (position == pics.length - 1) { 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]); TextView tvSkip = (TextView) view.findViewById(R.id.splsh_guide_tv_skip); DisplayMetrics outMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(outMetrics); RelativeLayout.LayoutParams rparams = (RelativeLayout.LayoutParams) tvSkip.getLayoutParams(); rparams.height = outMetrics.heightPixels / 4; tvSkip.setLayoutParams(rparams); tvSkip.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { launch(); } }); container.addView(view); return view; } else { ImageView imageView = new ImageView(container.getContext()); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setImageResource(pics[position]); ViewGroup.LayoutParams params = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); container.addView(imageView, params); return imageView; } } @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); } } @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 = null; if (!sp.getBoolean("isLoadFilter", false)) { filterManager = new FilterManager(getApplicationContext()); filterManager.loadFilter(); } // 检查是否存在旧版本光环助手包 checkOldGhFile(); // 更新过滤表,获取自动刷新的cd,获取版本对应表 String time = sp.getString("filter_time", null); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()); String today = format.format(new Date()); if (!today.equals(time)) { // 获取过滤包 if (filterManager == null) { filterManager = new FilterManager(getApplicationContext()); } filterManager.getFilterFromServer(today); // 获取版本代码、名称 PlatformUtils.getInstance(getApplicationContext()).getPlatform(); } // 获取下载按钮状态、开启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) { handler.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.getVersion(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.getVersion(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() { Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class); Bundle bundle = intent.getExtras(); if (intent.getBundleExtra("data") != null) { bundle = intent.getBundleExtra("data"); } intent.putExtra("data", bundle); startActivity(intent); finish(); } }