diff options
| author | Bananeweizen <bananeweizen@gmx.de> | 2013-05-10 09:10:44 +0200 |
|---|---|---|
| committer | Bananeweizen <bananeweizen@gmx.de> | 2013-05-10 09:11:28 +0200 |
| commit | d6e525eeba8fbd066138a9d6b98bb5683c18f449 (patch) | |
| tree | 071eeb0ebd1d59f462d944555ad29d99c30a1423 /main/src/cgeo/geocaching/VisitCacheActivity.java | |
| parent | 7aad1b422c9a06bf82283c5069be74b5fafea351 (diff) | |
| parent | ab6f4537d74b228ca2620abba571f5ae8bb71706 (diff) | |
| download | cgeo-d6e525eeba8fbd066138a9d6b98bb5683c18f449.zip cgeo-d6e525eeba8fbd066138a9d6b98bb5683c18f449.tar.gz cgeo-d6e525eeba8fbd066138a9d6b98bb5683c18f449.tar.bz2 | |
Merge remote-tracking branch 'origin/release'
Diffstat (limited to 'main/src/cgeo/geocaching/VisitCacheActivity.java')
| -rw-r--r-- | main/src/cgeo/geocaching/VisitCacheActivity.java | 176 |
1 files changed, 69 insertions, 107 deletions
diff --git a/main/src/cgeo/geocaching/VisitCacheActivity.java b/main/src/cgeo/geocaching/VisitCacheActivity.java index dce0fbf..cac0f89 100644 --- a/main/src/cgeo/geocaching/VisitCacheActivity.java +++ b/main/src/cgeo/geocaching/VisitCacheActivity.java @@ -12,6 +12,7 @@ import cgeo.geocaching.network.Parameters; import cgeo.geocaching.twitter.Twitter; import cgeo.geocaching.ui.Formatter; import cgeo.geocaching.ui.dialog.DateDialog; +import cgeo.geocaching.utils.AsyncTaskWithProgress; import cgeo.geocaching.utils.Log; import cgeo.geocaching.utils.LogTemplateProvider; import cgeo.geocaching.utils.LogTemplateProvider.LogContext; @@ -20,17 +21,15 @@ import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; +import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.Dialog; -import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.Intent; import android.net.Uri; import android.os.Bundle; -import android.os.Handler; -import android.os.Message; import android.support.v4.app.LoaderManager; import android.support.v4.content.Loader; import android.util.SparseArray; @@ -67,7 +66,6 @@ public class VisitCacheActivity extends AbstractLoggingActivity implements DateD private LayoutInflater inflater = null; private Geocache cache = null; - private ProgressDialog waitDialog = null; private String cacheid = null; private String geocode = null; private String text = null; @@ -112,7 +110,6 @@ public class VisitCacheActivity extends AbstractLoggingActivity implements DateD viewstates = Login.getViewstates(page); trackables = GCParser.parseTrackableLog(page); possibleLogTypes = GCParser.parseTypes(page); - possibleLogTypes.remove(LogType.UPDATE_COORDINATES); if (possibleLogTypes.isEmpty()) { showErrorLoadingData(); @@ -240,45 +237,9 @@ public class VisitCacheActivity extends AbstractLoggingActivity implements DateD return res.getString(R.string.log_post_rate) + " " + ratingTextValue(rating) + "*"; } - private final Handler postLogHandler = new Handler() { - - @Override - public void handleMessage(final Message msg) { - if (waitDialog != null) { - waitDialog.dismiss(); - } - - final StatusCode error = (StatusCode) msg.obj; - if (error == StatusCode.NO_ERROR) { - showToast(res.getString(R.string.info_log_posted)); - // No need to save the log when quitting if it has been posted. - text = currentLogText(); - finish(); - } else if (error == StatusCode.LOG_SAVED) { - showToast(res.getString(R.string.info_log_saved)); - - if (waitDialog != null) { - waitDialog.dismiss(); - } - - finish(); - } else { - showToast(error.getErrorString(res)); - } - } - }; - - public VisitCacheActivity() { - super("c:geo-log"); - } - @Override public void onCreate(final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setTheme(); - setContentView(R.layout.visit); - setTitle(res.getString(R.string.log_new_log)); + super.onCreate(savedInstanceState, R.layout.visit); // Get parameters from intent and basic cache information from database final Bundle extras = getIntent().getExtras(); @@ -549,81 +510,79 @@ public class VisitCacheActivity extends AbstractLoggingActivity implements DateD private class PostListener implements View.OnClickListener { @Override public void onClick(View arg0) { - waitDialog = ProgressDialog.show(VisitCacheActivity.this, null, - res.getString(StringUtils.isBlank(imageUri.getPath()) ? R.string.log_saving : R.string.log_saving_and_uploading), true); - waitDialog.setCancelable(true); - - final Thread thread = new PostLogThread(postLogHandler, currentLogText()); - thread.start(); + final String message = res.getString(StringUtils.isBlank(imageUri.getPath()) ? + R.string.log_saving : + R.string.log_saving_and_uploading); + new Poster(VisitCacheActivity.this, message).execute(currentLogText()); } } - private class PostLogThread extends Thread { - - private final Handler handler; - private final String log; + private class Poster extends AsyncTaskWithProgress<String, StatusCode> { - public PostLogThread(Handler handlerIn, String logIn) { - super("Post log"); - handler = handlerIn; - log = logIn; + public Poster(final Activity activity, final String progressMessage) { + super(activity, null, progressMessage, true); } @Override - public void run() { - final StatusCode status = postLogFn(log); - handler.sendMessage(handler.obtainMessage(0, status)); - } - } - - public StatusCode postLogFn(String log) { - - StatusCode result = StatusCode.LOG_POST_ERROR; - - try { - - final ImmutablePair<StatusCode, String> logResult = GCParser.postLog(geocode, cacheid, viewstates, typeSelected, - date.get(Calendar.YEAR), (date.get(Calendar.MONTH) + 1), date.get(Calendar.DATE), - log, trackables); - - result = logResult.left; - - if (logResult.left == StatusCode.NO_ERROR) { - final LogEntry logNow = new LogEntry(date, typeSelected, log); - - cache.getLogs().add(0, logNow); - - if (typeSelected == LogType.FOUND_IT || typeSelected == LogType.ATTENDED) { - cache.setFound(true); + protected StatusCode doInBackgroundInternal(final String[] logTexts) { + final String log = logTexts[0]; + try { + final ImmutablePair<StatusCode, String> postResult = GCParser.postLog(geocode, cacheid, viewstates, typeSelected, + date.get(Calendar.YEAR), (date.get(Calendar.MONTH) + 1), date.get(Calendar.DATE), + log, trackables); + + if (postResult.left == StatusCode.NO_ERROR) { + final LogEntry logNow = new LogEntry(date, typeSelected, log); + + cache.getLogs().add(0, logNow); + + if (typeSelected == LogType.FOUND_IT || typeSelected == LogType.ATTENDED) { + cache.setFound(true); + } + + cgData.saveChangedCache(cache); + cgData.clearLogOffline(geocode); + + if (typeSelected == LogType.FOUND_IT) { + if (tweetCheck.isChecked() && tweetBox.getVisibility() == View.VISIBLE) { + Twitter.postTweetCache(geocode); + } + GCVote.setRating(cache, rating); + } + + if (StringUtils.isNotBlank(imageUri.getPath())) { + ImmutablePair<StatusCode, String> imageResult = GCParser.uploadLogImage(postResult.right, imageCaption, imageDescription, imageUri); + final String uploadedImageUrl = imageResult.right; + if (StringUtils.isNotEmpty(uploadedImageUrl)) { + logNow.addLogImage(new Image(uploadedImageUrl, imageCaption, imageDescription)); + cgData.saveChangedCache(cache); + } + return imageResult.left; + } } - cgData.saveChangedCache(cache); - } - - if (logResult.left == StatusCode.NO_ERROR) { - cgData.clearLogOffline(geocode); + return postResult.left; + } catch (Exception e) { + Log.e("cgeovisit.postLogFn", e); } - if (logResult.left == StatusCode.NO_ERROR && typeSelected == LogType.FOUND_IT && Settings.isUseTwitter() - && Settings.isTwitterLoginValid() - && tweetCheck.isChecked() && tweetBox.getVisibility() == View.VISIBLE) { - Twitter.postTweetCache(geocode); - } - - if (logResult.left == StatusCode.NO_ERROR && typeSelected == LogType.FOUND_IT && Settings.isGCvoteLogin()) { - GCVote.setRating(cache, rating); - } + return StatusCode.LOG_POST_ERROR; + } - if (logResult.left == StatusCode.NO_ERROR && StringUtils.isNotBlank(imageUri.getPath())) { - result = GCParser.uploadLogImage(logResult.right, imageCaption, imageDescription, imageUri); + @Override + protected void onPostExecuteInternal(final StatusCode status) { + if (status == StatusCode.NO_ERROR) { + showToast(res.getString(R.string.info_log_posted)); + // No need to save the log when quitting if it has been posted. + text = currentLogText(); + finish(); + } else if (status == StatusCode.LOG_SAVED) { + showToast(res.getString(R.string.info_log_saved)); + finish(); + } else { + showToast(status.getErrorString(res)); } - - return result; - } catch (Exception e) { - Log.e("cgeovisit.postLogFn", e); } - - return StatusCode.LOG_POST_ERROR; } private void saveLog(final boolean force) { @@ -677,16 +636,19 @@ public class VisitCacheActivity extends AbstractLoggingActivity implements DateD } private void selectLogType() { + // use a local copy of the possible types, as that one might be modified in the background by the loader + final ArrayList<LogType> possible = new ArrayList<LogType>(possibleLogTypes); + Builder alert = new AlertDialog.Builder(this); - String[] choices = new String[possibleLogTypes.size()]; + String[] choices = new String[possible.size()]; for (int i = 0; i < choices.length; i++) { - choices[i] = possibleLogTypes.get(i).getL10n(); + choices[i] = possible.get(i).getL10n(); } - alert.setSingleChoiceItems(choices, possibleLogTypes.indexOf(typeSelected), new OnClickListener() { + alert.setSingleChoiceItems(choices, possible.indexOf(typeSelected), new OnClickListener() { @Override public void onClick(DialogInterface dialog, int position) { - setType(possibleLogTypes.get(position)); + setType(possible.get(position)); dialog.dismiss(); } }); |
