项目整理

This commit is contained in:
huangzhuanghua
2016-12-12 16:34:19 +08:00
parent b18146f699
commit 3cbc850d12
64 changed files with 1169 additions and 1804 deletions

View File

@ -8,56 +8,28 @@ import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
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.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
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.DeviceUtils;
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.TokenUtils;
import com.gh.common.util.Utils;
import com.gh.download.DataWatcher;
import com.gh.download.DownloadEntity;
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.eventbus.EBReuse;
import com.gh.gamecenter.eventbus.EBSkip;
import com.gh.gamecenter.manager.PackageManager;
import com.gh.gamecenter.retrofit.Response;
import com.gh.gamecenter.retrofit.RetrofitManager;
import com.gh.gamecenter.manager.UpdateManager;
import com.kyleduo.switchbutton.SwitchButton;
import org.json.JSONObject;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import de.greenrobot.event.EventBus;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.adapter.rxjava.HttpException;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
/**
* 游戏设置页面
@ -69,19 +41,11 @@ public class SettingActivity extends BaseActivity implements OnClickListener {
private SwitchButton setting_sb_autoinstall, setting_sb_autodelete,
setting_sb_deletedata, setting_sb_autoupdate, setting_sb_concerngame;
private TextView setting_tv_version, app_tv_speed, app_tv_percent,
app_btn_cancel, setting_tv_cache,setting_tv_size;
private ProgressBar app_pb_progress;
private TextView setting_tv_version, setting_tv_cache,setting_tv_size;
private SharedPreferences sp;
private Dialog loadingDialog = null;
private Dialog downloadDialog = null;
private AppEntity appEntity;
private boolean isChecking = false;
private boolean isShowDownload = false;
private Handler handler = new Handler();
@ -89,32 +53,6 @@ public class SettingActivity extends BaseActivity implements OnClickListener {
private Context context = this;
private DataWatcher dataWatcher = new DataWatcher() {
@Override
public void onDataChanged(DownloadEntity downloadEntity) {
if (downloadEntity.getName().contains("光环助手") && isShowDownload) {
app_tv_speed.setText(String.format("%s(剩%s)",
SpeedUtils.getSpeed(downloadEntity.getSpeed()),
SpeedUtils.getRemainTime(downloadEntity.getSize(), downloadEntity.getProgress(),
downloadEntity.getSpeed() * 1024)));
app_pb_progress.setProgress((int) (downloadEntity.getPercent() * 10));
app_tv_percent.setText(downloadEntity.getPercent() + "%");
if (DownloadStatus.done.equals(downloadEntity.getStatus())) {
DownloadManager.getInstance(getApplicationContext())
.cancel(downloadEntity.getUrl(), false);
if (appEntity != null && appEntity.isForce()) {
AppController.getInstance().finishActivity();
} else {
if (downloadDialog != null) {
downloadDialog.dismiss();
}
isShowDownload = false;
}
}
}
}
};
@Override
public void finish() {
saveCurrentSetting();
@ -139,8 +77,7 @@ public class SettingActivity extends BaseActivity implements OnClickListener {
findViewById(R.id.setting_cv_font_size).setOnClickListener(this);
findViewById(R.id.setting_rl_concerngame).setOnClickListener(this);
setting_tv_version.setText("当前版本V"
+ PackageUtils.getVersion(getApplicationContext()));
setting_tv_version.setText(String.format("当前版本V%s", PackageUtils.getVersion(getApplicationContext())));
setting_tv_cache.setText(getCacheSize());
@ -257,11 +194,7 @@ public class SettingActivity extends BaseActivity implements OnClickListener {
setting_sb_autoupdate.performClick();
break;
case R.id.setting_rl_update:
loadingDialog = DialogUtils.showWaitDialog(this, "检查更新中...");
if (!isChecking) {
isChecking = true;
checkUpdate();
}
UpdateManager.getInstance(this).checkUpdate(false);
break;
case R.id.setting_rl_cache:
DialogUtils.showWarningDialog(this, "清除缓存", "清空缓存后未安装的游戏可能需要重新下载,确定清空?",
@ -311,7 +244,7 @@ public class SettingActivity extends BaseActivity implements OnClickListener {
//设置正文字号
private void fontSize() {
final Dialog dialog = new Dialog(this);
View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_font_size, null);
View inflate = View.inflate(this, 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);
@ -347,8 +280,8 @@ public class SettingActivity extends BaseActivity implements OnClickListener {
@Override
public void run() {
long start = System.currentTimeMillis();
deleteFolder(getCacheDir());
deleteFolder(getExternalCacheDir());
FileUtils.deleteFolder(getCacheDir());
FileUtils.deleteFolder(getExternalCacheDir());
long time = System.currentTimeMillis() - start;
if (time < 1000) {
try {
@ -372,232 +305,10 @@ public class SettingActivity extends BaseActivity implements OnClickListener {
}.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 checkUpdate() {
String TD_CHANNEL_ID = (String) PackageUtils.getMetaData(this,
getPackageName(), "TD_CHANNEL_ID");
RetrofitManager.getApi().getUpdate(PackageUtils.getVersion(getApplicationContext()), TD_CHANNEL_ID)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<AppEntity>() {
@Override
public void onResponse(AppEntity response) {
isChecking = false;
if (loadingDialog != null) {
loadingDialog.dismiss();
}
appEntity = response;
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("官方版");
game.setId("5618b86e8ab49e17088b4575");
PackageManager.addUpdate(0, game);
String updateMD5 = MD5Utils.getUpdateMD5(
appEntity.getUrl(),
appEntity.getContent());
showUpdateDialog(updateMD5);
} else {
toast("已是最新版本");
}
}
@Override
public void onFailure(Throwable e) {
isChecking = false;
if (loadingDialog != null) {
loadingDialog.dismiss();
}
if (e instanceof HttpException) {
HttpException exception = (HttpException) e;
if (exception.code() == 304) {
toast("您的光环助手已是最新版本");
return;
}
}
toast("检查更新失败");
}
});
}
private void showUpdateDialog(final String md5) {
uploadUpgradeLog("notice"); //上传更新通知弹窗数据
final Dialog updateDialog = new Dialog(this);
View view = View.inflate(this, R.layout.app_update_hint_dialog, null);
TextView content = (TextView) view.findViewById(R.id.updeta_content);
content.setText(Html.fromHtml(appEntity.getContent()));
TextView versison = (TextView) view.findViewById(R.id.update_app_version);
versison.setText(String.format("光环助手V%s更新内容", appEntity.getVersion()));
TextView size = (TextView) view.findViewById(R.id.update_app_size);
size.setText(String.format("大小:%s", appEntity.getSize()));
view.findViewById(R.id.update_cannel).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
if (appEntity.isForce()) {
AppController.getInstance().finishActivity();
} else {
updateDialog.dismiss();
}
}
});
view.findViewById(R.id.updeta_confirm).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
updateDialog.dismiss();
String path = FileUtils.getDownloadPath(SettingActivity.this,
"光环助手V" + appEntity.getVersion() + "_" + md5 + ".apk");
File file = new File(path);
if (file.exists() && file.length() > 0) {
uploadUpgradeLog("install");//上传更新安装数据
startActivity(PackageUtils.getInstallIntent(path));
} else {
DataUtils.onEvent(SettingActivity.this, "软件更新", "下载开始");
showDownloadDialog(md5);
}
}
});
if (appEntity.isForce()) {
updateDialog.setCanceledOnTouchOutside(false);
updateDialog.setCancelable(false);
}
updateDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
updateDialog.setContentView(view);
updateDialog.show();
}
private void showDownloadDialog(String md5) {
final Dialog downloadDialog = new Dialog(SettingActivity.this);
downloadDialog.setCanceledOnTouchOutside(false);
downloadDialog.setCancelable(false);
downloadDialog.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_tv_cancel);
app_btn_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
DownloadManager.getInstance(getApplicationContext()).cancel(
appEntity.getUrl());
if (appEntity.isForce()) {
AppController.getInstance().finishActivity();
} else {
downloadDialog.dismiss();
isShowDownload = false;
}
}
});
downloadDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
downloadDialog.setContentView(view);
downloadDialog.show();
isShowDownload = true;
String path = FileUtils.getDownloadPath(SettingActivity.this, "光环助手V"
+ appEntity.getVersion() + "_" + md5 + ".apk");
File file = new File(path);
if (file.exists()) {
file.delete();
}
DownloadEntity downloadEntity = new DownloadEntity();
downloadEntity.setUrl(appEntity.getUrl());
downloadEntity.setName("光环助手V" + appEntity.getVersion());
downloadEntity.setPath(path);
downloadEntity.setPlatform("官方版");
downloadEntity.setPackageName(getPackageName());
DownloadManager.getInstance(getApplicationContext()).cancel(
downloadEntity.getUrl(), false);
DownloadManager.getInstance(getApplicationContext()).pauseAll();
DownloadManager.getInstance(getApplicationContext()).add(downloadEntity);
}
private void uploadUpgradeLog(String step) {
Map<String, Object> map = new HashMap<>();
String version = PackageUtils.getVersion(this);
String user = DeviceUtils.getDeviceID(this);
String channel = (String) PackageUtils.getMetaData(this,
getPackageName(), "TD_CHANNEL_ID");
map.put("step", step);
map.put("version", version);
map.put("user", user);
map.put("device_id", TokenUtils.getDeviceId(this));
map.put("channel", channel);
Map<String, String> params = new HashMap<>();
params.put("topic", "upgrade");
params.put("source", "GH-ASSIST-Client");
params.put("time", String.valueOf(Utils.getTime(this)));
params.put("content", new JSONObject(map).toString());
RequestBody body = RequestBody.create(MediaType.parse("application/json"),
new JSONObject(params).toString());
RetrofitManager.getData().postLog(body)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<ResponseBody>());
}
@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);
saveCurrentSetting();
}
@Override
protected void onDestroy() {
saveCurrentSetting();
super.onDestroy();
}
}