IndeterminateProgressDialog.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * Copyright (C) 2015 ownCloud Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2,
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package com.owncloud.android.ui.dialog;
  20. import android.app.Dialog;
  21. import android.app.ProgressDialog;
  22. import android.content.DialogInterface;
  23. import android.content.DialogInterface.OnKeyListener;
  24. import android.os.Bundle;
  25. import android.view.KeyEvent;
  26. import android.widget.ProgressBar;
  27. import com.owncloud.android.R;
  28. import com.owncloud.android.utils.theme.ThemeColorUtils;
  29. import androidx.annotation.NonNull;
  30. import androidx.fragment.app.DialogFragment;
  31. public class IndeterminateProgressDialog extends DialogFragment {
  32. private static final String ARG_MESSAGE_ID = IndeterminateProgressDialog.class.getCanonicalName() + ".ARG_MESSAGE_ID";
  33. private static final String ARG_CANCELABLE = IndeterminateProgressDialog.class.getCanonicalName() + ".ARG_CANCELABLE";
  34. /**
  35. * Public factory method to get dialog instances.
  36. *
  37. * @param messageId Resource id for a message to show in the dialog.
  38. * @param cancelable If 'true', the dialog can be cancelled by the user input (BACK button, touch outside...)
  39. * @return New dialog instance, ready to show.
  40. */
  41. public static IndeterminateProgressDialog newInstance(int messageId, boolean cancelable) {
  42. IndeterminateProgressDialog fragment = new IndeterminateProgressDialog();
  43. fragment.setStyle(DialogFragment.STYLE_NO_FRAME, R.style.ownCloud_AlertDialog);
  44. Bundle args = new Bundle();
  45. args.putInt(ARG_MESSAGE_ID, messageId);
  46. args.putBoolean(ARG_CANCELABLE, cancelable);
  47. fragment.setArguments(args);
  48. return fragment;
  49. }
  50. /**
  51. * {@inheritDoc}
  52. */
  53. @NonNull
  54. @Override
  55. public Dialog onCreateDialog(Bundle savedInstanceState) {
  56. /// create indeterminate progress dialog
  57. final ProgressDialog progressDialog = new ProgressDialog(getActivity(), R.style.ProgressDialogTheme);
  58. progressDialog.setIndeterminate(true);
  59. progressDialog.setOnShowListener(new DialogInterface.OnShowListener() {
  60. @Override
  61. public void onShow(DialogInterface dialog) {
  62. ProgressBar v = progressDialog.findViewById(android.R.id.progress);
  63. v.getIndeterminateDrawable().setColorFilter(ThemeColorUtils.primaryAccentColor(getContext()),
  64. android.graphics.PorterDuff.Mode.MULTIPLY);
  65. }
  66. });
  67. /// set message
  68. int messageId = getArguments().getInt(ARG_MESSAGE_ID, R.string.placeholder_sentence);
  69. progressDialog.setMessage(getString(messageId));
  70. /// set cancellation behavior
  71. boolean cancelable = getArguments().getBoolean(ARG_CANCELABLE, false);
  72. if (!cancelable) {
  73. progressDialog.setCancelable(false);
  74. // disable the back button
  75. OnKeyListener keyListener = new OnKeyListener() {
  76. @Override
  77. public boolean onKey(DialogInterface dialog, int keyCode,
  78. KeyEvent event) {
  79. return keyCode == KeyEvent.KEYCODE_BACK;
  80. }
  81. };
  82. progressDialog.setOnKeyListener(keyListener);
  83. }
  84. return progressDialog;
  85. }
  86. }