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
@@ -1,12 +1,12 @@
/* /*
* Copyright 2009 Google Inc. * Copyright 2009 Google Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not * 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 * use this file except in compliance with the License. You may obtain a copy of
* the License at * the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -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;
} }
} }
@@ -1,12 +1,12 @@
/* /*
* Copyright 2008 Google Inc. * Copyright 2008 Google Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not * 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 * use this file except in compliance with the License. You may obtain a copy of
* the License at * the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -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;
@@ -336,9 +337,9 @@ public class MapActivity extends com.google.android.maps.MapActivity
if (trackSelected) { if (trackSelected) {
busyPane.setVisibility(View.VISIBLE); busyPane.setVisibility(View.VISIBLE);
zoomMapToBoundaries(track); zoomMapToBoundaries(track);
mapOverlay.setShowEndMarker(!isRecording); mapOverlay.setShowEndMarker(!isRecording);
busyPane.setVisibility(View.GONE); busyPane.setVisibility(View.GONE);
} }
@@ -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);
@@ -1,12 +1,12 @@
/* /*
* Copyright 2011 Google Inc. * Copyright 2011 Google Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not * 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 * use this file except in compliance with the License. You may obtain a copy of
* the License at * the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -446,7 +446,7 @@ public class TrackDataHub {
/** /**
* Reloads all track data received so far into the specified listeners. * Reloads all track data received so far into the specified listeners.
* *
* Assumes it's called from a block that synchronizes on {@link #listeners}. * Assumes it's called from a block that synchronizes on {@link #listeners}.
*/ */
private void reloadDataForListener(final ListenerRegistration registration) { private void reloadDataForListener(final ListenerRegistration registration) {
@@ -615,7 +615,7 @@ public class TrackDataHub {
@Override @Override
public void run() { public void run() {
Set<TrackDataListener> displayListeners = Set<TrackDataListener> displayListeners =
getListenersFor(ListenerDataType.DISPLAY_PREFERENCES); getListenersFor(ListenerDataType.DISPLAY_PREFERENCES);
for (TrackDataListener listener : displayListeners) { for (TrackDataListener listener : displayListeners) {
// TODO: Do the reloading just once for all interested listeners // TODO: Do the reloading just once for all interested listeners
@@ -636,7 +636,7 @@ public class TrackDataHub {
runInListenerThread(new Runnable() { runInListenerThread(new Runnable() {
@Override @Override
public void run() { public void run() {
Set<TrackDataListener> displayListeners = getListenersFor(ListenerDataType.DISPLAY_PREFERENCES); Set<TrackDataListener> displayListeners = getListenersFor(ListenerDataType.DISPLAY_PREFERENCES);
for (TrackDataListener listener : displayListeners) { for (TrackDataListener listener : displayListeners) {
if (listener.onUnitsChanged(useMetricUnits)) { if (listener.onUnitsChanged(useMetricUnits)) {
@@ -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) {
@@ -851,7 +848,7 @@ public class TrackDataHub {
if (!LocationUtils.isValidLocation(waypoint.getLocation())) { if (!LocationUtils.isValidLocation(waypoint.getLocation())) {
continue; continue;
} }
for (TrackDataListener listener : listeners) { for (TrackDataListener listener : listeners) {
listener.onNewWaypoint(waypoint); listener.onNewWaypoint(waypoint);
} }
@@ -862,7 +859,7 @@ public class TrackDataHub {
cursor.close(); cursor.close();
} }
} }
for (TrackDataListener listener : listeners) { for (TrackDataListener listener : listeners) {
listener.onNewWaypointsDone(); listener.onNewWaypointsDone();
} }