@Override public void onCancel(DownloadTask task) throws RemoteException { if (task != null) { sendBroadcastingToAppCenter(task); stopAppGameNetLog(task); if (mNotificationManager != null) { if (mShowNotification && mNotification != null) { // 更新下载信息 mNotification.tickerText = mCancelTickerText; mNotification.contentIntent = mPendingIntent; mNotification.flags = Notification.FLAG_AUTO_CANCEL; // mNotificationManager.notify(NOTIFY_TAG, (int) task.getId(), mNotification); if (mNotification.contentView != null) { mNotificationManager.notify(NOTIFY_TAG, (int) task.getId(), mNotification); } } } String filePath = task.getSaveFilePath(); if (TextUtils.isEmpty(filePath)) { return; } File saveFile = new File(filePath); if (saveFile.exists()) { saveFile.delete(); } } }
@Override public void onStop(DownloadTask task) throws RemoteException { if (mNotificationManager != null && mNotification != null) { mNotification.tickerText = mStopTickerText; mNotification.flags = Notification.FLAG_AUTO_CANCEL; mNotification.contentIntent = mPendingIntent; // if (RecommAppsUtils.isZh()) { mNotification.setLatestEventInfo( mContext, mPauseText, mProgressText + " " + task.getAlreadyDownloadPercent() + "%", mPendingIntent); // } else { // mNotification.setLatestEventInfo(mContext, mPauseText, // task.getAlreadyDownloadPercent() + "%", // mPendingIntent); // } // mNotificationManager.notify(NOTIFY_TAG, (int) task.getId(), mNotification); if (mNotification.contentView != null) { mNotificationManager.notify(NOTIFY_TAG, (int) task.getId(), mNotification); } } if (task != null) { // add by xiedezhi sendBroadcastingToAppCenter(task); } }
@Override public void onFail(DownloadTask task) throws RemoteException { // TODO Auto-generated method stub if (mContext != null && task != null) { if (mNotificationManager != null) { // 移除之前的通知 mNotificationManager.cancel(NOTIFY_TAG, (int) task.getId()); // 更新下载信息 if (mShowNotification && mNotification != null) { mNotification.tickerText = mFailTickerText; mNotification.contentIntent = mPendingIntent; // mNotificationRemoteViews.setTextViewText(R.id.downloadTextView, mFailText); // mNotification.contentView = mNotificationRemoteViews; mNotification.setLatestEventInfo( mContext, mFailText, mProgressText + " " + task.getAlreadyDownloadPercent() + "%", mPendingIntent); mNotification.flags = Notification.FLAG_AUTO_CANCEL; // mNotificationManager.notify(NOTIFY_TAG, (int) task.getId(), mNotification); if (mNotification.contentView != null) { mNotificationManager.notify(NOTIFY_TAG, (int) task.getId(), mNotification); } } } } }
@Override public void onDestroy(DownloadTask task) throws RemoteException { if (task != null) { // 如果任务还没有下载完成 if (mNotificationManager != null) { // 把通知移除 mNotificationManager.cancel(NOTIFY_TAG, (int) task.getId()); } if (DEBUG) { Log.i("", "-----------------state: " + task.getState()); } } }
private void setDonwloadSpeed(DownloadTask downloadTask) { long time = System.currentTimeMillis() - mStartDonwload; if (time > 0) { String speed = String.valueOf((downloadTask.getTotalSize() - mAlreadyDownloadSize) / time); if (DEBUG) { Log.d(TAG, "download apk speed : " + speed); } AppGameNetLogControll.getInstance().setDownloadSpeed(mChildThreadCode, speed); } }
@Override public void onComplete(DownloadTask task) throws RemoteException { // TODO Auto-generated method stub if (task != null) { stopAppGameNetLog(task); String filePath = task.getSaveFilePath(); File saveFile = new File(filePath); if (saveFile.exists() && saveFile.isFile()) { sendBroadcastingToAppCenter(task); } if (mContext != null && mNotificationManager != null) { // 首先移除之前的通知 mNotificationManager.cancel(NOTIFY_TAG, (int) task.getId()); if (mShowNotification) { // 创建新的通知,该通知可清除,点击后自动消失,并且进行安装 Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType( Uri.fromFile(new File(filePath)), "application/vnd.android.package-archive"); intent.setClass(mContext, AppInstallActivity.class); PendingIntent downCompletedIntent = PendingIntent.getActivity(mContext, 0, intent, 0); Notification finishNotification = new Notification( R.drawable.notification_download_complete_icon, mCompletedTickerText, System.currentTimeMillis()); finishNotification.setLatestEventInfo( mContext, task.getDownloadName(), mCompleteText, downCompletedIntent); // 该通知可清除,点击后自动消失 finishNotification.flags = Notification.FLAG_AUTO_CANCEL; // mNotificationManager.notify(NOTIFY_TAG, (int) task.getId(), finishNotification); if (mNotification.contentView != null) { mNotificationManager.notify(NOTIFY_TAG, (int) task.getId(), finishNotification); } } } openFile(task.getSaveFilePath()); } }
@Override public void onUpdate(DownloadTask task) throws RemoteException { if (task == null) { return; } if (task != null) { long currentTime = System.currentTimeMillis(); if (mStartDonwload == 0) { mStartDonwload = currentTime; mAlreadyDownloadSize = task.getAlreadyDownloadSize(); } if (currentTime - mPrevRefreshTime > REFRESH_INTERVAL) { mPrevRefreshTime = currentTime; if (mShowNotification && mNotificationManager != null && mNotification != null) { // 更新通知栏的step为1% if (task.getAlreadyDownloadPercent() < mPercentCount && mPercentCount > 100) { return; } sendBroadcastingToAppCenter(task); mPercentCount += mStep; mDisplayPercent = task.getAlreadyDownloadPercent(); mNotification.flags = Notification.FLAG_ONGOING_EVENT; mNotification.contentIntent = mPendingIntent; mNotification.setLatestEventInfo( mContext, mDownloadText, mProgressText + " " + task.getAlreadyDownloadPercent() + "%", mPendingIntent); // mNotificationManager.notify(NOTIFY_TAG, (int) task.getId(), mNotification); if (mNotification.contentView != null) { mNotificationManager.notify(NOTIFY_TAG, (int) task.getId(), mNotification); } } } } }
private void handleDownloadFail(DownloadTask downloadTask) { long taskId = downloadTask.getId(); String filePath = downloadTask.getSaveFilePath(); if (filePath != null && filePath.length() > 0) { File saveFile = new File(filePath); if (saveFile.exists()) { saveFile.delete(); } } IDownloadService mDownloadController = GOLauncherApp.getApplication().getDownloadController(); try { if (mDownloadController != null) { mDownloadController.removeDownloadTaskById(taskId); } } catch (RemoteException e) { e.printStackTrace(); } sendBroadcastingToAppCenter(downloadTask); // 首先移除之前的通知 if (mNotificationManager != null) { // 移除之前的通知 // mNotificationManager.cancel(NOTIFY_TAG, (int) downloadTask.getId()); mNotificationManager.cancel(NOTIFY_TAG, (int) downloadTask.getId()); // 更新下载信息 if (mShowNotification && mNotification != null) { mNotification.tickerText = mFailTickerText; mNotification.contentIntent = mPendingIntent; mNotification.flags = Notification.FLAG_AUTO_CANCEL; // mNotificationManager.notify(NOTIFY_TAG, (int) downloadTask.getId(), mNotification); if (mNotification.contentView != null) { mNotificationManager.notify(NOTIFY_TAG, (int) downloadTask.getId(), mNotification); } } } }
@Override public void onException(DownloadTask task) throws RemoteException { ArrayList<Exception> list = task.getExceptionList(); if (mStartDonwload != 0) { setDonwloadSpeed(task); for (Exception e : list) { AppGameNetLogControll.getInstance().setExceptionCode(mChildThreadCode, e); } } else { AppGameNetLogControll.getInstance().setDownloadSpeed(mChildThreadCode, "0"); for (Exception e : list) { AppGameNetLogControll.getInstance() .setExceptionCode(AppGameNetLogControll.DEFAULT_CURRENT_THREAD_CODE, e); } } mStartConntion = 0; mStartDonwload = 0; }
@Override public void onStart(DownloadTask task) throws RemoteException { // TODO Auto-generated method stub if (task == null) { // add by zhaojunjie return; } AppGameNetLogControll.getInstance() .startRecord( mContext, AppGameNetLogControll.DEFAULT_CURRENT_THREAD_CODE, AppGameNetInfoLog.NETLOG_TYPE_FOR_DOWNLOAD_APK); AppGameNetLogControll.getInstance() .setUrl(AppGameNetLogControll.DEFAULT_CURRENT_THREAD_CODE, task.getDownloadUrl()); mChildThreadCode = Thread.currentThread().hashCode(); mStartConntion = System.currentTimeMillis(); sendBroadcastingToAppCenter(task); if (mContext != null && mNotificationManager != null) { String downloadName = task.getDownloadName(); if (downloadName != null) { mStartTickerText = String.format( DOWNLOAD_TICKER_FORMAT_STRING, downloadName, mContext.getString(R.string.appgame_notification_ticker_start_text)); mStopTickerText = String.format( DOWNLOAD_TICKER_FORMAT_STRING, downloadName, mContext.getString(R.string.appgame_notification_ticker_stop_text)); mFailTickerText = String.format( DOWNLOAD_TICKER_FORMAT_STRING, downloadName, mContext.getString(R.string.appgame_notification_ticker_fail_text)); mCompletedTickerText = String.format( DOWNLOAD_TICKER_FORMAT_STRING, downloadName, mContext.getString(R.string.appgame_notification_ticker_complete_text)); mCancelTickerText = String.format( DOWNLOAD_TICKER_FORMAT_STRING, downloadName, mContext.getString(R.string.appgame_notification_ticker_cancel_text)); mConnectText = String.format( DOWNLOAD_INFO_FORMAT_STRING, downloadName, mContext.getString(R.string.appgame_notification_connect_text)); mDownloadText = String.format( DOWNLOAD_INFO_FORMAT_STRING, downloadName, mContext.getString(R.string.appgame_notification_download_text)); mPauseText = String.format( DOWNLOAD_INFO_FORMAT_STRING, downloadName, mContext.getString(R.string.appgame_notification_pause_text)); mFailText = String.format( DOWNLOAD_INFO_FORMAT_STRING, downloadName, mContext.getString(R.string.appgame_notification_fail_text)); mCompleteText = mContext.getString(R.string.appgame_notification_complete_text); mProgressText = mContext.getString(R.string.appgame_notification_progress_text); Intent intent = new Intent(); intent.setClass(mContext, AppsDownloadActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mPendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); mNotification = new Notification( R.drawable.notification_download_icon, mStartTickerText, System.currentTimeMillis()); mNotification.contentIntent = mPendingIntent; mNotification.flags = Notification.FLAG_AUTO_CANCEL; mNotification.setLatestEventInfo( mContext, mConnectText, mProgressText + " " + task.getAlreadyDownloadPercent() + "%", mPendingIntent); if (mNotification.contentView != null) { mNotificationManager.notify(NOTIFY_TAG, (int) task.getId(), mNotification); } } else { mShowNotification = false; } } }