BaseActivity.java 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. package com.edufound.reader.base;
  2. import android.animation.Animator;
  3. import android.animation.AnimatorListenerAdapter;
  4. import android.animation.AnimatorSet;
  5. import android.animation.ObjectAnimator;
  6. import android.annotation.SuppressLint;
  7. import android.app.Activity;
  8. import android.content.Intent;
  9. import android.content.IntentFilter;
  10. import android.os.Build;
  11. import android.os.Bundle;
  12. import android.os.Handler;
  13. import android.os.Message;
  14. import android.util.DisplayMetrics;
  15. import android.view.Gravity;
  16. import android.view.LayoutInflater;
  17. import android.view.MotionEvent;
  18. import android.view.View;
  19. import android.view.ViewGroup;
  20. import android.view.WindowManager;
  21. import android.view.animation.LinearInterpolator;
  22. import android.widget.FrameLayout;
  23. import android.widget.ImageView;
  24. import android.widget.Toast;
  25. import androidx.annotation.NonNull;
  26. import androidx.appcompat.app.AppCompatActivity;
  27. import com.alibaba.ailabs.genie.assistant.sdk.asr.ASRClient;
  28. import com.alibaba.ailabs.genie.assistant.sdk.asr.ASRCommandReturn;
  29. import com.alibaba.ailabs.genie.assistant.sdk.asr.AppContextData;
  30. import com.alibaba.ailabs.genie.assistant.sdk.asr.OnASRCommandListener;
  31. import com.alibaba.ailabs.genie.assistant.sdk.asr.OnGetMediaPolicyListener;
  32. import com.alibaba.fastjson.JSONObject;
  33. import com.baidu.duer.bot.directive.payload.AmountInfo;
  34. import com.baidu.duer.botsdk.BotIntent;
  35. import com.baidu.duer.botsdk.IAccountChargeMsgListener;
  36. import com.baidu.duer.membersdk.MemberSdkManager;
  37. import com.baidu.duer.membersdk.view.FloatingView;
  38. import com.edufound.reader.R;
  39. import com.edufound.reader.activity.TextActivity;
  40. import com.edufound.reader.annotation.BindView;
  41. import com.edufound.reader.apiserver.UserApiServerImpl;
  42. import com.edufound.reader.application.EApplication;
  43. import com.edufound.reader.bean.BaiDuUserInfo;
  44. import com.edufound.reader.bean.ChannelCodeEnum;
  45. import com.edufound.reader.bean.HttpResultBean;
  46. import com.edufound.reader.bean.SignBean;
  47. import com.edufound.reader.bean.UserBean;
  48. import com.edufound.reader.botsdk.BotRegisterListener;
  49. import com.edufound.reader.botsdk.IBotIntentCallback;
  50. import com.edufound.reader.cusview.CusToast;
  51. import com.edufound.reader.receiver.HomeKeyEventReceiver;
  52. import com.edufound.reader.receiver.NetworkChangeReceiver;
  53. import com.edufound.reader.receiver.ScreenListener;
  54. import com.edufound.reader.receiver.ShowFollowReceiver;
  55. import com.edufound.reader.receiver.ShowPageLoadingReceiver;
  56. import com.edufound.reader.tmailsdk.TMailSDKUtil;
  57. import com.edufound.reader.util.Consts;
  58. import com.edufound.reader.util.DeviceUuidFactory;
  59. import com.edufound.reader.util.EfunboxUtil;
  60. import com.edufound.reader.util.LiuHaiScreenUtil;
  61. import com.edufound.reader.util.MMKVEncodeKey;
  62. import com.edufound.reader.util.MMKVUtil;
  63. import com.edufound.reader.util.SizeUtils;
  64. import com.google.gson.Gson;
  65. import com.google.gson.reflect.TypeToken;
  66. import com.jakewharton.rxbinding4.view.RxView;
  67. import com.okhttplib.HttpInfo;
  68. import com.okhttplib.callback.Callback;
  69. import com.orhanobut.logger.Logger;
  70. import java.io.IOException;
  71. import java.lang.reflect.Field;
  72. import java.lang.reflect.InvocationTargetException;
  73. import java.lang.reflect.Method;
  74. import java.util.HashMap;
  75. import java.util.Map;
  76. import java.util.concurrent.TimeUnit;
  77. import io.reactivex.rxjava3.annotations.Nullable;
  78. import io.reactivex.rxjava3.disposables.CompositeDisposable;
  79. import io.reactivex.rxjava3.disposables.Disposable;
  80. import io.reactivex.rxjava3.functions.Consumer;
  81. public abstract class BaseActivity extends AppCompatActivity {
  82. private static final int SHOW_FOLLOW = 0x5512;
  83. private static final int SHOW_FIRST_TIP = 0x5513;
  84. private static final int TMAIL_INIT_SUCCESS = 0x5514;
  85. private static final int TMAIL_INIT_ERROR = 0x5515;
  86. public CompositeDisposable mCompositeDisposable;
  87. //按home键的receiver
  88. HomeKeyEventReceiver mHomeReceiver;
  89. //登录广播action
  90. //Gson
  91. Gson mGson;
  92. View mFirstIntoView;
  93. ImageView pageLoading;
  94. boolean pageLoadingShow = false;
  95. //百度浮窗
  96. private FloatingView mFloating;
  97. private boolean hasResume = false;
  98. ScreenListener screenListener;
  99. @Override
  100. protected void onCreate(@Nullable Bundle savedInstanceState) {
  101. super.onCreate(savedInstanceState);
  102. // EfunboxUtil.setCustomDensity(this, Consts.getmApplicAtion());
  103. EApplication.mActivityList.add(this);
  104. EfunboxUtil.initDesignSize(this);
  105. setContentView(this.getLayoutId());
  106. initBase();
  107. bindViews(this);
  108. initView();
  109. if (Consts.getUmengChannel().equals(ChannelCodeEnum.TMAIL.getChannelCode())) {
  110. mAliTVASRManager = new ASRClient(this);
  111. if (Consts.getTmailUserId().equals("-1")) {
  112. TMailSDKUtil.getUserId(getBaseContext());
  113. TMailSDKUtil.getDevieId(getBaseContext());
  114. }
  115. }
  116. }
  117. void initBase() {
  118. getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  119. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
  120. LiuHaiScreenUtil.openFullScreenModel(this);
  121. }
  122. mGson = new Gson();
  123. //获取录音权限
  124. // PermissionsUtil.verifyAudioPermissions(this);
  125. if (Consts.getScreenSize() == null) {
  126. DisplayMetrics outMetrics = new DisplayMetrics();
  127. getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
  128. int widthPixels = outMetrics.widthPixels;
  129. int heightPixels = outMetrics.heightPixels;
  130. int arr[] = {widthPixels, heightPixels};
  131. Consts.setScreenSize(arr);
  132. }
  133. mCompositeDisposable = new CompositeDisposable();
  134. mHomeReceiver = new HomeKeyEventReceiver();
  135. //注册home广播
  136. registerReceiver(mHomeReceiver, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
  137. // 注册网络广播
  138. NetworkChangeReceiver.registerReceiver(this);
  139. NetworkChangeReceiver.registerObserver(NetStateChangeObserver);
  140. //注册百度广播
  141. BotRegisterListener.registerListener(mBotIntentCallBack, mIAccountChargeMsgListener);
  142. //注册小红花广播
  143. ShowFollowReceiver.registerReceiver(this);
  144. ShowFollowReceiver.registerObserver(showFollow);
  145. ShowPageLoadingReceiver.registerReceiver(this);
  146. ShowPageLoadingReceiver.registerObserver(pageLoadingReceiver);
  147. screenListener = new ScreenListener(this);
  148. screenListener.begin(new ScreenListener.ScreenStateListener() {
  149. @Override
  150. public void onScreenOn() {
  151. Logger.e("onScreenOn");
  152. }
  153. @Override
  154. public void onScreenOff() {
  155. Logger.e("onScreenOff");
  156. onAppScreenOff();
  157. }
  158. @Override
  159. public void onUserPresent() {
  160. onScreenUserPresent();
  161. }
  162. });
  163. }
  164. /**
  165. * 设置布局
  166. *
  167. * @return
  168. */
  169. public abstract int getLayoutId();
  170. /**
  171. * 初始化视图
  172. */
  173. public abstract void initView();
  174. public static void bindViews(Activity activity) {
  175. Class<? extends Activity> activityClass = activity.getClass();//获取activity的class
  176. Field[] fields = activityClass.getDeclaredFields();//获取activity的字段
  177. //遍历所有的字段
  178. for (Field field : fields) {
  179. //获取该字段的注解
  180. BindView bindView = field.getAnnotation(BindView.class);
  181. //!=null 说明该字段有注解并且是指定的注解
  182. if (bindView != null) {
  183. //获取到注解总传入的数值value
  184. int viewId = bindView.id();
  185. if (viewId != -1) {
  186. try {
  187. //获取到activity中findViewById的方法
  188. Method findViewByIdMethod = activityClass.getMethod("findViewById", int.class);
  189. try {
  190. //执行findViewById方法
  191. Object resView = findViewByIdMethod.invoke(activity, viewId);
  192. //允许通过反射访问私有变量
  193. field.setAccessible(true);
  194. //把字段的值设置该view的实例
  195. field.set(activity, resView);
  196. } catch (IllegalAccessException e) {
  197. e.printStackTrace();
  198. } catch (InvocationTargetException e) {
  199. e.printStackTrace();
  200. }
  201. } catch (NoSuchMethodException e) {
  202. e.printStackTrace();
  203. }
  204. }
  205. }
  206. }
  207. }
  208. /**
  209. * 添加订阅
  210. */
  211. public void addDisposable(Disposable mDisposable) {
  212. if (mCompositeDisposable == null) {
  213. mCompositeDisposable = new CompositeDisposable();
  214. }
  215. mCompositeDisposable.add(mDisposable);
  216. }
  217. /**
  218. * 取消所有订阅
  219. */
  220. public void clearDisposable() {
  221. if (mCompositeDisposable != null) {
  222. mCompositeDisposable.clear();
  223. }
  224. }
  225. @SuppressLint("AutoDispose")
  226. public void addUiClickListener(View view, Consumer onNext) {
  227. addDisposable(RxView.clicks(view).throttleFirst(2, TimeUnit.SECONDS).subscribe(onNext));
  228. }
  229. public abstract void onGetDisconnect();
  230. public abstract void onGetMobileConnect();
  231. public abstract void onGetWifiConnect();
  232. NetworkChangeReceiver.NetStateChangeObserver NetStateChangeObserver = new NetworkChangeReceiver.NetStateChangeObserver() {
  233. @Override
  234. public void onDisconnect() {
  235. //断网了
  236. onGetDisconnect();
  237. // CusToast.getInstance(Consts.getmApplicAtion()).show("网络异常,请检查网络。", 2000);
  238. Toast.makeText(Consts.getmApplicAtion(), "网络异常,请检查网络。", Toast.LENGTH_SHORT).show();
  239. }
  240. @Override
  241. public void onMobileConnect() {
  242. //有网了(移动网络)
  243. onGetMobileConnect();
  244. }
  245. @Override
  246. public void onWifiConnect() {
  247. //有网了(wifi)
  248. onGetWifiConnect();
  249. }
  250. };
  251. protected void onLoginSuccess() {
  252. }
  253. public ViewGroup getRootView() {
  254. return findViewById(android.R.id.content);
  255. }
  256. public void showFollowCountAnim(String count) {
  257. // View mFollowView = LayoutInflater.from(this).inflate(R.layout.anim_getfollow, null);
  258. // FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  259. // ((TextView) mFollowView.findViewById(R.id.anim_getfollow_followcount)).setText("+" + count);
  260. // params.gravity = Gravity.CENTER;
  261. // mFollowView.setLayoutParams(params);
  262. // ObjectAnimator mRotationImgAnim = ObjectAnimator.ofFloat(mFollowView, "translationY", 0, -150);
  263. // mRotationImgAnim.setDuration(1500);
  264. // LinearInterpolator interpolator = new LinearInterpolator();
  265. // mRotationImgAnim.setInterpolator(interpolator); //设置匀速旋转,不卡顿 icon_anim.start();
  266. // ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(mFollowView, "alpha", 1f, 0f);
  267. // alphaAnim.setDuration(1500);
  268. // getRootView().addView(mFollowView);
  269. // AnimatorSet animatorSet = new AnimatorSet();
  270. // animatorSet.play(mRotationImgAnim).with(alphaAnim);
  271. // animatorSet.addListener(new AnimatorListenerAdapter() {
  272. // @Override
  273. // public void onAnimationEnd(Animator animation) {
  274. // super.onAnimationEnd(animation);
  275. // getRootView().removeView(mFollowView);
  276. // }
  277. // });
  278. // animatorSet.start();
  279. //
  280. for (int i = 0; i < Integer.valueOf(count); i++) {
  281. Message message = new Message();
  282. message.what = SHOW_FOLLOW;
  283. message.arg1 = i;
  284. baseHandler.sendMessageDelayed(message, i * 250);
  285. }
  286. }
  287. Handler baseHandler = new Handler(new Handler.Callback() {
  288. @Override
  289. public boolean handleMessage(@NonNull Message message) {
  290. switch (message.what) {
  291. //region Description 显示花朵
  292. case SHOW_FOLLOW:
  293. ImageView imageView = new ImageView(getBaseContext());
  294. imageView.setImageResource(R.drawable.testhua);
  295. FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(SizeUtils.dp2px(Consts.getmApplicAtion().getBaseContext(), 50), SizeUtils.dp2px(Consts.getmApplicAtion().getBaseContext(), 50));
  296. params.gravity = Gravity.RIGHT | Gravity.CENTER;
  297. params.topMargin = SizeUtils.dp2px(Consts.getmApplicAtion().getBaseContext(), 80);
  298. if (Integer.valueOf(message.arg1) % 2 == 0) {
  299. //双数
  300. } else {
  301. params.rightMargin = SizeUtils.dp2px(Consts.getmApplicAtion().getBaseContext(), 20);
  302. params.rightMargin = SizeUtils.dp2px(Consts.getmApplicAtion().getBaseContext(), 60);
  303. }
  304. imageView.setLayoutParams(params);
  305. getRootView().addView(imageView);
  306. ObjectAnimator mTranslationY = ObjectAnimator.ofFloat(imageView, "translationY", 0, -200);
  307. mTranslationY.setDuration(1500);
  308. ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0f);
  309. alphaAnim.setDuration(1500);
  310. ObjectAnimator mRotationImgAnim = ObjectAnimator.ofFloat(imageView, "rotation", 0.0F, 359.0F);
  311. mRotationImgAnim.setRepeatCount(-1);
  312. mRotationImgAnim.setDuration(1000);
  313. ObjectAnimator scaleXAnim = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 2f);
  314. ObjectAnimator scaleYAnim = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 2f);
  315. scaleXAnim.setDuration(1000);
  316. scaleYAnim.setDuration(1000);
  317. LinearInterpolator rotation_interpolator = new LinearInterpolator();
  318. mRotationImgAnim.setInterpolator(rotation_interpolator); //设置匀速旋转,不卡顿 icon_anim.start();
  319. AnimatorSet animatorSet = new AnimatorSet();
  320. animatorSet.play(mTranslationY).with(alphaAnim).with(mRotationImgAnim).with(scaleXAnim).with(scaleYAnim);
  321. animatorSet.addListener(new AnimatorListenerAdapter() {
  322. @Override
  323. public void onAnimationEnd(Animator animation) {
  324. super.onAnimationEnd(animation);
  325. getRootView().removeView(imageView);
  326. }
  327. });
  328. animatorSet.start();
  329. break;
  330. //endregion
  331. //region 第一次进入时提示
  332. case SHOW_FIRST_TIP:
  333. if (mFirstIntoView == null) {
  334. mFirstIntoView = LayoutInflater.from(getBaseContext()).inflate(R.layout.first_tip, null);
  335. ImageView touch = mFirstIntoView.findViewById(R.id.first_touch);
  336. FrameLayout.LayoutParams first_params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  337. first_params.gravity = Gravity.RIGHT | Gravity.CENTER;
  338. first_params.rightMargin = SizeUtils.dp2px(Consts.getmApplicAtion().getBaseContext(), 30);
  339. mFirstIntoView.setLayoutParams(first_params);
  340. ObjectAnimator translationYUp = ObjectAnimator.ofFloat(touch, "translationY", 80, -30, 80);
  341. // ObjectAnimator translationYDown = ObjectAnimator.ofFloat(touch, "translationY", -30, 80);
  342. translationYUp.setDuration(1500);
  343. // translationYDown.setDuration(1500);
  344. translationYUp.setRepeatCount(-1);
  345. // translationYDown.setRepeatCount(-1);
  346. LinearInterpolator interpolator = new LinearInterpolator();
  347. translationYUp.setInterpolator(interpolator); //设置匀速旋转,不卡顿
  348. // translationYDown.setInterpolator(interpolator); //设置匀速旋转,不卡顿
  349. getRootView().addView(mFirstIntoView);
  350. AnimatorSet first_animatorSet = new AnimatorSet();
  351. // first_animatorSet.addListener(new AnimatorListenerAdapter() {
  352. // @Override
  353. // public void onAnimationEnd(Animator animation) {
  354. // super.onAnimationEnd(animation);
  355. // if (first_animatorSet != null) {
  356. // first_animatorSet.start();
  357. // }
  358. //
  359. // }
  360. // });
  361. mFirstIntoView.setOnTouchListener(new View.OnTouchListener() {
  362. @Override
  363. public boolean onTouch(View view, MotionEvent motionEvent) {
  364. first_animatorSet.pause();
  365. removeFirstTipView();
  366. return true;
  367. }
  368. });
  369. // first_animatorSet.playSequentially(translationYUp, translationYDown);
  370. first_animatorSet.play(translationYUp);
  371. first_animatorSet.start();
  372. }
  373. MMKVUtil.getInstance().encode("first_into", false);
  374. break;
  375. //endregion
  376. //region 天猫精灵初始化
  377. case TMAIL_INIT_SUCCESS:
  378. //天猫初始化成功
  379. Toast.makeText(getBaseContext(), message.obj.toString(), Toast.LENGTH_SHORT).show();
  380. break;
  381. case TMAIL_INIT_ERROR:
  382. //天猫初始化失败
  383. Toast.makeText(getBaseContext(), message.obj.toString(), Toast.LENGTH_SHORT).show();
  384. break;
  385. //endregion
  386. }
  387. return false;
  388. }
  389. });
  390. public void showFirstTouchView() {
  391. baseHandler.sendEmptyMessage(SHOW_FIRST_TIP);
  392. }
  393. public void removeFirstTipView() {
  394. if (mFirstIntoView != null) {
  395. getRootView().removeView(mFirstIntoView);
  396. mFirstIntoView = null;
  397. }
  398. }
  399. public void showPageLoading() {
  400. if (pageLoading == null) {
  401. pageLoading = new ImageView(this);
  402. pageLoading.setImageResource(R.drawable.page_loading);
  403. FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(SizeUtils.dp2px(this, 60), SizeUtils.dp2px(this, 60));
  404. params.gravity = Gravity.CENTER;
  405. pageLoading.setLayoutParams(params);
  406. ObjectAnimator mRotationImgAnim = ObjectAnimator.ofFloat(pageLoading, "rotation", 0.0F, 359.0F);
  407. mRotationImgAnim.setRepeatCount(-1);
  408. mRotationImgAnim.setDuration(1000);
  409. mRotationImgAnim.start();
  410. }
  411. if (pageLoadingShow) {
  412. return;
  413. }
  414. getRootView().addView(pageLoading);
  415. pageLoadingShow = true;
  416. }
  417. public void hidePageLoading() {
  418. if (pageLoading == null) {
  419. return;
  420. }
  421. if (pageLoadingShow) {
  422. getRootView().removeView(pageLoading);
  423. pageLoadingShow = false;
  424. }
  425. }
  426. public Gson getGson() {
  427. return mGson;
  428. }
  429. public void toNextActivity(Class clazz) {
  430. Intent intent = new Intent(this, clazz);
  431. startActivity(intent);
  432. }
  433. public void toNextActivity(Class clazz, Bundle bundle) {
  434. Intent intent = new Intent(this, clazz);
  435. intent.putExtra("params_bundle", bundle);
  436. startActivity(intent);
  437. }
  438. public void toNextActivityForResult(Class clazz, Bundle bundle, int resultCode) {
  439. Intent intent = new Intent(this, clazz);
  440. intent.putExtra("params_bundle", bundle);
  441. startActivityForResult(intent, resultCode);
  442. }
  443. @Override
  444. protected void onPause() {
  445. super.onPause();
  446. if (Consts.getUmengChannel().equals(ChannelCodeEnum.TMAIL.getChannelCode())) {
  447. mAliTVASRManager.release();
  448. } else if (Consts.getUmengChannel().equals(ChannelCodeEnum.BAIDU.getChannelCode())) {
  449. Logger.e("隐藏弹窗");
  450. hasResume = false;
  451. if (mFloating != null) {
  452. // 隐藏
  453. mFloating.dismissFloatView();
  454. }
  455. }
  456. }
  457. @Override
  458. protected void onResume() {
  459. super.onResume();
  460. if (Consts.getUmengChannel().equals(ChannelCodeEnum.TMAIL.getChannelCode())) {
  461. try {
  462. mAliTVASRManager.setOnASRCommandListener(mASRCommandListener);
  463. mAliTVASRManager.setASRListenerType(OnASRCommandListener.ASRListenerType.DEFAULT_LISTENER);
  464. mAliTVASRManager.setAliTVASREnable(true);
  465. mAliTVASRManager.setOnGetMediaPolicyListener(mOnGetMediaPolicyListener);
  466. mAliTVASRManager.init(getBaseContext(), true);
  467. } catch (Exception e) {
  468. e.printStackTrace();
  469. }
  470. } else if (Consts.getUmengChannel().equals(ChannelCodeEnum.BAIDU.getChannelCode())) {
  471. hasResume = true;
  472. if (mFloating != null) {
  473. mFloating.dismissFloatView();
  474. }
  475. mFloating = MemberSdkManager.getInstance().showFloatView(this);
  476. }
  477. }
  478. @Override
  479. protected void onDestroy() {
  480. super.onDestroy();
  481. Runtime.getRuntime().gc();
  482. System.gc();
  483. if (Consts.getUmengChannel().equals(ChannelCodeEnum.TMAIL.getChannelCode())) {
  484. mAliTVASRManager.release();
  485. }
  486. EApplication.mActivityList.remove(this);
  487. BotRegisterListener.removeListener(mBotIntentCallBack);
  488. clearDisposable();
  489. unregisterReceiver(mHomeReceiver);
  490. NetworkChangeReceiver.unRegisterReceiver(this);
  491. NetworkChangeReceiver.unRegisterObserver(NetStateChangeObserver);
  492. ShowFollowReceiver.unRegisterReceiver(this);
  493. ShowFollowReceiver.unRegisterObserver(showFollow);
  494. ShowPageLoadingReceiver.unRegisterReceiver(this);
  495. ShowPageLoadingReceiver.unRegisterObserver(pageLoadingReceiver);
  496. }
  497. ShowFollowReceiver.ShowFollow showFollow = new ShowFollowReceiver.ShowFollow() {
  498. @Override
  499. public void showFollow(Intent intent) {
  500. showFollowCountAnim(intent.getStringExtra("follow_count"));
  501. }
  502. };
  503. ShowPageLoadingReceiver.PageLoading pageLoadingReceiver = new ShowPageLoadingReceiver.PageLoading() {
  504. @Override
  505. public void show(Intent intent) {
  506. showPageLoading();
  507. }
  508. @Override
  509. public void hide(Intent intent) {
  510. hidePageLoading();
  511. }
  512. };
  513. //region Description 百度的回调逻辑代码块
  514. protected void botHandleIntent(BotIntent intent, String customData) {
  515. }
  516. protected void botOnClickLink(String url, HashMap<String, String> paramMap) {
  517. }
  518. protected void botOnHandleScreenNavigatorEvent(int event) {
  519. }
  520. protected void botOnLinkAccountSucceed(String s, String s1) {
  521. }
  522. protected void refUserInfo(UserBean bean) {
  523. }
  524. /**
  525. * 支付状态改变的通知
  526. *
  527. * @param purchaseResult 支付结果 SUCCESS 支付成功 - ERROR 支付发生错误
  528. * @param authorizationAmount 应收金额信息
  529. * @param capturedAmount 实际扣款信息
  530. * @param creationTimestamp 订单创建时间戳
  531. * @param baiduOrderReferenceId 此次交易百度生成的订单Id
  532. * @param sellerOrderId 对应支付的订单ID
  533. * @param msg 订单信息
  534. */
  535. protected void botOnChargeStatusUpdated(String purchaseResult, AmountInfo authorizationAmount,
  536. AmountInfo capturedAmount, long creationTimestamp,
  537. String baiduOrderReferenceId, String sellerOrderId, String msg) {
  538. }
  539. /**
  540. * 购买结果更新
  541. *
  542. * @param purchaseResult 支付结果,SUCCESS 支付成功 -ERROR 支付发生错误
  543. * @param productId 商品id
  544. * @param baiduOrderId 百度侧订单id
  545. * @param sellerOrderId 卖家订单id
  546. * @param msg 订单备注信息
  547. */
  548. protected void botOnBuyStatusUpdated(@NonNull final String purchaseResult,
  549. @NonNull final String productId,
  550. @NonNull final String baiduOrderId,
  551. @NonNull final String sellerOrderId, @Nullable final String msg, String token) {
  552. }
  553. IBotIntentCallback mBotIntentCallBack = new IBotIntentCallback() {
  554. @Override
  555. public void handleIntent(BotIntent intent, String customData) {
  556. MemberSdkManager.getInstance().handleIntent(intent, customData);
  557. Logger.e("fragment---intent.name:" + intent.name);
  558. if (intent.name.equals("all_login")) {
  559. //用户主动要求登录
  560. Consts.setIsNeedLogin(true);
  561. EfunboxUtil.userLogin(EApplication.mActivityList.get(EApplication.mActivityList.size() - 1));
  562. return;
  563. } else if (intent.name.equals("user_info")) {
  564. //获取是不是百度VIP
  565. Consts.setmConstsBaiDuUserBean(mGson.fromJson(customData, BaiDuUserInfo.class));
  566. Logger.e("json----" + mGson.toJson(Consts.getmConstsBaiDuUserBean()));
  567. if (Consts.getIsNeedLogin()) {
  568. BotRegisterListener.BaiDuLogin();
  569. } else {
  570. Consts.setIsNeedLogin(true);
  571. }
  572. botHandleIntent(intent, customData);
  573. return;
  574. }
  575. Logger.e("intent:" + intent.name);
  576. if (BotRegisterListener.isNoLoginIntent(intent)) {
  577. //不是获取手机号
  578. Logger.e("不是获取手机号--不是获取手机号:" + intent.name + "---" + intent.slots.toString());
  579. botHandleIntent(intent, customData);
  580. } else {
  581. Logger.e("是获取手机号--是获取手机号");
  582. //是获取手机号
  583. if (intent.slots.get(0).value.equals("SUCCESS")) {
  584. //获取手机号Token成功
  585. String token = intent.slots.get(1).value;
  586. UserApiServerImpl userApiServer = new UserApiServerImpl();
  587. Activity topActivity = EApplication.mActivityList.get(EApplication.mActivityList.size() - 1);
  588. userApiServer.getBaiDuUserPhone(topActivity, token, new Callback() {
  589. @Override
  590. public void onSuccess(HttpInfo info) throws IOException {
  591. JSONObject object = JSONObject.parseObject(info.getRetDetail().toString());
  592. String phone = JSONObject.parseObject(object.getString("data")).getString("phone");
  593. Logger.e("获取的手机号:" + phone);
  594. if (EfunboxUtil.isMobileNO(phone)) {
  595. Map<String, String> map = new HashMap<>();
  596. map.put("deviceCode", DeviceUuidFactory.getUuid());
  597. map.put("channel", Consts.getUmengChannel());
  598. map.put("mobileNo", phone);
  599. map.put("nickName", EfunboxUtil.setPhoneMiddle(phone));
  600. Logger.e("Consts.getmConstsBaiDuUserBean().getUserInfo().getUserId():" + Consts.getmConstsBaiDuUserBean().getUserInfo().getUserId());
  601. map.put("userId", Consts.getmConstsBaiDuUserBean().getUserInfo().getUserId());
  602. if (MMKVUtil.getInstance().decodeString(MMKVEncodeKey.USER_GRADE).equals("") || MMKVUtil.getInstance().decodeString(MMKVEncodeKey.USER_GRADE) == null) {
  603. //如果没有,默认给一年级
  604. map.put("grade", "PRIMARY_FIRST_GRADE");
  605. } else {
  606. map.put("grade", MMKVUtil.getInstance().decodeString(MMKVEncodeKey.USER_GRADE));
  607. }
  608. String json = mGson.toJson(map);
  609. Logger.e("json:" + json);
  610. userApiServer.registerUserId(topActivity, json, new Callback() {
  611. @Override
  612. public void onSuccess(HttpInfo info) throws IOException {
  613. Logger.e("手机号注册成功:" + info.getRetDetail());
  614. HttpResultBean<UserBean> bean = info.getRetDetail(new TypeToken<HttpResultBean<UserBean>>() {
  615. }.getType());
  616. Consts.setUID(bean.getData().getUid());
  617. //用户是否登录,登录了,创建Consts的userbean
  618. String nickName = EfunboxUtil.setPhoneMiddle(bean.getData().getMobile());
  619. bean.getData().setNickName(nickName);
  620. Consts.setmConstsUserBean(bean.getData());
  621. Logger.e("看看到底对没有:" + mGson.toJson(Consts.getmConstsUserBean()));
  622. refUserInfo(Consts.getmConstsUserBean());
  623. Toast.makeText(topActivity, "注册登录成功", Toast.LENGTH_SHORT).show();
  624. userApiServer.loginSign(topActivity, new Callback() {
  625. @Override
  626. public void onSuccess(HttpInfo info) throws IOException {
  627. Logger.e("info:" + info.getRetDetail());
  628. HttpResultBean<SignBean> bean = info.getRetDetail(new TypeToken<HttpResultBean<SignBean>>() {
  629. }.getType());
  630. if (bean.getData().getIsSign()) {
  631. CusToast.getInstance(topActivity).show("恭喜签到成功\n奖励" + bean.getData().getCount() + "朵小红花", 2000);
  632. }
  633. }
  634. @Override
  635. public void onFailure(HttpInfo info) throws IOException {
  636. }
  637. });
  638. //检查活动
  639. EfunboxUtil.checkEvents(topActivity, getRootView());
  640. }
  641. @Override
  642. public void onFailure(HttpInfo info) throws IOException {
  643. Logger.e("手机号注册失败:" + info.getRetDetail());
  644. // CusToast.getInstance(topActivity).show("注册失败", 2000);
  645. Toast.makeText(topActivity, "注册失败", Toast.LENGTH_SHORT).show();
  646. }
  647. });
  648. } else {
  649. // CusToast.getInstance(topActivity).show("手机号不正确", 2000);
  650. Toast.makeText(topActivity, "手机号不正确", Toast.LENGTH_SHORT).show();
  651. }
  652. }
  653. @Override
  654. public void onFailure(HttpInfo info) throws IOException {
  655. }
  656. });
  657. } else if (intent.slots.get(0).value.equals("FAIL")) {
  658. //获取手机号Token失败
  659. }
  660. }
  661. }
  662. @Override
  663. public void onClickLink(String url, HashMap<String, String> paramMap) {
  664. botOnClickLink(url, paramMap);
  665. }
  666. @Override
  667. public void onHandleScreenNavigatorEvent(int event) {
  668. botOnHandleScreenNavigatorEvent(event);
  669. }
  670. };
  671. IAccountChargeMsgListener mIAccountChargeMsgListener = new IAccountChargeMsgListener() {
  672. @Override
  673. public void onLinkAccountSucceed(String s, String s1) {
  674. botOnLinkAccountSucceed(s, s1);
  675. }
  676. @Override
  677. public void onChargeStatusUpdated(String purchaseResult, AmountInfo authorizationAmount,
  678. AmountInfo capturedAmount, long creationTimestamp,
  679. String baiduOrderReferenceId, String sellerOrderId, String msg) {
  680. botOnChargeStatusUpdated(purchaseResult, authorizationAmount, capturedAmount, creationTimestamp, baiduOrderReferenceId, sellerOrderId, msg);
  681. }
  682. @Override
  683. public void onBuyStatusUpdated(@NonNull final String purchaseResult,
  684. @NonNull final String productId,
  685. @NonNull final String baiduOrderId,
  686. @NonNull final String sellerOrderId, @Nullable final String msg, String token) {
  687. Consts.setIsNeedLogin(false);
  688. //处理支付回调
  689. Logger.e("purchaseResult:" + purchaseResult);
  690. Logger.e("productId:" + productId);
  691. Logger.e("baiduOrderId:" + baiduOrderId);
  692. Logger.e("sellerOrderId:" + sellerOrderId);
  693. Logger.e("msg:" + msg);
  694. Logger.e("token:" + token);
  695. if (purchaseResult.equals("SUCCESS")) {
  696. //成功
  697. BotRegisterListener.getBaiDuVipInfo();
  698. } else if (purchaseResult.equals("ERROR")) {
  699. //支付失败
  700. }
  701. botOnBuyStatusUpdated(purchaseResult, productId, baiduOrderId, sellerOrderId, msg, token);
  702. }
  703. };
  704. //endregion
  705. //region Description 天猫精灵的代码
  706. private ASRClient mAliTVASRManager;
  707. OnASRCommandListener mASRCommandListener = new OnASRCommandListener() {
  708. @Override
  709. public void onASRStatusUpdated(ASRStatus asrStatus, Bundle bundle) {
  710. Logger.e("mASRCommandListener");
  711. }
  712. @Override
  713. public void onASRServiceStatusUpdated(ASRServiceStatus asrServiceStatus) {
  714. Logger.e("onASRServiceStatusUpdated");
  715. Message message = new Message();
  716. if (asrServiceStatus == ASRServiceStatus.ASR_SERVICE_STATUS_CONNECTED) {
  717. message.what = TMAIL_INIT_SUCCESS;
  718. message.obj = "语音服务注册成功...(" + Thread.currentThread().getId() + ")";
  719. } else {
  720. message.what = TMAIL_INIT_ERROR;
  721. message.obj = "语音服务注册失败...(" + Thread.currentThread().getId() + ")";
  722. }
  723. baseHandler.dispatchMessage(message);
  724. }
  725. /**
  726. * 重要!!!接收到有限的语音指令结果并进行处理。
  727. */
  728. @Override
  729. public ASRCommandReturn onNLUResult(String commandDomain, String command, String commandParams, Bundle nlpResult) {
  730. Logger.e("ASRCommandReturn");
  731. return null;
  732. }
  733. @Override
  734. public void getAppContextData(AppContextData appContextData) {
  735. Logger.e("AppContextData");
  736. }
  737. @Override
  738. public Bundle getSceneInfo(Bundle bundle) {
  739. return null;
  740. }
  741. @Override
  742. public Bundle asrToClient(Bundle bundle) {
  743. return null;
  744. }
  745. };
  746. OnGetMediaPolicyListener mOnGetMediaPolicyListener = new OnGetMediaPolicyListener() {
  747. @Override
  748. public int onGetMediaPolicy() {
  749. return OnGetMediaPolicyListener.POLICY_CALL_PHONE;
  750. }
  751. };
  752. //endregion
  753. protected void onScreenUserPresent() {
  754. }
  755. protected void onAppScreenOff() {
  756. }
  757. }