OC-1508: App crashes when changing the orientation
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / dialog / IndeterminateProgressDialog.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 package com.owncloud.android.ui.dialog;
19
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
27 import com.actionbarsherlock.app.SherlockDialogFragment;
28 import com.owncloud.android.R;
29
30 public class IndeterminateProgressDialog extends SherlockDialogFragment {
31
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
36 /**
37 * Public factory method to get dialog instances.
38 *
39 * @param messageId Resource id for a message to show in the dialog.
40 * @param cancelable If 'true', the dialog can be cancelled by the user input (BACK button, touch outside...)
41 * @return New dialog instance, ready to show.
42 */
43 public static IndeterminateProgressDialog newInstance(int messageId, boolean cancelable) {
44 IndeterminateProgressDialog fragment = new IndeterminateProgressDialog();
45 Bundle args = new Bundle();
46 args.putInt(ARG_MESSAGE_ID, messageId);
47 args.putBoolean(ARG_CANCELABLE, cancelable);
48 fragment.setArguments(args);
49 return fragment;
50 }
51
52
53 /**
54 * {@inheritDoc}
55 */
56 @Override
57 public Dialog onCreateDialog(Bundle savedInstanceState) {
58 /// create indeterminate progress dialog
59 final ProgressDialog dialog = new ProgressDialog(getActivity());
60 dialog.setIndeterminate(true);
61
62 /// set message
63 int messageId = getArguments().getInt(ARG_MESSAGE_ID, R.string.placeholder_sentence);
64 dialog.setMessage(getString(messageId));
65
66 /// set cancellation behavior
67 boolean cancelable = getArguments().getBoolean(ARG_CANCELABLE, false);
68 if (!cancelable) {
69 dialog.setCancelable(false);
70 // disable the back button
71 OnKeyListener keyListener = new OnKeyListener() {
72 @Override
73 public boolean onKey(DialogInterface dialog, int keyCode,
74 KeyEvent event) {
75
76 if( keyCode == KeyEvent.KEYCODE_BACK){
77 return true;
78 }
79 return false;
80 }
81
82 };
83 dialog.setOnKeyListener(keyListener);
84 }
85
86 return dialog;
87 }
88
89 }
90
91