/*******************************************************************************
* 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 java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import fr.gotorennes.util.LocationUtils;
public abstract class AbstractActivity extends Activity {
public static final String ACTION_LOGOUT = "fr.gotorennes.LOGOUT";
protected double latitude;
protected double longitude;
protected Calendar calendrier = Calendar.getInstance();
public static final int DATE_DIALOG_ID = 0;
public static final int TIME_DIALOG_ID = 1;
protected GoToRennes goToRennes;
protected LayoutInflater layoutInflater;
protected BroadcastReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layoutInflater = this.getLayoutInflater();
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_LOGOUT);
registerReceiver(receiver, intentFilter);
final ProgressDialog waitDialog = ProgressDialog.show(this,
getString(R.string.titre), getString(R.string.chargement),
true, true);
final AsyncTask<Void, Void, Boolean> initTask = new AsyncTask<Void, Void, Boolean>() {
private String message = null;
@Override
protected Boolean doInBackground(Void... params) {
try {
goToRennes = GoToRennes.getInstance(AbstractActivity.this, waitDialog);
} catch (Throwable ex) {
Log.e("GoToRennes", "Error init go2Rennes " + ex.getMessage());
message = ex.getMessage();
return false;
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
waitDialog.dismiss();
if (result == null || !result) {
showError(getString(R.string.erreurInitialisation) + " : " + message, true);
} else {
init(getIntent());
goToRennes.track(getTrackingName());
}
}
};
waitDialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
if (initTask != null) {
initTask.cancel(true);
}
finish();
}
});
initTask.execute();
}
protected abstract void init(Intent intent);
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
init(intent);
}
protected abstract String getTrackingName();
protected void update() {
final ProgressDialog waitDialog = ProgressDialog.show(this,
getString(R.string.titre), getString(R.string.updateDatabase),
true, true);
final AsyncTask<Void, Void, Boolean> initTask = new AsyncTask<Void, Void, Boolean>() {
private String message = null;
@Override
protected Boolean doInBackground(Void... params) {
try {
goToRennes.getBusDao().update(waitDialog);
} catch (Throwable ex) {
Log.e("GoToRennes", "Error updating go2Rennes " + ex.getMessage());
message = ex.getMessage();
return false;
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
waitDialog.dismiss();
if (result == null || !result) {
showError(getString(R.string.erreurMiseAjour) + " : " + message);
}
else {
showError(getString(R.string.databaseUpdated));
}
}
};
waitDialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
if (initTask != null) {
initTask.cancel(true);
}
}
});
initTask.execute();
}
protected void logout() {
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(ACTION_LOGOUT);
sendBroadcast(broadcastIntent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.accueil:
goToRennes.trackEvent("Menu", "Accueil");
Intent intentAccueil = new Intent(getApplicationContext(),
GoToRennesActivity.class);
intentAccueil.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentAccueil);
return true;
case R.id.update:
goToRennes.trackEvent("Menu", "Update");
update();
return true;
case R.id.quitter:
goToRennes.trackEvent("Menu", "Quitter");
logout();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
if (isTaskRoot()) {
if (goToRennes != null)
goToRennes.stop();
}
if (adView != null) {
adView.destroy();
}
}
@Override
protected void onPause() {
super.onPause();
LocationUtils.desactiveLocation(this);
}
private AdView adView = null;
@Override
protected void onResume() {
super.onResume();
LocationUtils.activeLocation(this);
ViewGroup layout = (ViewGroup) findViewById(R.id.adLayout);
if (layout != null && adView == null) {
MobileAds.initialize(getApplicationContext(), "ca-app-pub-5396385527314971/1328911094");
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-5396385527314971/1328911094");
layout.addView(adView);
}
if (adView != null) {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
adView.loadAd(adRequest);
}
}
protected int getMapIcon() {
return 0;
}
protected void addCalendrier(Intent intent) {
calendrier = Calendar.getInstance();
calendrier.setTime(new Date(intent.getLongExtra("date",
System.currentTimeMillis())));
TextView date = (TextView) findViewById(R.id.date);
date.setText(DateFormat.format("dd/MM/yyyy", calendrier));
date.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
TextView time = (TextView) findViewById(R.id.time);
time.setText(DateFormat.format("kk:mm", calendrier));
time.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calendrier.set(Calendar.YEAR, year);
calendrier.set(Calendar.MONTH, monthOfYear);
calendrier.set(Calendar.DAY_OF_MONTH, dayOfMonth);
TextView date = (TextView) findViewById(R.id.date);
date.setText(DateFormat.format("dd/MM/yyyy", calendrier));
onCalendrierChanged();
}
};
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendrier.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendrier.set(Calendar.MINUTE, minute);
TextView time = (TextView) findViewById(R.id.time);
time.setText(DateFormat.format("kk:mm", calendrier));
onCalendrierChanged();
}
};
protected void onCalendrierChanged() {
// Nothing
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener,
calendrier.get(Calendar.YEAR),
calendrier.get(Calendar.MONTH),
calendrier.get(Calendar.DAY_OF_MONTH));
case TIME_DIALOG_ID:
return new TimePickerDialog(this, mTimeSetListener,
calendrier.get(Calendar.HOUR_OF_DAY),
calendrier.get(Calendar.MINUTE), true);
}
return null;
}
public void showMap(View view) {
showMap(latitude, longitude);
}
public void showMap(double latitude, double longitude) {
Intent intentMap = new Intent(getApplicationContext(),
ProximityMapActivity.class);
intentMap.putExtra("latitude", latitude);
intentMap.putExtra("longitude", longitude);
intentMap.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentMap);
}
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();
}
public static String getFormattedDistance(
fr.gotorennes.AbstractMapActivity.Location depart,
fr.gotorennes.AbstractMapActivity.Location arrivee) {
return AbstractMapActivity.getFormattedDistance(depart, arrivee);
}
protected String getFormattedDistance(double latitude, double longitude) {
return AbstractMapActivity.getFormattedDistance(this.latitude,
this.longitude, latitude, longitude);
}
protected String getMyLocationFormattedDistance(double latitude,
double longitude) {
Location location = LocationUtils.getLocation(this);
if (location != null) {
return AbstractMapActivity.getFormattedDistance(latitude,
longitude, location.getLatitude(), location.getLongitude());
} else {
return "--";
}
}
protected double getLatitude() {
Location location = LocationUtils.getLocation(this);
if (location != null) {
return location.getLatitude();
}
return 0;
}
protected double getLongitude() {
Location location = LocationUtils.getLocation(this);
if (location != null) {
return location.getLongitude();
}
return 0;
}
public void showProximite(View view) {
Intent intent = new Intent(this, ProximiteResultActivity.class);
intent.putExtra("latitude", latitude);
intent.putExtra("longitude", longitude);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}