Esempio n. 1
0
  private void queryDownloadStatus() {
    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterById(prefs.getLong(DL_ID, 0));
    Cursor c = mDownloadManager.query(query);
    if (c.moveToFirst()) {
      int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
      switch (status) {
        case DownloadManager.STATUS_PAUSED:
          Log.v("down", "STATUS_PAUSED");
        case DownloadManager.STATUS_PENDING:
          Log.v("down", "STATUS_PENDING");
        case DownloadManager.STATUS_RUNNING:
          // 正在下载,不做任何事情
          Log.v("down", "STATUS_RUNNING");
          break;
        case DownloadManager.STATUS_SUCCESSFUL:
          // 完成
          Log.v("down", "下载完成");
          // 自动安装apk
          installAPK(mDownloadManager.getUriForDownloadedFile(prefs.getLong(DL_ID, 0)));

          break;
        case DownloadManager.STATUS_FAILED:
          // 清除已下载的内容,重新下载
          Log.v("down", "STATUS_FAILED");
          mDownloadManager.remove(prefs.getLong(DL_ID, 0));
          prefs.edit().clear().commit();
          break;
      }
    }
  }
Esempio n. 2
0
  static void enumerate(DownloadManager dm, long handler) {
    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterByStatus(
        DownloadManager.STATUS_PAUSED
            | DownloadManager.STATUS_PENDING
            | DownloadManager.STATUS_RUNNING);
    Cursor c = dm.query(query);

    if (!c.moveToFirst()) return;

    final int columnLocalURI = c.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI);
    final int columnStatus = c.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS);
    final int columnSize = c.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES);
    final int columnPosition =
        c.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR);

    do {
      final String uri = c.getString(columnLocalURI);
      final String path = matchPath(uri);
      if (path == null) continue;

      /* strip the "file://" */
      int status = c.getInt(columnStatus);
      long size = c.getLong(columnSize);
      long position = status == DownloadManager.STATUS_RUNNING ? c.getLong(columnPosition) : -1;

      onDownloadAdded(handler, path, size, position);
    } while (c.moveToNext());
  }
Esempio n. 3
0
  private static boolean isDownloadFailedItem(Context context, SermonItem item) {
    DownloadManager downloadManager =
        (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterByStatus(DownloadManager.STATUS_FAILED);
    Logger.i(TAG, "=========== Download Failed Item Query ==========");
    Cursor c = downloadManager.query(query);
    if (c != null && c.getCount() > 0) {
      boolean loopFlag = true;
      while (c.moveToNext() && loopFlag) {
        String columnUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_URI));
        Logger.i(TAG, "COLUMN_URI : " + columnUri);
        Logger.i(TAG, "item.audioUrl : " + Const.BASE_URL + item.audioUrl);
        if (columnUri.equals(Const.BASE_URL + item.audioUrl)) {
          // 기존에 있던 파일이 정상적으로 다운로드 되지 않은 파일이므로 삭제
          File file =
              new File(Const.DIR_PUB_DOWNLOAD, item.titleWithDate + StringUtils.FILE_EXTENSION_MP3);
          file.delete();
          return true;
        }
      }
      c.close();
    } else {
      Logger.i(TAG, "cursor is null or count <= 0");
      return false;
    }

    return false;
  }
  private void checkForDownloadingStatus() {
    DownloadManager.Query query = new DownloadManager.Query();
    query =
        query.setFilterByStatus(
            DownloadManager.STATUS_RUNNING
                | DownloadManager.STATUS_PENDING
                | DownloadManager.STATUS_PAUSED
                | DownloadManager.STATUS_FAILED);
    downloadCursor = dm.query(query);
    // do one time for init data
    handler.sendEmptyMessage(EVENT_RELOAD_DOWNLOAD);

    downloadCursor.registerContentObserver(mDownloadObserver);
  }
  private void handleDownloadComplete(long downloadId) {
    if (downloadId == mDownloadId) {
      DownloadManager.Query query = new DownloadManager.Query();
      query.setFilterById(downloadId);
      Cursor c = mDM.query(query);
      File file = null;

      if (c.moveToFirst()) {
        int status = c.getInt(c.getColumnIndex(COLUMN_STATUS));
        if (status == STATUS_SUCCESSFUL) {
          int index = c.getColumnIndex(COLUMN_LOCAL_FILENAME);
          file = new File(c.getString(index));
        }
        if (mListener != null) {
          mListener.onDownloadComplete(file);
        }
      }
    }
  }
Esempio n. 6
0
  /** Find a download with the specified name. Returns -1 if none was found. */
  static long findPath(DownloadManager dm, String path) {
    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterByStatus(
        DownloadManager.STATUS_PAUSED
            | DownloadManager.STATUS_PENDING
            | DownloadManager.STATUS_RUNNING);
    Cursor c = dm.query(query);

    if (!c.moveToFirst()) return -1;

    final int columnID = c.getColumnIndexOrThrow(DownloadManager.COLUMN_ID);
    final int columnLocalURI = c.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI);

    do {
      final String uri = c.getString(columnLocalURI);
      if (uri != null && uri.endsWith(path)) return c.getLong(columnID);
    } while (c.moveToNext());

    return -1;
  }
Esempio n. 7
0
        @Override
        public void onReceive(Context arg0, Intent arg1) {
          if (mode != PutioUtils.ACTION_NOTHING) {
            for (long downloadId : downloadIds) {
              DownloadManager.Query query = new DownloadManager.Query();
              query.setFilterById(downloadId);
              Cursor cursor = downloadManager.query(query);

              if (cursor.moveToFirst()) {
                int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
                int status = cursor.getInt(columnIndex);
                int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
                int reason = cursor.getInt(columnReason);

                switch (status) {
                  case DownloadManager.STATUS_SUCCESSFUL:
                    Intent finishedIntent =
                        new Intent(PutioOpenFileService.this, FileFinished.class);
                    finishedIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    finishedIntent.putExtra("downloadId", downloadId);
                    finishedIntent.putExtra("id", id);
                    finishedIntent.putExtra("filename", filename);
                    finishedIntent.putExtra("mode", mode);
                    startActivity(finishedIntent);
                    break;
                  case DownloadManager.STATUS_FAILED:
                    break;
                  case DownloadManager.STATUS_PAUSED:
                    break;
                  case DownloadManager.STATUS_PENDING:
                    break;
                  case DownloadManager.STATUS_RUNNING:
                    break;
                }
              }
            }
          }
        }
  private void initData() {
    if (iDataORM.getDownloadCount(getBaseContext()) == 0) {
      findViewById(R.id.offline_media_scrollview).setVisibility(View.GONE);
    } else {
      // no need to show downloaded
      dm = (DownloadManager) getBaseContext().getSystemService(Context.DOWNLOAD_SERVICE);
      ArrayList<Long> ids = iDataORM.getDownloadIDs(getBaseContext(), 0);
      if (ids.size() > 0) {
        DownloadManager.Query query = new DownloadManager.Query();
        long[] inids = new long[ids.size()];
        for (int i = 0; i < ids.size(); i++) {
          inids[i] = ids.get(i);
        }
        query = query.setFilterById(inids);
        Cursor currentUI = dm.query(query);
        if (currentUI != null && currentUI.getCount() > 0 && currentUI.moveToFirst()) {
          do {
            MVDownloadManager.DownloadTablePojo dp =
                new MVDownloadManager.DownloadTablePojo(currentUI);

            if (dp.status == MVDownloadManager.DownloadTablePojo.DownloadSuccess) {
              iDataORM.downloadFinished(getBaseContext(), Integer.valueOf(dp.downloadId));
            }
          } while (currentUI.moveToNext());

          currentUI.close();
        }

        // check for downloading status
        checkForDownloadingStatus();

      } else {
        Log.d(TAG, "no current download task");
        // mLoadingBar.setVisibility(View.GONE);
      }
    }
  }
Esempio n. 9
0
  static void checkComplete(DownloadManager dm) {
    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterByStatus(DownloadManager.STATUS_FAILED | DownloadManager.STATUS_SUCCESSFUL);
    Cursor c = dm.query(query);

    if (!c.moveToFirst()) return;

    final int columnLocalURI = c.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI);
    final int columnId = c.getColumnIndexOrThrow(DownloadManager.COLUMN_ID);
    final int columnStatus = c.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS);

    do {
      final String uri = c.getString(columnLocalURI);
      final String path = matchPath(uri);
      if (path == null) continue;

      final int status = c.getInt(columnStatus);
      final boolean success = status == DownloadManager.STATUS_SUCCESSFUL;
      onDownloadComplete(path, success);

      final long id = c.getLong(columnId);
      dm.remove(id);
    } while (c.moveToNext());
  }
  @Override
  protected void onHandleIntent(Intent intent) {

    mDManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    long downloadManagerID = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);

    Log.d("DOWNLOAD", "DONE " + downloadManagerID);
    manager = new MagazineManager(this);
    Magazine magazine =
        manager.findByDownloadManagerID(downloadManagerID, Magazine.TABLE_DOWNLOADED_MAGAZINES);

    if (magazine != null) {
      DownloadManager.Query q = new DownloadManager.Query();
      q.setFilterById(downloadManagerID);
      Cursor c = mDManager.query(q);
      if (c.moveToFirst()) {
        int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
        if (status == DownloadManager.STATUS_SUCCESSFUL) {
          // process download
          String srcFileName = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
          magazine.clearMagazineDir();
          magazine.makeMagazineDir();
          StorageUtils.move(
              srcFileName,
              magazine.isSample() ? magazine.getSamplePdfPath() : magazine.getItemPath());
        }
      }
      c.close();

      NotificationCompat.Builder mBuilder =
          new NotificationCompat.Builder(this)
              .setSmallIcon(R.drawable.ic_launcher)
              .setContentTitle(
                  magazine.getTitle() + (magazine.isSample() ? " sample" : "") + " downloaded")
              .setContentText("Click to read");

      // Create large icon from magazine cover png
      Resources res = getResources();
      int height = (int) res.getDimension(android.R.dimen.notification_large_icon_height);
      int width = (int) res.getDimension(android.R.dimen.notification_large_icon_width);
      mBuilder.setLargeIcon(
          SystemHelper.decodeSampledBitmapFromFile(magazine.getPngPath(), height, width));

      // TODO show magazine cover as large image

      Intent resultIntent = new Intent(this, MuPDFActivity.class);
      resultIntent.setAction(Intent.ACTION_VIEW);
      resultIntent.setData(
          Uri.parse(magazine.isSample() ? magazine.getSamplePdfPath() : magazine.getItemPath()));
      resultIntent.putExtra(Magazine.FIELD_TITLE, magazine.getTitle());

      TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
      stackBuilder.addParentStack(MuPDFActivity.class);
      stackBuilder.addNextIntent(resultIntent);
      PendingIntent resultPendingIntent =
          stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
      mBuilder.setContentIntent(resultPendingIntent);
      mBuilder.setAutoCancel(true);
      NotificationManager mNotificationManager =
          (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      mNotificationManager.notify((int) magazine.getDownloadManagerId(), mBuilder.build());

      Date date = Calendar.getInstance().getTime();
      String downloadDate = new SimpleDateFormat(" dd.MM.yyyy").format(date);
      magazine.setDownloadDate(downloadDate);
      manager.removeDownloadedMagazine(this, magazine);
      manager.addMagazine(magazine, Magazine.TABLE_DOWNLOADED_MAGAZINES, true);
      EventBus.getDefault().post(new LoadPlistEvent());
      EventBus.getDefault().post(new MagazineDownloadedEvent(magazine));
      startLinksDownload(this, magazine);
      magazine.makeCompleteFile(magazine.isSample());
    } else {
      // Asset downloaded
      manager.setAssetDownloaded(downloadManagerID);
      DownloadManager.Query q = new DownloadManager.Query();
      q.setFilterById(downloadManagerID);
      Cursor c = mDManager.query(q);
      if (c.moveToFirst()) {
        int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
        if (status == DownloadManager.STATUS_SUCCESSFUL) {
          // process download
          String srcFileName = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
          StorageUtils.move(srcFileName, manager.getAssetFilename(downloadManagerID));
        }
      }
      c.close();
    }
  }