package it.paninoonto.activity;
import it.paninoonto.R;
import it.paninoonto.util.AndroidUtil;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
/**
*
* This activity can be used to get the current location, acquiring it from the
* network or from the GPS.
* <p/>
* When started, this activity shows a chooser dialog, from which the user can
* choose whether to get the location from the network or from the GPS. The
* activity then ensures the network/GPS is turned on and acquires the location.
* If succesfully acquired, the location is returned.
* <p/>
*
* To start this activity:
* <pre>
* Intent i = new Intent(this, AcquireLocationActivity.class);
* startActivityForResult(i, REQUEST_ACQUIRE_LOCATION);
* </pre>
*
* To get the result:
* <pre>
* protected void onActivityResult(int requestCode, int resultCode, Intent data) {
* if (resultCode != RESULT_OK) {
* return;
* }
* if(requestCode == REQUEST_ACQUIRE_LOCATION){
* Location location = (Location) data.getParcelableExtra(AcquireLocationActivity.RESULT_LOCATION);
* ...
* }
* }
* </pre>
*
* @author Mirko Luchi
*
*/
public class AcquireLocationActivity extends ChooserDialogActivity {
/**
* The key to which the returned data (a {@link Location} object) is
* associated.
*/
public static final String RESULT_LOCATION = "location";
/**
* The logger tag.
*/
private static final String TAG = AcquireLocationActivity.class.getName();
/**
* The index of the "get location from network" choice.
*/
private static final int CHOICE_NETWORK = 0;
/**
* The index of the "get location from GPS" choice.
*/
private static final int CHOICE_GPS = 1;
/**
* The id of the progress dialog which is shown while the location is being
* required.
*/
private static final int DIALOG_PROGRESS = 0;
/**
* The id of the dialog which asks the user to enable the Network.
*/
private static final int DIALOG_ENABLE_NETWORK = 1;
/**
* The id of the dialog which asks the user to enable the GPS.
*/
private static final int DIALOG_ENABLE_GPS = 2;
/**
* The id of the alert dialog which warns that there is not location
* settings page.
*/
private static final int DIALOG_NO_LOCATION_SETTINGS = 3;
/**
* The location manager.
*/
private LocationManager locationManager;
/**
* The listener of the location manager events.
*/
private final LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// Notify that location has been obtained
onLocationObtained(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Customize dialog
Resources r = getResources();
setDialogTitle(R.string.locationDialog_title);
setDialogChoices(new CharSequence[] {
r.getString(R.string.locationDialog_networkItem),
r.getString(R.string.locationDialog_gpsItem) });
// Get the location manager
this.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
@Override
protected Dialog onCreateDialog(int id) {
Resources r = getResources();
switch (id) {
case DIALOG_PROGRESS:
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog
.setMessage(r.getString(R.string.gettingLocation));
progressDialog.setCancelable(true);
progressDialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// Stop listening to location updates
locationManager.removeUpdates(locationListener);
// Cancel this activity
setResult(RESULT_CANCELED);
finish();
}
});
return progressDialog;
case DIALOG_ENABLE_NETWORK:
AlertDialog networkDialog = new AlertDialog.Builder(this)
.setTitle(R.string.enableNetwork_title)
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage(R.string.enableNetwork_message)
.setCancelable(true)
.setPositiveButton(R.string.ok,
new Dialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
enableGPSOrNetwork();
}
})
.setNegativeButton(R.string.cancel,
new Dialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setResult(RESULT_CANCELED);
finish();
}
})
.create();
return networkDialog;
case DIALOG_ENABLE_GPS:
AlertDialog gpsDialog = new AlertDialog.Builder(this)
.setTitle(R.string.enableGPS_title)
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage(R.string.enableGPS_message)
.setCancelable(true)
.setPositiveButton(R.string.ok,
new Dialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
enableGPSOrNetwork();
}
})
.setNegativeButton(R.string.cancel,
new Dialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setResult(RESULT_CANCELED);
finish();
}
})
.create();
return gpsDialog;
case DIALOG_NO_LOCATION_SETTINGS:
return new AlertDialog.Builder(this)
.setTitle(R.string.error)
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage(R.string.error_cropNotAvailable)
.setCancelable(false)
.setPositiveButton(R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setResult(RESULT_CANCELED);
finish();
}
})
.create();
default:
return null;
}
}
@Override
protected void onChoice(int choice) {
switch (choice) {
case CHOICE_NETWORK:
getLocationFromNetwork();
break;
case CHOICE_GPS:
getLocationFromGps();
break;
}
}
/**
* Get the current location from the network. If network is not enabled, ask
* the user to enable it.
*/
private void getLocationFromNetwork() {
Log.d(TAG, "Getting location from network...");
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
// Show a progress dialog
showDialog(DIALOG_PROGRESS);
// Start requiring updates
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
0,
0,
locationListener);
} else {
String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
if (AndroidUtil.isIntentAvailable(this, action)) {
// Start the settings pane so that user can enable Network
Intent intent = new Intent(action);
startActivity(intent);
} else {
showDialog(DIALOG_ENABLE_NETWORK);
}
}
}
/**
* Get the current location from the GPS. If GPS is not enabled, ask the
* user to enable it.
*/
private void getLocationFromGps() {
Log.d(TAG, "Getting location from GPS...");
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
// Show a progress dialog
showDialog(DIALOG_PROGRESS);
// Start requiring updates
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
locationListener);
} else {
showDialog(DIALOG_ENABLE_GPS);
}
}
/**
* Start the location source settings page, where the user can enable
* GPS/Network.
*/
private void enableGPSOrNetwork() {
String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
if (AndroidUtil.isIntentAvailable(this, action)) {
// Start the settings pane so that user can enable Network
Intent intent = new Intent(action);
startActivity(intent);
} else {
showDialog(DIALOG_NO_LOCATION_SETTINGS);
}
}
/**
* Once the location has been obtained, return it.
*
* @param location The location.
*/
private void onLocationObtained(Location location) {
Log.d(TAG, "Location obtained: " + location);
// Stop the progress dialog
dismissDialog(DIALOG_PROGRESS);
// Stop listening for location updates
locationManager.removeUpdates(locationListener);
// Return location
Intent data = new Intent();
data.putExtra(RESULT_LOCATION, location);
setResult(RESULT_OK, data);
finish();
}
}
|