Fixing Toast calls outside the UI thread.

This commit is contained in:
Rodrigo Damazio
2011-06-03 01:24:42 -03:00
parent 84835428ba
commit cc4be23802
3 changed files with 41 additions and 27 deletions
@@ -133,7 +133,12 @@ public class ExportAllTracks {
Log.i(Constants.TAG, "ExportAllTracks: Releasing wake lock."); Log.i(Constants.TAG, "ExportAllTracks: Releasing wake lock.");
} }
Log.i(Constants.TAG, "ExportAllTracks: Done"); Log.i(Constants.TAG, "ExportAllTracks: Done");
Toast.makeText(activity, R.string.export_done, Toast.LENGTH_SHORT).show(); activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, R.string.export_done, Toast.LENGTH_SHORT).show();
}
});
} }
private void makeProgressDialog(final int trackCount) { private void makeProgressDialog(final int trackCount) {
@@ -193,8 +198,13 @@ public class ExportAllTracks {
if (!writer.wasSuccess()) { if (!writer.wasSuccess()) {
// Abort the whole export on the first error. // Abort the whole export on the first error.
int error = writer.getErrorMessage(); final int error = writer.getErrorMessage();
Toast.makeText(activity, error, Toast.LENGTH_LONG).show(); activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, error, Toast.LENGTH_LONG).show();
}
});
return; return;
} }
} }
@@ -51,6 +51,7 @@ import android.widget.ImageButton;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import java.util.EnumSet; import java.util.EnumSet;
@@ -464,21 +465,21 @@ public class MapActivity extends com.google.android.maps.MapActivity
@Override @Override
public void onProviderStateChange(ProviderState state) { public void onProviderStateChange(ProviderState state) {
final int messageId; final int messageId;
final boolean bindClick; final boolean isGpsDisabled;
switch (state) { switch (state) {
case DISABLED: case DISABLED:
messageId = R.string.status_enable_gps; messageId = R.string.status_enable_gps;
bindClick = true; isGpsDisabled = true;
break; break;
case NO_FIX: case NO_FIX:
case BAD_FIX: case BAD_FIX:
messageId = R.string.wait_for_fix; messageId = R.string.wait_for_fix;
bindClick = false; isGpsDisabled = false;
break; break;
case GOOD_FIX: case GOOD_FIX:
// Nothing to show. // Nothing to show.
messageId = -1; messageId = -1;
bindClick = false; isGpsDisabled = false;
break; break;
default: default:
throw new IllegalArgumentException("Unexpected state: " + state); throw new IllegalArgumentException("Unexpected state: " + state);
@@ -491,7 +492,13 @@ public class MapActivity extends com.google.android.maps.MapActivity
messageText.setText(messageId); messageText.setText(messageId);
messagePane.setVisibility(View.VISIBLE); messagePane.setVisibility(View.VISIBLE);
if (bindClick) { if (isGpsDisabled) {
// Give a warning about this state.
Toast.makeText(MapActivity.this,
R.string.error_no_gps_location_provider,
Toast.LENGTH_LONG).show();
// Make clicking take the user to the location settings.
messagePane.setOnClickListener(MapActivity.this); messagePane.setOnClickListener(MapActivity.this);
} else { } else {
messagePane.setOnClickListener(null); messagePane.setOnClickListener(null);
@@ -654,9 +654,6 @@ public class TrackDataHub {
final TrackDataListener.ProviderState state; final TrackDataListener.ProviderState state;
if (!hasProviderEnabled) { if (!hasProviderEnabled) {
state = ProviderState.DISABLED; state = ProviderState.DISABLED;
// Give a global warning about this state.
Toast.makeText(context, R.string.error_no_gps_location_provider, Toast.LENGTH_LONG).show();
} else if (!hasFix) { } else if (!hasFix) {
state = ProviderState.NO_FIX; state = ProviderState.NO_FIX;
} else if (!hasGoodFix) { } else if (!hasGoodFix) {