NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

package com.configureit.videodownloader;

import android.content.Context;
import android.os.AsyncTask;

import com.configureit.apicall.utils.SharedPrefrenceUtil;
import com.configureit.videodownloader.database.VideoDetails;
import com.configureit.videodownloader.utils.FileUtility;
import com.configureit.videodownloader.utils.Utils;
import com.configureit.worldinbox.AppConstants;
import com.hiddenbrains.lib.config.exception.LOGHB;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
* Created by hb on 30/3/17.
*/

public class VideoDownloader extends AsyncTask<Void, Integer, Integer> {
public static final String TAG = "VideoDownloader";
public static final int SUCCESS = 1;
public static final int ALREADY_DOWNLOADED = 2;
public static final int ERROR = 0;
public static final int ERROR_FILE_INVALID = -2;
public static final int LOW_SPACE = 3;
public static final int NO_NETWORK = -1;
public static final String FILE_CONTENT = "file_content";
private Context context;
private String url;
private File fileLocal;
private String requestCode;
private IDownloaderListener downloaderListener;
public static final int BUFFER_SIZE = 2048;
private File chunkFileName;
private VideoDetails videoDetails;
private File sdcardDirectory;
private File internalDirectory;
private boolean sdcardExist = false;

public VideoDownloader(Context context, IDownloaderListener downloaderListener) {
this.context = context;
this.downloaderListener = downloaderListener;
}

public String getRequestCode() {
return this.videoDetails != null ? this.videoDetails.getVideoId() : "NOT_DOWNLOADING_ANYTHING";
}

public void setDownloaderListener(IDownloaderListener downloaderListener) {
this.downloaderListener = downloaderListener;
}

public void start(VideoDetails videoDetails) {
this.videoDetails = videoDetails;
this.url = this.videoDetails.getUrl();
this.fileLocal = new File(this.videoDetails.getFilepath());
this.filename = fileLocal.getName();
this.requestCode = this.videoDetails.getVideoId();
sdcardExist = FileUtility.isSDCardAvailable(context);
sdcardDirectory = FileUtility.getMemoryCardVideosDir(context);
internalDirectory = FileUtility.getVideoDirectoryLocal(context);

VideoDownloader.this.executeOnExecutor(THREAD_POOL_EXECUTOR);
}

String filename;

@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
if (this.downloaderListener != null)
if (values[0] == -1)
this.downloaderListener.onDownloadStart(this.requestCode, this.videoDetails, this.url, fileLocal);
else
this.downloaderListener.onDownloadProgress(this.requestCode, this.videoDetails, values[0], this.url, fileLocal);
}

@Override
protected Integer doInBackground(Void... voids) {
int result = 1;
FileOutputStream outputStream;
InputStream inputStream;
int totalDownloaded = 0;
try {
if (!Utils.isNetworkConnected(context)) {
return NO_NETWORK;
}
if (this.fileLocal.exists())
return ALREADY_DOWNLOADED;
URL url = new URL(this.url);
LOGHB.e("URL", this.url);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
chunkFileName = new File(fileLocal.getParent(), this.filename.replace(".mp4", ".part"));
int downloaded = 0;
SharedPrefrenceUtil.setPrefrence(context, "lowspace", false);
if (chunkFileName.exists()) {
downloaded = (int) chunkFileName.length();
httpConn.setRequestProperty("Range", "bytes=" + (chunkFileName.length()) + "-");
} else {
if (this.fileLocal.exists()) {
result = ALREADY_DOWNLOADED;
return result;
}
httpConn.setRequestProperty("Range", "bytes=" + downloaded + "-");
}
int responseCode = httpConn.getResponseCode();
int contentLength = httpConn.getContentLength();
if (contentLength > 0) {
if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_PARTIAL) {
contentLength = httpConn.getContentLength();
boolean spaceAvailable = true;
boolean saveToSDCard = SharedPrefrenceUtil.getPrefrence(context, AppConstants.SES_IS_SAVE_TO_EXT, true);

if (saveToSDCard && sdcardDirectory != null && new File(sdcardDirectory, chunkFileName.getName()).exists()) {
spaceAvailable = FileUtility.spaceAvailableInSDCard(context, contentLength - chunkFileName.length());

} else if (internalDirectory != null && new File(internalDirectory, chunkFileName.getName()).exists()) {
spaceAvailable = FileUtility.spaceAvailableInInternalMemory(context, contentLength - chunkFileName.length());
}
if (spaceAvailable) {
publishProgress(-1);
contentLength = contentLength + downloaded;
inputStream = httpConn.getInputStream();
outputStream = (downloaded == 0) ? new FileOutputStream(chunkFileName.getAbsolutePath()) : new FileOutputStream(chunkFileName.getAbsolutePath(), true);
int bytesRead;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) != -1) {
if (isCancelled())
break;
downloaded += bytesRead;
totalDownloaded = (int) (((float) downloaded * 100) / contentLength);
publishProgress(totalDownloaded);
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
} else {
result = LOW_SPACE;
}
} else {
result = ERROR;
}
} else {
result = ERROR_FILE_INVALID;
}
httpConn.disconnect();
} catch (Exception e) {
result = ERROR;
if (!Utils.isEmpty(e.getMessage()))
if (e.getMessage().contains("write failed: ENOSPC (No space left on device)") || e.getMessage().contains("No space left")) {
result = LOW_SPACE;
}

}
return result;
}

@Override
protected void onPostExecute(Integer s) {
super.onPostExecute(s);
switch (s) {
case ALREADY_DOWNLOADED:
this.downloaderListener.onDownloadCompleted(requestCode, this.videoDetails, url, fileLocal);
break;
case SUCCESS:
if (chunkFileName.exists()) {
FileUtility.renameFile(chunkFileName, fileLocal);
chunkFileName.delete();
this.downloaderListener.onDownloadCompleted(requestCode, this.videoDetails, url, fileLocal);
}
break;
case NO_NETWORK:
case ERROR:
case LOW_SPACE:
case ERROR_FILE_INVALID:
this.downloaderListener.onDownloadError(this.requestCode, this.videoDetails, s, "", this.url, fileLocal);
break;
}

}


}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.