Merge branch 'develop' into ignoreHiddenFiles
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / dialog / IndeterminateProgressDialog.java
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
20 package com.owncloud.android.ui.dialog;
21
22 import android.app.Dialog;
23 import android.app.ProgressDialog;
24 import android.content.DialogInterface;
25 import android.content.DialogInterface.OnKeyListener;
26 import android.os.Bundle;
27 import android.support.v4.app.DialogFragment;
28 import android.view.KeyEvent;
29 import android.widget.ProgressBar;
30
31 import com.owncloud.android.R;
32
33
34 public class IndeterminateProgressDialog extends DialogFragment {
35
36 private static final String ARG_MESSAGE_ID = IndeterminateProgressDialog.class.getCanonicalName() + ".ARG_MESSAGE_ID";
37 private static final String ARG_CANCELABLE = IndeterminateProgressDialog.class.getCanonicalName() + ".ARG_CANCELABLE";
38
39
40 /**
41 * Public factory method to get dialog instances.
42 *
43 * @param messageId Resource id for a message to show in the dialog.
44 * @param cancelable If 'true', the dialog can be cancelled by the user input (BACK button, touch outside...)
45 * @return New dialog instance, ready to show.
46 */
47 public static IndeterminateProgressDialog newInstance(int messageId, boolean cancelable) {
48 IndeterminateProgressDialog fragment = new IndeterminateProgressDialog();
49 fragment.setStyle(DialogFragment.STYLE_NO_FRAME, R.style.ownCloud_AlertDialog);
50 Bundle args = new Bundle();
51 args.putInt(ARG_MESSAGE_ID, messageId);
52 args.putBoolean(ARG_CANCELABLE, cancelable);
53 fragment.setArguments(args);
54 return fragment;
55 }
56
57
58 /**
59 * {@inheritDoc}
60 */
61 @Override
62 public Dialog onCreateDialog(Bundle savedInstanceState) {
63 /// create indeterminate progress dialog
64 final ProgressDialog progressDialog = new ProgressDialog(getActivity(), R.style.ProgressDialogTheme);
65 progressDialog.setIndeterminate(true);
66 progressDialog.setOnShowListener(new DialogInterface.OnShowListener() {
67 @Override
68 public void onShow(DialogInterface dialog) {
69 ProgressBar v = (ProgressBar) progressDialog.findViewById(android.R.id.progress);
70 v.getIndeterminateDrawable().setColorFilter(getResources().getColor(R.color.color_accent),
71 android.graphics.PorterDuff.Mode.MULTIPLY);
72
73 }
74 });
75
76 /// set message
77 int messageId = getArguments().getInt(ARG_MESSAGE_ID, R.string.placeholder_sentence);
78 progressDialog.setMessage(getString(messageId));
79
80 /// set cancellation behavior
81 boolean cancelable = getArguments().getBoolean(ARG_CANCELABLE, false);
82 if (!cancelable) {
83 progressDialog.setCancelable(false);
84 // disable the back button
85 OnKeyListener keyListener = new OnKeyListener() {
86 @Override
87 public boolean onKey(DialogInterface dialog, int keyCode,
88 KeyEvent event) {
89
90 if( keyCode == KeyEvent.KEYCODE_BACK) {
91 return true;
92 }
93 return false;
94 }
95
96 };
97 progressDialog.setOnKeyListener(keyListener);
98 }
99
100 return progressDialog;
101 }
102
103 }
104
105