024338a068b6200a00acb3cb6955746cb73bb93c.svn-base 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. @Override
  250. public void onNetConnected(NetworkType networkType) {
  251. //Log.e("NetConnected", getActivity().getComponentName().getClassName()+":网络已连接");
  252. final List<NetReConnectEntity> netReConnectEntities = NetReConnectDaoOpe.queryAll(this);
  253. if (netReConnectEntities.size() == 0) {
  254. // toast("wu");
  255. return;
  256. }
  257. for (int i = 0; i < netReConnectEntities.size(); i++) {
  258. final NetReConnectEntity netReConnectEntity = netReConnectEntities.get(i);
  259. Map<String, Object> mapFromJson = getMapFromJson(netReConnectEntity.getJsonmap());
  260. final int finalI = i;
  261. RxHttpUtils.createApi(ApiService.class)
  262. .netReConnect("" + netReConnectEntity.getInterfaceName(), "" + netReConnectEntity.getInterfaceUrl(), mapFromJson)
  263. .compose(Transformer.<ResponseBody>switchSchedulers())
  264. .subscribe(new CommonObserver<ResponseBody>() {
  265. @Override
  266. protected void onError(String errorMsg) {
  267. toast(errorMsg + "");
  268. }
  269. @Override
  270. protected void onSuccess(ResponseBody data) {
  271. NetReConnectDaoOpe.deleteByKeyData(getActivity(), netReConnectEntity.getId());
  272. try {
  273. String json = data.string();
  274. VerificationCodeBean verificationCodeBean = new Gson().fromJson(json, VerificationCodeBean.class);
  275. if (verificationCodeBean.getCode() != 200) {
  276. toast(verificationCodeBean.getMsg());
  277. }
  278. Gson gson = new Gson();
  279. if (netReConnectEntity.getType() == 1) {
  280. VnDetailBean vnDetailBean = gson.fromJson(json, VnDetailBean.class);
  281. if (vnDetailBean.getData() != null) {
  282. String vnDetailBeanTostr = new Gson().toJson(vnDetailBean);
  283. NRCReportEntity nrcReportEntity = new NRCReportEntity();
  284. nrcReportEntity.setVnDetailBean(vnDetailBeanTostr);
  285. nrcReportEntity.setIsWarning(netReConnectEntity.getIsWarning());
  286. NRCReportDaoOpe.insertData(getActivity(), nrcReportEntity);
  287. }
  288. Log.e("netReConnectEntity", "请求成功" + vnDetailBean.toString());
  289. } else if (netReConnectEntity.getType() == 2) {
  290. VnListBean vnListBean = gson.fromJson(json, VnListBean.class);
  291. Log.e("netReConnectEntity", "请求成功" + vnListBean.toString());
  292. }
  293. if (finalI == netReConnectEntities.size() - 1) {
  294. Log.e("netReConnectEntity", "最后一条");
  295. List<NRCReportEntity> nrcReportEntities = NRCReportDaoOpe.queryAll(getActivity());
  296. if (nrcReportEntities.size() > 0) {
  297. VnDetailBean vnDetailBean = new Gson().fromJson(nrcReportEntities.get(0).getVnDetailBean(), VnDetailBean.class);
  298. Intent in = new Intent(getActivity(), VnReportDetailForNctActivity.class);
  299. in.putExtra("dataBean", vnDetailBean.getData());
  300. in.putExtra("isWarning", nrcReportEntities.get(0).getIsWarning());
  301. in.putExtra("status", vnDetailBean.getData().getStatus());
  302. startActivity(in);
  303. }
  304. }
  305. } catch (IOException e) {
  306. e.printStackTrace();
  307. }
  308. }
  309. });
  310. }
  311. }
  312. @Override
  313. public void onNetDisconnected() {
  314. showNetDialog();
  315. }
  316. /**
  317. * map转string
  318. *
  319. * @param jsonString
  320. * @return
  321. */
  322. public Map<String, Object> getMapFromJson(String jsonString) {
  323. Map map = new Gson().fromJson(jsonString, Map.class);
  324. return map;
  325. }
  326. /**
  327. * string转map
  328. *
  329. * @param map
  330. * @return
  331. */
  332. public String mapToJson(Map<String, Object> map) {
  333. String jsonString = new Gson().toJson(map);
  334. return jsonString;
  335. }
  336. /**
  337. * 弹出设置网络框
  338. */
  339. private void showNetDialog() {
  340. //退出的操作
  341. new EvvmMsgDiago.Builder(getActivity())
  342. .setTitle("网络异常") // 标题可以不用填写
  343. .setConfirm("设置")
  344. .setCancel("取消") // 设置 null 表示不显示取消按钮
  345. //.setAutoDismiss(false) // 设置点击按钮后不关闭对话框
  346. .setListener(new EvvmMsgDiago.OnListener() {
  347. @Override
  348. public void onConfirm(Dialog dialog) {
  349. Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
  350. startActivity(intent);
  351. }
  352. @Override
  353. public void onCancel(Dialog dialog) {
  354. //toast("取消了");
  355. }
  356. })
  357. .show();
  358. }
  359. }