@Override public void update(Observable arg0, Object data) { if (data instanceof DownloadTask) { DownloadTask task = (DownloadTask) data; if (task.getType() == DownloadTask.SKIN) { reshUI(task); } } else if (data instanceof SkinThemeApp) { SkinThemeApp skinTheme = (SkinThemeApp) data; if (selectedIndex != -1) { reshPICStatusUI(selectedIndex); } for (int i = 0; i < datas.size(); i++) { if (skinTheme.getID().equals(datas.get(i).getTid())) { selectedIndex = i; reshPICStatusUI(selectedIndex); break; } } } else if (data instanceof MessageIntent) { MessageIntent mi = (MessageIntent) data; if (mi.getAction().equals(MessageIntent.SKINTHEMEERROR)) { ToastUtil.showText("皮肤加载失败!"); notifyDataSetChanged(); } } }
/** * 查找已选择的主题皮肤的索引 * * @return */ private int findOldSelectedIndex() { if (datas != null && datas.size() != 0) { for (int i = 0; i < datas.size(); i++) { DownloadTask downloadTask = datas.get(i); if (downloadTask.getTid().equals(Constants.skinID)) { return i; } } } return -1; }
/** * 刷新ui * * @param task */ private void reshUI(final DownloadTask task) { if (task.getStatus() == DownloadTask.DOWNLOAD_FINISH) { new Thread() { @Override public void run() { boolean isExists = SkinThemeDB.getSkinThemeDB(context).skinThemeIsExists(task.getTid()); if (!isExists) { // 往皮肤数据库添加数据 SkinThemeApp skinTheme = new SkinThemeApp(); skinTheme.setID(task.getTid()); skinTheme.setThemeName(task.gettName()); skinTheme.setAssetsType(SkinThemeApp.NET); skinTheme.setAddTime(DateUtil.dateToString(new Date())); skinTheme.setDownloadPath(task.getFilePath()); // 解压皮肤文件 String path = context.getFilesDir().getParent() + File.separator + "files"; String outputDirectory = path + File.separator + "skin" + File.separator + task.getTid(); String zipPath = task.getFilePath(); String zipName = task.getTid(); skinTheme.setUnZipPath(outputDirectory); SkinThemeDB.getSkinThemeDB(context).add(skinTheme); UnzipUtil.unZip(context, zipName, zipPath, outputDirectory); Message msg = new Message(); msg.obj = skinTheme; ObserverManage.getObserver().setMessage(msg); } } }.start(); ToastUtil.showText("皮肤: " + task.gettName() + " 下载完成"); } for (int i = 0; i < datas.size(); i++) { DownloadTask temp = datas.get(i); if (task.getTid().equals(temp.getTid())) { datas.remove(i); datas.add(i, task); reshPICStatusUI(i); break; } } }
/** * 下载皮肤 * * @param position * @param downloadTask */ protected void downloadSkin(int position, DownloadTask downloadTask) { if (!CrashApplication.tasks.contains(downloadTask)) { downloadTask.setThread(new FileDownloadThread(downloadTask)); // if (tasks.size() == 0) // tasks.add(downloadTask); // else { // // int i = 0; // for (i = 0; i < tasks.size(); i++) { // DownloadTask temp = tasks.get(i); // for (int j = 0; j < datas.size(); j++) { // if (temp.getTid().equals(datas.get(j).getTid())) { // if (position < j) { // if (tasks.size() == 0){ // // } // else // tasks.add(1, downloadTask); // break; // } else { // tasks.add(downloadTask); // break; // } // } // } // } // if (i == tasks.size() - 1) { // tasks.add(downloadTask); // } // } downloadTask.setStatus(DownloadTask.WAITING); CrashApplication.tasks.add(downloadTask); ObserverManage.getObserver().setMessage(downloadTask); } else { downloadTask.setStatus(DownloadTask.INT); downloadTask.setThread(new FileDownloadThread(downloadTask)); } // 启动线程去下载任务 if (CrashApplication.downloadSkinThread == null) { CrashApplication.downloadSkinThread = new Thread(runnable); CrashApplication.downloadSkinThread.start(); } else { // 唤醒任务下载队列 synchronized (runnable) { runnable.notify(); } } }
@Override public void run() { while (true) { while (CrashApplication.tasks.size() > 0) { DownloadTask task = CrashApplication.tasks.get(0); FileDownloadThread thread = task.getThread(); if (thread.isFinish() || thread.isCancel() || thread.isError() || thread.isPause()) { CrashApplication.tasks.remove(0); } else { thread.start(context); } } // System.out.println("zhangliangming" + "下载完成!!"); // 如果队列为空,则令线程等待 synchronized (this) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } }
/** * 加载皮肤 * * @param position * @param downloadTask * @param itemViewHolder */ protected void loadSkin(int position, DownloadTask downloadTask, ItemViewHolder itemViewHolder) { selectedIndex = findOldSelectedIndex(); if (selectedIndex != -1) { reshPICStatusUI(selectedIndex); } // 设置当前皮肤主题的id和保存,并通知各个页面去加载新的皮肤 Constants.skinID = downloadTask.getTid(); DataUtil.saveValue(context, Constants.skinID_KEY, Constants.skinID); SkinThemeApp skinTheme = SkinThemeDB.getSkinThemeDB(context).getSkinThemeInfo(Constants.skinID); if (skinTheme != null) { DataUtil.loadSkin(context); ObserverManage.getObserver().setMessage(skinTheme); } selectedIndex = position; reshPICStatusUI(selectedIndex); }
@Override public void onBindViewHolder(final ItemViewHolder itemViewHolder, final int position) { final DownloadTask downloadTask = datas.get(position); String sid = downloadTask.getTid(); String imageUrl = HttpUtil.getSkinThemePreviewImageByID(sid); ImageLoadUtil.loadImageFormUrl( imageUrl, itemViewHolder.getImavPic(), R.drawable.picture_manager_default, true); itemViewHolder.getMainTextView().setText(downloadTask.gettName() + ""); itemViewHolder.getSkinSizeTextView().setText(getFileSize(downloadTask.getFileSize()) + ""); itemViewHolder.getSkinProgressBar().setMax((int) downloadTask.getFileSize()); itemViewHolder.getSkinProgressBar().setProgress((int) downloadTask.getDownloadedSize()); reshViewHolder(position, itemViewHolder, downloadTask); }
/** * 刷新ui * * @param position * @param itemViewHolder * @param downloadTask */ private void reshViewHolder( final int position, final ItemViewHolder itemViewHolder, final DownloadTask downloadTask) { if (SkinThemeDB.getSkinThemeDB(context).skinThemeIsExists(downloadTask.getTid())) { // itemViewHolder.getDownloadstatusRelativeLayout().setVisibility( // View.INVISIBLE); // itemViewHolder.getSkinProgressBar().setVisibility(View.INVISIBLE); if (!downloadTask.getTid().equals(Constants.skinID)) { itemViewHolder.getselectImageView().setVisibility(View.INVISIBLE); itemViewHolder .getItembg() .setOnClickListener( new OnClickListener() { @Override public void onClick(View arg0) { // 加载皮肤数据 loadSkin(position, downloadTask, itemViewHolder); } }); } else { itemViewHolder.getItembg().setOnClickListener(null); // selectedIndex = position; itemViewHolder.getselectImageView().setVisibility(View.VISIBLE); } itemViewHolder.getSkinProgressBar().setVisibility(View.INVISIBLE); itemViewHolder.getDownloadstatusRelativeLayout().setVisibility(View.INVISIBLE); } else { itemViewHolder.getselectImageView().setVisibility(View.INVISIBLE); if (downloadTask.getStatus() == DownloadTask.DOWNLOAD_FINISH) { itemViewHolder.getSkinProgressBar().setVisibility(View.INVISIBLE); itemViewHolder.getDownloadstatusRelativeLayout().setVisibility(View.INVISIBLE); itemViewHolder .getItembg() .setOnClickListener( new OnClickListener() { @Override public void onClick(View arg0) { // 加载皮肤数据 loadSkin(position, downloadTask, itemViewHolder); } }); } else if (downloadTask.getStatus() == DownloadTask.DOWNLOING || downloadTask.getStatus() == DownloadTask.WAITING) { itemViewHolder.getSkinProgressBar().setVisibility(View.VISIBLE); itemViewHolder.getDownloadstatusRelativeLayout().setVisibility(View.INVISIBLE); itemViewHolder.getItembg().setOnClickListener(null); } else { itemViewHolder.getSkinProgressBar().setVisibility(View.INVISIBLE); itemViewHolder.getDownloadstatusRelativeLayout().setVisibility(View.VISIBLE); itemViewHolder .getItembg() .setOnClickListener( new OnClickListener() { @Override public void onClick(View arg0) { // 下载皮肤 downloadSkin(position, downloadTask); } }); } } }