|
|
@@ -0,0 +1,307 @@
|
|
|
+package com.modular.apputils.activity;
|
|
|
+
|
|
|
+import android.app.Activity;
|
|
|
+import android.content.pm.ActivityInfo;
|
|
|
+import android.content.pm.PackageManager;
|
|
|
+import android.content.res.Configuration;
|
|
|
+import android.content.res.Resources;
|
|
|
+import android.os.Build;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.support.annotation.NonNull;
|
|
|
+import android.support.v4.app.ActivityCompat;
|
|
|
+import android.support.v4.content.ContextCompat;
|
|
|
+import android.util.DisplayMetrics;
|
|
|
+import android.util.SparseArray;
|
|
|
+import android.view.View;
|
|
|
+
|
|
|
+import com.common.LogUtil;
|
|
|
+import com.common.config.BaseConfig;
|
|
|
+import com.common.data.StringUtil;
|
|
|
+import com.common.preferences.PreferenceUtils;
|
|
|
+import com.core.app.ActionBackActivity;
|
|
|
+import com.core.app.Constants;
|
|
|
+import com.core.base.BaseActivity;
|
|
|
+import com.core.utils.CommonUtil;
|
|
|
+import com.core.widget.CustomProgressDialog;
|
|
|
+import com.me.network.app.http.HttpClient;
|
|
|
+import com.me.network.app.http.rx.Result2Listener;
|
|
|
+import com.me.network.app.http.rx.ResultListener;
|
|
|
+import com.me.network.app.http.rx.ResultSubscriber;
|
|
|
+import com.modular.apputils.listener.OnSmartHttpListener;
|
|
|
+import com.modular.apputils.network.Parameter;
|
|
|
+import com.modular.apputils.network.Success;
|
|
|
+import com.modular.apputils.network.Tags;
|
|
|
+import com.umeng.analytics.MobclickAgent;
|
|
|
+
|
|
|
+import java.util.Locale;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by Bitlike on 2018/1/10.
|
|
|
+ */
|
|
|
+
|
|
|
+public abstract class BaseNetActivity extends ActionBackActivity {
|
|
|
+ protected Activity ct;
|
|
|
+ private CustomProgressDialog progressDialog;
|
|
|
+ private HttpClient httpClient;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onResume() {
|
|
|
+ super.onResume();
|
|
|
+ MobclickAgent.onResume(ct);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onPause() {
|
|
|
+ super.onPause();
|
|
|
+ MobclickAgent.onPause(ct);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected final void onCreate(Bundle savedInstanceState) {
|
|
|
+ preOnCreacte();
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
+ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
|
|
|
+ progressDialog = CustomProgressDialog.createDialog(this);
|
|
|
+ //重启之后恢复到之前的语言
|
|
|
+ switchLanguage(PreferenceUtils.getString(this, "language", "rCN"));
|
|
|
+ initHttpConfig();
|
|
|
+ View layout = getLayoutView();
|
|
|
+ if (layout == null) {
|
|
|
+ int layoutId = getLayoutId();
|
|
|
+ if (layoutId > 0) {
|
|
|
+ setContentView(layoutId);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ setContentView(layout);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ init();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void preOnCreacte() {
|
|
|
+ }
|
|
|
+
|
|
|
+ protected abstract int getLayoutId();
|
|
|
+
|
|
|
+ protected View getLayoutView() {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected abstract void init() throws Exception;
|
|
|
+
|
|
|
+ protected abstract String getBaseUrl();
|
|
|
+
|
|
|
+
|
|
|
+ /**************网络请求**********************/
|
|
|
+
|
|
|
+ private void initHttpConfig() {
|
|
|
+ String baseUrl = getBaseUrl();
|
|
|
+ if (!StringUtil.isEmpty(baseUrl)) {
|
|
|
+ httpClient = new HttpClient.Builder(baseUrl).isDebug(true)
|
|
|
+ .connectTimeout(5000)
|
|
|
+ .readTimeout(5000).build();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ protected void requestHttp(Parameter.Builder builder, final OnSmartHttpListener onHttpListener) {
|
|
|
+ if (httpClient != null && builder != null) {
|
|
|
+ HttpClient.Builder httpBuilder = new HttpClient.Builder();
|
|
|
+ final Parameter parameter = builder.builder();
|
|
|
+ httpBuilder.addParams(parameter.getParams())
|
|
|
+ .addHeaders(parameter.getHeaders())
|
|
|
+ .method(parameter.getMode())
|
|
|
+ .url(parameter.getUrl());
|
|
|
+ if (parameter.autoProgress()) {
|
|
|
+ showProgress();
|
|
|
+ }
|
|
|
+ httpClient.Api().send(httpBuilder.build(), new ResultSubscriber<>(new Result2Listener<Object>() {
|
|
|
+ @Override
|
|
|
+ public void onResponse(Object o) {
|
|
|
+ if (onHttpListener != null) try {
|
|
|
+ Tags tags = parameter.getTag();
|
|
|
+ int record = 0;
|
|
|
+ if (tags != null) {
|
|
|
+ record = tags.getRecord();
|
|
|
+ }
|
|
|
+ Success success = new Success(o.toString(), tags);
|
|
|
+ onHttpListener.onSuccess(record, success);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ dismissProgress();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFailure(Object t) {
|
|
|
+ if (onHttpListener != null) try {
|
|
|
+ Tags tags = parameter.getTag();
|
|
|
+ int record = 0;
|
|
|
+ if (tags != null) {
|
|
|
+ record = tags.getRecord();
|
|
|
+ }
|
|
|
+ Success success = new Success(t.toString(), tags);
|
|
|
+ onHttpListener.onSuccess(record, success);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ dismissProgress();
|
|
|
+ }
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void requestCompanyHttp(Parameter.Builder builder, final OnSmartHttpListener onHttpListener) {
|
|
|
+ if (builder != null) {
|
|
|
+ String sessionId = CommonUtil.getSharedPreferences(ct, "sessionId");
|
|
|
+ String emCode = CommonUtil.getEmcode();
|
|
|
+ builder.addSuperParams("sessionId", sessionId);
|
|
|
+ builder.addSuperParams("master", CommonUtil.getMaster());
|
|
|
+ builder.addSuperParams("sessionUser", emCode);
|
|
|
+ builder.addSuperHeaders("sessionUser", emCode);
|
|
|
+ builder.addSuperHeaders("Cookie", "JSESSIONID=" + sessionId);
|
|
|
+ requestHttp(builder, onHttpListener);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /*****************************显示进度框***************************************/
|
|
|
+ protected void showProgress() {
|
|
|
+ showProgress(true, "", "");
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void showProgress(boolean cancelable) {
|
|
|
+ showProgress(cancelable, "", "");
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void showProgress(boolean cancelable, String message) {
|
|
|
+ showProgress(cancelable, "", message);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void showProgress(boolean cancelable, String title, String message) {
|
|
|
+ if (progressDialog != null) {
|
|
|
+ progressDialog.setMessage(message);
|
|
|
+ progressDialog.setMessage(title);
|
|
|
+ progressDialog.setCanceledOnTouchOutside(cancelable);
|
|
|
+ progressDialog.setCancelable(cancelable);
|
|
|
+ progressDialog.show();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void dismissProgress() {
|
|
|
+ if (progressDialog != null && progressDialog.isShowing()) {
|
|
|
+ progressDialog.dismiss();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /*********************切换语言***************************/
|
|
|
+ protected void switchLanguage(String language) {
|
|
|
+ Resources resources = getResources();
|
|
|
+ Configuration config = resources.getConfiguration();
|
|
|
+ DisplayMetrics dm = resources.getDisplayMetrics();
|
|
|
+ if (language.equals("en")) {
|
|
|
+ config.locale = Locale.ENGLISH;
|
|
|
+ } else if (language.equals("rCN")) {
|
|
|
+ config.locale = Locale.SIMPLIFIED_CHINESE;
|
|
|
+ } else if (language.equals("rTW")) {
|
|
|
+ config.locale = Locale.TRADITIONAL_CHINESE;
|
|
|
+ } else if (language.equals("sys")) {
|
|
|
+ config.locale = Locale.getDefault();
|
|
|
+ }
|
|
|
+ resources.updateConfiguration(config, dm);
|
|
|
+ PreferenceUtils.putString("language", language);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /*********************处理权限问题*********************/
|
|
|
+ private SparseArray<Runnable> allowablePermissionRunnables;
|
|
|
+ private SparseArray<Runnable> disallowablePermissionRunnables;
|
|
|
+ private int permissionItem = 0;
|
|
|
+
|
|
|
+ /***********************判断是否缺少权限**********************/
|
|
|
+ protected boolean lacksPermission(String permission) {
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
+ return ContextCompat.checkSelfPermission(ct, permission) != PackageManager.PERMISSION_GRANTED;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 请求权限,先判断,如果没有权限就去请求
|
|
|
+ * 尽量不将权限请求放在onResume 中,会出现不断循环请求
|
|
|
+ *
|
|
|
+ * @param permission 权限
|
|
|
+ * @param allowableRunnable 当取得权限后执行操作,主线程
|
|
|
+ * @param disallowableRunnable 当用户拒绝权限后执行操作,主线程
|
|
|
+ */
|
|
|
+ public void requestPermission(String permission, Runnable allowableRunnable, Runnable disallowableRunnable) {
|
|
|
+ permissionItem++;
|
|
|
+ if (allowableRunnable != null) {
|
|
|
+ if (allowablePermissionRunnables == null) {
|
|
|
+ allowablePermissionRunnables = new SparseArray<>();
|
|
|
+ }
|
|
|
+ allowablePermissionRunnables.put(permissionItem, allowableRunnable);
|
|
|
+ }
|
|
|
+ if (disallowableRunnable != null) {
|
|
|
+ if (disallowablePermissionRunnables == null) {
|
|
|
+ disallowablePermissionRunnables = new SparseArray<>();
|
|
|
+ }
|
|
|
+ disallowablePermissionRunnables.put(permissionItem, disallowableRunnable);
|
|
|
+ }
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
+ //减少是否拥有权限
|
|
|
+ int checkCallPhonePermission = ContextCompat.checkSelfPermission(getApplicationContext(), permission);
|
|
|
+ if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {//没有获取到权限
|
|
|
+// if (!shouldShowRequestPermissionRationale(permission)) {
|
|
|
+ //弹出对话框接收权限
|
|
|
+ ActivityCompat.requestPermissions(ct, new String[]{permission}, permissionItem);
|
|
|
+// } else {
|
|
|
+// ToastUtil.showToast(ct, R.string.not_camera_permission);
|
|
|
+// }
|
|
|
+ } else {
|
|
|
+ if (allowableRunnable != null) {
|
|
|
+ allowableRunnable.run();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (allowableRunnable != null) {
|
|
|
+ allowableRunnable.run();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
+ super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
+ if (permissions != null) {
|
|
|
+ for (String p : permissions) {
|
|
|
+ LogUtil.i("permission=" + p);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (grantResults != null && grantResults.length > 0) {
|
|
|
+ if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
|
+ if (allowablePermissionRunnables != null) {
|
|
|
+ Runnable allowRun = allowablePermissionRunnables.get(requestCode);
|
|
|
+ if (allowRun != null) {
|
|
|
+ allowRun.run();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ if (disallowablePermissionRunnables != null) {
|
|
|
+ Runnable disallowRun = disallowablePermissionRunnables.get(requestCode);
|
|
|
+ if (disallowRun != null) {
|
|
|
+ disallowRun.run();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|