|
@ -1,6 +1,8 @@ |
|
|
package gr.thmmy.mthmmy.utils.FileManager; |
|
|
package gr.thmmy.mthmmy.utils.FileManager; |
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context; |
|
|
import android.os.Environment; |
|
|
import android.os.Environment; |
|
|
|
|
|
import android.os.StatFs; |
|
|
import android.support.annotation.Nullable; |
|
|
import android.support.annotation.Nullable; |
|
|
import android.webkit.MimeTypeMap; |
|
|
import android.webkit.MimeTypeMap; |
|
|
|
|
|
|
|
@ -18,10 +20,7 @@ import static gr.thmmy.mthmmy.activities.base.BaseActivity.getClient; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Used for downloading and storing a file from the forum using {@link okhttp3}. |
|
|
* Used for downloading and storing a file from the forum using {@link okhttp3}. |
|
|
* <p>Class has one constructor: <ul><li>{@link #ThmmyFile(URL, String, String)}</li></ul> |
|
|
* <p>Class has one constructor, {@link #ThmmyFile(URL, String, String)}. |
|
|
* and the methods:<ul><li>getters</li> |
|
|
|
|
|
* <li>{@link #download()}</li> |
|
|
|
|
|
* <li>{@link #download()}</li></ul></p> |
|
|
|
|
|
*/ |
|
|
*/ |
|
|
public class ThmmyFile { |
|
|
public class ThmmyFile { |
|
|
/** |
|
|
/** |
|
@ -33,9 +32,22 @@ public class ThmmyFile { |
|
|
private String extension, filePath; |
|
|
private String extension, filePath; |
|
|
private File file; |
|
|
private File file; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* This constructor only creates a empty ThmmyFile object and <b>does not download</b> the file. To download |
|
|
|
|
|
* the file use {@link #download(Context)} after setting file's url! |
|
|
|
|
|
*/ |
|
|
|
|
|
public ThmmyFile() { |
|
|
|
|
|
this.fileUrl = null; |
|
|
|
|
|
this.filename = null; |
|
|
|
|
|
this.fileInfo = null; |
|
|
|
|
|
this.extension = null; |
|
|
|
|
|
this.filePath = null; |
|
|
|
|
|
this.file = null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* This constructor only creates a ThmmyFile object and <b>does not download</b> the file. To download |
|
|
* This constructor only creates a ThmmyFile object and <b>does not download</b> the file. To download |
|
|
* the file use {@link #download()} or {@link #download()}! |
|
|
* the file use {@link #download(Context)}! |
|
|
* |
|
|
* |
|
|
* @param fileUrl {@link URL} object with file's url |
|
|
* @param fileUrl {@link URL} object with file's url |
|
|
* @param filename {@link String} with desired file name |
|
|
* @param filename {@link String} with desired file name |
|
@ -63,7 +75,7 @@ public class ThmmyFile { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* This is null until {@link #download()} or {@link #download()} is called and has succeeded. |
|
|
* This is null until {@link #download(Context)} is called and has succeeded. |
|
|
* |
|
|
* |
|
|
* @return String with file's extension or null |
|
|
* @return String with file's extension or null |
|
|
*/ |
|
|
*/ |
|
@ -73,7 +85,7 @@ public class ThmmyFile { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* This is null until {@link #download()} or {@link #download()} is called and has succeeded. |
|
|
* This is null until {@link #download(Context)} is called and has succeeded. |
|
|
* |
|
|
* |
|
|
* @return String with file's path or null |
|
|
* @return String with file's path or null |
|
|
*/ |
|
|
*/ |
|
@ -83,7 +95,7 @@ public class ThmmyFile { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* This is null until {@link #download()} or {@link #download()} is called and has succeeded. |
|
|
* This is null until {@link #download(Context)} is called and has succeeded. |
|
|
* |
|
|
* |
|
|
* @return {@link File} or null |
|
|
* @return {@link File} or null |
|
|
*/ |
|
|
*/ |
|
@ -112,7 +124,10 @@ public class ThmmyFile { |
|
|
* @throws SecurityException if the requested file is not hosted by the forum. |
|
|
* @throws SecurityException if the requested file is not hosted by the forum. |
|
|
*/ |
|
|
*/ |
|
|
@Nullable |
|
|
@Nullable |
|
|
public File download() throws IOException, SecurityException { |
|
|
public File download(Context context) throws IOException, SecurityException, OutOfMemoryError { |
|
|
|
|
|
if (fileUrl == null) { |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
if (!Objects.equals(fileUrl.getHost(), "www.thmmy.gr")) |
|
|
if (!Objects.equals(fileUrl.getHost(), "www.thmmy.gr")) |
|
|
throw new SecurityException("Downloading files from other sources is not supported"); |
|
|
throw new SecurityException("Downloading files from other sources is not supported"); |
|
|
|
|
|
|
|
@ -122,7 +137,7 @@ public class ThmmyFile { |
|
|
if (!response.isSuccessful()) { |
|
|
if (!response.isSuccessful()) { |
|
|
throw new IOException("Failed to download file: " + response); |
|
|
throw new IOException("Failed to download file: " + response); |
|
|
} |
|
|
} |
|
|
file = getOutputMediaFile(filename); |
|
|
file = getOutputMediaFile(context, filename, fileInfo); |
|
|
if (file == null) { |
|
|
if (file == null) { |
|
|
Report.d(TAG, "Error creating media file, check storage permissions!"); |
|
|
Report.d(TAG, "Error creating media file, check storage permissions!"); |
|
|
} else { |
|
|
} else { |
|
@ -138,24 +153,40 @@ public class ThmmyFile { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Nullable |
|
|
@Nullable |
|
|
private static File getOutputMediaFile(String fileName) { |
|
|
private File getOutputMediaFile(Context context, String fileName, String fileInfo) throws OutOfMemoryError, IOException { |
|
|
// To be safe, you should check that the SDCard is mounted
|
|
|
File mediaStorageDir; |
|
|
// using Environment.getExternalStorageState() before doing this.
|
|
|
String extState = Environment.getExternalStorageState(); |
|
|
File mediaStorageDir = new File(Environment.getExternalStorageDirectory() |
|
|
if (Environment.isExternalStorageRemovable() && |
|
|
|
|
|
Objects.equals(extState, Environment.MEDIA_MOUNTED)) { |
|
|
|
|
|
mediaStorageDir = new File(Environment.getExternalStorageDirectory() |
|
|
+ "/Android/data/gr.thmmy.mthmmy/" |
|
|
+ "/Android/data/gr.thmmy.mthmmy/" |
|
|
+ "Downloads/"); |
|
|
+ "Downloads/"); |
|
|
|
|
|
} else { |
|
|
|
|
|
mediaStorageDir = new File(context.getFilesDir(), "Downloads"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// This location works best if you want the created files to be shared
|
|
|
//Creates the storage directory if it does not exist
|
|
|
// between applications and persist after your app has been uninstalled.
|
|
|
|
|
|
|
|
|
|
|
|
// Create the storage directory if it does not exist
|
|
|
|
|
|
if (!mediaStorageDir.exists()) { |
|
|
if (!mediaStorageDir.exists()) { |
|
|
if (!mediaStorageDir.mkdirs()) { |
|
|
if (!mediaStorageDir.mkdirs()) { |
|
|
Report.d(TAG, "problem!"); |
|
|
Report.d(TAG, "problem!"); |
|
|
return null; |
|
|
throw new IOException("Error.\nCouldn't create the path!"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
// Create a media file name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fileInfo != null) { |
|
|
|
|
|
if (fileInfo.contains("KB")) { |
|
|
|
|
|
float fileSize = Float.parseFloat(fileInfo |
|
|
|
|
|
.substring(fileInfo.indexOf("(") + 1, fileInfo.indexOf("KB") - 1)); |
|
|
|
|
|
|
|
|
|
|
|
StatFs stat = new StatFs(mediaStorageDir.getPath()); |
|
|
|
|
|
long bytesAvailable = stat.getBlockSizeLong() * stat.getAvailableBlocksLong(); |
|
|
|
|
|
if ((bytesAvailable / 1024.f) < fileSize) |
|
|
|
|
|
throw new OutOfMemoryError("There is not enough memory!"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//Creates a media file name
|
|
|
File mediaFile; |
|
|
File mediaFile; |
|
|
mediaFile = new File(mediaStorageDir.getPath() + File.separator + fileName); |
|
|
mediaFile = new File(mediaStorageDir.getPath() + File.separator + fileName); |
|
|
return mediaFile; |
|
|
return mediaFile; |
|
|