ListProgressBar.txt 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. package com.wzkj.vzone.org.ui.fragment.history;
  2. import android.annotation.TargetApi;
  3. import android.content.Intent;
  4. import android.os.Build;
  5. import android.os.Bundle;
  6. import android.support.annotation.Nullable;
  7. import android.support.v4.app.Fragment;
  8. import android.support.v7.widget.LinearLayoutManager;
  9. import android.support.v7.widget.RecyclerView;
  10. import android.text.TextUtils;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.view.Window;
  15. import android.view.WindowManager;
  16. import android.widget.Button;
  17. import android.widget.LinearLayout;
  18. import android.widget.TextView;
  19. import com.loopj.android.http.JsonHttpResponseHandler;
  20. import com.loopj.android.http.RequestParams;
  21. import com.loopj.android.http.ResponseHandlerInterface;
  22. import com.readystatesoftware.systembartint.SystemBarTintManager;
  23. import com.tencent.bugly.crashreport.CrashReport;
  24. import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;
  25. import com.wzkj.vzone.org.R;
  26. import com.wzkj.vzone.org.base.Operation;
  27. import com.wzkj.vzone.org.common.CommonUrl;
  28. import com.wzkj.vzone.org.common.ConstantUtil;
  29. import com.wzkj.vzone.org.entity.SuccessEntity;
  30. import com.wzkj.vzone.org.entity.TypeGameEntity;
  31. import com.wzkj.vzone.org.entity.TypeTask;
  32. import com.wzkj.vzone.org.eventEntity.InstallInfo;
  33. import com.wzkj.vzone.org.listener.OnTypeTaskClickListener;
  34. import com.wzkj.vzone.org.ui.activity.ForumActivity;
  35. import com.wzkj.vzone.org.ui.activity.GameDetailTypeActivity;
  36. import com.wzkj.vzone.org.ui.activity.LoginActivity;
  37. import com.wzkj.vzone.org.util.AppTool;
  38. import com.wzkj.vzone.org.util.DialogTool;
  39. import com.wzkj.vzone.org.util.FastJsonUtils;
  40. import com.wzkj.vzone.org.util.ToastUtils;
  41. import com.wzkj.vzone.org.util.ToolFile;
  42. import com.wzkj.vzone.org.util.ToolHttp;
  43. import com.wzkj.vzone.org.util.ToolImage;
  44. import com.wzkj.vzone.org.util.ToolPhone;
  45. import com.wzkj.vzone.org.widget.progressBar.MyProgressBar;
  46. import com.wzkj.vzone.org.widget.roundedImageView.RoundedImageView;
  47. import org.apache.http.Header;
  48. import org.greenrobot.eventbus.EventBus;
  49. import org.greenrobot.eventbus.Subscribe;
  50. import org.json.JSONObject;
  51. import org.xutils.common.Callback;
  52. import org.xutils.common.task.PriorityExecutor;
  53. import org.xutils.x;
  54. import java.io.File;
  55. import java.util.ArrayList;
  56. import java.util.HashMap;
  57. import java.util.List;
  58. import java.util.concurrent.Executor;
  59. /**
  60. * 推荐游戏
  61. *
  62. * @author Jason
  63. */
  64. public class NetGameFragment extends Fragment {
  65. private PullLoadMoreRecyclerView plmrv_recommend;
  66. private RecyclerViewAdapter recommendAdapter;
  67. private List<TypeTask> currentTasks;
  68. private LinearLayout ll_retry;
  69. private Button btn_retry;
  70. private boolean isEmpty = true;
  71. private int page = 1;
  72. public static NetGameFragment getInstance(String title) {
  73. NetGameFragment sf = new NetGameFragment();
  74. return sf;
  75. }
  76. @Override
  77. public void onCreate(@Nullable Bundle savedInstanceState) {
  78. super.onCreate(savedInstanceState);
  79. EventBus.getDefault().register(this);
  80. }
  81. @Nullable
  82. @Override
  83. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  84. View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_net_game, null);
  85. /**
  86. * 设置状态栏背景
  87. */
  88. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  89. setTranslucentStatus(true);
  90. SystemBarTintManager tintManager = new SystemBarTintManager(this.getActivity());
  91. tintManager.setStatusBarTintEnabled(true);
  92. tintManager.setStatusBarTintResource(R.color.transparent);
  93. tintManager.setNavigationBarTintEnabled(true);
  94. tintManager.setStatusBarAlpha(1f);
  95. }
  96. ll_retry = (LinearLayout) view.findViewById(R.id.ll_retry);
  97. btn_retry = (Button) view.findViewById(R.id.btn_retry);
  98. btn_retry.setOnClickListener(new View.OnClickListener() {
  99. @Override
  100. public void onClick(View v) {
  101. if (ToolHttp.checkNetwork()) { // 有网
  102. ll_retry.setVisibility(View.GONE);
  103. getCurrentGameData();
  104. }
  105. }
  106. });
  107. //正在进行的任务任务listview
  108. currentTasks = new ArrayList<>();
  109. plmrv_recommend = (PullLoadMoreRecyclerView) view.findViewById(R.id.plmrv_recommend);
  110. plmrv_recommend.setLinearLayout();
  111. plmrv_recommend.setPullRefreshEnable(false);// 需要下拉刷新
  112. plmrv_recommend.setPushRefreshEnable(true);// 需要上拉刷新
  113. plmrv_recommend.setFooterViewText("正在加载");
  114. recommendAdapter = new RecyclerViewAdapter(currentTasks);
  115. recommendAdapter.setOnTypeTaskClickListener(new OnTypeTaskClickListener() {
  116. @Override
  117. public void onCurrentTaskClick(TypeTask currentTask, String hasGift) {
  118. if (ConstantUtil.isUserLogin()) {
  119. // 把详情请求地址放在intent里传递过去
  120. Intent gameDetailIntent = new Intent(getActivity(), GameDetailTypeActivity.class);
  121. gameDetailIntent.putExtra("url", currentTask.getAnd_dow_address());
  122. gameDetailIntent.putExtra("gameId", currentTask.getId());
  123. gameDetailIntent.putExtra("packageName", currentTask.getAnd_package());
  124. new Operation(getActivity()).forward(gameDetailIntent);
  125. } else {
  126. new Operation(getActivity()).forward(LoginActivity.class);
  127. }
  128. }
  129. @Override
  130. public void onGameGiftClick(TypeTask currentTask) {
  131. if (ConstantUtil.isUserLogin()) {
  132. // 领取游戏礼包
  133. getGameGift(currentTask.getId(), 2);
  134. } else {
  135. new Operation(getActivity()).forward(LoginActivity.class);
  136. }
  137. }
  138. @Override
  139. public void onForumClick(String url) {
  140. if (!TextUtils.isEmpty(url)) {
  141. Intent forumIntent = new Intent(getActivity(), ForumActivity.class);
  142. forumIntent.putExtra("forum_url", url);
  143. startActivity(forumIntent);
  144. } else {
  145. ToastUtils.showShort(getActivity(), "暂无相关论坛资讯");
  146. }
  147. }
  148. });
  149. plmrv_recommend.setAdapter(recommendAdapter);
  150. plmrv_recommend.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {
  151. @Override
  152. public void onRefresh() {
  153. page = 1;
  154. getCurrentGameData();
  155. }
  156. @Override
  157. public void onLoadMore() {
  158. page++;
  159. getCurrentGameData();
  160. }
  161. });
  162. if (ToolHttp.checkNetwork()) { // 有网
  163. getCurrentGameData();
  164. } else {
  165. showEmptyView();
  166. }
  167. //上报后的Crash会显示该标签
  168. CrashReport.setUserSceneTag(getActivity(), 15489);
  169. //得到缓存对象
  170. return view;
  171. }
  172. /**
  173. * 设置状态栏背景
  174. */
  175. @TargetApi(19)
  176. private void setTranslucentStatus(boolean on) {
  177. Window win = getActivity().getWindow();
  178. WindowManager.LayoutParams winParams = win.getAttributes();
  179. final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
  180. if (on) {
  181. winParams.flags |= bits;
  182. } else {
  183. winParams.flags &= ~bits;
  184. }
  185. win.setAttributes(winParams);
  186. }
  187. @Override
  188. public void onDestroy() {
  189. super.onDestroy();
  190. EventBus.getDefault().unregister(this);
  191. }
  192. /**
  193. * 初始化当前任务
  194. */
  195. public void getCurrentGameData() {
  196. // 获取正在进行的任务
  197. RequestParams currentParams = new RequestParams();
  198. currentParams.put("platform", 0);
  199. currentParams.put("uid", ConstantUtil.UID);
  200. currentParams.put("type", 1);
  201. currentParams.put("page", page);// 起始页为1
  202. ToolHttp.getClient().get(CommonUrl.URL_TYPE_GAME, currentParams, getCurrentTaskResponseHandler());
  203. }
  204. /**
  205. * 获取正在进行的任务
  206. */
  207. private ResponseHandlerInterface getCurrentTaskResponseHandler() {
  208. return new JsonHttpResponseHandler() {
  209. @Override
  210. public void onFinish() {
  211. super.onFinish();
  212. plmrv_recommend.setPullLoadMoreCompleted();
  213. }
  214. @Override
  215. public void onProgress(int bytesWritten, int totalSize) {
  216. super.onProgress(bytesWritten, totalSize);
  217. }
  218. @Override
  219. public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
  220. super.onSuccess(statusCode, headers, response);
  221. if (response != null) {
  222. TypeGameEntity gameEntity = FastJsonUtils.stringToObject(response.toString(), TypeGameEntity.class);
  223. if (gameEntity != null) {
  224. if (gameEntity.getCode() == 200) {
  225. //放入缓存中
  226. setCurrentGameData(gameEntity, page);
  227. } else if (gameEntity.getCode() == 401) {
  228. ToastUtils.showLong(NetGameFragment.this.getActivity(), gameEntity.getMsg());
  229. new Operation(getActivity()).forward(LoginActivity.class);
  230. } else {
  231. ToastUtils.showLong(NetGameFragment.this.getActivity(), gameEntity.getMsg());
  232. }
  233. }
  234. }
  235. }
  236. @Override
  237. public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
  238. super.onFailure(statusCode, headers, throwable, errorResponse);
  239. if (errorResponse == null) {
  240. ToastUtils.showLong(NetGameFragment.this.getActivity(), ConstantUtil.ERRORMSG);
  241. } else {
  242. ToastUtils.showShort(getActivity(), errorResponse.toString());
  243. }
  244. if (page > 1) {
  245. page--;
  246. }
  247. }
  248. };
  249. }
  250. /**
  251. * 更新正在进行的任务
  252. */
  253. private void setCurrentGameData(TypeGameEntity gameEntity, int page) {
  254. if (page == 1) {
  255. if (currentTasks != null) {
  256. if (currentTasks.size() > 0) {
  257. currentTasks.clear();
  258. }
  259. }
  260. }
  261. List<TypeTask> games = gameEntity.getList();
  262. if (games == null || games.size() == 0) {
  263. ToastUtils.showShort(getActivity(), "没有更多数据啦");
  264. } else {
  265. currentTasks.addAll(games);
  266. recommendAdapter.notifyDataSetChanged();
  267. }
  268. }
  269. /**
  270. * 显示重试
  271. */
  272. private void showEmptyView() {
  273. if (ll_retry != null) {
  274. if (isEmpty) {
  275. ll_retry.setVisibility(View.VISIBLE);
  276. } else {
  277. ll_retry.setVisibility(View.GONE);
  278. }
  279. }
  280. }
  281. /**
  282. * 领取游戏礼包
  283. */
  284. private void getGameGift(String gameId, int type) {
  285. // 2表示游戏礼包
  286. RequestParams params = new RequestParams();
  287. params.put("uid", ConstantUtil.UID);
  288. params.put("token", ConstantUtil.TOKEN);
  289. params.put("gid", gameId);
  290. params.put("p_i", ToolPhone.getImei(getActivity()));
  291. params.put("t", type);
  292. ToolHttp.getClient().get(CommonUrl.gameAwardURL, params, getGameGiftResponseHandler());
  293. }
  294. /**
  295. * 领取游戏礼包
  296. */
  297. private ResponseHandlerInterface getGameGiftResponseHandler() {
  298. return new JsonHttpResponseHandler() {
  299. @Override
  300. public void onFinish() {
  301. super.onFinish();
  302. }
  303. @Override
  304. public void onProgress(int bytesWritten, int totalSize) {
  305. super.onProgress(bytesWritten, totalSize);
  306. }
  307. @Override
  308. public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
  309. super.onSuccess(statusCode, headers, response);
  310. if (response != null) {
  311. SuccessEntity successEntity = FastJsonUtils.stringToObject(response.toString(), SuccessEntity.class);
  312. if (successEntity != null) {
  313. // 刷新个人资料
  314. if (!TextUtils.isEmpty(successEntity.getNovice())) {
  315. DialogTool.gameGiftDialog(getActivity(), successEntity.getNovice(), successEntity.getGiftbag_name(), successEntity.getDesribe());
  316. } else {
  317. ToastUtils.showShort(getActivity(), "暂无礼包");
  318. }
  319. } else {
  320. ToastUtils.showShort(getActivity(), "暂无礼包");
  321. }
  322. }
  323. }
  324. @Override
  325. public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
  326. super.onFailure(statusCode, headers, throwable, errorResponse);
  327. ToastUtils.showShort(getActivity(), "暂无礼包");
  328. }
  329. };
  330. }
  331. class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
  332. private List<TypeTask> recommendTasks;
  333. private OnTypeTaskClickListener onTypeTaskClickListener;
  334. public void setOnTypeTaskClickListener(OnTypeTaskClickListener onTypeTaskClickListener) {
  335. this.onTypeTaskClickListener = onTypeTaskClickListener;
  336. }
  337. public RecyclerViewAdapter(List<TypeTask> recommendTasks) {
  338. this.recommendTasks = recommendTasks;
  339. }
  340. @Override
  341. public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  342. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_net_game, null, false);
  343. ViewHolder viewHolder = new ViewHolder(view);
  344. return viewHolder;
  345. }
  346. @Override
  347. public void onBindViewHolder(final ViewHolder holder, final int position) {
  348. if (recommendTasks != null && recommendTasks.size() > 0) {
  349. // 海报,解决图片错乱
  350. final TypeTask typeTask = recommendTasks.get(position);
  351. if (!TextUtils.isEmpty(typeTask.getIcon())) {
  352. final String tag = (String) holder.riv_game_icon.getTag();
  353. final String uri = typeTask.getIcon();
  354. if (!uri.equals(tag)) {
  355. // 设置默认图片
  356. holder.riv_game_icon.setImageResource(R.mipmap.show_default);
  357. holder.riv_game_icon.setTag(uri);
  358. ToolImage.getImageLoader().displayImage(uri, holder.riv_game_icon, ToolImage.getFadeOptions(R.mipmap.show_default, R.mipmap.show_default));
  359. }
  360. } else {
  361. // 设置默认图片
  362. holder.riv_game_icon.setImageResource(R.mipmap.show_default);
  363. }
  364. // 游戏名称
  365. holder.tv_game_name.setText(typeTask.getGame_name());
  366. holder.tv_desc.setText(typeTask.getIntroduction());
  367. holder.tv_down_size.setText(String.format("%s %s人下载", typeTask.getGame_size(), typeTask.getDow_num()));
  368. final String gift = typeTask.getGift_bag();// 0" 无礼包不显示礼包按钮,"1"有礼包,显示礼包按钮
  369. if (TextUtils.equals(gift, "0")) {
  370. holder.btn_get_gift.setVisibility(View.GONE);
  371. } else {
  372. holder.btn_get_gift.setVisibility(View.VISIBLE);
  373. }
  374. holder.riv_game_icon.setOnClickListener(new View.OnClickListener() {
  375. @Override
  376. public void onClick(View v) {
  377. if (onTypeTaskClickListener != null) {
  378. onTypeTaskClickListener.onCurrentTaskClick(typeTask, gift);
  379. }
  380. }
  381. });
  382. holder.ll_net_game.setOnClickListener(new View.OnClickListener() {
  383. @Override
  384. public void onClick(View v) {
  385. if (onTypeTaskClickListener != null) {
  386. onTypeTaskClickListener.onCurrentTaskClick(typeTask, gift);
  387. }
  388. }
  389. });
  390. /**
  391. * 点击领取礼包
  392. */
  393. holder.btn_get_gift.setOnClickListener(new View.OnClickListener() {
  394. @Override
  395. public void onClick(View v) {
  396. if (onTypeTaskClickListener != null) {
  397. onTypeTaskClickListener.onGameGiftClick(typeTask);
  398. }
  399. }
  400. });
  401. /**
  402. * 点击论坛攻略
  403. */
  404. holder.btn_forum_tips.setOnClickListener(new View.OnClickListener() {
  405. @Override
  406. public void onClick(View v) {
  407. if (onTypeTaskClickListener != null) {
  408. onTypeTaskClickListener.onForumClick(typeTask.getGame_bbs());
  409. }
  410. }
  411. });
  412. // 查看当前状态
  413. if (typeTask.isRefreshing()) {
  414. // 当前状态正在刷新,隐藏下载按钮,显示进度条,并设置进度条的值
  415. holder.btn_download_install_open.setVisibility(View.GONE);
  416. holder.mpb_download_progress.setVisibility(View.VISIBLE);
  417. holder.mpb_download_progress.setProgress(typeTask.getProgress());
  418. } else {
  419. // 当前不是刷新状态,隐藏进度条,显示下载按钮
  420. holder.btn_download_install_open.setVisibility(View.VISIBLE);
  421. holder.mpb_download_progress.setVisibility(View.GONE);
  422. }
  423. if (AppTool.checkApkExist(getActivity(), typeTask.getAnd_package())) {
  424. // 游戏已安装
  425. holder.btn_download_install_open.setText("打开");
  426. holder.btn_download_install_open.setOnClickListener(new View.OnClickListener() {
  427. @Override
  428. public void onClick(View v) {
  429. AppTool.openAppByPackageName(getActivity(), typeTask.getAnd_package());
  430. }
  431. });
  432. } else {
  433. final String localGamePath = ConstantUtil.LOCAL_DOWNLOAD_PATH+typeTask.getGame_name()+".apk";
  434. typeTask.setGameLocalPath(localGamePath);
  435. File file = new File(localGamePath);
  436. if (file.exists()) {
  437. // 游戏已下载,但还没安装
  438. holder.btn_download_install_open.setText("安装");
  439. holder.btn_download_install_open.setOnClickListener(new View.OnClickListener() {
  440. @Override
  441. public void onClick(View v) {
  442. AppTool.install(getActivity(), localGamePath);
  443. }
  444. });
  445. } else {
  446. // 游戏还没下载
  447. holder.btn_download_install_open.setText("下载");
  448. holder.btn_download_install_open.setOnClickListener(new View.OnClickListener() {
  449. @Override
  450. public void onClick(View v) {
  451. // 下载游戏之前的准备工作
  452. if (TextUtils.isEmpty(typeTask.getAnd_dow_address())) {
  453. ToastUtils.showShort(getActivity(), "游戏下载地址为空");
  454. return;
  455. }
  456. int gameSize = 0;
  457. try {
  458. gameSize = Integer.parseInt(typeTask.getGame_size().toLowerCase().replace("m", "").replace("b", ""));
  459. } catch (Exception e) {
  460. e.printStackTrace();
  461. }
  462. if (gameSize > ToolFile.getSDFreeSize()) {
  463. ToastUtils.showShort(getActivity(), "存储空间不足,请清理缓存后重试");
  464. return;
  465. }
  466. if (!TextUtils.isEmpty(typeTask.getGameLocalPath())) {
  467. } else {
  468. ToastUtils.showShort(getActivity(), "未找到本地下载路径");
  469. }
  470. if (ConstantUtil.DOWNLOAD_TASKS != null && ConstantUtil.DOWNLOAD_TASKS.size() > 3) {
  471. ToastUtils.showShort(getActivity(), "您最多能同时下载3个任务");
  472. return;
  473. }
  474. typeTask.setRefreshing(true);// 状态为正在刷新
  475. holder.btn_download_install_open.setVisibility(View.GONE);// 开始下载,隐藏下载按钮
  476. holder.mpb_download_progress.setVisibility(View.VISIBLE);// 显示进度条
  477. holder.mpb_download_progress.setProgress(0);// 初始化进度为0%
  478. downloadGame(position, typeTask);
  479. // 已开始下载的话
  480. }
  481. });
  482. }
  483. }
  484. // 点击进度条取消当前任务
  485. holder.mpb_download_progress.setOnClickListener(new View.OnClickListener() {
  486. @Override
  487. public void onClick(View v) {
  488. if (ConstantUtil.DOWNLOAD_TASKS != null) {
  489. Callback.Cancelable cancelable = ConstantUtil.DOWNLOAD_TASKS.get(typeTask.getId());
  490. if (cancelable != null) {
  491. cancelable.cancel();
  492. }
  493. typeTask.setRefreshing(false);// 现在停止刷新
  494. holder.mpb_download_progress.setVisibility(View.GONE);// 隐藏进度条
  495. holder.btn_download_install_open.setVisibility(View.VISIBLE);// 显示下载|安装|打开按钮
  496. }
  497. }
  498. });
  499. }
  500. }
  501. @Override
  502. public int getItemCount() {
  503. return recommendTasks == null ? 0 : recommendTasks.size();
  504. }
  505. public class ViewHolder extends RecyclerView.ViewHolder {
  506. LinearLayout ll_net_game;
  507. RoundedImageView riv_game_icon;
  508. Button btn_forum_tips;
  509. Button btn_get_gift;
  510. TextView tv_game_name;
  511. TextView tv_desc;// 描述
  512. TextView tv_down_size;// 描述
  513. Button btn_download_install_open;
  514. MyProgressBar mpb_download_progress;
  515. public ViewHolder(View itemView) {
  516. super(itemView);
  517. // 这里查找控件
  518. ll_net_game = (LinearLayout) itemView.findViewById(R.id.ll_net_game);
  519. riv_game_icon = (RoundedImageView) itemView.findViewById(R.id.riv_game_icon);
  520. btn_forum_tips = (Button) itemView.findViewById(R.id.btn_forum_tips);
  521. btn_get_gift = (Button) itemView.findViewById(R.id.btn_get_gift);
  522. tv_game_name = (TextView) itemView.findViewById(R.id.tv_game_name);
  523. tv_desc = (TextView) itemView.findViewById(R.id.tv_desc);
  524. tv_down_size = (TextView) itemView.findViewById(R.id.tv_down_size);
  525. btn_download_install_open = (Button) itemView.findViewById(R.id.btn_download_install_open);
  526. mpb_download_progress = (MyProgressBar) itemView.findViewById(R.id.mpb_download_progress);
  527. }
  528. }
  529. }
  530. private final Executor executor = new PriorityExecutor(8, true);
  531. /**
  532. * 下载游戏,这个方法在Activity销毁之后仍会继续执行
  533. */
  534. private void downloadGame(final int position, final TypeTask typeTask) {
  535. org.xutils.http.RequestParams params = new org.xutils.http.RequestParams(typeTask.getAnd_dow_address());
  536. params.setAutoResume(true);
  537. params.setAutoRename(false);
  538. params.setSaveFilePath(typeTask.getGameLocalPath());
  539. params.setExecutor(executor);
  540. params.setCancelFast(true);
  541. Callback.Cancelable downloadTask = x.http().get(params, new Callback.ProgressCallback<File>() {
  542. @Override
  543. public void onWaiting() {
  544. }
  545. @Override
  546. public void onStarted() {
  547. }
  548. @Override
  549. public void onLoading(long total, long current, boolean isDownloading) {
  550. // 更新下载进度
  551. int progress = (int)(current*100/total);
  552. // 把进度写进我们的对象里
  553. typeTask.setProgress(progress);
  554. // 发送进度广播
  555. Intent processIntent = new Intent("process");
  556. processIntent.putExtra("process", progress);
  557. processIntent.putExtra("id", typeTask.getId());
  558. getActivity().sendBroadcast(processIntent);
  559. updateProgress(position, typeTask, false);
  560. }
  561. @Override
  562. public void onSuccess(File result) {
  563. }
  564. @Override
  565. public void onError(Throwable ex, boolean isOnCallback) {
  566. ToastUtils.showShort(getActivity(), "下载失败");
  567. typeTask.setRefreshing(false);
  568. // 发送下载失败广播
  569. Intent processIntent = new Intent();
  570. processIntent.putExtra("process", -1);
  571. processIntent.putExtra("id", typeTask.getId());
  572. getActivity().sendBroadcast(processIntent);
  573. updateProgress(position, typeTask, true);
  574. }
  575. @Override
  576. public void onCancelled(CancelledException cex) {
  577. }
  578. @Override
  579. public void onFinished() {
  580. // 从列表中移除
  581. if (ConstantUtil.DOWNLOAD_TASKS != null) {
  582. ConstantUtil.DOWNLOAD_TASKS.remove(typeTask.getId());
  583. }
  584. typeTask.setRefreshing(false);// 无论是下载完成还是下载失败,此时状态为停止刷新
  585. }
  586. });
  587. // 下载任务放进队列里
  588. if (ConstantUtil.DOWNLOAD_TASKS == null) {
  589. ConstantUtil.DOWNLOAD_TASKS = new HashMap<>();
  590. }
  591. ConstantUtil.DOWNLOAD_TASKS.put(typeTask.getId(), downloadTask);
  592. }
  593. /**
  594. * 更新控件的进度,注意要判空
  595. * @param position 要更新的位置
  596. * @param isError 是否发生错误(下载失败)
  597. */
  598. public void updateProgress(int position, final TypeTask typeTask, boolean isError) {
  599. // 根据点击的position找到之前点击的下载按钮所在的ViewHolder
  600. RecyclerViewAdapter.ViewHolder viewHolder = (RecyclerViewAdapter.ViewHolder) plmrv_recommend.getRecyclerView().findViewHolderForAdapterPosition(position);
  601. if (viewHolder != null) {
  602. // 通过viewholder可以找回我们刚才点击的按钮和进度条
  603. Button btn_download_install_open = viewHolder.btn_download_install_open;
  604. MyProgressBar mpb_download_progress = viewHolder.mpb_download_progress;
  605. if (isError) {
  606. // 下载过程发生错误,显示下载按钮,隐藏进度条
  607. btn_download_install_open.setVisibility(View.VISIBLE);
  608. mpb_download_progress.setVisibility(View.GONE);
  609. } else {
  610. // 更新进度
  611. mpb_download_progress.setProgress(typeTask.getProgress());
  612. if (typeTask.getProgress() == 100) {
  613. if (AppTool.checkApkExist(getActivity(), typeTask.getAnd_package())) {
  614. btn_download_install_open.setText("打开");
  615. btn_download_install_open.setOnClickListener(new View.OnClickListener() {
  616. @Override
  617. public void onClick(View v) {
  618. AppTool.openAppByPackageName(getActivity(), typeTask.getAnd_package());
  619. }
  620. });
  621. } else {
  622. btn_download_install_open.setText("安装");
  623. btn_download_install_open.setOnClickListener(new View.OnClickListener() {
  624. @Override
  625. public void onClick(View v) {
  626. AppTool.install(getActivity(), typeTask.getGameLocalPath());
  627. }
  628. });
  629. }
  630. btn_download_install_open.setVisibility(View.VISIBLE);
  631. mpb_download_progress.setVisibility(View.GONE);
  632. } else {
  633. btn_download_install_open.setVisibility(View.GONE);
  634. mpb_download_progress.setVisibility(View.VISIBLE);
  635. }
  636. }
  637. }
  638. }
  639. @Subscribe
  640. public void onEventMainThread(final InstallInfo installInfo) {
  641. // 安装完成
  642. if (plmrv_recommend == null) {
  643. return;
  644. }
  645. for (int i = 0, j = plmrv_recommend.getLayoutManager().getChildCount(); i < j; i++) {
  646. if (TextUtils.equals(currentTasks.get(i).getAnd_package(), installInfo.getPackageName())) {
  647. View view = plmrv_recommend.getLayoutManager().findViewByPosition(i);
  648. Button btn_download_install_open = (Button) view.findViewById(R.id.btn_download_install_open);
  649. btn_download_install_open.setText("打开");
  650. btn_download_install_open.setOnClickListener(new View.OnClickListener() {
  651. @Override
  652. public void onClick(View v) {
  653. AppTool.openAppByPackageName(getActivity(), installInfo.getPackageName());
  654. }
  655. });
  656. if (ConstantUtil.deleteAfterInstall) {
  657. // 自动删除
  658. File file = new File(currentTasks.get(i).getGameLocalPath());
  659. if (file.exists()) {
  660. file.delete();
  661. }
  662. }
  663. break;
  664. }
  665. }
  666. }
  667. }