Files
assistant-android/app/src/main/java/com/gh/gamecenter/SplashScreenActivity.java
huangzhuanghua 6ec976ccb6 项目整理
2016-07-06 09:37:33 +08:00

699 lines
20 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.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.entity.GameUpdateEntity;
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.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
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.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
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 boolean isFirst;
public static boolean isShow = true;
private String from;
private long start = 0L;
private long end = 0L;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
start = System.currentTimeMillis();
isFirst = true;
sp = getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
from = getIntent().getStringExtra("from");
if (sp.getBoolean("isFirstLaunch", 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_colors);
LayoutParams params = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
mActionBar.setCustomView(relativeLayout, params);// 自定义ActionBar布局
}
}
setContentView(R.layout.activity_splash);
if (sp.getBoolean("isFirstLaunch", true)) {
findViewById(R.id.splash_ll_normal).setVisibility(View.GONE);
ViewPager splash_viewPager = (ViewPager) findViewById(R.id.splash_viewPager);
splash_viewPager.setVisibility(View.VISIBLE);
splash_viewPager.setAdapter(new ViewPagerAdapter());
}
}
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 iv = new ImageView(SplashScreenActivity.this);
// iv.setBackgroundResource(pics[position]);
ImageUtils.getInstance(SplashScreenActivity.this).display("drawable://" + pics[position], iv, -1);
if (position == pics.length - 1) {
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(SplashScreenActivity.this,
MainActivity.class);
startActivity(intent);
finish();
}
});
}
container.addView(iv);
return iv;
}
@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);
object = null;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
if (sp.getBoolean("isFirstLaunch", true)) {
Intent intent = new Intent(SplashScreenActivity.this,
MainActivity.class);
startActivity(intent);
finish();
} else {
return true;
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus && isFirst) {
// 第一次启动把package.txt文件内容加载进数据库
if (!sp.getBoolean("isLoadFilter", false)) {
try {
List<FilterInfo> list = new ArrayList<FilterInfo>();
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();
}
}
isFirst = false;
checkUpdateFile();
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();
}
// getDownloadStatus();
getUISetting();
/*
* 上传数据
*/
DataCollectionManager.getInstance(getApplicationContext()).upload();
/*
* 初始化已安装软件map获取游戏更新列表和已安装列表
*/
PackageManager manager = new PackageManager(getApplicationContext());
manager.getInstalledMapFromLocal();
getInstalledListFromServer();
}
}
/*
* 获取界面设置
*/
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());
// "platform_order":{"normal":["9u","baidu","360"]}}
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();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
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 {
isShow = response.getBoolean("status");
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
isShow = true;
}
});
AppController.addToRequestQueue(request, SplashScreenActivity.class);
}
/*
* 检查下载文件夹下是否有旧版本的光环助手的包,有则删除
*/
private void checkUpdateFile() {
File file = new File(FileUtils.getDownloadDir(this) + File.separator);
if (file.isDirectory()) {
for (File f : file.listFiles()) {
if (!f.isDirectory() && f.getName().startsWith("光环助手V")) {
String name = f.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) {
f.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<String>();
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();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
AppController.addToRequestQueue(request, SplashScreenActivity.class);
}
private int iCount;
private void getInstalledListFromServer() {
List<String> list = PackageManager.getLocalPackageName(getApplicationContext());
final int count = list.size();
final List<JSONObject> data = new ArrayList<JSONObject>();
final List<String> useParams = new ArrayList<String>();
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/" + list.get(i) + "/game/digest",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
iCount++;
if (response.length() != 0) {
data.add(response);
useParams.add(packageName);
}
if (iCount == count) {
processingInstalledData(data, useParams);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
iCount++;
if (iCount == count) {
processingInstalledData(data, useParams);
}
}
});
AppController.addToRequestQueue(request, SplashScreenActivity.class);
}
}
private void processingInstalledData(List<JSONObject> data,
List<String> useParams) {
List<String> list = new ArrayList<String>();
Gson gson = new Gson();
ConcernManager manager = new ConcernManager(getApplicationContext());
for (int i = 0, size = data.size(); i < size; i++) {
GameDigestEntity gameDigestEntity = gson.fromJson(data.get(i)
.toString(), GameDigestEntity.class);
GameInfo gameEntity = new GameInfo();
gameEntity.setId(gameDigestEntity.getId());
gameEntity.setPackageName(useParams.get(i));
manager.updateByEntity(gameEntity, false);
list.add(useParams.get(i));
}
// 移除疑似游戏数据库中所有数据
SuspectedGameManager suspectedGameManager = new SuspectedGameManager(
getApplicationContext());
suspectedGameManager.deleteAll();
getUpdateListFromServer(list);
updateConcern();
}
private void getUpdateListFromServer(List<String> list) {
Comparator<String> comparator = new Comparator<String>() {
@Override
public int compare(String lhs, String rhs) {
char[] clhs = lhs.toCharArray();
char[] crhs = rhs.toCharArray();
int length;
if (clhs.length < crhs.length) {
length = clhs.length;
} else if (clhs.length > crhs.length) {
length = crhs.length;
} else {
length = clhs.length;
}
for (int i = 0; i < length; i++) {
if (clhs[i] > crhs[i]) {
return 1;
} else if (clhs[i] < crhs[i]) {
return -1;
} else if (i == length - 1) {
if (clhs.length < crhs.length) {
return -1;
} else if (clhs.length > crhs.length) {
return 1;
} else {
return 0;
}
}
}
return 0;
}
};
Collections.sort(list, comparator);
StringBuilder builder = new StringBuilder();
String packages;
PackageManager.clearUpdateList();
for (int i = 0, sizei = (list.size() / 10) + 1; i < sizei; i++) {
builder.delete(0, builder.length());
for (int j = i * 10, sizej = (i + 1) * 10 > list.size() ? list
.size() : (i + 1) * 10; j < sizej; j++) {
builder.append(list.get(j) + "-");
}
if (builder.length() != 0) {
packages = builder.substring(0, builder.length() - 1);
JsonArrayExtendedRequest request = new JsonArrayExtendedRequest(
Config.HOST + "v1d45/support/package/update?package="
+ packages, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Utils.log("getUpdateList=" + response.toString());
if (response.length() != 0) {
Gson gson = new Gson();
Type listType = new TypeToken<ArrayList<GameUpdateEntity>>() {}.getType();
ArrayList<GameUpdateEntity> games = gson.fromJson(response.toString(), listType);
GameUpdateEntity gameUpdateEntity;
for (int i = 0; i < games.size(); i++) {
gameUpdateEntity = games.get(i);
// 判断是否gh_version是否相同
String gh_version = (String) PackageUtils
.getMetaData(SplashScreenActivity.this, gameUpdateEntity.getPackageName(), "gh_version");
if (gh_version != null) {
gh_version = gh_version.substring(2);
// 判断gh_version是否相同
if (gh_version.equals(gameUpdateEntity.getGhVersion())) {
// 判断version是否相同
String version = PackageUtils
.getVersionByPackage(SplashScreenActivity.this, gameUpdateEntity.getPackageName());
if (version.equals(gameUpdateEntity.getVersion())) {
// 版本相同,无需显示插件更新,继续查看是否有可更新的游戏包
games.remove(i);
i--;
continue;
}
}
}
}
PackageManager.addUpdateList(games);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
AppController.addToRequestQueue(request,
SplashScreenActivity.class);
}
}
}
private int cCount;
private void updateConcern() {
ArrayList<String> concernId = new ArrayList<String>();
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 + "v1d45/game/" + concernId.get(i)
+ "/digest",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
cCount++;
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<String, Boolean>();
for (int j = 0, sizej = list.size(); j < sizej; j++) {
packageNames.put(
list.get(j).getPackageName(), false);
}
entity.setTime(System.currentTimeMillis());
entity.setPackageNames(packageNames);
concernManager.updateByConcern(entity);
}
if (cCount == count) {
update();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
cCount++;
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<String, Boolean>();
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 ? true : false);
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<String>(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("isFirstLaunch", 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();
}
}
Intent intent = new Intent(SplashScreenActivity.this,
MainActivity.class);
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"));
}
end = System.currentTimeMillis();
Utils.log("time = " + (end - start));
startActivity(intent);
finish();
}
}
}