Files
assistant-android/app/src/main/java/com/gh/gamecenter/SplashScreenActivity.java
2016-09-19 13:07:47 +08:00

621 lines
19 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.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.support.v4.util.ArrayMap;
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.RelativeLayout;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.gh.base.AppController;
import com.gh.base.BaseActivity;
import com.gh.common.constant.Config;
import com.gh.common.constant.Constants;
import com.gh.common.util.FileUtils;
import com.gh.common.util.ImageUtils;
import com.gh.common.util.PackageUtils;
import com.gh.common.util.TimestampUtils;
import com.gh.common.util.TrafficUtils;
import com.gh.common.util.Utils;
import com.gh.gamecenter.db.info.ConcernInfo;
import com.gh.gamecenter.db.info.FilterInfo;
import com.gh.gamecenter.db.info.GameInfo;
import com.gh.gamecenter.entity.ApkEntity;
import com.gh.gamecenter.entity.GameDigestEntity;
import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.manager.ConcernManager;
import com.gh.gamecenter.manager.DataCollectionManager;
import com.gh.gamecenter.manager.FilterManager;
import com.gh.gamecenter.manager.PackageManager;
import com.gh.gamecenter.manager.SuspectedGameManager;
import com.gh.gamecenter.volley.extended.JsonArrayExtendedRequest;
import com.gh.gamecenter.volley.extended.JsonObjectExtendedRequest;
import com.gh.gamecenter.volley.extended.StringExtendedRequest;
import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
/**
* 引导页面
*
* @author 黄壮华
*
*/
public class SplashScreenActivity extends BaseActivity {
private ConcernManager concernManager;
private SharedPreferences sp;
private String from;
private boolean isFirst;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isFirst = true;
sp = getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
from = getIntent().getStringExtra("from");
if (sp.getBoolean("isNewFirstLaunch", true)
|| sp.getInt("actionbar_height", 0) != 0) {
setTheme(R.style.AppTheme_Guide);
} else {
setTheme(R.style.AppTheme_Fullscreen);
// 自定义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 (sp.getBoolean("isNewFirstLaunch", true)) {
setContentView(R.layout.activity_splash_intro);
ViewPager splash_viewPager = (ViewPager) findViewById(R.id.splash_viewPager);
splash_viewPager.setAdapter(new ViewPagerAdapter());
} else {
setContentView(R.layout.activity_splash_normal);
}
}
private class ViewPagerAdapter extends PagerAdapter {
private int[] pics = { R.drawable.splash_01, R.drawable.splash_02,
R.drawable.splash_03, R.drawable.splash_04 };
@Override
public int getCount() {
return pics.length;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(SplashScreenActivity.this);
ImageUtils.getInstance(SplashScreenActivity.this).
display("drawable://" + pics[position], imageView, -1);
if (position == pics.length - 1) {
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
launch();
}
});
}
container.addView(imageView);
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 (sp.getBoolean("isNewFirstLaunch", true)) {
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文件内容加载进数据库
if (!sp.getBoolean("isLoadFilter", false)) {
try {
List<FilterInfo> list = new ArrayList<>();
BufferedReader reader = new BufferedReader(
new InputStreamReader(getAssets().open("package.txt")));
String line;
while ((line = reader.readLine()) != null) {
list.add(new FilterInfo(line));
}
reader.close();
FilterManager filterManager = new FilterManager(getApplicationContext());
filterManager.addAllFilter(list);
sp.edit().putBoolean("isLoadFilter", true).apply();
} catch (IOException e1) {
e1.printStackTrace();
}
}
checkGhFile();
concernManager = new ConcernManager(getApplicationContext());
/*
* 更新过滤表获取自动刷新的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)) {
FilterManager manager = new FilterManager(getApplicationContext());
manager.getFilterFromServer(today);
getPlatform();
}
// 获取下载按钮状态、开启or关闭
getDownloadStatus();
getUISetting();
getTime();
/*
* 上传数据
*/
DataCollectionManager.getInstance(getApplicationContext()).upload();
/*
* 初始化已安装软件map获取游戏更新列表和已安装列表
*/
PackageManager manager = new PackageManager(getApplicationContext());
manager.getInstalledMapFromLocal();
getInstalledListFromServer(sp.getBoolean("isNewFirstLaunch", true));
}
}
/*
* 获取服务器时间
*/
private void getTime() {
StringExtendedRequest request = new StringExtendedRequest(
Request.Method.GET, Config.HOST + "v2d0/support/time/current",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Editor editor = sp.edit();
editor.putLong("server_time", Long.parseLong(response));
editor.putLong("client_time", System.currentTimeMillis() / 1000);
editor.apply();
}
}, null);
request.setShouldCache(false);
AppController.addToRequestQueue(request, SplashScreenActivity.class);
}
/*
* 获取界面设置
*/
private void getUISetting() {
JsonObjectExtendedRequest request = new JsonObjectExtendedRequest(
Config.HOST + "v1d45/support/setting/ui",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Utils.log(response.toString());
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();
}
}
}, null);
AppController.addToRequestQueue(request, SplashScreenActivity.class);
}
/*
* 获取下载按钮显示状态
*/
private void getDownloadStatus() {
String TD_CHANNEL_ID = (String) PackageUtils.getMetaData(this,
getPackageName(), "TD_CHANNEL_ID");
JsonObjectExtendedRequest request = new JsonObjectExtendedRequest(
Config.HOST + "v1d45/support/setting/download/" + TD_CHANNEL_ID
+ "/switch2?version="
+ PackageUtils.getVersion(getApplicationContext()),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
boolean isShow = response.getBoolean("status");
sp.edit().putBoolean("isShow", isShow).apply();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (!sp.getBoolean("isShow", false)) {
sp.edit().putBoolean("isShow", false).apply();
}
}
});
AppController.addToRequestQueue(request, SplashScreenActivity.class);
}
/*
* 检查下载文件夹下是否有旧版本的光环助手的包,有则删除
*/
private void checkGhFile() {
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();
}
}
}
}
}
}
private void getPlatform() {
JsonArrayExtendedRequest request = new JsonArrayExtendedRequest(
TimestampUtils.addTimestamp(
Config.HOST + "v1d45/support/setting/platform",
Constants.PLATFORM_CD),
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
Set<String> pset = new HashSet<>();
for (int i = 0; i < response.length(); i++) {
JSONObject jsonObject = response
.getJSONObject(i);
String code = jsonObject.getString("code");
String name = jsonObject.getString("name");
String pic = jsonObject.getString("pic");
String color = jsonObject.getString("color");
pset.add(code + "=" + name + "=" + pic + "="
+ color);
}
SharedPreferences sharedPreferences = getSharedPreferences(
"gh_platform", Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putStringSet("platform", pset);
editor.apply();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, null);
AppController.addToRequestQueue(request, SplashScreenActivity.class);
}
private int iCount;
private void addInstalledCount() {
synchronized (SplashScreenActivity.class) {
iCount++;
}
}
private void getInstalledListFromServer(final boolean isFirst) {
List<String> list = PackageManager.getLocalPackageName(getApplicationContext());
final int count = list.size();
final List<JSONObject> data = new ArrayList<>();
final List<String> useParams = new ArrayList<>();
iCount = 0;
for (int i = 0, size = list.size(); i < size; i++) {
final String packageName = list.get(i);
JsonObjectExtendedRequest request = new JsonObjectExtendedRequest(
Config.HOST + "v1d45/support/package/" + packageName + "/game/digest",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (response.length() != 0) {
data.add(response);
useParams.add(packageName);
}
addInstalledCount();
if (iCount == count) {
processingInstalledData(data, useParams, isFirst);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
addInstalledCount();
if (iCount == count) {
processingInstalledData(data, useParams, isFirst);
}
}
});
AppController.addToRequestQueue(request, SplashScreenActivity.class);
}
}
private void processingInstalledData(List<JSONObject> data, List<String> useParams, boolean isFirst) {
Gson gson = new Gson();
for (int i = 0, size = data.size(); i < size; i++) {
GameDigestEntity gameDigestEntity = gson.fromJson(data.get(i)
.toString(), GameDigestEntity.class);
GameInfo gameInfo = new GameInfo();
gameInfo.setId(gameDigestEntity.getId());
gameInfo.setPackageName(useParams.get(i));
concernManager.updateByEntity(gameInfo);
}
// 移除疑似游戏数据库中所有数据
SuspectedGameManager suspectedGameManager = new SuspectedGameManager(
getApplicationContext());
suspectedGameManager.deleteAll();
updateConcern(isFirst);
}
private int cCount;
private void addConcernCount() {
synchronized (SplashScreenActivity.class) {
cCount++;
}
}
private void updateConcern(final boolean isFirst) {
ArrayList<String> concernId = new ArrayList<>();
for (ConcernInfo entity : concernManager.getAllConcern()) {
concernId.add(entity.getId());
}
if (concernId.size() != 0) {
cCount = 0;
final int count = concernId.size();
for (int i = 0, size = concernId.size(); i < size; i++) {
JsonObjectExtendedRequest request = new JsonObjectExtendedRequest(
Config.HOST + "v2d0/game/" + concernId.get(i) + "/digest",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
addConcernCount();
Gson gson = new Gson();
GameEntity gameEntity = gson.fromJson(
response.toString(), GameEntity.class);
ArrayMap<String, ConcernInfo> map = concernManager
.getConcernMap();
ConcernInfo entity = map.get(gameEntity.getId());
if (entity != null) {
List<ApkEntity> list = gameEntity.getApk();
HashMap<String, Boolean> packageNames = new HashMap<>();
for (int j = 0, sizej = list.size(); j < sizej; j++) {
packageNames.put(list.get(j).getPackageName(), false);
}
entity.setTime(System.currentTimeMillis());
entity.setPackageNames(packageNames);
if (isFirst) {
//默认安装即为关注
if (!concernManager.isConcern(entity.getId())) {
concernManager.addByEntity(gameEntity);
}
}
concernManager.updateByConcern(entity);
}
if (cCount == count) {
update();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
addConcernCount();
if (cCount == count) {
update();
}
}
});
AppController.addToRequestQueue(request, SplashScreenActivity.class);
}
} else {
new Thread() {
@Override
public void run() {
update();
}
}.start();
}
}
private void update() {
/*
* 更新关注列表数据
*/
TrafficUtils spy = TrafficUtils.getInstance(getApplicationContext(), true);
List<ConcernInfo> concernEntities = concernManager.getAllConcern();
for (ConcernInfo concernEntity : concernEntities) {
concernEntity.setTime(System.currentTimeMillis());
int quantity = 0;
HashMap<String, Boolean> packageNames = new HashMap<>();
for (String packageName : concernEntity.getPackageNames().keySet()) {
if (PackageManager.isInstalled(packageName)) {
quantity++;
packageNames.put(packageName, true);
} else {
packageNames.put(packageName, false);
}
}
concernEntity.setPackageNames(packageNames);
concernEntity.setInstalled(quantity > 0);
concernEntity.setInstalledQuantity(quantity);
String tag = null;
int weight = 0;
if (concernEntity.isConcern() && concernEntity.isInstalled()) {
tag = "已关注,已安装";
weight = 2;
} else if (concernEntity.isConcern()
&& !concernEntity.isInstalled()) {
tag = "已关注";
weight = 1;
} else if (!concernEntity.isConcern()
&& concernEntity.isInstalled()) {
tag = "已安装";
weight = 1;
}
concernEntity.setTag(tag);
long traffic = spy.getTraffic(new ArrayList<>(concernEntity
.getPackageNames().keySet()));
concernEntity.setTraffic(traffic);
if (traffic > 0) {
weight++;
}
concernEntity.setWeight(weight);
if (weight == 0) {
concernManager.deleteConcern(concernEntity.getId());
} else {
concernManager.updateByConcern(concernEntity);
}
}
// 不是第一次启动
if (!sp.getBoolean("isNewFirstLaunch", true)) {
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();
}
}
launch();
}
}
// 跳转到主界面
private void launch() {
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
Bundle bundle = getIntent().getExtras();
if (bundle != null && bundle.getString("to") != null) {
intentControl(bundle, intent);
} else {
intent.putExtra("from", from);
if ("plugin".equals(from)) {
intent.putExtra("packageName", getIntent().getStringExtra("packageName"));
} else if ("mipush_news".equals(from)) {
intent.putExtra("entity", getIntent().getSerializableExtra("entity"));
} else if ("mipush_plugin".equals(from)) {
intent.putExtra("data", getIntent().getStringExtra("data"));
} else if ("plugin_install".equals(from)) {
intent.putExtra("path", getIntent().getStringExtra("path"));
}
}
startActivity(intent);
finish();
}
private void intentControl(Bundle bundle, Intent intent) {
String to = bundle.getString("to");
if ("NewsActivity".equals(to)) {
intent.putExtra("newsId", bundle.getString("newsId"));
intent.putExtra("entrance", bundle.getString("entrance"));
} else if("DownloadManagerActivity".equals(to)) {
intent.putExtra("packageName", bundle.getString("packageName"));
} else if ("GameDetailsActivity".equals(to)) {
intent.putExtra("gameId", bundle.getString("gameId"));
intent.putExtra("entrance", bundle.getString("entrance"));
} else if ("SubjectActivity".equals(to)) {
intent.putExtra("id", bundle.getString("id"));
intent.putExtra("name", bundle.getString("name"));
intent.putExtra("order", bundle.getBoolean("order"));
} else if ("ViewImageActivity".equals(to)) {
intent.putExtra("urls", bundle.getStringArrayList("urls"));
intent.putExtra("current", bundle.getInt("current",0));
intent.putExtra("ScaleType", bundle.getString("ScaleType"));
}
if ("NewsActivity".equals(to)) {
intent.putExtra("to", "NewsDetailActivity");
} else if ("GameDetailsActivity".equals(to)) {
intent.putExtra("to", "GameDetailActivity");
} else {
intent.putExtra("to", to);
}
}
}