350 lines
12 KiB
Java
350 lines
12 KiB
Java
package com.gh.gamecenter;
|
|
|
|
import android.app.Activity;
|
|
import android.app.Dialog;
|
|
import android.content.Context;
|
|
import android.content.DialogInterface;
|
|
import android.content.Intent;
|
|
import android.content.SharedPreferences;
|
|
import android.content.SharedPreferences.Editor;
|
|
import android.os.Bundle;
|
|
import android.support.annotation.NonNull;
|
|
import android.support.v7.app.AlertDialog;
|
|
import android.view.View;
|
|
import android.view.View.OnClickListener;
|
|
import android.widget.RadioButton;
|
|
import android.widget.RadioGroup;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import com.gh.base.BaseActivity;
|
|
import com.gh.common.constant.Config;
|
|
import com.gh.common.util.DialogUtils;
|
|
import com.gh.common.util.EntranceUtils;
|
|
import com.gh.common.util.FileUtils;
|
|
import com.gh.gamecenter.eventbus.EBReuse;
|
|
import com.gh.gamecenter.eventbus.EBSkip;
|
|
import com.kyleduo.switchbutton.SwitchButton;
|
|
|
|
import org.greenrobot.eventbus.EventBus;
|
|
|
|
import java.io.File;
|
|
|
|
import butterknife.BindView;
|
|
import butterknife.OnClick;
|
|
import rx.Observable;
|
|
import rx.Observable.OnSubscribe;
|
|
import rx.Observer;
|
|
import rx.Subscriber;
|
|
import rx.android.schedulers.AndroidSchedulers;
|
|
import rx.schedulers.Schedulers;
|
|
|
|
import static com.gh.gamecenter.R.id.setting_rl_about;
|
|
import static java.lang.Thread.sleep;
|
|
|
|
/**
|
|
* 游戏设置页面
|
|
*
|
|
* @author 吕方
|
|
* @since 0814
|
|
*/
|
|
public class SettingActivity extends BaseActivity implements OnClickListener {
|
|
|
|
@BindView(R.id.setting_sb_autoinstall)
|
|
SwitchButton mSettingAutoinstallSb;
|
|
@BindView(R.id.setting_sb_autodelete)
|
|
SwitchButton mSettingAutodeleteSb;
|
|
@BindView(R.id.setting_sb_concerngame)
|
|
SwitchButton mSettingConcerngameSb;
|
|
@BindView(R.id.setting_tv_cache)
|
|
TextView mSettingCacheTv;
|
|
@BindView(R.id.setting_tv_size)
|
|
TextView mSettingSizeTv;
|
|
|
|
private SharedPreferences sp;
|
|
|
|
private Dialog loadingDialog = null;
|
|
|
|
private int checkSizeIndex;
|
|
|
|
@NonNull
|
|
public static Intent getIntent(Context context, String entrance) {
|
|
Intent intent = new Intent(context, SettingActivity.class);
|
|
intent.putExtra(EntranceUtils.KEY_ENTRANCE, entrance);
|
|
return intent;
|
|
}
|
|
|
|
@Override
|
|
public void finish() {
|
|
saveCurrentSetting();
|
|
super.finish();
|
|
}
|
|
|
|
@Override
|
|
protected int getLayoutId() {
|
|
return R.layout.activity_setting;
|
|
}
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
|
|
initTitle(getString(R.string.title_settings));
|
|
|
|
mSettingCacheTv.setText(getCacheSize());
|
|
|
|
sp = getSharedPreferences(Config.PREFERENCE, Activity.MODE_PRIVATE);
|
|
|
|
// 未打开下载按钮,显示修复下载按钮
|
|
if (!sp.getBoolean("isShow", true)) {
|
|
findViewById(R.id.setting_cv_fix_download).setVisibility(View.VISIBLE);
|
|
}
|
|
|
|
mSettingAutoinstallSb.setChecked(sp.getBoolean("autoinstall", true));
|
|
mSettingAutodeleteSb.setChecked(sp.getBoolean("autodelete", true));
|
|
mSettingConcerngameSb.setChecked(sp.getBoolean("concerngame", true));
|
|
|
|
checkSizeIndex = sp.getInt("fontsize", 1);
|
|
if (checkSizeIndex == 0) {
|
|
checkSizeIndex = 1;
|
|
}
|
|
fontTextSize(checkSizeIndex);
|
|
}
|
|
|
|
@Override
|
|
public void onPause() {
|
|
super.onPause();
|
|
saveCurrentSetting();
|
|
}
|
|
|
|
private void saveCurrentSetting() {
|
|
Editor mEditor = sp.edit();
|
|
mEditor.putBoolean("autoinstall", mSettingAutoinstallSb.isChecked());
|
|
mEditor.putBoolean("autodelete", mSettingAutodeleteSb.isChecked());
|
|
mEditor.putBoolean("concerngame", mSettingConcerngameSb.isChecked());
|
|
mEditor.putInt("fontsize", checkSizeIndex);
|
|
mEditor.apply();
|
|
}
|
|
|
|
// 获取缓存大小
|
|
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";
|
|
}
|
|
|
|
@OnClick({
|
|
R.id.setting_cv_fix_download,
|
|
R.id.setting_rl_autoinstall,
|
|
R.id.setting_rl_autodelete,
|
|
R.id.setting_rl_cache,
|
|
R.id.setting_cv_font_size,
|
|
R.id.setting_rl_concerngame,
|
|
R.id.setting_rl_about
|
|
})
|
|
@Override
|
|
public void onClick(final View v) {
|
|
|
|
switch (v.getId()) {
|
|
case R.id.setting_cv_fix_download:
|
|
Editor editor = sp.edit();
|
|
editor.putBoolean("isShow", true);
|
|
editor.putBoolean("isCheckShow", false);
|
|
editor.apply();
|
|
toast("修复成功");
|
|
EventBus.getDefault().post(new EBReuse("Refresh"));
|
|
finish();
|
|
new Thread() {
|
|
@Override
|
|
public void run() {
|
|
try {
|
|
sleep(800);
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
EventBus.getDefault().post(new EBSkip(MainActivity.EB_SKIP_GAMEFRAGMENT, 0));
|
|
}
|
|
}.start();
|
|
break;
|
|
case R.id.setting_rl_autoinstall:
|
|
mSettingAutoinstallSb.performClick();
|
|
break;
|
|
case R.id.setting_rl_autodelete:
|
|
mSettingAutodeleteSb.performClick();
|
|
break;
|
|
case R.id.setting_rl_cache:
|
|
DialogUtils.showWarningDialog(this, "清除缓存", "清空缓存后未安装的游戏可能需要重新下载,确定清空?",
|
|
new DialogUtils.ConfirmListener() {
|
|
@Override
|
|
public void onConfirm() {
|
|
loadingDialog = DialogUtils.showWaitDialog(SettingActivity.this, "清除缓存中...");
|
|
clearCache();
|
|
}
|
|
});
|
|
break;
|
|
case R.id.setting_cv_font_size:
|
|
fontSize();
|
|
break;
|
|
case setting_rl_about:
|
|
startActivity(AboutActivity.getIntent(this));
|
|
break;
|
|
case R.id.setting_rl_concerngame:
|
|
mSettingConcerngameSb.performClick();
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void fontTextSize(int i) {
|
|
switch (i) {
|
|
case 1:
|
|
mSettingSizeTv.setText("小字号");
|
|
break;
|
|
case 2:
|
|
mSettingSizeTv.setText("中字号");
|
|
break;
|
|
case 3:
|
|
mSettingSizeTv.setText("大字号");
|
|
break;
|
|
case 4:
|
|
mSettingSizeTv.setText("特大字号");
|
|
break;
|
|
}
|
|
}
|
|
|
|
//设置正文字号
|
|
private void fontSize() {
|
|
View inflate = View.inflate(this, R.layout.dialog_font_size, null);
|
|
final RadioGroup radioGroup = (RadioGroup) inflate.findViewById(R.id.font_size_radiogroup);
|
|
((RadioButton) (radioGroup.getChildAt(checkSizeIndex - 1))).setChecked(true);
|
|
AlertDialog alertDialog = new AlertDialog.Builder(this, R.style.GhAlertDialog)
|
|
.setTitle(getString(R.string.font_primary))
|
|
.setPositiveButton("取消", new DialogInterface.OnClickListener() {
|
|
@Override
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
}
|
|
})
|
|
.setNegativeButton("确认", new DialogInterface.OnClickListener() {
|
|
@Override
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
checkSizeIndex = radioGroup.getCheckedRadioButtonId() % 4;
|
|
|
|
if (checkSizeIndex == 0) {
|
|
checkSizeIndex = 4;
|
|
}
|
|
dialog.cancel();
|
|
saveCurrentSetting();
|
|
fontTextSize(checkSizeIndex);
|
|
}
|
|
})
|
|
.setView(inflate)
|
|
.create();
|
|
alertDialog.show();
|
|
|
|
|
|
// final Dialog dialog = new Dialog(this);
|
|
// 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);
|
|
// ((RadioButton) (radioGroup.getChildAt(checkSizeIndex - 1))).setChecked(true);
|
|
//
|
|
// tv_negative.setOnClickListener(new OnClickListener() {
|
|
// @Override
|
|
// public void onClick(View v) {
|
|
// dialog.cancel();
|
|
// }
|
|
// });
|
|
// tv_positive.setOnClickListener(new OnClickListener() {
|
|
// @Override
|
|
// public void onClick(View v) {
|
|
// checkSizeIndex = radioGroup.getCheckedRadioButtonId() % 4;
|
|
//
|
|
// if (checkSizeIndex == 0) {
|
|
// checkSizeIndex = 4;
|
|
// }
|
|
// dialog.cancel();
|
|
// saveCurrentSetting();
|
|
// fontTextSize(checkSizeIndex);
|
|
// }
|
|
// });
|
|
// dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
|
// dialog.setContentView(inflate);
|
|
// dialog.show();
|
|
}
|
|
|
|
// 清除缓存
|
|
private void clearCache() {
|
|
|
|
Observable.create(new OnSubscribe<Object>() {
|
|
@Override
|
|
public void call(Subscriber<? super Object> subscriber) {
|
|
|
|
long start = System.currentTimeMillis();
|
|
FileUtils.deleteFolder(getCacheDir());
|
|
FileUtils.deleteFolder(getExternalCacheDir());
|
|
long time = System.currentTimeMillis() - start;
|
|
if (time < 1000) {
|
|
try {
|
|
sleep(1000 - time);
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
subscriber.onCompleted();
|
|
}
|
|
|
|
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Object>() {
|
|
@Override
|
|
public void onCompleted() {
|
|
if (loadingDialog != null) {
|
|
loadingDialog.dismiss();
|
|
}
|
|
mSettingCacheTv.setText(getCacheSize());
|
|
Toast.makeText(SettingActivity.this, "缓存清除成功", Toast.LENGTH_SHORT).show();
|
|
}
|
|
|
|
@Override
|
|
public void onError(Throwable e) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onNext(Object o) {
|
|
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
}
|