BaseActivity.java 41 KB

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