Skip to content

Commit f044dee

Browse files
author
Shushant
committed
Added configuration editor
1 parent c249d5f commit f044dee

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package org.opendroidphp.app.fragments.dialogs;
2+
3+
import android.app.Dialog;
4+
import android.os.AsyncTask;
5+
import android.os.Bundle;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.widget.Button;
9+
import android.widget.EditText;
10+
11+
import com.actionbarsherlock.app.SherlockDialogFragment;
12+
13+
import org.apache.commons.io.FileUtils;
14+
import org.opendroidphp.R;
15+
16+
import java.io.File;
17+
import java.io.IOException;
18+
19+
20+
public class EditorDialogFragment extends SherlockDialogFragment {
21+
22+
private Dialog dialog;
23+
private DialogClickEvent event;
24+
25+
private EditText etSource;
26+
27+
private String source;
28+
private File file;
29+
30+
public static EditorDialogFragment create(final String source) {
31+
EditorDialogFragment fragment = new EditorDialogFragment();
32+
fragment.setSource(source);
33+
return fragment;
34+
}
35+
36+
public static EditorDialogFragment create(final File file) {
37+
EditorDialogFragment fragment = new EditorDialogFragment();
38+
fragment.setFile(file);
39+
return fragment;
40+
}
41+
42+
public static EditorDialogFragment create(final String source, final DialogClickEvent event) {
43+
EditorDialogFragment fragment = new EditorDialogFragment();
44+
fragment.setSource(source);
45+
fragment.addClickEventListener(event);
46+
return fragment;
47+
}
48+
49+
@Override
50+
public Dialog onCreateDialog(Bundle savedInstanceState) {
51+
LayoutInflater inflater = LayoutInflater.from(getSherlockActivity());
52+
// if ((source == null) || (file == null)) {
53+
// throw new NullPointerException("source or file cannot be empty");
54+
// }
55+
dialog = new Dialog(getSherlockActivity(), R.style.Theme_DroidPHP_Dialog);
56+
dialog.setContentView(inflater.inflate(R.layout.dialog_editor, null));
57+
etSource = (EditText) dialog.findViewById(R.id.content);
58+
59+
if (source != null)
60+
etSource.setText(source);
61+
else new FileLoaderTask().execute();
62+
//Buttons and events listener
63+
Button btnNotConfirm = (Button) dialog.findViewById(R.id.negative);
64+
btnNotConfirm.setText(R.string.confirm_exit);
65+
66+
Button btnConfirm = (Button) dialog.findViewById(R.id.positive);
67+
btnConfirm.setText(R.string.confirm);
68+
69+
btnNotConfirm.setOnClickListener(new View.OnClickListener() {
70+
@Override
71+
public void onClick(View view) {
72+
dismiss();
73+
if (event != null) event.onConfirmExit();
74+
}
75+
});
76+
btnConfirm.setOnClickListener(new View.OnClickListener() {
77+
@Override
78+
public void onClick(View view) {
79+
FileSaveTask task = new FileSaveTask();
80+
task.execute(etSource.getText().toString());
81+
dismiss();
82+
if (event != null) event.onConfirm();
83+
}
84+
});
85+
86+
return dialog;
87+
}
88+
89+
protected EditorDialogFragment setFile(final File file) {
90+
this.file = file;
91+
return this;
92+
}
93+
94+
public String getSource() {
95+
return source;
96+
}
97+
98+
public EditorDialogFragment setSource(final String source) {
99+
this.source = source;
100+
return this;
101+
}
102+
103+
public EditorDialogFragment addClickEventListener(DialogClickEvent event) {
104+
this.event = event;
105+
return this;
106+
}
107+
108+
public interface DialogClickEvent {
109+
public void onConfirm();
110+
111+
public void onConfirmExit();
112+
}
113+
114+
private class FileLoaderTask extends AsyncTask<Void, String, Void> {
115+
@Override
116+
protected Void doInBackground(Void... voids) {
117+
try {
118+
String source = FileUtils.readFileToString(file, "UTF-8");
119+
setSource(source);
120+
publishProgress(source);
121+
} catch (IOException e) {
122+
e.printStackTrace();
123+
}
124+
return null;
125+
}
126+
127+
@Override
128+
protected void onProgressUpdate(String... sourceCode) {
129+
super.onProgressUpdate(sourceCode);
130+
if (etSource != null)
131+
etSource.setText(sourceCode[0]);
132+
}
133+
}
134+
135+
private class FileSaveTask extends AsyncTask<String, String, Void> {
136+
@Override
137+
protected Void doInBackground(String... sourceCode) {
138+
try {
139+
FileUtils.writeStringToFile(file, sourceCode[0], "UTF-8");
140+
} catch (IOException e) {
141+
e.printStackTrace();
142+
}
143+
return null;
144+
}
145+
}
146+
}

0 commit comments

Comments
 (0)