/* * Copyright (c) 2013, Will Szumski * Copyright (c) 2013, Doug Szumski * * This file is part of Cyclismo. * * Cyclismo is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Cyclismo is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Cyclismo. If not, see <http://www.gnu.org/licenses/>. */ /* * Copyright 2012 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package org.cowboycoders.cyclismo.io.sendtogoogle; import android.os.AsyncTask; /** * The abstract class for AsyncTasks sending a track to Google. * * @author Jimmy Shih */ public abstract class AbstractSendAsyncTask extends AsyncTask<Void, Integer, Boolean> { /** * The activity associated with this AsyncTask. */ private AbstractSendActivity activity; /** * True if the AsyncTask result is success. */ private boolean success; /** * True if the AsyncTask has completed. */ private boolean completed; /** * True if can retry the AsyncTask. */ private boolean canRetry; /** * Creates an AsyncTask. * * @param activity the activity currently associated with this AsyncTask */ public AbstractSendAsyncTask(AbstractSendActivity activity) { this.activity = activity; success = false; completed = false; canRetry = true; } /** * Sets the current activity associated with this AyncTask. * * @param activity the current activity, can be null */ public void setActivity(AbstractSendActivity activity) { this.activity = activity; if (completed && activity != null) { activity.onAsyncTaskCompleted(success); } } @Override protected void onPreExecute() { activity.showProgressDialog(); } @Override protected Boolean doInBackground(Void... params) { try { return performTask(); } finally { closeConnection(); } } @Override protected void onProgressUpdate(Integer... values) { if (activity != null) { activity.setProgressDialogValue(values[0]); } } @Override protected void onPostExecute(Boolean result) { success = result; completed = true; if (success) { saveResult(); } if (activity != null) { activity.onAsyncTaskCompleted(success); } } /** * Retries the task. First, invalidates the auth token. If can retry, invokes * {@link #performTask()}. Returns false if cannot retry. * * @return the result of the retry. */ protected boolean retryTask() { if (isCancelled()) { return false; } invalidateToken(); if (canRetry) { canRetry = false; return performTask(); } return false; } /** * Closes any AsyncTask connection. */ protected abstract void closeConnection(); /** * Saves any AsyncTask result. */ protected abstract void saveResult(); /** * Performs the AsyncTask. * * @return true if success */ protected abstract boolean performTask(); /** * Invalidates the auth token. */ protected abstract void invalidateToken(); }