mirror of
https://github.com/moparisthebest/k-9
synced 2024-11-23 18:02:15 -05:00
Extract methods to make code more readable
This commit is contained in:
parent
5175ff9df4
commit
1f27897679
@ -13,6 +13,7 @@ import android.content.Context;
|
|||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
|
import android.util.Log;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.fsck.k9.K9;
|
import com.fsck.k9.K9;
|
||||||
@ -42,82 +43,122 @@ class DownloadImageTask extends AsyncTask<String, Void, String> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String doInBackground(String... params) {
|
protected String doInBackground(String... params) {
|
||||||
String urlString = params[0];
|
String url = params[0];
|
||||||
try {
|
try {
|
||||||
boolean externalImage = urlString.startsWith("http");
|
boolean isExternalImage = url.startsWith("http");
|
||||||
|
|
||||||
String filename = null;
|
String fileName;
|
||||||
String mimeType = null;
|
if (isExternalImage) {
|
||||||
InputStream in = null;
|
fileName = downloadAndStoreImage(url);
|
||||||
|
} else {
|
||||||
|
fileName = fetchAndStoreImage(url);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
return fileName;
|
||||||
if (externalImage) {
|
} catch (Exception e) {
|
||||||
|
Log.e(K9.LOG_TAG, "Error while downloading image", e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(String fileName) {
|
||||||
|
boolean errorSavingFile = (fileName == null);
|
||||||
|
|
||||||
|
String text;
|
||||||
|
if (errorSavingFile) {
|
||||||
|
text = context.getString(R.string.image_saving_failed);
|
||||||
|
} else {
|
||||||
|
text = context.getString(R.string.image_saved_as, fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String downloadAndStoreImage(String urlString) throws IOException {
|
||||||
URL url = new URL(urlString);
|
URL url = new URL(urlString);
|
||||||
URLConnection conn = url.openConnection();
|
URLConnection conn = url.openConnection();
|
||||||
in = conn.getInputStream();
|
|
||||||
|
InputStream in = conn.getInputStream();
|
||||||
|
try {
|
||||||
|
String fileName = getFileNameFromUrl(url);
|
||||||
|
String mimeType = getMimeType(conn, fileName);
|
||||||
|
|
||||||
|
String fileNameWithExtension = getFileNameWithExtension(fileName, mimeType);
|
||||||
|
return writeFileToStorage(fileNameWithExtension, in);
|
||||||
|
} finally {
|
||||||
|
in.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getFileNameFromUrl(URL url) {
|
||||||
|
String fileName;
|
||||||
|
|
||||||
String path = url.getPath();
|
String path = url.getPath();
|
||||||
|
|
||||||
// Try to get the filename from the URL
|
|
||||||
int start = path.lastIndexOf("/");
|
int start = path.lastIndexOf("/");
|
||||||
if (start != -1 && start + 1 < path.length()) {
|
if (start != -1 && start + 1 < path.length()) {
|
||||||
filename = UrlEncodingHelper.decodeUtf8(path.substring(start + 1));
|
fileName = UrlEncodingHelper.decodeUtf8(path.substring(start + 1));
|
||||||
} else {
|
} else {
|
||||||
// Use a dummy filename if necessary
|
fileName = DEFAULT_FILE_NAME;
|
||||||
filename = DEFAULT_FILE_NAME;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the MIME type if we couldn't find a file extension
|
return fileName;
|
||||||
if (filename.indexOf('.') == -1) {
|
}
|
||||||
|
|
||||||
|
private String getMimeType(URLConnection conn, String fileName) {
|
||||||
|
String mimeType = null;
|
||||||
|
if (fileName.indexOf('.') == -1) {
|
||||||
mimeType = conn.getContentType();
|
mimeType = conn.getContentType();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
|
return mimeType;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String fetchAndStoreImage(String urlString) throws IOException {
|
||||||
ContentResolver contentResolver = context.getContentResolver();
|
ContentResolver contentResolver = context.getContentResolver();
|
||||||
Uri uri = Uri.parse(urlString);
|
Uri uri = Uri.parse(urlString);
|
||||||
|
|
||||||
// Get the filename from AttachmentProvider
|
String fileName = getFileNameFromContentProvider(contentResolver, uri);
|
||||||
|
String mimeType = getMimeType(contentResolver, uri, fileName);
|
||||||
|
|
||||||
|
InputStream in = contentResolver.openInputStream(uri);
|
||||||
|
try {
|
||||||
|
String fileNameWithExtension = getFileNameWithExtension(fileName, mimeType);
|
||||||
|
return writeFileToStorage(fileNameWithExtension, in);
|
||||||
|
} finally {
|
||||||
|
in.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getMimeType(ContentResolver contentResolver, Uri uri, String fileName) {
|
||||||
|
String mimeType = null;
|
||||||
|
if (fileName.indexOf('.') == -1) {
|
||||||
|
mimeType = contentResolver.getType(uri);
|
||||||
|
}
|
||||||
|
|
||||||
|
return mimeType;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getFileNameFromContentProvider(ContentResolver contentResolver, Uri uri) {
|
||||||
|
String displayName = DEFAULT_FILE_NAME;
|
||||||
|
|
||||||
Cursor cursor = contentResolver.query(uri, ATTACHMENT_PROJECTION, null, null, null);
|
Cursor cursor = contentResolver.query(uri, ATTACHMENT_PROJECTION, null, null, null);
|
||||||
if (cursor != null) {
|
if (cursor != null) {
|
||||||
try {
|
try {
|
||||||
if (cursor.moveToNext()) {
|
if (cursor.moveToNext() && !cursor.isNull(DISPLAY_NAME_INDEX)) {
|
||||||
filename = cursor.getString(DISPLAY_NAME_INDEX);
|
displayName = cursor.getString(DISPLAY_NAME_INDEX);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
cursor.close();
|
cursor.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use a dummy filename if necessary
|
return displayName;
|
||||||
if (filename == null) {
|
|
||||||
filename = DEFAULT_FILE_NAME;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the MIME type if we couldn't find a file extension
|
private String getFileNameWithExtension(String fileName, String mimeType) {
|
||||||
if (filename.indexOf('.') == -1) {
|
if (fileName.indexOf('.') != -1) {
|
||||||
mimeType = contentResolver.getType(uri);
|
return fileName;
|
||||||
}
|
|
||||||
|
|
||||||
in = contentResolver.openInputStream(uri);
|
|
||||||
}
|
|
||||||
|
|
||||||
filename = getFileNameWithExtension(filename, mimeType);
|
|
||||||
|
|
||||||
return writeFileToStorage(filename, in);
|
|
||||||
|
|
||||||
} finally {
|
|
||||||
if (in != null) {
|
|
||||||
in.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getFileNameWithExtension(String filename, String mimeType) {
|
|
||||||
if (filename.indexOf('.') != -1) {
|
|
||||||
return filename;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use JPEG as fallback
|
// Use JPEG as fallback
|
||||||
@ -129,11 +170,11 @@ class DownloadImageTask extends AsyncTask<String, Void, String> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return filename + "." + extension;
|
return fileName + "." + extension;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String writeFileToStorage(String filename, InputStream in) throws IOException {
|
private String writeFileToStorage(String fileName, InputStream in) throws IOException {
|
||||||
String sanitized = FileHelper.sanitizeFilename(filename);
|
String sanitized = FileHelper.sanitizeFilename(fileName);
|
||||||
|
|
||||||
File directory = new File(K9.getAttachmentDefaultPath());
|
File directory = new File(K9.getAttachmentDefaultPath());
|
||||||
File file = FileHelper.createUniqueFile(directory, sanitized);
|
File file = FileHelper.createUniqueFile(directory, sanitized);
|
||||||
@ -148,16 +189,4 @@ class DownloadImageTask extends AsyncTask<String, Void, String> {
|
|||||||
|
|
||||||
return file.getName();
|
return file.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPostExecute(String filename) {
|
|
||||||
String text;
|
|
||||||
if (filename == null) {
|
|
||||||
text = context.getString(R.string.image_saving_failed);
|
|
||||||
} else {
|
|
||||||
text = context.getString(R.string.image_saved_as, filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user