Skip to content
This repository has been archived by the owner on Jan 12, 2023. It is now read-only.

Commit

Permalink
Changed download dialog from Fragment to Dialog with callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Emily Kager committed Jul 18, 2017
1 parent 9ceb5fd commit 864ec31
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 130 deletions.
28 changes: 9 additions & 19 deletions app/src/main/java/org/mozilla/focus/fragment/BrowserFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.app.Activity;
import android.app.DownloadManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
Expand All @@ -24,7 +23,6 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
Expand All @@ -46,6 +44,7 @@
import org.mozilla.focus.broadcastreceiver.DownloadBroadcastReceiver;
import org.mozilla.focus.locale.LocaleAwareAppCompatActivity;
import org.mozilla.focus.menu.BrowserMenu;
import org.mozilla.focus.menu.DownloadDialogMenu;
import org.mozilla.focus.menu.WebContextMenu;
import org.mozilla.focus.notification.BrowsingNotificationService;
import org.mozilla.focus.open.OpenWithFragment;
Expand All @@ -67,14 +66,13 @@
/**
* Fragment for displaying the browser UI.
*/
public class BrowserFragment extends WebFragment implements View.OnClickListener, DownloadDialogFragment.DownloadDialogListener {
public class BrowserFragment extends WebFragment implements View.OnClickListener {
public static final String FRAGMENT_TAG = "browser";

private static int REQUEST_CODE_STORAGE_PERMISSION = 101;
private static final int ANIMATION_DURATION = 300;
private static final String ARGUMENT_URL = "url";
private static final String RESTORE_KEY_DOWNLOAD = "download";
private static final String DOWNLOAD_DIALOG_TAG = "should-download-prompt-dialog";

public static BrowserFragment create(String url) {
Bundle arguments = new Bundle();
Expand Down Expand Up @@ -424,7 +422,7 @@ public void onDownloadStart(Download download) {
if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Long press image displays its own dialog and we handle other download cases here
if (!isDownloadFromLongPressImage(download)) {
showDownloadPromptDialog(download);
DownloadDialogMenu.show(getContext(), this, download);
} else {
// Download dialog has already been shown from long press on image. Proceed with download.
queueDownload(download);
Expand All @@ -442,6 +440,11 @@ public void onDownloadStart(Download download) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_STORAGE_PERMISSION);
}
}

@Override
public void onDownloadDialogConfirmed(Download download) {
queueDownload(download);
}
};
}

Expand Down Expand Up @@ -498,25 +501,12 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
}

if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
showDownloadPromptDialog(pendingDownload);
DownloadDialogMenu.show(getContext(), createCallback(), pendingDownload);
}

pendingDownload = null;
}

void showDownloadPromptDialog(Download download) {
final DialogFragment newFragment = DownloadDialogFragment.newInstance(download);
newFragment.setTargetFragment(BrowserFragment.this, 300);
newFragment.show(getFragmentManager(), DOWNLOAD_DIALOG_TAG);
}

@Override
public void onFinishDownloadDialog(Download download, boolean shouldDownload) {
if (shouldDownload) {
queueDownload(download);
}
}

@Override
public void onCreateViewCalled() {
manager = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ public boolean handleExternalUrl(final String url) {
public void onDownloadStart(Download download) {
}

@Override
public void onDownloadDialogConfirmed(Download download){
}

@Override
public void onLongPress(IWebView.HitTarget hitTarget) {}

Expand Down
99 changes: 99 additions & 0 deletions app/src/main/java/org/mozilla/focus/menu/DownloadDialogMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.mozilla.focus.menu;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.URLUtil;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import org.mozilla.focus.R;
import org.mozilla.focus.telemetry.TelemetryWrapper;
import org.mozilla.focus.web.Download;
import org.mozilla.focus.web.IWebView;

public class DownloadDialogMenu {

public static void show(final @NonNull Context context, final @NonNull IWebView.Callback callback, final @NonNull Download download) {

String fileName = URLUtil.guessFileName(
download.getUrl(), download.getContentDisposition(), download.getMimeType());

final AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.DialogStyle);
builder.setCancelable(true);
builder.setTitle(context.getString(R.string.download_dialog_title));

final View dialogView = LayoutInflater.from(context).inflate(R.layout.download_dialog, null);
builder.setView(dialogView);

final ImageView downloadDialogIcon = (ImageView) dialogView.findViewById(R.id.download_dialog_icon);
final TextView downloadDialogMessage = (TextView) dialogView.findViewById(R.id.download_dialog_file_name);
final Button downloadDialogCancelButton = (Button) dialogView.findViewById(R.id.download_dialog_cancel);
final Button downloadDialogDownloadButton = (Button) dialogView.findViewById(R.id.download_dialog_download);
final TextView downloadDialogWarningMessage = (TextView) dialogView.findViewById(R.id.download_dialog_warning);

downloadDialogIcon.setImageResource(R.drawable.ic_insert_drive_file_white_24px);
downloadDialogMessage.setText(fileName);
downloadDialogCancelButton.setText(context.getString(R.string.download_dialog_action_cancel));
downloadDialogDownloadButton.setText(context.getString(R.string.download_dialog_action_download));
downloadDialogWarningMessage.setText(getSpannedTextFromHtml(context, R.string.download_dialog_warning, R.string.app_name));

builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// This even is only sent when the back button is pressed, or when a user
// taps outside of the dialog:
TelemetryWrapper.downloadDialogCancelEvent();
}
});

final AlertDialog alert = builder.create();

setCancelButtonOnClickListener(downloadDialogCancelButton, alert);
setDownloadButtonOnClickListener(callback, downloadDialogDownloadButton, alert, download);

alert.show();
}

private static void setDownloadButtonOnClickListener(final @NonNull IWebView.Callback callback, Button button, final AlertDialog dialog, final Download download) {
button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
callback.onDownloadDialogConfirmed(download);
TelemetryWrapper.downloadDialogDownloadEvent();
}
});
}

private static void setCancelButtonOnClickListener(Button button, final AlertDialog dialog) {
button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
TelemetryWrapper.downloadDialogCancelEvent();
}
});
}

public static Spanned getSpannedTextFromHtml(Context context, int text, int replaceString) {
if (Build.VERSION.SDK_INT >= 24) {
return (Html.fromHtml(String
.format(context.getText(text)
.toString(), context.getString(replaceString)), Html.FROM_HTML_MODE_LEGACY));
} else {
return (Html.fromHtml(String
.format(context.getText(text)
.toString(), context.getString(replaceString))));
}
}
}
4 changes: 3 additions & 1 deletion app/src/main/java/org/mozilla/focus/web/IWebView.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ interface Callback {

void onDownloadStart(Download download);

void onDownloadDialogConfirmed(Download download);

void onLongPress(final HitTarget hitTarget);

/**
Expand Down Expand Up @@ -108,4 +110,4 @@ interface FullscreenCallback {
void restoreWebviewState(Bundle savedInstanceState);

void onSaveInstanceState(Bundle outState);
}
}

0 comments on commit 864ec31

Please sign in to comment.