/*******************************************************************************
* Copyright (c) 2011 Michel DAVID mimah35-at-gmail.com
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package fr.gotorennes;
import android.app.Activity;
import android.app.AlertDialog;
import android.appwidget.AppWidgetManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import fr.gotorennes.util.BackgroundTask;
public abstract class AbstractConfigurationActivity extends Activity {
protected GoToRennes goToRennes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BackgroundTask<Boolean> backgroundInit = new BackgroundTask<Boolean>() {
@Override
protected Boolean execute() {
try {
goToRennes = GoToRennes.getInstance(AbstractConfigurationActivity.this, null);
} catch (Exception ex) {
return false;
}
return true;
}
@Override
protected void callback(Boolean result) {
if (result == null || !result) {
showError(getString(R.string.erreurInitialisation), true);
} else {
init();
goToRennes.track(getTrackingName());
}
}
};
backgroundInit.start(this);
}
protected abstract void init();
protected abstract String getTrackingName();
@Override
protected void onDestroy() {
super.onDestroy();
if (goToRennes != null)
goToRennes.stop();
}
protected int getWidgetId() {
Intent intent = getIntent();
Bundle extras = intent.getExtras();
int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
if (extras != null) {
appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
}
Log.i("Go2Rennes", "WidgetId" + appWidgetId);
return appWidgetId;
}
protected void showError(String message) {
showError(message, false);
}
protected void showError(String message, final boolean finish) {
AlertDialog.Builder build = new AlertDialog.Builder(this);
build.setMessage(message);
build.setPositiveButton("Ok", new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if (finish)
finish();
}
});
build.create().show();
}
}