612 lines
23 KiB
Java
612 lines
23 KiB
Java
package com.gh.gamecenter;
|
||
|
||
import android.app.Activity;
|
||
import android.app.Dialog;
|
||
import android.content.Intent;
|
||
import android.content.SharedPreferences;
|
||
import android.content.SharedPreferences.Editor;
|
||
import android.os.Bundle;
|
||
import android.os.Handler;
|
||
import android.text.Html;
|
||
import android.text.Spanned;
|
||
import android.text.TextUtils;
|
||
import android.view.LayoutInflater;
|
||
import android.view.View;
|
||
import android.view.View.OnClickListener;
|
||
import android.view.Window;
|
||
import android.widget.ProgressBar;
|
||
import android.widget.RadioGroup;
|
||
import android.widget.TextView;
|
||
import android.widget.Toast;
|
||
|
||
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.util.DataUtils;
|
||
import com.gh.common.util.DialogUtils;
|
||
import com.gh.common.util.FileUtils;
|
||
import com.gh.common.util.MD5Utils;
|
||
import com.gh.common.util.PackageUtils;
|
||
import com.gh.common.util.SpeedUtils;
|
||
import com.gh.common.util.Utils;
|
||
import com.gh.download.DataWatcher;
|
||
import com.gh.download.DownloadEntry;
|
||
import com.gh.download.DownloadManager;
|
||
import com.gh.download.DownloadStatus;
|
||
import com.gh.gamecenter.entity.AppEntity;
|
||
import com.gh.gamecenter.entity.GameUpdateEntity;
|
||
import com.gh.gamecenter.manager.PackageManager;
|
||
import com.gh.gamecenter.volley.extended.JsonObjectExtendedRequest;
|
||
import com.google.gson.Gson;
|
||
import com.kyleduo.switchbutton.SwitchButton;
|
||
|
||
import org.json.JSONObject;
|
||
|
||
import java.io.File;
|
||
import java.util.HashMap;
|
||
import java.util.Map.Entry;
|
||
|
||
/**
|
||
* 游戏设置页面
|
||
*
|
||
* @author 吕方
|
||
* @since 0814
|
||
*/
|
||
public class SettingActivity extends BaseActivity implements OnClickListener {
|
||
|
||
private SwitchButton setting_sb_autoinstall, setting_sb_autodelete,
|
||
setting_sb_deletedata, setting_sb_autoupdate;
|
||
private TextView setting_tv_version, app_tv_speed, app_tv_percent,
|
||
app_btn_cancel, setting_tv_cache;
|
||
private ProgressBar app_pb_progress;
|
||
|
||
private SharedPreferences sp;
|
||
|
||
private Dialog dialog = null;
|
||
|
||
private AppEntity appEntity;
|
||
|
||
private boolean isChecking = false;
|
||
private boolean isShowDownload = false;
|
||
|
||
private Handler handler = new Handler();
|
||
|
||
private DataWatcher dataWatcher = new DataWatcher() {
|
||
|
||
@Override
|
||
public void onDataChanged(HashMap<String, DownloadEntry> downloads) {
|
||
for (Entry<String, DownloadEntry> entry : downloads.entrySet()) {
|
||
DownloadEntry downloadEntry = entry.getValue();
|
||
if (downloadEntry.getName().contains("光环助手") && isShowDownload) {
|
||
app_tv_speed.setText(SpeedUtils.getSpeed(downloadEntry
|
||
.getSpeed())
|
||
+ "(剩"
|
||
+ SpeedUtils.getRemainTime(downloadEntry.getSize(),
|
||
downloadEntry.getProgress(),
|
||
downloadEntry.getSpeed() * 1024) + ")");
|
||
app_pb_progress.setProgress((int) (downloadEntry
|
||
.getPercent() * 10));
|
||
app_tv_percent.setText(downloadEntry.getPercent() + "%");
|
||
if (DownloadStatus.done.equals(downloadEntry.getStatus())) {
|
||
DownloadManager.getInstance(getApplicationContext())
|
||
.cancel(downloadEntry.getUrl(), false);
|
||
if (appEntity != null && appEntity.isIs_force()) {
|
||
AppController.getInstance().finishActivity();
|
||
} else {
|
||
if (dialog != null) {
|
||
dialog.dismiss();
|
||
}
|
||
isShowDownload = false;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
};
|
||
|
||
@Override
|
||
public void finish() {
|
||
saveCurrentSetting();
|
||
super.finish();
|
||
}
|
||
|
||
@Override
|
||
protected void onCreate(Bundle savedInstanceState) {
|
||
super.onCreate(savedInstanceState);
|
||
|
||
View contentView = View.inflate(this, R.layout.activity_setting, null);
|
||
|
||
init(contentView, "设置");
|
||
|
||
findViewById(R.id.setting_rl_autoinstall).setOnClickListener(this);
|
||
findViewById(R.id.setting_rl_autodelete).setOnClickListener(this);
|
||
findViewById(R.id.setting_rl_deletedata).setOnClickListener(this);
|
||
findViewById(R.id.setting_rl_autoupdate).setOnClickListener(this);
|
||
findViewById(R.id.setting_rl_update).setOnClickListener(this);
|
||
findViewById(R.id.setting_rl_feedback).setOnClickListener(this);
|
||
findViewById(R.id.setting_rl_cache).setOnClickListener(this);
|
||
findViewById(R.id.setting_rl_font_size).setOnClickListener(this);
|
||
|
||
if (AppController.get("user", false) != null) {
|
||
findViewById(R.id.setting_tv_account).setOnClickListener(this);
|
||
} else {
|
||
findViewById(R.id.setting_tv_account).setVisibility(View.GONE);
|
||
}
|
||
|
||
setting_tv_version.setText("当前版本:V"
|
||
+ PackageUtils.getVersion(getApplicationContext()));
|
||
|
||
setting_tv_cache.setText(getCacheSize());
|
||
|
||
sp = getSharedPreferences(Config.PREFERENCE, Activity.MODE_PRIVATE);
|
||
|
||
setting_sb_autoinstall.setChecked(sp.getBoolean("autoinstall", true));
|
||
setting_sb_autodelete.setChecked(sp.getBoolean("autodelete", true));
|
||
setting_sb_deletedata.setChecked(sp.getBoolean("deletedata", true));
|
||
setting_sb_autoupdate.setChecked(sp.getBoolean("autoupdate", true));
|
||
|
||
if (sp.getBoolean("isShowDisclaimer", false)) {
|
||
TextView setting_tv_disclaimer = (TextView) findViewById(R.id.setting_tv_disclaimer);
|
||
setting_tv_disclaimer.setVisibility(View.VISIBLE);
|
||
setting_tv_disclaimer.setOnClickListener(this);
|
||
}
|
||
}
|
||
|
||
// 获取缓存大小
|
||
private String getCacheSize() {
|
||
File ecDir = getExternalCacheDir();
|
||
long cacheLength = getFolderSize(getCacheDir());
|
||
if (ecDir != null) {
|
||
cacheLength += getFolderSize(ecDir);
|
||
}
|
||
return long2Size(cacheLength);
|
||
}
|
||
|
||
private long getFolderSize(File folder) {
|
||
long size = 0;
|
||
size += folder.length();
|
||
if (folder.isDirectory()) {
|
||
for (File file : folder.listFiles()) {
|
||
if (file.isDirectory()) {
|
||
size += getFolderSize(file);
|
||
} else {
|
||
size += file.length();
|
||
}
|
||
}
|
||
}
|
||
return size;
|
||
}
|
||
|
||
private String long2Size(Long length) {
|
||
float m = length / 1024f / 1024f;
|
||
String str = Float.toString(m);
|
||
int index = str.lastIndexOf(".");
|
||
if (index != -1 && str.length() > index + 3) {
|
||
str = str.substring(0, index + 3);
|
||
}
|
||
return str + "M";
|
||
}
|
||
|
||
private void saveCurrentSetting() {
|
||
Editor mEditor = sp.edit();
|
||
mEditor.putBoolean("autoinstall", setting_sb_autoinstall.isChecked());
|
||
mEditor.putBoolean("autodelete", setting_sb_autodelete.isChecked());
|
||
mEditor.putBoolean("deletedata", setting_sb_deletedata.isChecked());
|
||
mEditor.putBoolean("autoupdate", setting_sb_autoupdate.isChecked());
|
||
mEditor.apply();
|
||
}
|
||
|
||
@Override
|
||
public void onClick(final View v) {
|
||
switch (v.getId()) {
|
||
case R.id.actionbar_rl_back:
|
||
finish();
|
||
break;
|
||
case R.id.setting_rl_autoinstall:
|
||
setting_sb_autoinstall.performClick();
|
||
break;
|
||
case R.id.setting_rl_autodelete:
|
||
setting_sb_autodelete.performClick();
|
||
break;
|
||
case R.id.setting_rl_deletedata:
|
||
setting_sb_deletedata.performClick();
|
||
break;
|
||
case R.id.setting_rl_autoupdate:
|
||
setting_sb_autoupdate.performClick();
|
||
break;
|
||
case R.id.setting_rl_update:
|
||
dialog = DialogUtils.showWaitDialog(this, "检查更新中...");
|
||
if (isChecking)
|
||
break;
|
||
isChecking = true;
|
||
checkUpdate();
|
||
break;
|
||
case R.id.setting_rl_cache:
|
||
dialog = DialogUtils.showWaitDialog(this, "清除缓存中...");
|
||
claerCache();
|
||
break;
|
||
case R.id.setting_rl_feedback:
|
||
startActivity(new Intent(SettingActivity.this,
|
||
SuggestionActivity.class));
|
||
break;
|
||
case R.id.setting_tv_disclaimer:
|
||
String content = sp.getString("disclaimer", null);
|
||
if (!TextUtils.isEmpty(content)) {
|
||
showDisclaimerDialog(content);
|
||
}
|
||
break;
|
||
case R.id.setting_rl_font_size:
|
||
final Dialog dialog = new Dialog(this);
|
||
View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_font_size, null);
|
||
TextView tv_negative = (TextView) inflate.findViewById(R.id.font_size_negative);
|
||
TextView tv_positive = (TextView) inflate.findViewById(R.id.font_size_positive);
|
||
final RadioGroup radioGroup = (RadioGroup) inflate.findViewById(R.id.font_size_radiogroup);
|
||
tv_negative.setOnClickListener(new OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
dialog.cancel();
|
||
}
|
||
});
|
||
tv_positive.setOnClickListener(new OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
int checkedRadioButtonId = radioGroup.getCheckedRadioButtonId();
|
||
int index = checkedRadioButtonId % 4;
|
||
if (index == 0) {
|
||
index = 4;
|
||
}
|
||
Toast.makeText(getApplicationContext(), "" +index, Toast.LENGTH_SHORT).show();
|
||
dialog.cancel();
|
||
}
|
||
});
|
||
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||
dialog.setContentView(inflate);
|
||
dialog.show();
|
||
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
// 清除缓存
|
||
private void claerCache() {
|
||
new Thread() {
|
||
@Override
|
||
public void run() {
|
||
long start = System.currentTimeMillis();
|
||
deleteFolder(getCacheDir());
|
||
deleteFolder(getExternalCacheDir());
|
||
long time = System.currentTimeMillis() - start;
|
||
if (time < 1000) {
|
||
try {
|
||
sleep(1000 - time);
|
||
} catch (InterruptedException e) {
|
||
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
if (dialog != null) {
|
||
dialog.dismiss();
|
||
}
|
||
handler.post(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
setting_tv_cache.setText(getCacheSize());
|
||
}
|
||
});
|
||
}
|
||
}.start();
|
||
}
|
||
|
||
private void deleteFolder(File folder) {
|
||
if (folder != null) {
|
||
if (folder.isDirectory()) {
|
||
for (File file : folder.listFiles()) {
|
||
if (file.isDirectory()) {
|
||
deleteFolder(file);
|
||
} else {
|
||
file.delete();
|
||
}
|
||
}
|
||
}
|
||
folder.delete();
|
||
}
|
||
}
|
||
|
||
// 弹出免责声明的窗口
|
||
private void showDisclaimerDialog(String content) {
|
||
final Dialog disclaimerDialog = new Dialog(this);
|
||
View view = View
|
||
.inflate(this, R.layout.setting_disclaimer_dialog, null);
|
||
TextView title = (TextView) view
|
||
.findViewById(R.id.disclaimer_dialog_title);
|
||
title.setText("免责声明");
|
||
TextView message = (TextView) view
|
||
.findViewById(R.id.disclaimer_dialog_message);
|
||
Spanned spanned = Html.fromHtml(content);
|
||
message.setText(spanned);
|
||
view.findViewById(R.id.disclaimer_dialog_confirm).setOnClickListener(
|
||
new OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
disclaimerDialog.dismiss();
|
||
}
|
||
});
|
||
disclaimerDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||
disclaimerDialog.setContentView(view);
|
||
disclaimerDialog.show();
|
||
}
|
||
|
||
private void checkUpdate() {
|
||
String TD_CHANNEL_ID = (String) PackageUtils.getMetaData(this,
|
||
getPackageName(), "TD_CHANNEL_ID");
|
||
String url = Config.HOST + "v2/version?version_name="
|
||
+ PackageUtils.getVersion(getApplicationContext())
|
||
+ "&channel=" + TD_CHANNEL_ID;
|
||
JsonObjectExtendedRequest request = new JsonObjectExtendedRequest(url,
|
||
new Response.Listener<JSONObject>() {
|
||
@Override
|
||
public void onResponse(JSONObject response) {
|
||
|
||
isChecking = false;
|
||
if (dialog != null) {
|
||
dialog.dismiss();
|
||
}
|
||
|
||
if (response.length() == 0) {
|
||
toast("已是最新版本");
|
||
} else {
|
||
Gson gson = new Gson();
|
||
appEntity = gson.fromJson(response.toString(),
|
||
AppEntity.class);
|
||
|
||
float version = Float.valueOf(appEntity
|
||
.getVersion());
|
||
float currentVersion = Float.valueOf(PackageUtils
|
||
.getVersion(getApplicationContext()));
|
||
|
||
if (version > currentVersion) {
|
||
// 光环助手 有更新
|
||
GameUpdateEntity game = new GameUpdateEntity();
|
||
game.setName("光环助手V" + appEntity.getVersion());
|
||
game.setPackageName(getPackageName());
|
||
game.setSize(appEntity.getSize());
|
||
game.setVersion(appEntity.getVersion());
|
||
game.setUrl(appEntity.getUrl());
|
||
game.setPlatform("官方版");
|
||
PackageManager.addUpdate(0, game);
|
||
|
||
String updateMD5 = MD5Utils.getUpdateMD5(
|
||
appEntity.getUrl(),
|
||
appEntity.getContent());
|
||
|
||
showUpdateDialog(updateMD5);
|
||
} else {
|
||
showCancelDialog();
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
}, new Response.ErrorListener() {
|
||
|
||
@Override
|
||
public void onErrorResponse(VolleyError error) {
|
||
isChecking = false;
|
||
if (dialog != null) {
|
||
dialog.dismiss();
|
||
}
|
||
|
||
Utils.log("error = " + error.toString());
|
||
|
||
toast("检查更新失败");
|
||
}
|
||
});
|
||
AppController.addToRequestQueue(request, SettingActivity.class);
|
||
}
|
||
|
||
private void showUpdateDialog(final String md5) {
|
||
dialog = new Dialog(this);
|
||
View view = View.inflate(this, R.layout.search_history_delete_dialog,
|
||
null);
|
||
TextView title = (TextView) view.findViewById(R.id.delete_dialog_title);
|
||
title.setText("更新");
|
||
TextView content = (TextView) view
|
||
.findViewById(R.id.delete_dialog_message);
|
||
|
||
Spanned dialogContent = Html
|
||
.fromHtml("<p>发现新版本!</p>"
|
||
+ "<font color='#1ba4fc' style='line-height:1.5'>更新不会影响用户信息!</font><br>"
|
||
+ "<font style='line-height:1.5'>最新版本:</font><font color='#3D3D3D' style='line-height:1.5'><b>V"
|
||
+ appEntity.getVersion()
|
||
+ "</b></font><br>"
|
||
+ "<font style='line-height:1.5'>更新包大小:</font><font color='#3D3D3D' style='line-height:1.5'><b>"
|
||
+ appEntity.getSize()
|
||
+ "</b></font><br>"
|
||
+ "更新内容:<br><font color='#2E2E2E' style='line-height:1.5'><b>"
|
||
+ appEntity.getContent() + "</b></font><br>"
|
||
+ "<br> 确定更新吗?");
|
||
|
||
content.setText(dialogContent);
|
||
|
||
if (appEntity.isIs_force()) {
|
||
dialog.setCanceledOnTouchOutside(false);
|
||
dialog.setCancelable(false);
|
||
}
|
||
view.findViewById(R.id.delete_dialog_cancel).setOnClickListener(
|
||
new OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
if (appEntity.isIs_force()) {
|
||
Intent data = new Intent();
|
||
data.putExtra("isForce", true);
|
||
setResult(RESULT_OK, data);
|
||
finish();
|
||
} else {
|
||
if (dialog != null) {
|
||
dialog.dismiss();
|
||
}
|
||
}
|
||
}
|
||
});
|
||
view.findViewById(R.id.delete_dialog_confirm).setOnClickListener(
|
||
new OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
if (dialog != null) {
|
||
dialog.dismiss();
|
||
}
|
||
String path = FileUtils.getDownloadPath(
|
||
SettingActivity.this,
|
||
"光环助手V" + appEntity.getVersion() + "_" + md5
|
||
+ ".apk");
|
||
File file = new File(path);
|
||
if (file.exists() && file.length() > 0) {
|
||
startActivity(PackageUtils.getInstallIntent(path));
|
||
} else {
|
||
DataUtils.onEvent(SettingActivity.this, "软件更新", "下载开始");
|
||
showDownloadDialog(md5);
|
||
}
|
||
}
|
||
});
|
||
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||
dialog.setContentView(view);
|
||
dialog.show();
|
||
}
|
||
|
||
private void showDownloadDialog(String md5) {
|
||
dialog = new Dialog(SettingActivity.this);
|
||
dialog.setCanceledOnTouchOutside(false);
|
||
dialog.setCancelable(false);
|
||
dialog.closeOptionsMenu();
|
||
View view = View.inflate(this, R.layout.app_updating_dialog, null);
|
||
|
||
app_pb_progress = (ProgressBar) view.findViewById(R.id.app_pb_progress);
|
||
app_tv_speed = (TextView) view.findViewById(R.id.app_tv_speed);
|
||
app_tv_percent = (TextView) view.findViewById(R.id.app_tv_percent);
|
||
app_btn_cancel = (TextView) view.findViewById(R.id.app_btn_cancel);
|
||
|
||
app_btn_cancel.setOnClickListener(new OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
DownloadManager.getInstance(getApplicationContext()).cancel(
|
||
appEntity.getUrl());
|
||
if (appEntity.isIs_force()) {
|
||
Intent data = new Intent();
|
||
data.putExtra("isForce", true);
|
||
setResult(RESULT_OK, data);
|
||
finish();
|
||
} else {
|
||
if (dialog != null) {
|
||
dialog.dismiss();
|
||
}
|
||
isShowDownload = false;
|
||
}
|
||
}
|
||
});
|
||
|
||
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||
dialog.setContentView(view);
|
||
dialog.show();
|
||
|
||
isShowDownload = true;
|
||
|
||
String path = FileUtils.getDownloadPath(SettingActivity.this, "光环助手V"
|
||
+ appEntity.getVersion() + "_" + md5 + ".apk");
|
||
|
||
File file = new File(path);
|
||
|
||
if (file.exists()) {
|
||
file.delete();
|
||
}
|
||
|
||
DownloadEntry downloadEntry = new DownloadEntry();
|
||
downloadEntry.setUrl(appEntity.getUrl());
|
||
downloadEntry.setName("光环助手V" + appEntity.getVersion());
|
||
downloadEntry.setPath(path);
|
||
HashMap<String, String> meta = new HashMap<String, String>();
|
||
meta.put("platform", "官方版");
|
||
downloadEntry.setMeta(meta);
|
||
|
||
DownloadManager.getInstance(getApplicationContext()).cancel(
|
||
downloadEntry.getUrl(), false);
|
||
|
||
DownloadManager.getInstance(getApplicationContext()).pauseAll();
|
||
|
||
DownloadManager.getInstance(getApplicationContext()).add(downloadEntry);
|
||
}
|
||
|
||
private void showCancelDialog() {
|
||
|
||
final Dialog dialog = new Dialog(SettingActivity.this);
|
||
View view = View.inflate(SettingActivity.this,
|
||
R.layout.search_history_delete_dialog, null);
|
||
TextView title = (TextView) view.findViewById(R.id.delete_dialog_title);
|
||
title.setText("更新提示");
|
||
TextView content = (TextView) view
|
||
.findViewById(R.id.delete_dialog_message);
|
||
|
||
content.setText("已经是最新版本!");
|
||
|
||
view.findViewById(R.id.delete_dialog_rl_cancel)
|
||
.setVisibility(View.GONE);
|
||
view.findViewById(R.id.delete_dialog_rl_confirm).setVisibility(
|
||
View.VISIBLE);
|
||
|
||
TextView cancel = (TextView) view
|
||
.findViewById(R.id.delete_dialog_confirm);
|
||
|
||
cancel.setVisibility(View.VISIBLE);
|
||
cancel.setOnClickListener(new OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
dialog.dismiss();
|
||
}
|
||
});
|
||
|
||
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||
dialog.setContentView(view);
|
||
dialog.show();
|
||
}
|
||
|
||
@Override
|
||
public void onResume() {
|
||
super.onResume();
|
||
DownloadManager.getInstance(SettingActivity.this).addObserver(
|
||
dataWatcher);
|
||
}
|
||
|
||
@Override
|
||
public void onPause() {
|
||
saveCurrentSetting();
|
||
super.onPause();
|
||
DownloadManager.getInstance(SettingActivity.this).removeObserver(
|
||
dataWatcher);
|
||
}
|
||
|
||
@Override
|
||
protected void onDestroy() {
|
||
saveCurrentSetting();
|
||
super.onDestroy();
|
||
setting_sb_autoinstall = null;
|
||
setting_sb_autodelete = null;
|
||
setting_sb_deletedata = null;
|
||
setting_sb_autoupdate = null;
|
||
setting_tv_version = null;
|
||
app_tv_speed = null;
|
||
app_tv_percent = null;
|
||
app_btn_cancel = null;
|
||
setting_tv_cache = null;
|
||
app_pb_progress = null;
|
||
sp = null;
|
||
dialog = null;
|
||
appEntity = null;
|
||
handler = null;
|
||
dataWatcher = null;
|
||
}
|
||
}
|