481d7fb52c7a2f53b78caa12b5161d74a57759a9.svn-base 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. package eVVM.apk.common;
  2. import android.app.Dialog;
  3. import android.content.Intent;
  4. import android.content.pm.ActivityInfo;
  5. import android.graphics.drawable.Drawable;
  6. import android.provider.Settings;
  7. import android.support.annotation.DrawableRes;
  8. import android.support.annotation.Nullable;
  9. import android.support.annotation.StringRes;
  10. import android.util.Log;
  11. import android.view.Gravity;
  12. import android.view.View;
  13. import com.allen.library.RxHttpUtils;
  14. import com.allen.library.interceptor.Transformer;
  15. import com.allen.library.observer.CommonObserver;
  16. import com.google.gson.Gson;
  17. import com.hjq.bar.OnTitleBarListener;
  18. import com.hjq.bar.TitleBar;
  19. import com.hjq.base.BaseDialog;
  20. import com.hjq.dialog.MenuDialog;
  21. import com.hjq.toast.ToastUtils;
  22. import com.tianma.netdetector.lib.NetStateChangeObserver;
  23. import com.tianma.netdetector.lib.NetStateChangeReceiver;
  24. import com.tianma.netdetector.lib.NetworkType;
  25. import java.io.IOException;
  26. import java.util.ArrayList;
  27. import java.util.List;
  28. import java.util.Map;
  29. import butterknife.ButterKnife;
  30. import butterknife.Unbinder;
  31. import eVVM.apk.api.ApiService;
  32. import eVVM.apk.app.MyApplication;
  33. import eVVM.apk.db.netteconnectdb.NetReConnectDaoOpe;
  34. import eVVM.apk.db.nrcreportdb.NRCReportDaoOpe;
  35. import eVVM.apk.entity.NRCReportEntity;
  36. import eVVM.apk.entity.NetReConnectEntity;
  37. import eVVM.apk.helper.ActivityStackManager;
  38. import eVVM.apk.helper.DebugUtils;
  39. import eVVM.apk.helper.GPS.LocationUtils;
  40. import eVVM.apk.helper.NfcUtil;
  41. import eVVM.apk.other.EventBusManager;
  42. import eVVM.apk.other.StatusManager;
  43. import eVVM.apk.ui.bean.VerificationCodeBean;
  44. import eVVM.apk.ui.bean.VnDetailBean;
  45. import eVVM.apk.ui.bean.VnListBean;
  46. import eVVM.apk.ui.vaccination.VnReportDetailForNctActivity;
  47. import eVVM.apk.widget.EvvmMsgDiago;
  48. import okhttp3.ResponseBody;
  49. public abstract class MyActivity extends UIActivity implements OnTitleBarListener, NetStateChangeObserver {
  50. @Override
  51. protected void initActivity() {
  52. super.initActivity();
  53. ActivityStackManager.getInstance().onActivityCreated(this);
  54. }
  55. // ButterKnife 注解
  56. private Unbinder mButterKnife;
  57. @Override
  58. protected void initLayout() {
  59. super.initLayout();
  60. // 初始化标题栏的监听
  61. if (getTitleId() > 0) {
  62. if (findViewById(getTitleId()) instanceof TitleBar) {
  63. ((TitleBar) findViewById(getTitleId())).setOnTitleBarListener(this);
  64. }
  65. }
  66. mButterKnife = ButterKnife.bind(this);
  67. EventBusManager.register(this);
  68. initOrientation();
  69. }
  70. /**
  71. * 初始化横竖屏方向,会和 LauncherTheme 主题样式有冲突,注意不要同时使用
  72. */
  73. protected void initOrientation() {
  74. // 当前 Activity 不能是透明的并且没有指定屏幕方向,默认设置为竖屏
  75. if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
  76. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  77. }
  78. }
  79. /**
  80. * 设置标题栏的标题
  81. */
  82. @Override
  83. public void setTitle(int titleId) {
  84. setTitle(getText(titleId));
  85. }
  86. /**
  87. * 设置标题栏的标题
  88. */
  89. @Override
  90. public void setTitle(CharSequence title) {
  91. super.setTitle(title);
  92. TitleBar titleBar = getTitleBar();
  93. if (titleBar != null) {
  94. titleBar.setTitle(title);
  95. }
  96. }
  97. @Nullable
  98. public TitleBar getTitleBar() {
  99. if (getTitleId() > 0 && findViewById(getTitleId()) instanceof TitleBar) {
  100. return findViewById(getTitleId());
  101. }
  102. return null;
  103. }
  104. @Override
  105. public boolean statusBarDarkFont() {
  106. //返回true表示黑色字体
  107. return true;
  108. }
  109. /**
  110. * {@link OnTitleBarListener}
  111. */
  112. // TitleBar 左边的View被点击了
  113. @Override
  114. public void onLeftClick(View v) {
  115. onBackPressed();
  116. }
  117. // TitleBar 中间的View被点击了
  118. @Override
  119. public void onTitleClick(View v) {
  120. }
  121. // TitleBar 右边的View被点击了
  122. @Override
  123. public void onRightClick(View v) {
  124. }
  125. @Override
  126. protected void onResume() {
  127. super.onResume();
  128. // UmengClient.onResume(this);
  129. if (needRegisterNetworkChangeObserver()) {
  130. NetStateChangeReceiver.registerObserver(this);
  131. }
  132. }
  133. @Override
  134. protected void onPause() {
  135. // UmengClient.onPause(this);
  136. super.onPause();
  137. }
  138. @Override
  139. protected void onStop() {
  140. super.onStop();
  141. if (needRegisterNetworkChangeObserver()) {
  142. NetStateChangeReceiver.unregisterObserver(this);
  143. }
  144. }
  145. @Override
  146. protected void onDestroy() {
  147. super.onDestroy();
  148. if (mButterKnife != null) mButterKnife.unbind();
  149. EventBusManager.unregister(this);
  150. ActivityStackManager.getInstance().onActivityDestroyed(this);
  151. }
  152. private List<String> toastList = new ArrayList<>();
  153. /**
  154. * 显示吐司
  155. */
  156. public void toast(CharSequence s) {
  157. // ToastUtils.show(s);
  158. toastList.clear();
  159. toastList.add(s.toString());
  160. final BaseDialog show = new MenuDialog.Builder(this)
  161. .setCancel(null) // 设置 null 表示不显示取消按钮
  162. .setAutoDismiss(false) // 设置点击按钮后不关闭对话框
  163. .setList(toastList)
  164. .setGravity(Gravity.BOTTOM)
  165. .setAnimStyle(BaseDialog.AnimStyle.BOTTOM)
  166. .show();
  167. postDelayed(new Runnable() {
  168. @Override
  169. public void run() {
  170. show.dismiss();
  171. }
  172. }, 2000);
  173. }
  174. public void toast(@StringRes int id) {
  175. ToastUtils.show(getString(id));
  176. }
  177. public void toast(Object object) {
  178. ToastUtils.show(object);
  179. }
  180. /**
  181. * 打印日志
  182. */
  183. public void log(Object object) {
  184. if (DebugUtils.isDebug(this)) {
  185. Log.v(getClass().getSimpleName(), object != null ? object.toString() : "null");
  186. }
  187. }
  188. /**
  189. * 获取当前的 Application 对象
  190. */
  191. public final MyApplication getMyApplication() {
  192. return (MyApplication) getApplication();
  193. }
  194. private final StatusManager mStatusManager = new StatusManager();
  195. /**
  196. * 显示加载中
  197. */
  198. public void showLoading() {
  199. mStatusManager.showLoading(this);
  200. }
  201. /**
  202. * 显示加载完成
  203. */
  204. public void showComplete() {
  205. mStatusManager.showComplete();
  206. }
  207. /**
  208. * 显示空提示
  209. */
  210. public void showEmpty() {
  211. mStatusManager.showEmpty(getContentView());
  212. }
  213. /**
  214. * 显示错误提示
  215. */
  216. public void showError() {
  217. mStatusManager.showError(getContentView());
  218. }
  219. /**
  220. * 显示自定义提示
  221. */
  222. public void showLayout(@DrawableRes int iconId, @StringRes int textId) {
  223. mStatusManager.showLayout(getContentView(), iconId, textId);
  224. }
  225. public void showLayout(Drawable drawable, CharSequence hint) {
  226. mStatusManager.showLayout(getContentView(), drawable, hint);
  227. }
  228. /**
  229. * 判断是否支持GPS和NFC功能
  230. */
  231. protected boolean checkGpsAndNfc() {
  232. switch (NfcUtil.isOPen(MyActivity.this)) {
  233. case 0:
  234. toast("您的手机没有NFC功能, 不能使用");
  235. return false;
  236. case 1:
  237. toast("NFC功能未开启");
  238. return false;
  239. }
  240. if (!LocationUtils.isOPen(MyActivity.this)) {
  241. toast("GPS功能未开启");
  242. return false;
  243. }
  244. return true;
  245. }
  246. protected boolean needRegisterNetworkChangeObserver() {
  247. return true;
  248. }
  249. private static int readErrorCounts = 0;
  250. @Override
  251. public void onNetConnected(NetworkType networkType) {
  252. readErrorCounts = 0;
  253. //Log.e("NetConnected", getActivity().getComponentName().getClassName()+":网络已连接");
  254. //Log.e("","");
  255. final List<NetReConnectEntity> netReConnectEntities = NetReConnectDaoOpe.queryAll(this);
  256. Log.e("onNetConnected", "" + netReConnectEntities.toString());
  257. if (netReConnectEntities.size() == 0) {
  258. // toast("wu");
  259. return;
  260. }
  261. for (int i = 0; i < netReConnectEntities.size(); i++) {
  262. final NetReConnectEntity netReConnectEntity = netReConnectEntities.get(i);
  263. Map<String, Object> mapFromJson = getMapFromJson(netReConnectEntity.getJsonmap());
  264. final int finalI = i;
  265. RxHttpUtils.createApi(ApiService.class)
  266. .netReConnect("" + netReConnectEntity.getInterfaceName(), "" + netReConnectEntity.getInterfaceUrl(), mapFromJson)
  267. .compose(Transformer.<ResponseBody>switchSchedulers())
  268. .subscribe(new CommonObserver<ResponseBody>() {
  269. @Override
  270. protected void onError(String errorMsg) {
  271. toast(errorMsg + "");
  272. }
  273. @Override
  274. protected void onSuccess(ResponseBody data) {
  275. NetReConnectDaoOpe.deleteByKeyData(getActivity(), netReConnectEntity.getId());
  276. String json = null;
  277. try {
  278. json = data.string();
  279. } catch (IOException e) {
  280. e.printStackTrace();
  281. }
  282. VerificationCodeBean verificationCodeBean = new Gson().fromJson(json, VerificationCodeBean.class);
  283. if (verificationCodeBean.getCode() != 200) {
  284. //ToastUtils.show(verificationCodeBean.getMsg());
  285. readErrorCounts += 1;
  286. }
  287. Gson gson = new Gson();
  288. if (netReConnectEntity.getType() == 1) {
  289. VnDetailBean vnDetailBean = gson.fromJson(json, VnDetailBean.class);
  290. if (vnDetailBean.getData() != null) {
  291. String vnDetailBeanTostr = new Gson().toJson(vnDetailBean);
  292. NRCReportEntity nrcReportEntity = new NRCReportEntity();
  293. nrcReportEntity.setVnDetailBean(vnDetailBeanTostr);
  294. nrcReportEntity.setIsWarning(netReConnectEntity.getIsWarning());
  295. NRCReportDaoOpe.insertData(getActivity(), nrcReportEntity);
  296. }
  297. Log.e("netReConnectEntity", "请求成功" + vnDetailBean.toString());
  298. } else if (netReConnectEntity.getType() == 2) {
  299. VnListBean vnListBean = gson.fromJson(json, VnListBean.class);
  300. Log.e("netReConnectEntity", "请求成功" + vnListBean.toString());
  301. }
  302. if (finalI == netReConnectEntities.size() - 1) {
  303. Log.e("netReConnectEntity", "最后一条");
  304. postDelayed(new Runnable() {
  305. @Override
  306. public void run() {
  307. List<NRCReportEntity> nrcReportEntities = NRCReportDaoOpe.queryAll(getActivity());
  308. if (nrcReportEntities.size() > 0) {
  309. VnDetailBean vnDetailBean = new Gson().fromJson(nrcReportEntities.get(0).getVnDetailBean(), VnDetailBean.class);
  310. Intent in = new Intent(getActivity(), VnReportDetailForNctActivity.class);
  311. in.putExtra("dataBean", vnDetailBean.getData());
  312. in.putExtra("isWarning", nrcReportEntities.get(0).getIsWarning());
  313. in.putExtra("status", vnDetailBean.getData().getStatus());
  314. in.putExtra("readErrorCounts", readErrorCounts);
  315. startActivity(in);
  316. } else {
  317. if (readErrorCounts > 0) {
  318. ToastUtils.show("检测到无网期间有" + readErrorCounts + "个芯片有异常。");
  319. }
  320. }
  321. }
  322. }, 1000);
  323. }
  324. }
  325. });
  326. }
  327. }
  328. @Override
  329. public void onNetDisconnected() {
  330. showNetDialog();
  331. }
  332. /**
  333. * map转string
  334. *
  335. * @param jsonString
  336. * @return
  337. */
  338. public Map<String, Object> getMapFromJson(String jsonString) {
  339. Map map = new Gson().fromJson(jsonString, Map.class);
  340. return map;
  341. }
  342. /**
  343. * string转map
  344. *
  345. * @param map
  346. * @return
  347. */
  348. public String mapToJson(Map<String, Object> map) {
  349. String jsonString = new Gson().toJson(map);
  350. return jsonString;
  351. }
  352. /**
  353. * 弹出设置网络框
  354. */
  355. private void showNetDialog() {
  356. //退出的操作
  357. new EvvmMsgDiago.Builder(getActivity())
  358. .setTitle("网络异常") // 标题可以不用填写
  359. .setConfirm("设置")
  360. .setCancel("取消") // 设置 null 表示不显示取消按钮
  361. //.setAutoDismiss(false) // 设置点击按钮后不关闭对话框
  362. .setListener(new EvvmMsgDiago.OnListener() {
  363. @Override
  364. public void onConfirm(Dialog dialog) {
  365. Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
  366. startActivity(intent);
  367. }
  368. @Override
  369. public void onCancel(Dialog dialog) {
  370. //toast("取消了");
  371. }
  372. })
  373. .show();
  374. }
  375. }