/** * Save sharing settings * * @param toast toast to show after saving is finished * @return false if login is required & save should be halted */ @SuppressWarnings("nls") public boolean saveSharingSettings(String toast) { if (task == null) return false; saveToast = toast; boolean dirty = false; try { JSONObject userJson = null; TextView assignedView = null; if (assignedCustom.getVisibility() == View.VISIBLE) { userJson = PeopleContainer.createUserJson(assignedCustom); assignedView = assignedCustom; } else if (assignedSpinner.getSelectedItem() != null) { userJson = ((AssignedToUser) assignedSpinner.getSelectedItem()).user; } if (userJson != null && userJson.optString("email").indexOf('@') == -1) { throw new ParseSharedException( assignedView, activity.getString(R.string.actfm_EPA_invalid_email, userJson.optString("email"))); } if (userJson == null || userJson.optLong("id", -1) == 0) { dirty = task.getValue(Task.USER_ID) == 0L ? dirty : true; task.setValue(Task.USER_ID, 0L); if (!TextUtils.isEmpty(task.getValue(Task.USER))) task.setValue(Task.USER, "{}"); } else { String user = userJson.toString(); long taskUserId = -1; String taskUserEmail = ""; try { JSONObject taskUser = new JSONObject(task.getValue(Task.USER)); taskUserId = taskUser.optLong("id", -1); taskUserEmail = taskUser.optString("email"); } catch (JSONException e) { // sad times } long userId = userJson.optLong("id", -1); String userEmail = userJson.optString("email"); boolean match = (userId == taskUserId && userId != -1); match = match || (userEmail.equals(taskUserEmail) && !TextUtils.isEmpty(userEmail)); dirty = match ? dirty : true; task.setValue(Task.USER_ID, userJson.optLong("id", -1)); task.setValue(Task.USER, user); } JSONObject sharedWith = parseSharedWithAndTags(); dirty = dirty || sharedWith.has("p"); if (!TextUtils.isEmpty(task.getValue(Task.SHARED_WITH)) || sharedWith.length() != 0) task.setValue(Task.SHARED_WITH, sharedWith.toString()); if (dirty) taskService.save(task); if (dirty && !actFmPreferenceService.isLoggedIn()) { activity.startActivityForResult( new Intent(activity, ActFmLoginActivity.class), loginRequestCode); return false; } if (dirty) shareTask(sharedWith); else showSaveToast(); return true; } catch (JSONException e) { exceptionService.displayAndReportError(activity, "save-people", e); } catch (ParseSharedException e) { if (e.view != null) { e.view.setTextColor(Color.RED); e.view.requestFocus(); } DialogUtilities.okDialog(activity, e.message, null); } return false; }
@SuppressWarnings("nls") private void setUpData() { try { JSONObject sharedWith; if (task.getValue(Task.SHARED_WITH).length() > 0) sharedWith = new JSONObject(task.getValue(Task.SHARED_WITH)); else sharedWith = new JSONObject(); cbFacebook.setChecked(sharedWith.optBoolean("fb", false)); cbTwitter.setChecked(sharedWith.optBoolean("tw", false)); final ArrayList<JSONObject> sharedPeople = new ArrayList<JSONObject>(); JSONArray people = sharedWith.optJSONArray("p"); if (people != null) { for (int i = 0; i < people.length(); i++) { String person = people.getString(i); TextView textView = sharedWithContainer.addPerson(person); textView.setEnabled(false); sharedPeople.add(PeopleContainer.createUserJson(textView)); } } new Thread( new Runnable() { @Override public void run() { ArrayList<JSONObject> collaborators = new ArrayList<JSONObject>(); TodorooCursor<Metadata> tags = TagService.getInstance().getTags(task.getId()); try { Metadata metadata = new Metadata(); for (tags.moveToFirst(); !tags.isAfterLast(); tags.moveToNext()) { metadata.readFromCursor(tags); final String tag = metadata.getValue(TagService.TAG); TagData tagData = tagDataService.getTag( tag, TagData.MEMBER_COUNT, TagData.MEMBERS, TagData.USER); if (tagData != null && tagData.getValue(TagData.MEMBER_COUNT) > 0) { JSONArray members = new JSONArray(tagData.getValue(TagData.MEMBERS)); for (int i = 0; i < members.length(); i++) { JSONObject user = members.getJSONObject(i); user.put("tag", tag); sharedPeople.add(user); collaborators.add(user); } if (!TextUtils.isEmpty(tagData.getValue(TagData.USER))) { JSONObject user = new JSONObject(tagData.getValue(TagData.USER)); user.put("tag", tag); sharedPeople.add(user); collaborators.add(user); } } else { nonSharedTags.add((Metadata) metadata.clone()); } } if (collaborators.size() > 0) buildCollaborators(collaborators); buildAssignedToSpinner(sharedPeople); } catch (JSONException e) { exceptionService.reportError("json-reading-data", e); } finally { tags.close(); } } }) .start(); } catch (JSONException e) { exceptionService.reportError("json-reading-data", e); } }