Search in sources :

Example 6 with NotificationManager

use of android.app.NotificationManager in project SimplifyReader by chentao0707.

the class DownloadManager method deleteDownloadeds.

public boolean deleteDownloadeds(final ArrayList<DownloadInfo> infos) {
    Logger.d(TAG, "deleteDownloadeds() : ArrayList");
    if (infos == null || infos.size() == 0)
        return true;
    String nId = YoukuPlayerApplication.getPreference(KEY_LAST_NOTIFY_TASKID);
    for (DownloadInfo info : infos) {
        downloadedData.remove(info.videoid);
        if (nId.equals(info.taskId)) {
            NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            nm.cancel(NOTIFY_ID);
            YoukuPlayerApplication.savePreference(KEY_LAST_NOTIFY_TASKID, "");
        }
    }
    new Thread() {

        public void run() {
            for (DownloadInfo info : infos) {
                PlayerUtil.deleteFile(new File(info.savePath));
            }
        }

        ;
    }.start();
    return true;
}
Also used : NotificationManager(android.app.NotificationManager) File(java.io.File)

Example 7 with NotificationManager

use of android.app.NotificationManager in project SimplifyReader by chentao0707.

the class FileDownloadThread method downloadSegment.

/**
	 * 下载分片
	 * 
	 * @param info
	 * @return true下载完成;false下载失败或取消下载
	 */
private boolean downloadSegment(DownloadInfo info) {
    Logger.d("DownloadFlow", "FileDownloadThread: downloadSegment()");
    File f = checkAndGetFile(info);
    if (f == null) {
        cancel = true;
        info.setState(DownloadInfo.STATE_EXCEPTION);
        return false;
    }
    // 当前分片的结束位置
    final long endPosition = info.segsSize[info.segId - 1];
    // 当前分片的下载进度位置
    long curPosition = info.segDownloadedSize;
    if (curPosition >= endPosition) {
        // 当前分片已下载完成
        return true;
    }
    InputStream is = getInputStreamFromURL(info, download.canUseAcc());
    if (is == null) {
        cancel = true;
        info.setState(DownloadInfo.STATE_EXCEPTION);
        return false;
    }
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
        bis = new BufferedInputStream(is);
        bos = new BufferedOutputStream(new FileOutputStream(f, true));
        int len = 0;
        byte[] buf = new byte[BUFFER_SIZE];
        while (cancel == false && curPosition < endPosition && info.getState() == DownloadInfo.STATE_DOWNLOADING && (len = bis.read(buf, 0, BUFFER_SIZE)) != -1 && cancel == false && info.getState() == DownloadInfo.STATE_DOWNLOADING) {
            // 因为read()是耗时操作,所以需要二次判断
            bos.write(buf, 0, len);
            curPosition += len;
            if (curPosition > endPosition) {
                Logger.d(TAG, "curPosition > endPosition,curPosition:" + curPosition + ",endPosition:" + endPosition);
                info.segDownloadedSize += (len - (curPosition - endPosition) + 1);
                info.downloadedSize += (len - (curPosition - endPosition) + 1);
            } else {
                info.segDownloadedSize = curPosition;
                info.downloadedSize += len;
            }
            info.setProgress(((double) info.downloadedSize * 100) / info.size);
            if (info.retry != 0)
                info.retry = 0;
            while (pause) {
                try {
                    sleep(500L);
                } catch (InterruptedException e) {
                }
            }
        }
        if (curPosition >= endPosition)
            return true;
    } catch (SocketTimeoutException e) {
        Logger.e("DownloadFlow", "FileDownloadThread: downloadSegment(): " + e.toString());
        Logger.e(TAG, e);
        if (info.getState() != DownloadInfo.STATE_PAUSE && info.getState() != DownloadInfo.STATE_CANCEL) {
            if (Util.hasInternet()) {
                info.setExceptionId(DownloadInfo.EXCEPTION_TIMEOUT);
                if (info.retry == 0) {
                    PlayerUtil.showTips(info.getExceptionInfo());
                }
            } else {
                info.setExceptionId(DownloadInfo.EXCEPTION_NO_NETWORK);
            }
            cancel = true;
            info.setState(DownloadInfo.STATE_EXCEPTION);
        }
    } catch (SocketException e) {
        Logger.e("DownloadFlow", "FileDownloadThread: downloadSegment(): " + e.toString());
        Logger.e(TAG, e);
        if (info.getState() != DownloadInfo.STATE_PAUSE && info.getState() != DownloadInfo.STATE_CANCEL) {
            if (Util.hasInternet()) {
                info.setExceptionId(DownloadInfo.EXCEPTION_TIMEOUT);
                if (info.retry == 0) {
                    PlayerUtil.showTips(info.getExceptionInfo());
                }
            } else {
                info.setExceptionId(DownloadInfo.EXCEPTION_NO_NETWORK);
            }
            cancel = true;
            info.setState(DownloadInfo.STATE_EXCEPTION);
        }
    } catch (FileNotFoundException e) {
        // SD卡被拔出
        Logger.e("DownloadFlow", "FileDownloadThread: downloadSegment(): " + e.toString());
        Logger.e(TAG, e);
        NotificationManager nm = (NotificationManager) YoukuPlayerApplication.context.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.cancel(IDownload.NOTIFY_ID);
    } catch (IOException e) {
        Logger.e("DownloadFlow", "FileDownloadThread: downloadSegment(): " + e.toString());
        Logger.e(TAG, e);
        if (info.getState() != DownloadInfo.STATE_PAUSE && info.getState() != DownloadInfo.STATE_CANCEL) {
            // if (info.createTime < DownloadManager222.deleteAllTimestamp)
            // {
            // 如果删除后立马又创建了该视频,则需要重新删掉
            // download.deleteCache(info);
            // } else {
            String[] temp = info.savePath.split(YoukuPlayerApplication.getDownloadPath());
            SDCardManager m = new SDCardManager(temp[0]);
            if (!m.exist()) {
                info.setExceptionId(DownloadInfo.EXCEPTION_NO_SDCARD);
                PlayerUtil.showTips(info.getExceptionInfo());
            } else if (m.getFreeSize() - info.size <= 0) {
                info.setExceptionId(DownloadInfo.EXCEPTION_NO_SPACE);
                PlayerUtil.showTips(info.getExceptionInfo());
            }
            cancel = true;
            info.setState(DownloadInfo.STATE_EXCEPTION);
        // }
        }
    } finally {
        try {
            if (bos != null)
                bos.close();
            if (bis != null)
                bis.close();
            if (is != null)
                is.close();
        } catch (IOException e) {
        }
    }
    return false;
}
Also used : SocketException(java.net.SocketException) NotificationManager(android.app.NotificationManager) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) SocketTimeoutException(java.net.SocketTimeoutException) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 8 with NotificationManager

use of android.app.NotificationManager in project SimplifyReader by chentao0707.

the class DownloadManager method deleteAllDownloaded.

@SuppressWarnings({ "rawtypes", "unchecked" })
public boolean deleteAllDownloaded() {
    Logger.d(TAG, "deleteAllDownloaded()");
    if (getDownloadedData().size() == 0)
        return true;
    final HashMap<String, DownloadInfo> clone = (HashMap<String, DownloadInfo>) getDownloadedData().clone();
    String nId = YoukuPlayerApplication.getPreference(KEY_LAST_NOTIFY_TASKID);
    // 获得map的Iterator
    Iterator iter = getDownloadedData().entrySet().iterator();
    while (iter.hasNext()) {
        Entry entry = (Entry) iter.next();
        DownloadInfo info = (DownloadInfo) entry.getValue();
        if (nId.equals(info.taskId)) {
            NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            nm.cancel(NOTIFY_ID);
            YoukuPlayerApplication.savePreference(KEY_LAST_NOTIFY_TASKID, "");
        }
    }
    new Thread() {

        public void run() {
            Iterator iter = clone.entrySet().iterator();
            while (iter.hasNext()) {
                Entry entry = (Entry) iter.next();
                DownloadInfo info = (DownloadInfo) entry.getValue();
                PlayerUtil.deleteFile(new File(info.savePath));
            }
        }

        ;
    }.start();
    getDownloadedData().clear();
    return true;
}
Also used : Entry(java.util.Map.Entry) NotificationManager(android.app.NotificationManager) HashMap(java.util.HashMap) Iterator(java.util.Iterator) File(java.io.File)

Example 9 with NotificationManager

use of android.app.NotificationManager in project SimplifyReader by chentao0707.

the class DownloadManager method deleteDownloaded.

public boolean deleteDownloaded(final DownloadInfo info) {
    Logger.d(TAG, "deleteDownloaded() :" + info.title);
    // info.setState(DownloadInfo.STATE_CANCEL);
    downloadedData.remove(info.videoid);
    if (YoukuPlayerApplication.getPreference(KEY_LAST_NOTIFY_TASKID).equals(info.taskId)) {
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.cancel(NOTIFY_ID);
        YoukuPlayerApplication.savePreference(KEY_LAST_NOTIFY_TASKID, "");
    }
    new Thread() {

        public void run() {
            PlayerUtil.deleteFile(new File(info.savePath));
        }

        ;
    }.start();
    startNewTask();
    return true;
}
Also used : NotificationManager(android.app.NotificationManager) File(java.io.File)

Example 10 with NotificationManager

use of android.app.NotificationManager in project SimplifyReader by chentao0707.

the class DownloadServiceManager method deleteDownloading.

@Override
public boolean deleteDownloading(String taskId) {
    final DownloadInfo info = getDownloadingData().get(taskId);
    // 如果是下载中的视频,改变状态自己就会删除
    info.setState(DownloadInfo.STATE_CANCEL);
    downloadingData.remove(taskId);
    if (thread != null && thread.isStop() == false && taskId.equals(thread.getTaskId())) {
        thread.cancel();
    }
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    if (YoukuPlayerApplication.getPreference(KEY_LAST_NOTIFY_TASKID).equals(info.taskId)) {
        nm.cancel(NOTIFY_ID);
        YoukuPlayerApplication.savePreference(KEY_LAST_NOTIFY_TASKID, "");
    }
    new Thread() {

        public void run() {
            PlayerUtil.deleteFile(new File(info.savePath));
        }

        ;
    }.start();
    startNewTask();
    return true;
}
Also used : NotificationManager(android.app.NotificationManager) File(java.io.File)

Aggregations

NotificationManager (android.app.NotificationManager)374 Intent (android.content.Intent)187 PendingIntent (android.app.PendingIntent)184 Notification (android.app.Notification)147 NotificationCompat (android.support.v4.app.NotificationCompat)107 Context (android.content.Context)32 Bitmap (android.graphics.Bitmap)24 Uri (android.net.Uri)23 Resources (android.content.res.Resources)22 TaskStackBuilder (android.support.v4.app.TaskStackBuilder)20 TaskStackBuilder (android.app.TaskStackBuilder)15 Bundle (android.os.Bundle)15 StatusBarNotification (android.service.notification.StatusBarNotification)15 Random (java.util.Random)14 BitmapFactory (android.graphics.BitmapFactory)13 SharedPreferences (android.content.SharedPreferences)12 ComponentName (android.content.ComponentName)10 IntentFilter (android.content.IntentFilter)9 ArrayList (java.util.ArrayList)9 Date (java.util.Date)9