225 lines
8.4 KiB
Java
225 lines
8.4 KiB
Java
package com.gh.base;
|
||
|
||
import android.app.Activity;
|
||
import android.arch.lifecycle.Lifecycle;
|
||
import android.content.Intent;
|
||
import android.os.Bundle;
|
||
import android.os.Handler;
|
||
import android.os.Message;
|
||
import android.support.annotation.NonNull;
|
||
import android.text.TextUtils;
|
||
import android.view.Window;
|
||
|
||
import com.gh.common.util.DataUtils;
|
||
import com.gh.common.util.DialogUtils;
|
||
import com.gh.common.util.PackageUtils;
|
||
import com.gh.common.util.RunningUtils;
|
||
import com.gh.common.util.ShareUtils;
|
||
import com.gh.common.util.StringUtils;
|
||
import com.gh.gamecenter.LoginActivity;
|
||
import com.gh.gamecenter.R;
|
||
import com.gh.gamecenter.SuggestionActivity;
|
||
import com.gh.gamecenter.eventbus.EBShowDialog;
|
||
import com.gh.gamecenter.suggest.SuggestType;
|
||
import com.lightgame.download.FileUtils;
|
||
import com.lightgame.utils.Utils;
|
||
import com.tencent.tauth.Tencent;
|
||
|
||
import org.greenrobot.eventbus.EventBus;
|
||
import org.greenrobot.eventbus.Subscribe;
|
||
import org.greenrobot.eventbus.ThreadMode;
|
||
import org.json.JSONException;
|
||
import org.json.JSONObject;
|
||
|
||
import java.lang.ref.WeakReference;
|
||
import java.lang.reflect.Field;
|
||
import java.lang.reflect.Method;
|
||
import java.util.List;
|
||
|
||
import butterknife.ButterKnife;
|
||
import pub.devrel.easypermissions.EasyPermissions;
|
||
|
||
import static com.gh.common.util.EntranceUtils.KEY_ENTRANCE;
|
||
|
||
public abstract class BaseActivity extends BaseToolBarActivity implements EasyPermissions.PermissionCallbacks {
|
||
|
||
protected String mEntrance;
|
||
|
||
private boolean mIsPause;
|
||
private boolean mIsExistLogoutDialog;
|
||
|
||
protected final Handler mBaseHandler = new BaseHandler(this);
|
||
|
||
protected static class BaseHandler extends Handler {
|
||
private final WeakReference<BaseActivity> mActivityWeakReference;
|
||
|
||
BaseHandler(BaseActivity activity) {
|
||
mActivityWeakReference = new WeakReference<>(activity);
|
||
}
|
||
|
||
@Override
|
||
public void handleMessage(Message msg) {
|
||
super.handleMessage(msg);
|
||
BaseActivity activity = mActivityWeakReference.get();
|
||
if (activity != null) activity.handleMessage(msg);
|
||
}
|
||
}
|
||
|
||
protected void handleMessage(Message msg) {
|
||
|
||
}
|
||
|
||
//接收QQ或者QQ空间分享回调
|
||
@Override
|
||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||
super.onActivityResult(requestCode, resultCode, data);
|
||
if (requestCode == com.tencent.connect.common.Constants.REQUEST_QQ_SHARE
|
||
|| requestCode == com.tencent.connect.common.Constants.REQUEST_QZONE_SHARE) {
|
||
Tencent.onActivityResultData(requestCode, resultCode, data, ShareUtils.getInstance(this).QqShareListener);
|
||
}
|
||
}
|
||
|
||
// 小米沉浸式黑色字体
|
||
public void setStatusBarDarkMode(boolean darkmode, Activity activity) {
|
||
Class<? extends Window> clazz = activity.getWindow().getClass();
|
||
try {
|
||
int darkModeFlag = 0;
|
||
Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
|
||
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
|
||
darkModeFlag = field.getInt(layoutParams);
|
||
Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
|
||
extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
|
||
} catch (Exception ignore) {
|
||
|
||
}
|
||
}
|
||
|
||
@Override
|
||
protected void onCreate(Bundle savedInstanceState) {
|
||
super.onCreate(savedInstanceState);
|
||
setStatusBarDarkMode(true, this);
|
||
EventBus.getDefault().register(this);
|
||
ButterKnife.bind(this);
|
||
mEntrance = getIntent().getStringExtra(KEY_ENTRANCE);
|
||
}
|
||
|
||
@Override
|
||
protected void onDestroy() {
|
||
EventBus.getDefault().unregister(this);
|
||
mBaseHandler.removeCallbacksAndMessages(null);
|
||
super.onDestroy();
|
||
}
|
||
|
||
@Override
|
||
protected boolean onNavigationIconClicked() {
|
||
onBackPressed();
|
||
return true;
|
||
}
|
||
|
||
public void toast(String msg) {
|
||
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
|
||
Utils.toast(this, msg);
|
||
}
|
||
|
||
public void toast(int msg) {
|
||
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
|
||
toast(getString(msg));
|
||
}
|
||
|
||
public void showShare(String url, String icon, String shareTitle, String shareSummary, ShareUtils.ShareType shareType) {
|
||
|
||
ShareUtils.getInstance(this).showShareWindows(this, getWindow().getDecorView(), url, icon, shareTitle, shareSummary, shareType);
|
||
|
||
if (shareType == ShareUtils.ShareType.game || shareType == ShareUtils.ShareType.plugin) {
|
||
DataUtils.onEvent(this, "内容分享", shareTitle + shareSummary);
|
||
} else {
|
||
DataUtils.onEvent(this, "内容分享", shareTitle);
|
||
}
|
||
}
|
||
|
||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||
public void onEventMainThread(final EBShowDialog showDialog) {
|
||
if (!mIsPause && this.getClass().getName().equals(RunningUtils.getTopActivity(this))) {
|
||
if ("hijack".equals(showDialog.getType())) {
|
||
DialogUtils.showQqSessionDialog(this, "2586716223");// 建议用户联系客服
|
||
} else if ("plugin".equals(showDialog.getType())) {
|
||
DialogUtils.showPluginDialog(this, new DialogUtils.ConfirmListener() {
|
||
@Override
|
||
public void onConfirm() {
|
||
if (FileUtils.isEmptyFile(showDialog.getPath())) {
|
||
toast(R.string.install_failure_hint);
|
||
} else {
|
||
startActivity(PackageUtils.getUninstallIntent(BaseActivity.this, showDialog.getPath()));
|
||
}
|
||
}
|
||
});
|
||
} else if ("loginException".equals(showDialog.getType())) {
|
||
if (mIsExistLogoutDialog) return;
|
||
mIsExistLogoutDialog = true;
|
||
try {
|
||
JSONObject object = new JSONObject(showDialog.getPath());
|
||
JSONObject device = object.getJSONObject("device");
|
||
String manufacturer = device.getString("manufacturer");
|
||
String model = device.getString("model");
|
||
DialogUtils.showAlertDialog(this, "你的账号已在另外一台设备登录"
|
||
, StringUtils.buildString("(", manufacturer, " - ", model, ")")
|
||
, "知道了", "重新登录"
|
||
, null
|
||
, () -> startActivity(LoginActivity.getIntent(BaseActivity.this))
|
||
);
|
||
mBaseHandler.postDelayed(() -> mIsExistLogoutDialog = false, 5000);
|
||
} catch (JSONException e) {
|
||
e.printStackTrace();
|
||
}
|
||
} else if ("notfound".equals(showDialog.getType())) {
|
||
DialogUtils.showAlertDialog(this, "下载失败", "下载链接已失效,建议提交反馈"
|
||
, "立即反馈", "取消"
|
||
, () -> {
|
||
SuggestionActivity.startSuggestionActivity(this, SuggestType.gameQuestion,
|
||
null, showDialog.getPath() + ",问题反馈:下载链接失效");
|
||
}, null);
|
||
}
|
||
}
|
||
}
|
||
|
||
@Override
|
||
protected void onPause() {
|
||
super.onPause();
|
||
mIsPause = true;
|
||
}
|
||
|
||
@Override
|
||
protected void onResume() {
|
||
super.onResume();
|
||
mIsPause = false;
|
||
}
|
||
|
||
@Override
|
||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
|
||
}
|
||
|
||
@Override
|
||
public void onPermissionsDenied(int requestCode, List<String> perms) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onPermissionsGranted(int requestCode, List<String> perms) {
|
||
|
||
}
|
||
|
||
|
||
public static String mergeEntranceAndPath(String entrance, String path) {
|
||
if (TextUtils.isEmpty(entrance) && TextUtils.isEmpty(path)) return "";
|
||
if (TextUtils.isEmpty(entrance) && !TextUtils.isEmpty(path)) {
|
||
return StringUtils.buildString("(", path, ")");
|
||
}
|
||
if (!TextUtils.isEmpty(entrance) && TextUtils.isEmpty(path)) {
|
||
return entrance;
|
||
}
|
||
return StringUtils.buildString(entrance, "+(", path, ")");
|
||
}
|
||
}
|