package com.gh.gamecenter; 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.preference.PreferenceManager; import android.support.annotation.NonNull; import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RelativeLayout; import android.widget.TextView; import com.gh.base.BaseActivity; import com.gh.common.util.DialogUtils; import com.gh.common.util.EntranceUtils; import com.gh.common.util.LoginUtils; import com.gh.common.util.StringUtils; import com.gh.gamecenter.entity.LoginResponseEntity; import com.gh.gamecenter.eventbus.EBReuse; import com.gh.gamecenter.eventbus.EBSkip; import com.kyleduo.switchbutton.SwitchButton; import com.lightgame.download.FileUtils; import com.lightgame.utils.Utils; import com.tencent.bugly.beta.Beta; 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; /** * 游戏设置页面 * * @ 吕方 * @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; @BindView(R.id.setting_logout_tv) TextView mSettingLoginType; @BindView(R.id.setting_logout_rl) RelativeLayout mSettingLoginRl; 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 = PreferenceManager.getDefaultSharedPreferences(this); // 未打开下载按钮,显示修复下载按钮 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); initLoginStatus(); } private void initLoginStatus() { LoginResponseEntity loginToken = LoginUtils.getLoginToken(this); if (loginToken != null) { if (!TextUtils.isEmpty(loginToken.getLoginType())) { String loginType = loginToken.getLoginType().trim(); switch (loginType) { case "qq": loginType = "QQ"; break; case "wechat": loginType = "微信"; break; case "weibo": loginType = "新浪微博"; break; default: if (loginType.length() == 11) { String sub1 = loginType.substring(0, 3); String sub2 = loginType.substring(9, 11); loginType = StringUtils.buildString(sub1, "******", sub2); } break; } mSettingLoginType.setText(loginType); } mSettingLoginRl.setVisibility(View.VISIBLE); } else { mSettingLoginRl.setVisibility(View.GONE); } } @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, R.id.setting_logout_rl }) @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: if (BuildConfig.DEBUG) { Beta.checkUpgrade(); // 手动检查热补丁 } startActivity(AboutActivity.getIntent(this)); break; case R.id.setting_rl_concerngame: mSettingConcerngameSb.performClick(); break; case R.id.setting_logout_rl: DialogUtils.showForceDialog(this, "注销登录", "退出账号即会回到游客状态,很多功能将无法使用(例如评论、客服消息),确定退出吗?", "确定退出", "取消", new DialogUtils.ConfirmListener() { @Override public void onConfirm() { loadingDialog = DialogUtils.showWaitDialog(SettingActivity.this, "退出登录中..."); LoginUtils.logout(SettingActivity.this, new LoginUtils.OnLogoutListener() { @Override public void onCompleted() { LoginUtils.cleanUserData(SettingActivity.this); if (loadingDialog != null) loadingDialog.dismiss(); finish(); } }); } }, null); break; 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(); TextView mesage = (TextView) alertDialog.findViewById(android.R.id.message); Button positiveBtn = alertDialog.getButton(android.app.AlertDialog.BUTTON_POSITIVE); Button negativeBtn = alertDialog.getButton(android.app.AlertDialog.BUTTON_NEGATIVE); positiveBtn.setTextSize(14); positiveBtn.setTextColor(ContextCompat.getColor(this, R.color.theme)); negativeBtn.setTextSize(14); negativeBtn.setTextColor(ContextCompat.getColor(this, R.color.theme)); if (mesage != null) { mesage.setTextSize(14); mesage.setTextColor(ContextCompat.getColor(this, R.color.system_bar)); } // 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() { @Override public void call(Subscriber 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() { @Override public void onCompleted() { if (loadingDialog != null) { loadingDialog.dismiss(); } mSettingCacheTv.setText(getCacheSize()); Utils.toast(SettingActivity.this, "缓存清除成功"); } @Override public void onError(Throwable e) { } @Override public void onNext(Object o) { } }); } }