@Override public void handleMessage(Message msg) { switch (msg.what) { case 2: if (mAppInfoForManage == null) { return; } String packageName = mAppInfoForManage.getPackageName(); int size = DownloadService.downtask.size(); boolean found = false; for (int i = 0; i < size; i++) { DownloadTask task = DownloadService.downtask.get(i); if (task.getPackageName().equals(packageName)) { found = true; int percent = 0; int totalSize = task.getTotalSize(); int currentSize = task.getCurrentSize(); mProgressBar.setMax(totalSize); mProgressBar.setProgress(currentSize); if (totalSize > 0) { percent = currentSize * 100 / totalSize; } String text = String.format( "%d%% %dKB/%dKB", percent, currentSize / 1000, totalSize / 1000); mDownloadingText.setText(text); break; } } if (found) { this.sendEmptyMessageDelayed(2, 500); } else { tempCount += 1; this.sendEmptyMessage(7); } break; case 7: mInstallApk.setClickable(true); mDownloadingText.setVisibility(View.GONE); mProgressBar.setVisibility(View.GONE); TextView textDesc = (TextView) findViewById(R.id.textDesc); textDesc.setText(R.string.softmanage_install_now_text); isDownloading = false; updateButton(); break; } }
@Override public void onClick(View v) { if (v.getId() == R.id.installApk) { if (isDownloading) { // cancel downloading cancelDownloading(); } else { isDownloading = true; DownloadTask task = new DownloadTask(); task.setTitle(mAppInfoForManage.getLabel().toString()); task.setId(mAppInfoForManage.getAppId()); if (mAppInfoForManage.getIcon() != null) { Bitmap bitmap = ((BitmapDrawable) mAppInfoForManage.getIcon()).getBitmap(); task.setIcon(bitmap); } task.setPackageName(mAppInfoForManage.getPackageName()); task.setDownUrl(mAppInfoForManage.getPackagePath()); task.setTotalSize((int) mAppInfoForManage.getSize()); if (DownloadService.addDownTask(task)) { Intent i = new Intent(DownloadService.UPDATE_DOWN_TASK); i.setClass(this, DownloadService.class); startService(i); } mProgressBar.setVisibility(View.VISIBLE); mDownloadingText.setVisibility(View.VISIBLE); mHandler.sendEmptyMessage(2); } updateButton(); } }