368fe546530a364784066d8708c0384fa3ebdb81.svn-base 15 KB

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