2012-04-25 09:10:12 -04:00
|
|
|
/*
|
2014-04-13 11:27:15 -04:00
|
|
|
* Copyright (C) 2012-2014 Dominik Schürmann <dominik@dominikschuermann.de>
|
2012-04-25 09:10:12 -04:00
|
|
|
*
|
2013-09-06 10:17:01 -04:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2012-04-25 09:10:12 -04:00
|
|
|
*
|
2013-09-06 10:17:01 -04:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2012-04-25 09:10:12 -04:00
|
|
|
*
|
2013-09-06 10:17:01 -04:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-04-25 09:10:12 -04:00
|
|
|
*/
|
2012-04-20 06:12:07 -04:00
|
|
|
|
2013-01-16 08:31:16 -05:00
|
|
|
package org.sufficientlysecure.keychain.ui.dialog;
|
2012-04-20 06:12:07 -04:00
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.Dialog;
|
2015-05-31 13:45:25 -04:00
|
|
|
import android.app.DialogFragment;
|
2012-04-20 06:12:07 -04:00
|
|
|
import android.app.ProgressDialog;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.DialogInterface.OnKeyListener;
|
2014-08-31 11:32:13 -04:00
|
|
|
import android.content.Intent;
|
2014-08-31 14:05:19 -04:00
|
|
|
import android.graphics.Color;
|
2012-04-20 06:12:07 -04:00
|
|
|
import android.os.Bundle;
|
2015-05-31 13:45:25 -04:00
|
|
|
import android.support.annotation.NonNull;
|
2014-08-20 14:13:25 -04:00
|
|
|
import android.view.ContextThemeWrapper;
|
2012-04-20 06:12:07 -04:00
|
|
|
import android.view.KeyEvent;
|
2014-08-31 11:32:13 -04:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
import android.widget.Button;
|
2014-04-15 16:52:21 -04:00
|
|
|
|
2014-03-02 15:06:43 -05:00
|
|
|
import org.sufficientlysecure.keychain.R;
|
2014-08-31 11:32:13 -04:00
|
|
|
import org.sufficientlysecure.keychain.service.KeychainIntentService;
|
2014-03-02 15:06:43 -05:00
|
|
|
|
2015-05-28 07:32:34 -04:00
|
|
|
/**
|
|
|
|
* meant to be used
|
|
|
|
*/
|
2014-02-02 08:13:09 -05:00
|
|
|
public class ProgressDialogFragment extends DialogFragment {
|
2014-03-10 17:37:13 -04:00
|
|
|
private static final String ARG_MESSAGE = "message";
|
2012-04-25 09:10:12 -04:00
|
|
|
private static final String ARG_STYLE = "style";
|
2014-03-02 15:06:43 -05:00
|
|
|
private static final String ARG_CANCELABLE = "cancelable";
|
2015-03-21 18:01:13 -04:00
|
|
|
private static final String ARG_SERVICE_TYPE = "service_class";
|
|
|
|
|
2014-08-31 14:05:19 -04:00
|
|
|
boolean mCanCancel = false, mPreventCancel = false, mIsCancelled = false;
|
|
|
|
|
2015-03-21 18:01:13 -04:00
|
|
|
/**
|
|
|
|
* creates a new instance of this fragment
|
|
|
|
* @param message the message to be displayed initially above the progress bar
|
|
|
|
* @param style the progress bar style, as defined in ProgressDialog (horizontal or spinner)
|
|
|
|
* @param cancelable should we let the user cancel this operation
|
|
|
|
*/
|
2015-05-28 07:32:34 -04:00
|
|
|
public static ProgressDialogFragment newInstance(String message, int style, boolean cancelable) {
|
2012-04-20 06:12:07 -04:00
|
|
|
ProgressDialogFragment frag = new ProgressDialogFragment();
|
|
|
|
Bundle args = new Bundle();
|
2014-03-10 17:19:43 -04:00
|
|
|
args.putString(ARG_MESSAGE, message);
|
2012-04-25 09:10:12 -04:00
|
|
|
args.putInt(ARG_STYLE, style);
|
2014-03-02 15:06:43 -05:00
|
|
|
args.putBoolean(ARG_CANCELABLE, cancelable);
|
2012-04-25 09:10:12 -04:00
|
|
|
|
2012-04-20 06:12:07 -04:00
|
|
|
frag.setArguments(args);
|
2014-03-02 20:55:13 -05:00
|
|
|
|
2012-04-20 06:12:07 -04:00
|
|
|
return frag;
|
|
|
|
}
|
|
|
|
|
2012-04-25 09:10:12 -04:00
|
|
|
public void setProgress(int messageId, int progress, int max) {
|
|
|
|
setProgress(getString(messageId), progress, max);
|
|
|
|
}
|
2012-04-20 06:12:07 -04:00
|
|
|
|
2012-04-25 09:10:12 -04:00
|
|
|
public void setProgress(int progress, int max) {
|
2015-05-31 13:45:25 -04:00
|
|
|
ProgressDialog dialog = (ProgressDialog) getDialog();
|
|
|
|
|
|
|
|
if (mIsCancelled || dialog == null) {
|
2014-08-31 14:05:19 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-25 09:10:12 -04:00
|
|
|
dialog.setProgress(progress);
|
|
|
|
dialog.setMax(max);
|
2012-04-20 06:12:07 -04:00
|
|
|
}
|
|
|
|
|
2012-04-25 09:10:12 -04:00
|
|
|
public void setProgress(String message, int progress, int max) {
|
|
|
|
ProgressDialog dialog = (ProgressDialog) getDialog();
|
|
|
|
|
2015-05-10 07:16:03 -04:00
|
|
|
if (mIsCancelled || dialog == null) {
|
|
|
|
return;
|
|
|
|
}
|
2015-05-10 07:16:37 -04:00
|
|
|
|
2012-04-25 09:10:12 -04:00
|
|
|
dialog.setMessage(message);
|
|
|
|
dialog.setProgress(progress);
|
|
|
|
dialog.setMax(max);
|
|
|
|
}
|
|
|
|
|
2015-05-31 13:45:25 -04:00
|
|
|
@NonNull
|
2012-04-20 06:12:07 -04:00
|
|
|
@Override
|
|
|
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
2014-03-02 15:06:43 -05:00
|
|
|
final Activity activity = getActivity();
|
2012-04-20 06:12:07 -04:00
|
|
|
|
2014-08-20 14:13:25 -04:00
|
|
|
// if the progress dialog is displayed from the application class, design is missing
|
|
|
|
// hack to get holo design (which is not automatically applied due to activity's Theme.NoDisplay
|
|
|
|
ContextThemeWrapper context = new ContextThemeWrapper(activity,
|
|
|
|
R.style.Theme_AppCompat_Light);
|
|
|
|
|
|
|
|
ProgressDialog dialog = new ProgressDialog(context);
|
2012-04-20 06:12:07 -04:00
|
|
|
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
|
2014-08-31 11:32:13 -04:00
|
|
|
// We never use the builtin cancel method
|
2012-04-20 06:12:07 -04:00
|
|
|
dialog.setCancelable(false);
|
2012-04-25 09:10:12 -04:00
|
|
|
dialog.setCanceledOnTouchOutside(false);
|
2012-04-20 06:12:07 -04:00
|
|
|
|
2014-03-10 17:19:43 -04:00
|
|
|
String message = getArguments().getString(ARG_MESSAGE);
|
2012-04-25 09:10:12 -04:00
|
|
|
int style = getArguments().getInt(ARG_STYLE);
|
2014-08-31 14:05:19 -04:00
|
|
|
mCanCancel = getArguments().getBoolean(ARG_CANCELABLE);
|
2012-04-20 06:12:07 -04:00
|
|
|
|
2014-03-10 17:19:43 -04:00
|
|
|
dialog.setMessage(message);
|
2012-04-25 09:10:12 -04:00
|
|
|
dialog.setProgressStyle(style);
|
2012-04-20 06:12:07 -04:00
|
|
|
|
2014-08-31 11:32:13 -04:00
|
|
|
// If this is supposed to be cancelable, add our (custom) cancel mechanic
|
2014-08-31 14:05:19 -04:00
|
|
|
if (mCanCancel) {
|
2014-08-31 11:32:13 -04:00
|
|
|
// Just show the button, take care of the onClickListener afterwards (in onStart)
|
2014-03-02 15:06:43 -05:00
|
|
|
dialog.setButton(DialogInterface.BUTTON_NEGATIVE,
|
2014-08-31 11:32:13 -04:00
|
|
|
activity.getString(R.string.progress_cancel), (DialogInterface.OnClickListener) null);
|
2014-03-02 15:06:43 -05:00
|
|
|
}
|
|
|
|
|
2014-08-31 11:32:13 -04:00
|
|
|
// Disable the back button regardless
|
2012-04-25 09:10:12 -04:00
|
|
|
OnKeyListener keyListener = new OnKeyListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
|
|
|
|
if (keyCode == KeyEvent.KEYCODE_BACK) {
|
2014-08-31 14:05:19 -04:00
|
|
|
if (mCanCancel) {
|
2014-08-31 11:32:13 -04:00
|
|
|
((ProgressDialog) dialog).getButton(
|
|
|
|
DialogInterface.BUTTON_NEGATIVE).performClick();
|
|
|
|
}
|
|
|
|
// return true, indicating we handled this
|
2012-04-25 09:10:12 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
dialog.setOnKeyListener(keyListener);
|
2012-04-20 06:12:07 -04:00
|
|
|
|
|
|
|
return dialog;
|
|
|
|
}
|
2014-08-31 11:32:13 -04:00
|
|
|
|
2014-08-31 14:05:19 -04:00
|
|
|
public void setPreventCancel(boolean preventCancel) {
|
|
|
|
// Don't care if we can't cancel anymore either way!
|
|
|
|
if (mIsCancelled || ! mCanCancel) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPreventCancel = preventCancel;
|
2015-05-31 13:45:25 -04:00
|
|
|
ProgressDialog dialog = (ProgressDialog) getDialog();
|
|
|
|
if (dialog == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final Button negative = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
|
2014-10-26 17:25:44 -04:00
|
|
|
negative.setEnabled(mIsCancelled && !preventCancel);
|
2014-08-31 14:05:19 -04:00
|
|
|
}
|
|
|
|
|
2014-08-31 11:32:13 -04:00
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
|
|
|
|
// Override the default behavior so the dialog is NOT dismissed on click
|
2014-08-31 14:05:19 -04:00
|
|
|
final Button negative = ((ProgressDialog) getDialog()).getButton(DialogInterface.BUTTON_NEGATIVE);
|
2014-08-31 11:32:13 -04:00
|
|
|
negative.setOnClickListener(new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2014-08-31 14:05:19 -04:00
|
|
|
// nvm if we are already cancelled, or weren't able to begin with
|
|
|
|
if (mIsCancelled || ! mCanCancel) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remember this, and don't allow another click
|
|
|
|
mIsCancelled = true;
|
|
|
|
negative.setClickable(false);
|
|
|
|
negative.setTextColor(Color.GRAY);
|
|
|
|
|
2014-08-31 11:32:13 -04:00
|
|
|
// send a cancel message. note that this message will be handled by
|
|
|
|
// KeychainIntentService.onStartCommand, which runs in this thread,
|
|
|
|
// not the service one, and will not queue up a command.
|
2015-05-28 07:32:34 -04:00
|
|
|
Intent serviceIntent = new Intent(getActivity(), KeychainIntentService.class);
|
2015-03-21 18:01:13 -04:00
|
|
|
|
|
|
|
serviceIntent.setAction(KeychainIntentService.ACTION_CANCEL);
|
|
|
|
getActivity().startService(serviceIntent);
|
2015-05-31 13:45:25 -04:00
|
|
|
|
|
|
|
// Set the progress bar accordingly
|
|
|
|
ProgressDialog dialog = (ProgressDialog) getDialog();
|
|
|
|
if (dialog == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dialog.setIndeterminate(true);
|
|
|
|
dialog.setMessage(getString(R.string.progress_cancelling));
|
|
|
|
|
|
|
|
|
2014-08-31 11:32:13 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-13 14:41:53 -04:00
|
|
|
}
|