Browse Source

fixes for downloads page

- fixes open downloads
- fix if download dialog canceled
- save image downloads
master
DF1E 8 years ago
parent
commit
eddba7e480
  1. 10
      app/src/main/AndroidManifest.xml
  2. 29
      app/src/main/java/acr/browser/lightning/constant/DownloadsPage.java
  3. 27
      app/src/main/java/acr/browser/lightning/dialog/LightningDialogBuilder.java
  4. 11
      app/src/main/java/acr/browser/lightning/download/DownloadHandler.java
  5. 2
      app/src/main/java/acr/browser/lightning/download/FetchUrlMimeType.java
  6. 35
      app/src/main/java/acr/browser/lightning/download/LightningDownloadListener.java
  7. 9
      app/src/main/java/acr/browser/lightning/utils/Utils.java
  8. 30
      app/src/main/java/acr/browser/lightning/view/LightningWebClient.java
  9. 5
      app/src/main/res/xml/filepaths.xml

10
app/src/main/AndroidManifest.xml

@ -157,6 +157,16 @@ @@ -157,6 +157,16 @@
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
</application>
</manifest>

29
app/src/main/java/acr/browser/lightning/constant/DownloadsPage.java

@ -23,6 +23,7 @@ import acr.browser.lightning.R; @@ -23,6 +23,7 @@ import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.database.downloads.DownloadItem;
import acr.browser.lightning.database.downloads.DownloadsModel;
import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.Preconditions;
import acr.browser.lightning.utils.Utils;
@ -58,6 +59,7 @@ public final class DownloadsPage { @@ -58,6 +59,7 @@ public final class DownloadsPage {
private File mFilesDir;
@Inject Application mApp;
@Inject PreferenceManager mPreferenceManager;
@Inject DownloadsModel mManager;
@NonNull private final String mTitle;
@ -74,7 +76,7 @@ public final class DownloadsPage { @@ -74,7 +76,7 @@ public final class DownloadsPage {
public void onSubscribe(@NonNull SingleSubscriber<String> subscriber) {
mFilesDir = mApp.getFilesDir();
buildDownloadsPage(null);
buildDownloadsPage();
File downloadsWebPage = new File(mFilesDir, FILENAME);
@ -84,36 +86,41 @@ public final class DownloadsPage { @@ -84,36 +86,41 @@ public final class DownloadsPage {
});
}
private void buildDownloadsPage(@Nullable final String folder) {
private void buildDownloadsPage() {
mManager.getAllDownloads()
.subscribe(new SingleOnSubscribe<List<DownloadItem>>() {
@Override
public void onItem(@Nullable List<DownloadItem> list) {
Preconditions.checkNonNull(list);
String directory = mPreferenceManager.getDownloadDirectory();
final File downloadsWebPage;
if (folder == null || folder.isEmpty()) {
downloadsWebPage = new File(mFilesDir, FILENAME);
} else {
downloadsWebPage = new File(mFilesDir, folder + '-' + FILENAME);
}
final StringBuilder downloadsBuilder = new StringBuilder(HEADING_1 + mTitle + HEADING_2);
for (int n = 0, size = list.size(); n < size; n++) {
final DownloadItem item = list.get(n);
downloadsBuilder.append(PART1);
downloadsBuilder.append(item.getUrl());
downloadsBuilder.append("file://");
downloadsBuilder.append(directory);
downloadsBuilder.append("/");
downloadsBuilder.append(item.getTitle());
downloadsBuilder.append(PART2);
downloadsBuilder.append(item.getTitle());
downloadsBuilder.append(PART3);
if (!item.getContentSize().isEmpty()) {
downloadsBuilder.append(" [");
downloadsBuilder.append(item.getContentSize());
downloadsBuilder.append("]");
}
downloadsBuilder.append(PART3);
downloadsBuilder.append(item.getUrl());
downloadsBuilder.append(PART4);
}
downloadsBuilder.append(END);
FileWriter bookWriter = null;
try {
//noinspection IOResourceOpenedButNotSafelyClosed
bookWriter = new FileWriter(downloadsWebPage, false);
bookWriter = new FileWriter(new File(mFilesDir, FILENAME), false);
bookWriter.write(downloadsBuilder.toString());
} catch (IOException e) {
e.printStackTrace();

27
app/src/main/java/acr/browser/lightning/dialog/LightningDialogBuilder.java

@ -10,6 +10,7 @@ import android.support.v7.app.AlertDialog; @@ -10,6 +10,7 @@ import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.URLUtil;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
@ -27,7 +28,6 @@ import acr.browser.lightning.activity.MainActivity; @@ -27,7 +28,6 @@ import acr.browser.lightning.activity.MainActivity;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.constant.BookmarkPage;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.constant.DownloadsPage;
import acr.browser.lightning.controller.UIController;
import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.database.bookmark.BookmarkModel;
@ -169,21 +169,7 @@ public class LightningDialogBuilder { @@ -169,21 +169,7 @@ public class LightningDialogBuilder {
@NonNull final UIController uiController,
@NonNull final String url) {
BrowserDialog.show(activity, R.string.action_bookmarks,
new BrowserDialog.Item(R.string.dialog_delete_download) {
@Override
public void onClick() {
mDownloadsModel.deleteDownload(url).subscribe(new SingleOnSubscribe<Boolean>() {
@Override
public void onItem(@Nullable Boolean item) {
if (item != null && !item)
Log.i(TAG, "error deleting download from database");
else
uiController.handleDownloadDeleted();
}
});
}
},
BrowserDialog.show(activity, R.string.action_downloads,
new BrowserDialog.Item(R.string.dialog_delete_all_downloads) {
@Override
public void onClick() {
@ -393,6 +379,15 @@ public class LightningDialogBuilder { @@ -393,6 +379,15 @@ public class LightningDialogBuilder {
@Override
public void onClick() {
Utils.downloadFile(activity, mPreferenceManager, url, userAgent, "attachment");
mDownloadsModel.addDownloadIfNotExists(new DownloadItem(url, URLUtil.guessFileName(url, null, null), ""))
.subscribe(new SingleOnSubscribe<Boolean>() {
@Override
public void onItem(@Nullable Boolean item) {
if (item != null && !item)
Log.i(TAG, "error saving download to database");
}
});
}
});
}

11
app/src/main/java/acr/browser/lightning/download/DownloadHandler.java

@ -47,15 +47,6 @@ public class DownloadHandler { @@ -47,15 +47,6 @@ public class DownloadHandler {
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
.getPath();
@Nullable
static String guessFileExtension(@NonNull String filename) {
int lastIndex = filename.lastIndexOf('.') + 1;
if (lastIndex > 0 && filename.length() > lastIndex) {
return filename.substring(lastIndex, filename.length());
}
return null;
}
/**
* Notify the host application a download should be done, or that the data
* should be streamed if a streaming viewer is available.
@ -222,7 +213,7 @@ public class DownloadHandler { @@ -222,7 +213,7 @@ public class DownloadHandler {
Utils.showSnackbar(context, R.string.problem_location_download);
return;
}
String newMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(guessFileExtension(filename));
String newMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(Utils.guessFileExtension(filename));
Log.d(TAG, "New mimetype: " + newMimeType);
request.setMimeType(newMimeType);
request.setDestinationUri(Uri.parse(Constants.FILE + location + filename));

2
app/src/main/java/acr/browser/lightning/download/FetchUrlMimeType.java

@ -93,7 +93,7 @@ class FetchUrlMimeType extends Thread { @@ -93,7 +93,7 @@ class FetchUrlMimeType extends Thread {
if (mimeType.equalsIgnoreCase("text/plain")
|| mimeType.equalsIgnoreCase("application/octet-stream")) {
String newMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
DownloadHandler.guessFileExtension(mUri));
Utils.guessFileExtension(mUri));
if (newMimeType != null) {
mRequest.setMimeType(newMimeType);
}

35
app/src/main/java/acr/browser/lightning/download/LightningDownloadListener.java

@ -50,7 +50,15 @@ public class LightningDownloadListener implements DownloadListener { @@ -50,7 +50,15 @@ public class LightningDownloadListener implements DownloadListener {
new PermissionsResultAction() {
@Override
public void onGranted() {
String fileName = URLUtil.guessFileName(url, contentDisposition, mimetype);
final String fileName = URLUtil.guessFileName(url, contentDisposition, mimetype);
final String downloadSize;
if (contentLength > 0) {
downloadSize = Formatter.formatFileSize(mActivity, contentLength);
} else {
downloadSize = mActivity.getString(R.string.unknown_size);
}
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
@ -58,6 +66,15 @@ public class LightningDownloadListener implements DownloadListener { @@ -58,6 +66,15 @@ public class LightningDownloadListener implements DownloadListener {
case DialogInterface.BUTTON_POSITIVE:
DownloadHandler.onDownloadStart(mActivity, mPreferenceManager, url, userAgent,
contentDisposition, mimetype);
downloadsModel.addDownloadIfNotExists(new DownloadItem(url, fileName, downloadSize))
.subscribe(new SingleOnSubscribe<Boolean>() {
@Override
public void onItem(@Nullable Boolean item) {
if (item != null && !item)
Log.i(TAG, "error saving download to database");
}
});
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
@ -66,12 +83,6 @@ public class LightningDownloadListener implements DownloadListener { @@ -66,12 +83,6 @@ public class LightningDownloadListener implements DownloadListener {
};
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity); // dialog
String downloadSize;
if (contentLength > 0) {
downloadSize = Formatter.formatFileSize(mActivity, contentLength);
} else {
downloadSize = mActivity.getString(R.string.unknown_size);
}
String message = mActivity.getString(R.string.dialog_download, downloadSize);
Dialog dialog = builder.setTitle(fileName)
.setMessage(message)
@ -81,16 +92,6 @@ public class LightningDownloadListener implements DownloadListener { @@ -81,16 +92,6 @@ public class LightningDownloadListener implements DownloadListener {
dialogClickListener).show();
BrowserDialog.setDialogSize(mActivity, dialog);
Log.i(TAG, "Downloading: " + fileName);
downloadsModel.addDownloadIfNotExists(new DownloadItem(url, fileName, downloadSize)).subscribe(new SingleOnSubscribe<Boolean>() {
@Override
public void onItem(@Nullable Boolean item) {
super.onItem(item);
if (item != null && !item)
Log.i(TAG, "error saving download to database");
}
});
}
@Override

9
app/src/main/java/acr/browser/lightning/utils/Utils.java

@ -456,4 +456,13 @@ public final class Utils { @@ -456,4 +456,13 @@ public final class Utils {
return inSampleSize;
}
@Nullable
public static String guessFileExtension(@NonNull String filename) {
int lastIndex = filename.lastIndexOf('.') + 1;
if (lastIndex > 0 && filename.length() > lastIndex) {
return filename.substring(lastIndex, filename.length());
}
return null;
}
}

30
app/src/main/java/acr/browser/lightning/view/LightningWebClient.java

@ -8,30 +8,31 @@ import android.content.DialogInterface; @@ -8,30 +8,31 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.MailTo;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.LayoutInflaterCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AlertDialog;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.HttpAuthHandler;
import android.webkit.MimeTypeMap;
import android.webkit.SslErrorHandler;
import android.webkit.URLUtil;
import android.webkit.ValueCallback;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
@ -39,6 +40,7 @@ import java.util.Map; @@ -39,6 +40,7 @@ import java.util.Map;
import javax.inject.Inject;
import acr.browser.lightning.BuildConfig;
import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.constant.Constants;
@ -358,6 +360,26 @@ public class LightningWebClient extends WebViewClient { @@ -358,6 +360,26 @@ public class LightningWebClient extends WebViewClient {
}
return true;
}
} else if (url.startsWith("file://")) {
File file = new File(url.replace("file://", ""));
if (file.exists()) {
String newMimeType = MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(Utils.guessFileExtension(file.toString()));
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(mActivity, BuildConfig.APPLICATION_ID + ".fileprovider", file);
intent.setDataAndType(contentUri, newMimeType);
try {
mActivity.startActivity(intent);
} catch (Exception e) {
System.out.println("LightningWebClient: cannot open downloaded file");
}
return true;
}
}
return false;
}

5
app/src/main/res/xml/filepaths.xml

@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="share" path="/" />
<external-path name="external_files" path="."/>
</paths>
Loading…
Cancel
Save