Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ujjwalseva.samadhanseva.utils;
- import android.annotation.SuppressLint;
- import android.app.IntentService;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Environment;
- import android.support.v4.BuildConfig;
- import android.support.v4.app.NotificationCompat.Builder;
- import android.support.v4.content.FileProvider;
- import android.util.Log;
- import android.webkit.MimeTypeMap;
- import com.ujjwalseva.samadhanseva.R;
- import java.io.BufferedInputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.URL;
- import java.net.URLConnection;
- public class DownloadService extends IntentService {
- public static final int UPDATE_PROGRESS = 8344;
- int id = 1;
- int progress;
- int check;
- Builder build1;
- int i = 0;
- String ujjwal;
- String pdfIcon;
- private NotificationManager mNotifyManager;
- private Builder build;
- Intent openFile;
- File file;
- String type;
- private static final String AUTHORITY="com.ujjwalseva.samadhanseva.utils.provider";
- public DownloadService() {
- super("DownloadShareOrmaService");
- }
- @SuppressLint("LongLogTag")
- @Override
- protected void onHandleIntent(Intent intent) {
- Log.e("download service", "stated");
- String urlToDownload = intent.getStringExtra("url");
- urlToDownload = urlToDownload.replace(" ", "%20");
- pdfIcon = intent.getStringExtra("pdf_icon");
- Log.e("urlToDownload", urlToDownload);
- /* ResultReceiver receiver = (ResultReceiver) intent.getParcelableExtra("receiver");*/
- try {
- URL url = new URL(urlToDownload);
- URLConnection connection = (URLConnection) url.openConnection();
- // connection.setRequestMethod("GET");
- connection.connect();
- // this will be useful so that you can show a typical 0-100% progress bar
- int fileLength = connection.getContentLength();
- Log.e("filelength", fileLength + "-->");
- long time = System.currentTimeMillis();
- // download the file
- File root = android.os.Environment.getExternalStorageDirectory();
- File dir = new File(root.getAbsolutePath() + "/Ujjwal/");
- dir.mkdirs();
- InputStream input = new BufferedInputStream(connection.getInputStream());
- OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory() + "/Ujjwal/" + time + "ujjwal.pdf");
- ujjwal = Environment.getExternalStorageDirectory() + "/Ujjwal/" + time + "ujjwal.pdf";
- mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- build = new Builder(getApplicationContext());
- build.setContentTitle("Download")
- .setContentText("Download in progress")
- .setSmallIcon(R.mipmap.ic_launcher);
- file = new File(ujjwal);
- MimeTypeMap mime = MimeTypeMap.getSingleton();
- String ext = file.getName().substring(file.getName().indexOf(".") + 1);
- type = mime.getMimeTypeFromExtension(ext);
- byte data[] = new byte[1024];
- long total = 0;
- int count;
- while ((count = input.read(data)) != -1) {
- total += count;
- // publishing the progress....
- Bundle resultData = new Bundle();
- resultData.putInt("progress", (int) (total * 100 / fileLength));
- progress = (int) (total * 100 / fileLength);
- // receiver.send(UPDATE_PROGRESS, resultData);
- output.write(data, 0, count);
- // itemset.add(0);
- if (check != progress) {
- Log.e("check ", check + " progress " + progress);
- build.setProgress(100, progress, false);
- mNotifyManager.notify(id, build.build());
- }
- check = progress;
- }
- output.flush();
- output.close();
- input.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (progress == 100) {
- // mProgressDialog.dismiss();
- Uri myUri = null;
- myUri= Uri.fromFile(file);
- /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
- Log.e("test file path",ujjwal);
- myUri = FileProvider.getUriForFile(this, SHARED_PROVIDER_AUTHORITY, file);
- }else{
- }*/
- openFile = new Intent(Intent.ACTION_VIEW, FileProvider.getUriForFile(this, AUTHORITY,file));
- openFile.setDataAndType(myUri, type);
- intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
- intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
- PendingIntent p = PendingIntent.getActivity(getApplicationContext(), 0, openFile, 0);
- build1 = new Builder(getApplicationContext());
- build1.setContentTitle("Downloaded")
- .setContentText("Download Complete")
- .setContentIntent(p)
- .setSmallIcon(R.mipmap.ic_launcher);
- build.setProgress(0, 0, false);
- // mNotifyManager.notify(id, build.build());
- mNotifyManager.notify(id, build1.build());
- i = 999;
- }
- }
- }
Add Comment
Please, Sign In to add comment