HomeActivity.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. package com.github.tvbox.osc.ui.activity;
  2. import android.Manifest;
  3. import android.animation.Animator;
  4. import android.animation.AnimatorSet;
  5. import android.animation.IntEvaluator;
  6. import android.animation.ObjectAnimator;
  7. import android.annotation.SuppressLint;
  8. import android.content.DialogInterface;
  9. import android.content.Intent;
  10. import android.content.res.Resources;
  11. import android.net.ConnectivityManager;
  12. import android.net.NetworkInfo;
  13. import android.net.Uri;
  14. import android.os.Bundle;
  15. import android.os.Handler;
  16. import android.provider.Settings;
  17. import android.view.KeyEvent;
  18. import android.view.View;
  19. import android.view.ViewGroup;
  20. import android.view.animation.AccelerateInterpolator;
  21. import android.view.animation.BounceInterpolator;
  22. import android.widget.ImageView;
  23. import android.widget.LinearLayout;
  24. import android.widget.TextView;
  25. import android.widget.Toast;
  26. import androidx.annotation.NonNull;
  27. import androidx.lifecycle.Observer;
  28. import androidx.lifecycle.ViewModelProvider;
  29. import androidx.recyclerview.widget.DiffUtil;
  30. import androidx.viewpager.widget.ViewPager;
  31. import com.github.tvbox.osc.R;
  32. import com.github.tvbox.osc.api.ApiConfig;
  33. import com.github.tvbox.osc.base.BaseActivity;
  34. import com.github.tvbox.osc.base.BaseLazyFragment;
  35. import com.github.tvbox.osc.bean.AbsSortXml;
  36. import com.github.tvbox.osc.bean.MovieSort;
  37. import com.github.tvbox.osc.bean.SourceBean;
  38. import com.github.tvbox.osc.event.RefreshEvent;
  39. import com.github.tvbox.osc.server.ControlManager;
  40. import com.github.tvbox.osc.ui.adapter.HomePageAdapter;
  41. import com.github.tvbox.osc.ui.adapter.SelectDialogAdapter;
  42. import com.github.tvbox.osc.ui.adapter.SortAdapter;
  43. import com.github.tvbox.osc.ui.dialog.SelectDialog;
  44. import com.github.tvbox.osc.ui.dialog.TipDialog;
  45. import com.github.tvbox.osc.ui.fragment.GridFragment;
  46. import com.github.tvbox.osc.ui.fragment.UserFragment;
  47. import com.github.tvbox.osc.ui.tv.widget.DefaultTransformer;
  48. import com.github.tvbox.osc.ui.tv.widget.FixedSpeedScroller;
  49. import com.github.tvbox.osc.ui.tv.widget.NoScrollViewPager;
  50. import com.github.tvbox.osc.ui.tv.widget.ViewObj;
  51. import com.github.tvbox.osc.util.AppManager;
  52. import com.github.tvbox.osc.util.DefaultConfig;
  53. import com.github.tvbox.osc.util.FileUtils;
  54. import com.github.tvbox.osc.util.HawkConfig;
  55. import com.github.tvbox.osc.util.LOG;
  56. import com.github.tvbox.osc.viewmodel.SourceViewModel;
  57. import com.orhanobut.hawk.Hawk;
  58. import com.owen.tvrecyclerview.widget.TvRecyclerView;
  59. import com.owen.tvrecyclerview.widget.V7LinearLayoutManager;
  60. import org.greenrobot.eventbus.EventBus;
  61. import org.greenrobot.eventbus.Subscribe;
  62. import org.greenrobot.eventbus.ThreadMode;
  63. import org.jetbrains.annotations.NotNull;
  64. import java.io.File;
  65. import java.lang.reflect.Field;
  66. import java.text.SimpleDateFormat;
  67. import java.util.ArrayList;
  68. import java.util.Date;
  69. import java.util.List;
  70. import me.jessyan.autosize.utils.AutoSizeUtils;
  71. import androidx.constraintlayout.widget.ConstraintLayout;
  72. import com.owen.tvrecyclerview.widget.V7GridLayoutManager;
  73. public class HomeActivity extends BaseActivity {
  74. // takagen99: Added to allow read string
  75. private static Resources res;
  76. private LinearLayout topLayout;
  77. private LinearLayout contentLayout;
  78. private TextView tvName;
  79. private ImageView tvWifi;
  80. private ImageView tvFind;
  81. private ImageView tvMenu;
  82. private TextView tvDate;
  83. private TvRecyclerView mGridView;
  84. private NoScrollViewPager mViewPager;
  85. private SourceViewModel sourceViewModel;
  86. private SortAdapter sortAdapter;
  87. private HomePageAdapter pageAdapter;
  88. private final List<BaseLazyFragment> fragments = new ArrayList<>();
  89. private boolean isDownOrUp = false;
  90. private boolean sortChange = false;
  91. private int currentSelected = 0;
  92. private int sortFocused = 0;
  93. public View sortFocusView = null;
  94. private final Handler mHandler = new Handler();
  95. private long mExitTime = 0;
  96. private final Runnable mRunnable = new Runnable() {
  97. @SuppressLint({"DefaultLocale", "SetTextI18n"})
  98. @Override
  99. public void run() {
  100. Date date = new Date();
  101. @SuppressLint("SimpleDateFormat")
  102. SimpleDateFormat timeFormat = new SimpleDateFormat(getString(R.string.hm_date1) + ", " + getString(R.string.hm_date2));
  103. tvDate.setText(timeFormat.format(date));
  104. mHandler.postDelayed(this, 1000);
  105. }
  106. };
  107. @Override
  108. protected int getLayoutResID() {
  109. return R.layout.activity_home;
  110. }
  111. boolean useCacheConfig = false;
  112. @Override
  113. protected void init() {
  114. // takagen99: Added to allow read string
  115. res = getResources();
  116. EventBus.getDefault().register(this);
  117. ControlManager.get().startServer();
  118. initView();
  119. initViewModel();
  120. useCacheConfig = false;
  121. Intent intent = getIntent();
  122. if (intent != null && intent.getExtras() != null) {
  123. Bundle bundle = intent.getExtras();
  124. useCacheConfig = bundle.getBoolean("useCache", false);
  125. }
  126. initData();
  127. }
  128. // takagen99: Added to allow read string
  129. public static Resources getRes() {
  130. return res;
  131. }
  132. private void initView() {
  133. this.topLayout = findViewById(R.id.topLayout);
  134. this.tvName = findViewById(R.id.tvName);
  135. this.tvWifi = findViewById(R.id.tvWifi);
  136. this.tvFind = findViewById(R.id.tvFind);
  137. this.tvMenu = findViewById(R.id.tvMenu);
  138. this.tvDate = findViewById(R.id.tvDate);
  139. this.contentLayout = findViewById(R.id.contentLayout);
  140. this.mGridView = findViewById(R.id.mGridView);
  141. this.mViewPager = findViewById(R.id.mViewPager);
  142. this.sortAdapter = new SortAdapter();
  143. this.mGridView.setLayoutManager(new V7LinearLayoutManager(this.mContext, 0, false));
  144. this.mGridView.setSpacingWithMargins(0, AutoSizeUtils.dp2px(this.mContext, 10.0f));
  145. this.mGridView.setAdapter(this.sortAdapter);
  146. this.mGridView.setOnItemListener(new TvRecyclerView.OnItemListener() {
  147. public void onItemPreSelected(TvRecyclerView tvRecyclerView, View view, int position) {
  148. if (view != null && !HomeActivity.this.isDownOrUp) {
  149. view.animate().scaleX(1.0f).scaleY(1.0f).setDuration(250).start();
  150. TextView textView = view.findViewById(R.id.tvTitle);
  151. textView.getPaint().setFakeBoldText(false);
  152. textView.setTextColor(HomeActivity.this.getResources().getColor(R.color.color_FFFFFF_70));
  153. textView.invalidate();
  154. view.findViewById(R.id.tvFilter).setVisibility(View.GONE);
  155. }
  156. }
  157. public void onItemSelected(TvRecyclerView tvRecyclerView, View view, int position) {
  158. if (view != null) {
  159. HomeActivity.this.isDownOrUp = false;
  160. HomeActivity.this.sortChange = true;
  161. view.animate().scaleX(1.1f).scaleY(1.1f).setInterpolator(new BounceInterpolator()).setDuration(250).start();
  162. TextView textView = view.findViewById(R.id.tvTitle);
  163. textView.getPaint().setFakeBoldText(true);
  164. textView.setTextColor(HomeActivity.this.getResources().getColor(R.color.color_FFFFFF));
  165. textView.invalidate();
  166. if (!sortAdapter.getItem(position).filters.isEmpty())
  167. view.findViewById(R.id.tvFilter).setVisibility(View.VISIBLE);
  168. HomeActivity.this.sortFocusView = view;
  169. HomeActivity.this.sortFocused = position;
  170. mHandler.removeCallbacks(mDataRunnable);
  171. mHandler.postDelayed(mDataRunnable, 200);
  172. }
  173. }
  174. @Override
  175. public void onItemClick(TvRecyclerView parent, View itemView, int position) {
  176. if (itemView != null && currentSelected == position) {
  177. BaseLazyFragment baseLazyFragment = fragments.get(currentSelected);
  178. if ((baseLazyFragment instanceof GridFragment) && !sortAdapter.getItem(position).filters.isEmpty()) {// 弹出筛选
  179. ((GridFragment) baseLazyFragment).showFilter();
  180. } else if (baseLazyFragment instanceof UserFragment) {
  181. showSiteSwitch();
  182. }
  183. }
  184. }
  185. });
  186. this.mGridView.setOnInBorderKeyEventListener(new TvRecyclerView.OnInBorderKeyEventListener() {
  187. public final boolean onInBorderKeyEvent(int direction, View view) {
  188. if(direction == View.FOCUS_UP){
  189. BaseLazyFragment baseLazyFragment = fragments.get(sortFocused);
  190. if ((baseLazyFragment instanceof GridFragment) ) {// 弹出筛选
  191. ((GridFragment) baseLazyFragment).forceRefresh();
  192. }
  193. }
  194. if (direction != View.FOCUS_DOWN) {
  195. return false;
  196. }
  197. BaseLazyFragment baseLazyFragment = fragments.get(sortFocused);
  198. if (!(baseLazyFragment instanceof GridFragment)) {
  199. return false;
  200. }
  201. return !((GridFragment) baseLazyFragment).isLoad();
  202. }
  203. });
  204. // Button : TVBOX >> Go into Source Select / Longclick to delete Cache --
  205. tvName.setOnClickListener(new View.OnClickListener() {
  206. @Override
  207. public void onClick(View v) {
  208. dataInitOk = false;
  209. jarInitOk = true;
  210. showSiteSwitch();
  211. }
  212. });
  213. tvName.setOnLongClickListener(new View.OnLongClickListener() {
  214. @Override
  215. public boolean onLongClick(View v) {
  216. File dir = mContext.getCacheDir();
  217. FileUtils.recursiveDelete(dir);
  218. Toast.makeText(HomeActivity.this, getString(R.string.hm_cache_del), Toast.LENGTH_SHORT).show();
  219. return true;
  220. }
  221. });
  222. // Button : Wifi >> Go into Android Wifi Settings -------------
  223. tvWifi.setOnClickListener(new View.OnClickListener() {
  224. @Override
  225. public void onClick(View view) {
  226. startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
  227. }
  228. });
  229. // Button : Search --------------------------------------------
  230. tvFind.setOnClickListener(new View.OnClickListener() {
  231. @Override
  232. public void onClick(View view) {
  233. jumpActivity(SearchActivity.class);
  234. }
  235. });
  236. // Button : Settings >> To go into Settings --------------------
  237. tvMenu.setOnClickListener(new View.OnClickListener() {
  238. @Override
  239. public void onClick(View view) {
  240. jumpActivity(SettingActivity.class);
  241. }
  242. });
  243. // Button : Settings >> To go into App Settings ----------------
  244. tvMenu.setOnLongClickListener(new View.OnLongClickListener() {
  245. @Override
  246. public boolean onLongClick(View view) {
  247. startActivity(new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", getPackageName(), null)));
  248. return true;
  249. }
  250. });
  251. // Button : Date >> Go into Android Date Settings --------------
  252. tvDate.setOnClickListener(new View.OnClickListener() {
  253. @Override
  254. public void onClick(View view) {
  255. startActivity(new Intent(Settings.ACTION_DATE_SETTINGS));
  256. }
  257. });
  258. setLoadSir(this.contentLayout);
  259. //mHandler.postDelayed(mFindFocus, 250);
  260. }
  261. private void initViewModel() {
  262. sourceViewModel = new ViewModelProvider(this).get(SourceViewModel.class);
  263. sourceViewModel.sortResult.observe(this, new Observer<AbsSortXml>() {
  264. @Override
  265. public void onChanged(AbsSortXml absXml) {
  266. showSuccess();
  267. if (absXml != null && absXml.classes != null && absXml.classes.sortList != null) {
  268. sortAdapter.setNewData(DefaultConfig.adjustSort(ApiConfig.get().getHomeSourceBean().getKey(), absXml.classes.sortList, true));
  269. } else {
  270. sortAdapter.setNewData(DefaultConfig.adjustSort(ApiConfig.get().getHomeSourceBean().getKey(), new ArrayList<>(), true));
  271. }
  272. initViewPager(absXml);
  273. }
  274. });
  275. }
  276. private boolean dataInitOk = false;
  277. private boolean jarInitOk = false;
  278. // takagen99 : Switch to show / hide source title
  279. boolean HomeShow = Hawk.get(HawkConfig.HOME_SHOW_SOURCE, false);
  280. // takagen99 : Check if network is available
  281. boolean isNetworkAvailable() {
  282. ConnectivityManager cm
  283. = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
  284. NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
  285. return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
  286. }
  287. private void initData() {
  288. SourceBean home = ApiConfig.get().getHomeSourceBean();
  289. // takagen99 : Switch to show / hide source title
  290. if (HomeShow) {
  291. if (home != null && home.getName() != null && !home.getName().isEmpty())
  292. tvName.setText(home.getName());
  293. }
  294. // takagen99: If network available, check connected Wifi or Lan
  295. if (isNetworkAvailable()) {
  296. ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
  297. if (cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
  298. tvWifi.setImageDrawable(res.getDrawable(R.drawable.hm_wifi));
  299. } else if (cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_MOBILE) {
  300. tvWifi.setImageDrawable(res.getDrawable(R.drawable.hm_mobile));
  301. } else if (cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_ETHERNET) {
  302. tvWifi.setImageDrawable(res.getDrawable(R.drawable.hm_lan));
  303. }
  304. }
  305. mGridView.requestFocus();
  306. if (dataInitOk && jarInitOk) {
  307. showLoading();
  308. sourceViewModel.getSort(ApiConfig.get().getHomeSourceBean().getKey());
  309. if (hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
  310. LOG.e("有");
  311. } else {
  312. LOG.e("无");
  313. }
  314. return;
  315. }
  316. showLoading();
  317. if (dataInitOk && !jarInitOk) {
  318. if (!ApiConfig.get().getSpider().isEmpty()) {
  319. ApiConfig.get().loadJar(useCacheConfig, ApiConfig.get().getSpider(), new ApiConfig.LoadConfigCallback() {
  320. @Override
  321. public void success() {
  322. jarInitOk = true;
  323. mHandler.postDelayed(new Runnable() {
  324. @Override
  325. public void run() {
  326. if (!useCacheConfig)
  327. Toast.makeText(HomeActivity.this, getString(R.string.hm_ok), Toast.LENGTH_SHORT).show();
  328. initData();
  329. }
  330. }, 50);
  331. }
  332. @Override
  333. public void retry() {
  334. }
  335. @Override
  336. public void error(String msg) {
  337. jarInitOk = true;
  338. mHandler.post(new Runnable() {
  339. @Override
  340. public void run() {
  341. Toast.makeText(HomeActivity.this, getString(R.string.hm_notok), Toast.LENGTH_SHORT).show();
  342. initData();
  343. }
  344. });
  345. }
  346. });
  347. }
  348. return;
  349. }
  350. ApiConfig.get().loadConfig(useCacheConfig, new ApiConfig.LoadConfigCallback() {
  351. TipDialog dialog = null;
  352. @Override
  353. public void retry() {
  354. mHandler.post(new Runnable() {
  355. @Override
  356. public void run() {
  357. initData();
  358. }
  359. });
  360. }
  361. @Override
  362. public void success() {
  363. dataInitOk = true;
  364. if (ApiConfig.get().getSpider().isEmpty()) {
  365. jarInitOk = true;
  366. }
  367. mHandler.postDelayed(new Runnable() {
  368. @Override
  369. public void run() {
  370. initData();
  371. }
  372. }, 50);
  373. }
  374. @Override
  375. public void error(String msg) {
  376. if (msg.equalsIgnoreCase("-1")) {
  377. mHandler.post(new Runnable() {
  378. @Override
  379. public void run() {
  380. dataInitOk = true;
  381. jarInitOk = true;
  382. initData();
  383. }
  384. });
  385. return;
  386. }
  387. mHandler.post(new Runnable() {
  388. @Override
  389. public void run() {
  390. if (dialog == null)
  391. dialog = new TipDialog(HomeActivity.this, msg, getString(R.string.hm_retry), getString(R.string.hm_cancel), new TipDialog.OnListener() {
  392. @Override
  393. public void left() {
  394. mHandler.post(new Runnable() {
  395. @Override
  396. public void run() {
  397. initData();
  398. dialog.hide();
  399. }
  400. });
  401. }
  402. @Override
  403. public void right() {
  404. dataInitOk = true;
  405. jarInitOk = true;
  406. mHandler.post(new Runnable() {
  407. @Override
  408. public void run() {
  409. initData();
  410. dialog.hide();
  411. }
  412. });
  413. }
  414. @Override
  415. public void cancel() {
  416. dataInitOk = true;
  417. jarInitOk = true;
  418. mHandler.post(new Runnable() {
  419. @Override
  420. public void run() {
  421. initData();
  422. dialog.hide();
  423. }
  424. });
  425. }
  426. });
  427. if (!dialog.isShowing())
  428. dialog.show();
  429. }
  430. });
  431. }
  432. }, this);
  433. }
  434. private void initViewPager(AbsSortXml absXml) {
  435. if (sortAdapter.getData().size() > 0) {
  436. for (MovieSort.SortData data : sortAdapter.getData()) {
  437. if (data.id.equals("my0")) {
  438. if (Hawk.get(HawkConfig.HOME_REC, 0) == 1 && absXml != null && absXml.videoList != null && absXml.videoList.size() > 0) {
  439. fragments.add(UserFragment.newInstance(absXml.videoList));
  440. } else {
  441. fragments.add(UserFragment.newInstance(null));
  442. }
  443. } else {
  444. fragments.add(GridFragment.newInstance(data));
  445. }
  446. }
  447. pageAdapter = new HomePageAdapter(getSupportFragmentManager(), fragments);
  448. try {
  449. Field field = ViewPager.class.getDeclaredField("mScroller");
  450. field.setAccessible(true);
  451. FixedSpeedScroller scroller = new FixedSpeedScroller(mContext, new AccelerateInterpolator());
  452. field.set(mViewPager, scroller);
  453. scroller.setmDuration(300);
  454. } catch (Exception e) {
  455. }
  456. mViewPager.setPageTransformer(true, new DefaultTransformer());
  457. mViewPager.setAdapter(pageAdapter);
  458. mViewPager.setCurrentItem(currentSelected, false);
  459. }
  460. }
  461. @Override
  462. public void onBackPressed() {
  463. int i;
  464. if (this.fragments.size() <= 0 || this.sortFocused >= this.fragments.size() || (i = this.sortFocused) < 0) {
  465. exit();
  466. return;
  467. }
  468. BaseLazyFragment baseLazyFragment = this.fragments.get(i);
  469. if (baseLazyFragment instanceof GridFragment) {
  470. View view = this.sortFocusView;
  471. GridFragment grid = (GridFragment) baseLazyFragment;
  472. if (grid.restoreView()) {
  473. return;
  474. }// 还原上次保存的UI内容
  475. if (view != null && !view.isFocused()) {
  476. this.sortFocusView.requestFocus();
  477. } else if (this.sortFocused != 0) {
  478. this.mGridView.setSelection(0);
  479. } else {
  480. exit();
  481. }
  482. } else {
  483. exit();
  484. }
  485. }
  486. private void exit() {
  487. if (System.currentTimeMillis() - mExitTime < 2000) {
  488. //这一段借鉴来自 q群老哥 IDCardWeb
  489. EventBus.getDefault().unregister(this);
  490. AppManager.getInstance().appExit(0);
  491. ControlManager.get().stopServer();
  492. finish();
  493. super.onBackPressed();
  494. } else {
  495. mExitTime = System.currentTimeMillis();
  496. Toast.makeText(mContext, getString(R.string.hm_exit), Toast.LENGTH_SHORT).show();
  497. }
  498. }
  499. @Override
  500. protected void onResume() {
  501. super.onResume();
  502. mHandler.post(mRunnable);
  503. }
  504. @Override
  505. protected void onPause() {
  506. super.onPause();
  507. mHandler.removeCallbacksAndMessages(null);
  508. }
  509. @Subscribe(threadMode = ThreadMode.MAIN)
  510. public void refresh(RefreshEvent event) {
  511. if (event.type == RefreshEvent.TYPE_PUSH_URL) {
  512. if (ApiConfig.get().getSource("push_agent") != null) {
  513. Intent newIntent = new Intent(mContext, DetailActivity.class);
  514. newIntent.putExtra("id", (String) event.obj);
  515. newIntent.putExtra("sourceKey", "push_agent");
  516. newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
  517. HomeActivity.this.startActivity(newIntent);
  518. }
  519. }
  520. }
  521. private final Runnable mDataRunnable = new Runnable() {
  522. @Override
  523. public void run() {
  524. if (sortChange) {
  525. sortChange = false;
  526. if (sortFocused != currentSelected) {
  527. currentSelected = sortFocused;
  528. mViewPager.setCurrentItem(sortFocused, false);
  529. changeTop(sortFocused != 0);
  530. }
  531. }
  532. }
  533. };
  534. @Override
  535. public boolean dispatchKeyEvent(KeyEvent event) {
  536. if (topHide < 0)
  537. return false;
  538. if (event.getAction() == KeyEvent.ACTION_DOWN) {
  539. if (event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
  540. showSiteSwitch();
  541. }
  542. } else if (event.getAction() == KeyEvent.ACTION_UP) {
  543. }
  544. return super.dispatchKeyEvent(event);
  545. }
  546. byte topHide = 0;
  547. private void changeTop(boolean hide) {
  548. ViewObj viewObj = new ViewObj(topLayout, (ViewGroup.MarginLayoutParams) topLayout.getLayoutParams());
  549. AnimatorSet animatorSet = new AnimatorSet();
  550. animatorSet.addListener(new Animator.AnimatorListener() {
  551. @Override
  552. public void onAnimationStart(Animator animation) {
  553. }
  554. @Override
  555. public void onAnimationEnd(Animator animation) {
  556. topHide = (byte) (hide ? 1 : 0);
  557. }
  558. @Override
  559. public void onAnimationCancel(Animator animation) {
  560. }
  561. @Override
  562. public void onAnimationRepeat(Animator animation) {
  563. }
  564. });
  565. // Hide Top =======================================================
  566. if (hide && topHide == 0) {
  567. animatorSet.playTogether(ObjectAnimator.ofObject(viewObj, "marginTop", new IntEvaluator(),
  568. Integer.valueOf(AutoSizeUtils.mm2px(this.mContext, 20.0f)),
  569. Integer.valueOf(AutoSizeUtils.mm2px(this.mContext, 0.0f))),
  570. ObjectAnimator.ofObject(viewObj, "height", new IntEvaluator(),
  571. Integer.valueOf(AutoSizeUtils.mm2px(this.mContext, 50.0f)),
  572. Integer.valueOf(AutoSizeUtils.mm2px(this.mContext, 1.0f))),
  573. ObjectAnimator.ofFloat(this.topLayout, "alpha", 1.0f, 0.0f));
  574. animatorSet.setDuration(250);
  575. animatorSet.start();
  576. tvWifi.setFocusable(false);
  577. tvFind.setFocusable(false);
  578. tvMenu.setFocusable(false);
  579. return;
  580. }
  581. // Show Top =======================================================
  582. if (!hide && topHide == 1) {
  583. animatorSet.playTogether(ObjectAnimator.ofObject(viewObj, "marginTop", new IntEvaluator(),
  584. Integer.valueOf(AutoSizeUtils.mm2px(this.mContext, 0.0f)),
  585. Integer.valueOf(AutoSizeUtils.mm2px(this.mContext, 20.0f))),
  586. ObjectAnimator.ofObject(viewObj, "height", new IntEvaluator(),
  587. Integer.valueOf(AutoSizeUtils.mm2px(this.mContext, 1.0f)),
  588. Integer.valueOf(AutoSizeUtils.mm2px(this.mContext, 50.0f))),
  589. ObjectAnimator.ofFloat(this.topLayout, "alpha", 0.0f, 1.0f));
  590. animatorSet.setDuration(250);
  591. animatorSet.start();
  592. tvWifi.setFocusable(true);
  593. tvFind.setFocusable(true);
  594. tvMenu.setFocusable(true);
  595. return;
  596. }
  597. }
  598. @Override
  599. protected void onDestroy() {
  600. super.onDestroy();
  601. EventBus.getDefault().unregister(this);
  602. AppManager.getInstance().appExit(0);
  603. ControlManager.get().stopServer();
  604. }
  605. // Site Switch on Home Button
  606. void showSiteSwitch() {
  607. List<SourceBean> sites = ApiConfig.get().getSourceBeanList();
  608. if (sites.size() > 0) {
  609. String homeSourceKey = ApiConfig.get().getHomeSourceBean().getKey();
  610. SelectDialog<SourceBean> dialog = new SelectDialog<>(HomeActivity.this);
  611. TvRecyclerView tvRecyclerView = dialog.findViewById(R.id.list);
  612. int spanCount;
  613. spanCount = (int)Math.floor(sites.size()/4);
  614. spanCount = Math.min(spanCount, 2);
  615. tvRecyclerView.setLayoutManager(new V7GridLayoutManager(dialog.getContext(), spanCount));
  616. ConstraintLayout cl_root = dialog.findViewById(R.id.cl_root);
  617. ViewGroup.LayoutParams clp = cl_root.getLayoutParams();
  618. clp.width = AutoSizeUtils.mm2px(dialog.getContext(), 300+140*spanCount);
  619. dialog.setTip(getString(R.string.dia_source));
  620. dialog.setAdapter(new SelectDialogAdapter.SelectDialogInterface<SourceBean>() {
  621. @Override
  622. public void click(SourceBean value, int pos) {
  623. ApiConfig.get().setSourceBean(value);
  624. Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
  625. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
  626. Bundle bundle = new Bundle();
  627. bundle.putBoolean("useCache", true);
  628. intent.putExtras(bundle);
  629. HomeActivity.this.startActivity(intent);
  630. }
  631. @Override
  632. public String getDisplay(SourceBean val) {
  633. return val.getName();
  634. }
  635. }, new DiffUtil.ItemCallback<SourceBean>() {
  636. @Override
  637. public boolean areItemsTheSame(@NonNull @NotNull SourceBean oldItem, @NonNull @NotNull SourceBean newItem) {
  638. return oldItem == newItem;
  639. }
  640. @Override
  641. public boolean areContentsTheSame(@NonNull @NotNull SourceBean oldItem, @NonNull @NotNull SourceBean newItem) {
  642. return oldItem.getKey().equals(newItem.getKey());
  643. }
  644. }, sites, sites.indexOf(ApiConfig.get().getHomeSourceBean()));
  645. dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
  646. @Override
  647. public void onDismiss(DialogInterface dialog) {
  648. // if (homeSourceKey != null && !homeSourceKey.equals(Hawk.get(HawkConfig.HOME_API, ""))) {
  649. // Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
  650. // intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
  651. // Bundle bundle = new Bundle();
  652. // bundle.putBoolean("useCache", true);
  653. // intent.putExtras(bundle);
  654. // HomeActivity.this.startActivity(intent);
  655. // }
  656. }
  657. });
  658. dialog.show();
  659. }
  660. }
  661. // public void onClick(View v) {
  662. // FastClickCheckUtil.check(v);
  663. // if (v.getId() == R.id.tvFind) {
  664. // jumpActivity(SearchActivity.class);
  665. // } else if (v.getId() == R.id.tvMenu) {
  666. // jumpActivity(SettingActivity.class);
  667. // }
  668. // }
  669. }