-
Notifications
You must be signed in to change notification settings - Fork 229
/
EditActivity.java
380 lines (315 loc) · 12.7 KB
/
EditActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package com.matburt.mobileorg.Gui.Capture;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.ActionBar;
import android.support.v4.app.ActionBar.Tab;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.MenuInflater;
import com.matburt.mobileorg.R;
import com.matburt.mobileorg.Parsing.MobileOrgApplication;
import com.matburt.mobileorg.Parsing.NodeWrapper;
import com.matburt.mobileorg.Parsing.OrgDatabase;
import com.matburt.mobileorg.Parsing.OrgFile;
import com.matburt.mobileorg.Synchronizers.Synchronizer;
public class EditActivity extends FragmentActivity {
public final static String ACTIONMODE_CREATE = "create";
public final static String ACTIONMODE_EDIT = "edit";
public final static String ACTIONMODE_ADDCHILD = "add_child";
private NodeWrapper node;
private String actionMode;
private OrgDatabase orgDB;
private MobileOrgApplication appInst;
private EditDetailsFragment detailsFragment;
private EditPayloadFragment payloadFragment;
private EditPayloadFragment rawPayloadFragment;
private long node_id;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit);
Intent intent = getIntent();
this.actionMode = intent.getStringExtra("actionMode");
this.node_id = intent.getLongExtra("node_id", -1);
this.appInst = (MobileOrgApplication) this.getApplication();
this.orgDB = appInst.getDB();
if (savedInstanceState != null) {
this.detailsFragment = (EditDetailsFragment) getSupportFragmentManager()
.getFragment(savedInstanceState,
EditDetailsFragment.class.getName());
this.payloadFragment = (EditPayloadFragment) getSupportFragmentManager()
.getFragment(savedInstanceState,
EditPayloadFragment.class.getName());
this.rawPayloadFragment = (EditPayloadFragment) getSupportFragmentManager()
.getFragment(savedInstanceState,
EditPayloadFragment.class.getName() + "raw");
}
init();
setupActionbarTabs(savedInstanceState);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState,
EditPayloadFragment.class.getName(), payloadFragment);
getSupportFragmentManager().putFragment(outState,
EditPayloadFragment.class.getName() + "raw", rawPayloadFragment);
getSupportFragmentManager().putFragment(outState,
EditDetailsFragment.class.getName(), detailsFragment);
outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex());
}
private void init() {
if (this.detailsFragment == null)
this.detailsFragment = new EditDetailsFragment();
if (this.payloadFragment == null)
this.payloadFragment = new EditPayloadFragment();
if (this.rawPayloadFragment == null)
this.rawPayloadFragment = new EditPayloadFragment();
String defaultTodo = PreferenceManager
.getDefaultSharedPreferences(this).getString("defaultTodo", "");
Intent intent = getIntent();
if (this.actionMode == null) {
String subject = intent
.getStringExtra("android.intent.extra.SUBJECT");
if(subject == null)
subject = "";
String text = intent.getStringExtra("android.intent.extra.TEXT");
if(text == null)
text = "";
node = new NodeWrapper(null);
this.detailsFragment.init(this.node, this.actionMode, defaultTodo, subject);
this.payloadFragment.init(text, true);
this.actionMode = ACTIONMODE_CREATE;
} else if (this.actionMode.equals(ACTIONMODE_CREATE)) {
node = new NodeWrapper(null);
this.detailsFragment.init(this.node, this.actionMode, defaultTodo);
this.payloadFragment.init("", true);
} else if (this.actionMode.equals(ACTIONMODE_EDIT)) {
long nodeId = intent.getLongExtra("node_id", 0);
node = new NodeWrapper(appInst.getDB().getNode(nodeId));
this.detailsFragment.init(this.node, this.actionMode, defaultTodo);
this.payloadFragment.init(node.getCleanedPayload(orgDB), true);
} else if (this.actionMode.equals(ACTIONMODE_ADDCHILD)) {
node = new NodeWrapper(null);
this.detailsFragment.init(this.node, this.actionMode, defaultTodo);
this.payloadFragment.init("", true);
}
this.rawPayloadFragment.init(node.getRawPayload(orgDB), false);
}
private void setupActionbarTabs(Bundle savedInstanceState) {
ActionBar actionbar = getSupportActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab detailsTab = actionbar.newTab().setText("Details");
detailsTab.setTabListener(new TabListener(detailsFragment, "details"));
actionbar.addTab(detailsTab);
ActionBar.Tab payloadTab = actionbar.newTab().setText("Payload");
payloadTab.setTabListener(new TabListener(payloadFragment, "payload"));
actionbar.addTab(payloadTab);
ActionBar.Tab rawPayloadTab = actionbar.newTab().setText("Raw Payload");
rawPayloadTab.setTabListener(new TabListener(rawPayloadFragment, "raw_payload"));
actionbar.addTab(rawPayloadTab);
if (savedInstanceState != null) {
actionbar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
}
}
private class TabListener implements ActionBar.TabListener {
Fragment fragment;
public TabListener(Fragment fragment, String tag) {
this.fragment = fragment;
FragmentTransaction fragmentTransaction = getSupportFragmentManager()
.beginTransaction();
if (fragment != null && fragment.isAdded() == false) {
fragmentTransaction.add(R.id.editnode_fragment_container,
fragment, tag);
}
fragmentTransaction.hide(fragment).commit();
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager()
.beginTransaction();
fragmentTransaction.show(fragment).commit();
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (fragment != null) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager()
.beginTransaction();
fragmentTransaction.hide(fragment).commit();
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
};
@Override
public boolean onCreateOptionsMenu(android.support.v4.view.Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.edit, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(android.support.v4.view.MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
doCancel();
return true;
case R.id.nodeedit_save:
save();
setResult(RESULT_OK);
finish();
return true;
case R.id.nodeedit_cancel:
doCancel();
return true;
}
return false;
}
@Override
public void onBackPressed() {
doCancel();
}
private void doCancel() {
if(this.detailsFragment.hasEdits() == false) {
setResult(RESULT_CANCELED);
finish();
return;
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.node_edit_prompt)
.setCancelable(false)
.setPositiveButton(R.string.yes,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
setResult(RESULT_CANCELED);
finish();
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.create().show();
}
private StringBuilder insertOrReplace(StringBuilder payloadResidue,
String key, String value) {
final Pattern schedulePattern = Pattern.compile(key + "\\s*<[^>]+>");
Matcher matcher = schedulePattern.matcher(payloadResidue);
if (matcher.find()) {
if (TextUtils.isEmpty(value))
payloadResidue.delete(matcher.start(), matcher.end());
else
payloadResidue.replace(matcher.start(), matcher.end(), value);
}
else if(TextUtils.isEmpty(value) == false)
payloadResidue.insert(0, value).append("\n");
return payloadResidue;
}
private StringBuilder getNewPayloadResidue() {
StringBuilder result = new StringBuilder();
StringBuilder originalPayloadResidue = new StringBuilder(node.getPayloadResidue(orgDB));
String newScheduled = this.detailsFragment.getScheduled();
String newDeadline = this.detailsFragment.getDeadline();
result = insertOrReplace(originalPayloadResidue, "SCHEDULED:", newScheduled);
result = insertOrReplace(result, "DEADLINE:", newDeadline);
return result;
}
private void save() {
String newTitle = this.detailsFragment.getTitle();
String newTodo = this.detailsFragment.getTodo();
String newPriority = this.detailsFragment.getPriority();
String newTags = this.detailsFragment.getTags();
StringBuilder newCleanedPayload = new StringBuilder(this.payloadFragment.getText());
StringBuilder newPayloadResidue = getNewPayloadResidue();
if (this.actionMode.equals(ACTIONMODE_CREATE)) {
MobileOrgApplication appInst = (MobileOrgApplication) this.getApplication();
OrgDatabase orgDB = appInst.getDB();
long file_id = orgDB.addOrUpdateFile(OrgFile.CAPTURE_FILE, "Captures", "", true);
Long parent = orgDB.getFileNodeId(OrgFile.CAPTURE_FILE);
long node_id = orgDB.addNode(parent, newTitle, newTodo, newPriority, newTags, file_id);
boolean addTimestamp = PreferenceManager.getDefaultSharedPreferences(
this).getBoolean("captureWithTimestamp", false);
if(addTimestamp)
newCleanedPayload.append("\n").append(getTimestamp()).append("\n");
orgDB.addNodePayload(node_id, newCleanedPayload.toString() + newPayloadResidue.toString());
} else if (this.actionMode.equals(ACTIONMODE_ADDCHILD)) {
MobileOrgApplication appInst = (MobileOrgApplication) this.getApplication();
OrgDatabase orgDB = appInst.getDB();
long file_id = orgDB.addOrUpdateFile(OrgFile.CAPTURE_FILE, "Captures", "", true);
Long parent = this.node_id;
long node_id = orgDB.addNode(parent, newTitle, newTodo, newPriority, newTags, file_id);
boolean addTimestamp = PreferenceManager.getDefaultSharedPreferences(
this).getBoolean("captureWithTimestamp", false);
if(addTimestamp)
newCleanedPayload.append("\n").append(getTimestamp()).append("\n");
orgDB.addNodePayload(node_id, newCleanedPayload.toString() + newPayloadResidue.toString());
} else if (this.actionMode.equals(ACTIONMODE_EDIT)) {
try {
editNode(newTitle, newTodo, newPriority,
newCleanedPayload.toString(),
newPayloadResidue.toString(), newTags);
} catch (IOException e) {
}
}
Intent intent = new Intent(Synchronizer.SYNC_UPDATE);
intent.putExtra(Synchronizer.SYNC_DONE, true);
intent.putExtra("showToast", false);
sendBroadcast(intent);
}
/**
* Takes a Node and five strings, representing edits to the node.
* This function will generate a new edit entry for each value that was
* changed.
*/
private void editNode(String newTitle, String newTodo,
String newPriority, String newCleanedPayload, String newPayloadResidue, String newTags) throws IOException {
boolean generateEdits = !node.getFileName(orgDB).equals(OrgFile.CAPTURE_FILE);
if (!node.getName().equals(newTitle)) {
if (generateEdits)
orgDB.addEdit("heading", node.getNodeId(orgDB), newTitle, node.getName(), newTitle);
node.setName(newTitle, orgDB);
}
if (newTodo != null && !node.getTodo().equals(newTodo)) {
if (generateEdits)
orgDB.addEdit("todo", node.getNodeId(orgDB), newTitle, node.getTodo(), newTodo);
node.setTodo(newTodo, orgDB);
}
if (newPriority != null && !node.getPriority().equals(newPriority)) {
if (generateEdits)
orgDB.addEdit("priority", node.getNodeId(orgDB), newTitle, node.getPriority(),
newPriority);
node.setPriority(newPriority, orgDB);
}
if (!node.getCleanedPayload(orgDB).equals(newCleanedPayload)
|| !node.getPayloadResidue(orgDB).equals(newPayloadResidue)) {
String newRawPayload = newPayloadResidue + newCleanedPayload;
if (generateEdits)
orgDB.addEdit("body", node.getNodeId(orgDB), newTitle, node.getRawPayload(orgDB), newRawPayload);
node.setPayload(newRawPayload, orgDB);
}
if(!node.getTags().equals(newTags)) {
if (generateEdits) {
orgDB.addEdit("tags", node.getNodeId(orgDB), newTitle,
node.getTagsWithoutInheritet(),
NodeWrapper.getTagsWithoutInheritet(newTags));
}
node.setTags(newTags, orgDB);
}
}
public static String getTimestamp() {
SimpleDateFormat sdf = new SimpleDateFormat("[yyyy-MM-dd EEE HH:mm]");
return sdf.format(new Date());
}
}