示例#1
0
 private void updateLanguageListWithDownloadManagerStatus(OCRLanguageAdapter adapter) {
   if (adapter != null) {
     // find languages that are currently beeing downloaded
     Query query = new Query();
     query.setFilterByStatus(
         DownloadManager.STATUS_RUNNING
             | DownloadManager.STATUS_PENDING
             | DownloadManager.STATUS_PAUSED);
     final DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
     Cursor c = dm.query(query);
     int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_TITLE);
     while (c.moveToNext()) {
       final String title = c.getString(columnIndex);
       adapter.setDownloading(title, true);
     }
     adapter.notifyDataSetChanged();
     c.close();
   }
 }
        @Override
        public void onReceive(final Context context, final Intent intent) {
          long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -2);
          if (enqueue != -1 && id != -2 && id == enqueue) {
            Query query = new Query();
            query.setFilterById(id);
            DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
            Cursor c = dm.query(query);
            if (c.moveToFirst()) {
              int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
              int status = c.getInt(columnIndex);
              if (status == DownloadManager.STATUS_SUCCESSFUL) {

                if (Utils.checkMD5(matchedMd5, new File(dir, apkFilename))) {

                  NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

                  builder
                      .setSmallIcon(R.drawable.ic_stat_ytd)
                      .setContentTitle(getString(R.string.title_activity_share))
                      .setContentText(
                          "v" + matchedVersion + " " + getString(R.string.new_v_install));

                  NotificationManager notificationManager =
                      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

                  Intent intent1 = new Intent();
                  intent1.setAction(android.content.Intent.ACTION_VIEW);
                  intent1.setDataAndType(fileUri, "application/vnd.android.package-archive");
                  PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent1, 0);
                  builder.setContentIntent(contentIntent);

                  notificationManager.notify(2, builder.build());
                } else {
                  deleteBadDownload(context, intent);
                }
              }
            }
          }
          stopSelf();
        }
  @Override
  public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
      Query query = new Query();
      query.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL);
      Cursor c = dm.query(query);
      if (c.moveToFirst()) {
        int titleColumnIndex = c.getColumnIndex(DownloadManager.COLUMN_TITLE);
        String title = context.getString(R.string.menu_view) + " " + c.getString(titleColumnIndex);
        int pdfPathColumnIndex = c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
        String pdfPath = c.getString(pdfPathColumnIndex);
        Intent intentPdf = new Intent(Intent.ACTION_VIEW);
        intentPdf.setDataAndType(Uri.parse(pdfPath), "application/pdf");
        PendingIntent contentIntent =
            PendingIntent.getActivity(context, 0, intentPdf, PendingIntent.FLAG_CANCEL_CURRENT);

        SharedPreferences settings =
            context.getSharedPreferences("IssueDownloadNotification", Context.MODE_PRIVATE);
        if (settings.getBoolean(title, false)) return;
        settings.edit().putBoolean(title, true).commit();

        Notification noti =
            new NotificationCompat.Builder(context)
                .setContentTitle(title)
                .setContentText(context.getString(R.string.download_completed_text))
                .setContentIntent(contentIntent)
                .setSmallIcon(R.drawable.new_issue)
                .getNotification();

        NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        noti.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(ID_NOTIFICATION++, noti);
      }
      c.close();
    }
  }
  private void onReceivedDownloadNotification(Context context, Intent intent) {
    if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
      long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
      DownloadItem item = Controller.getInstance().getDownloadItemById(id);

      if (item != null) {
        // This is one of our downloads.
        final DownloadManager downloadManager =
            (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        Query query = new Query();
        query.setFilterById(id);
        Cursor cursor = downloadManager.query(query);

        if (cursor.moveToFirst()) {
          int localUriIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
          int reasonIndex = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
          int statusIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);

          int status = cursor.getInt(statusIndex);

          if (status == DownloadManager.STATUS_SUCCESSFUL) {
            String localUri = cursor.getString(localUriIndex);

            Toast.makeText(
                    context,
                    String.format(getString(R.string.DownloadComplete), localUri),
                    Toast.LENGTH_SHORT)
                .show();
            Controller.getInstance().getDownloadsList().remove(item);

            showNotification(
                getString(R.string.DownloadComplete),
                item.getFileName(),
                getString(R.string.DownloadComplete));

          } else if (status == DownloadManager.STATUS_FAILED) {
            int reason = cursor.getInt(reasonIndex);

            String message;
            switch (reason) {
              case DownloadManager.ERROR_FILE_ERROR:
              case DownloadManager.ERROR_DEVICE_NOT_FOUND:
              case DownloadManager.ERROR_INSUFFICIENT_SPACE:
                message = getString(R.string.DownloadErrorDisk);
                break;
              case DownloadManager.ERROR_HTTP_DATA_ERROR:
              case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
                message = getString(R.string.DownloadErrorHttp);
                break;
              case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
                message = getString(R.string.DownloadErrorRedirection);
                break;
              default:
                message = getString(R.string.DownloadErrorUnknown);
                break;
            }

            Toast.makeText(
                    context,
                    String.format(getString(R.string.DownloadFailedWithErrorMessage), message),
                    Toast.LENGTH_SHORT)
                .show();
            Controller.getInstance().getDownloadsList().remove(item);
          }
        }
      }
    } else if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(intent.getAction())) {
      Intent i = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
      startActivity(i);
    }
  }