Address Bart's comments and more dialog clean up.

This commit is contained in:
Sandor Dornbush
2010-11-04 17:44:08 -07:00
parent 4bc5aa050c
commit 26c381a4c4
4 changed files with 109 additions and 106 deletions
@@ -24,6 +24,7 @@ import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager.BadTokenException;
/**
* A class to handle a dialog related events for My Tracks.
@@ -164,4 +165,67 @@ public class DialogManager {
public SendToGoogleDialog getSendToGoogleDialog() {
return sendToGoogleDialog;
}
/**
* Shows a dialog with the given message.
* Does it on the UI thread.
*
* @param success if true, displays an info icon/title, otherwise an error
* icon/title
* @param message resource string id
*/
public void showMessageDialog(final int message, final boolean success) {
activity.runOnUiThread(new Runnable() {
public void run() {
AlertDialog dialog = null;
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(activity.getString(message));
builder.setNegativeButton(activity.getString(R.string.ok), null);
builder.setIcon(success ? android.R.drawable.ic_dialog_info :
android.R.drawable.ic_dialog_alert);
builder.setTitle(success ? R.string.success : R.string.error);
dialog = builder.create();
dialog.show();
}
});
}
/**
* Just like showDialog, but will catch a BadTokenException that sometimes
* (very rarely) gets thrown. This might happen if the user hits the "back"
* button immediately after sending tracks to google.
*
* @param id the dialog id
*/
public void showDialogSafely(final int id) {
activity.runOnUiThread(new Runnable() {
public void run() {
try {
activity.showDialog(id);
} catch (BadTokenException e) {
Log.w(MyTracksConstants.TAG,
"Could not display dialog with id " + id, e);
} catch (IllegalStateException e) {
Log.w(MyTracksConstants.TAG,
"Could not display dialog with id " + id, e);
}
}
});
}
/**
* Dismisses the progress dialog if it is showing. Executed on the UI thread.
*/
public void dismissDialogSafely(final int id) {
activity.runOnUiThread(new Runnable() {
public void run() {
try {
activity.dismissDialog(id);
} catch (IllegalArgumentException e) {
// This will be thrown if this dialog was not shown before.
}
}
});
}
}
@@ -34,7 +34,6 @@ import com.google.android.apps.mytracks.io.SendToMyMaps.OnSendCompletedListener;
import com.google.android.apps.mytracks.io.TempFileCleaner;
import com.google.android.apps.mytracks.io.TrackWriter;
import com.google.android.apps.mytracks.io.TrackWriterFactory;
import com.google.android.apps.mytracks.io.SendToMyMaps.OnSendCompletedListener;
import com.google.android.apps.mytracks.io.TrackWriterFactory.TrackFileFormat;
import com.google.android.apps.mytracks.services.ITrackRecordingService;
import com.google.android.apps.mytracks.services.StatusAnnouncerFactory;
@@ -71,7 +70,6 @@ import android.view.View;
import android.view.Window;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager.BadTokenException;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.Toast;
@@ -104,14 +102,6 @@ public class MyTracks extends TabActivity implements OnTouchListener,
private ChartActivity chartActivity;
public ChartActivity getChartActivity() {
return chartActivity;
}
public void setChartActivity(ChartActivity chartActivity) {
this.chartActivity = chartActivity;
}
/*
* Authentication
*/
@@ -373,7 +363,7 @@ public class MyTracks extends TabActivity implements OnTouchListener,
protected void onStop() {
Log.d(MyTracksConstants.TAG, "MyTracks.onStop");
// Clean up any temporary track files.
(new TempFileCleaner()).clean();
TempFileCleaner.clean();
super.onStop();
}
@@ -450,17 +440,17 @@ public class MyTracks extends TabActivity implements OnTouchListener,
authenticate(results, MyTracksConstants.SEND_TO_GOOGLE,
MyMapsConstants.MAPSHOP_SERVICE);
} else {
dismissDialogSafely(DIALOG_PROGRESS);
dialogManager.dismissDialogSafely(DIALOG_PROGRESS);
}
break;
}
case MyTracksConstants.GET_LOGIN: {
if (resultCode == RESULT_OK && auth != null) {
if (!auth.authResult(resultCode, results)) {
dismissDialogSafely(DIALOG_PROGRESS);
dialogManager.dismissDialogSafely(DIALOG_PROGRESS);
}
} else {
dismissDialogSafely(DIALOG_PROGRESS);
dialogManager.dismissDialogSafely(DIALOG_PROGRESS);
}
break;
}
@@ -516,7 +506,7 @@ public class MyTracks extends TabActivity implements OnTouchListener,
authenticate(results,
MyTracksConstants.AUTHENTICATE_TO_TRIX, "writely");
} else {
dismissDialogSafely(DIALOG_PROGRESS);
dialogManager.dismissDialogSafely(DIALOG_PROGRESS);
}
break;
}
@@ -527,7 +517,7 @@ public class MyTracks extends TabActivity implements OnTouchListener,
R.string.progress_message_authenticating_docs);
authenticate(results, MyTracksConstants.SEND_TO_DOCS, "wise");
} else {
dismissDialogSafely(DIALOG_PROGRESS);
dialogManager.dismissDialogSafely(DIALOG_PROGRESS);
}
break;
}
@@ -542,7 +532,7 @@ public class MyTracks extends TabActivity implements OnTouchListener,
Runnable onCompletion = new Runnable() {
public void run() {
setProgressValue(100);
dismissDialogSafely(DIALOG_PROGRESS);
dialogManager.dismissDialogSafely(DIALOG_PROGRESS);
runOnUiThread(new Runnable() {
public void run() {
sendToDocsMessage = sender.getStatusMessage();
@@ -555,13 +545,13 @@ public class MyTracks extends TabActivity implements OnTouchListener,
sender.setOnCompletion(onCompletion);
sender.run();
} else {
dismissDialogSafely(DIALOG_PROGRESS);
dialogManager.dismissDialogSafely(DIALOG_PROGRESS);
}
break;
}
case MyTracksConstants.SEND_TO_GOOGLE_DIALOG: {
shareRequested = false;
showDialogSafely(DIALOG_SEND_TO_GOOGLE);
dialogManager.showDialogSafely(DIALOG_SEND_TO_GOOGLE);
break;
}
case MyTracksConstants.SEND_TO_GOOGLE: {
@@ -602,7 +592,7 @@ public class MyTracks extends TabActivity implements OnTouchListener,
onActivityResult(MyTracksConstants.AUTHENTICATE_TO_DOCS,
RESULT_OK, new Intent());
} else {
dismissDialogSafely(DIALOG_PROGRESS);
dialogManager.dismissDialogSafely(DIALOG_PROGRESS);
runOnUiThread(new Runnable() {
public void run() {
handleMapsFinish();
@@ -619,7 +609,7 @@ public class MyTracks extends TabActivity implements OnTouchListener,
Handler handler = new Handler(handlerThread.getLooper());
handler.post(sender);
} else {
dismissDialogSafely(DIALOG_PROGRESS);
dialogManager.dismissDialogSafely(DIALOG_PROGRESS);
}
break;
}
@@ -649,7 +639,7 @@ public class MyTracks extends TabActivity implements OnTouchListener,
shareLinkToMyMap(selectedTrack.getMapId());
} else {
shareRequested = true;
showDialogSafely(DIALOG_SEND_TO_GOOGLE);
dialogManager.showDialogSafely(DIALOG_SEND_TO_GOOGLE);
}
}
break;
@@ -763,7 +753,7 @@ public class MyTracks extends TabActivity implements OnTouchListener,
}
private void importGpxFile(final String fileName) {
showDialogSafely(DIALOG_IMPORT_PROGRESS);
dialogManager.showDialogSafely(DIALOG_IMPORT_PROGRESS);
Thread t = new Thread() {
@Override
public void run() {
@@ -795,7 +785,7 @@ public class MyTracks extends TabActivity implements OnTouchListener,
// select last track from import file
setSelectedTrack(trackIdsImported[trackIdsImported.length - 1]);
} else {
MyTracks.this.showMessageDialog(message, false/* success */);
dialogManager.showMessageDialog(message, false/* success */);
}
} finally {
runOnUiThread(new Runnable() {
@@ -809,44 +799,6 @@ public class MyTracks extends TabActivity implements OnTouchListener,
t.start();
}
/**
* Just like showDialog, but will catch a BadTokenException that sometimes
* (very rarely) gets thrown. This might happen if the user hits the "back"
* button immediately after sending tracks to google.
*
* @param id the dialog id
*/
public void showDialogSafely(final int id) {
runOnUiThread(new Runnable() {
public void run() {
try {
showDialog(id);
} catch (BadTokenException e) {
Log.w(MyTracksConstants.TAG,
"Could not display dialog with id " + id, e);
} catch (IllegalStateException e) {
Log.w(MyTracksConstants.TAG,
"Could not display dialog with id " + id, e);
}
}
});
}
/**
* Dismisses the progress dialog if it is showing. Executed on the UI thread.
*/
public void dismissDialogSafely(final int id) {
runOnUiThread(new Runnable() {
public void run() {
try {
dismissDialog(id);
} catch (IllegalArgumentException e) {
// This will be thrown if this dialog was not shown before.
}
}
});
}
// ProgressIndicator implementation
@Override
@@ -1070,7 +1022,7 @@ public class MyTracks extends TabActivity implements OnTouchListener,
@Override
public void handleAccountSelected(Account account) {
if (account == null) {
dismissDialogSafely(DIALOG_PROGRESS);
dialogManager.dismissDialogSafely(DIALOG_PROGRESS);
return;
}
doLogin(results, requestCode, service, account);
@@ -1163,7 +1115,7 @@ public class MyTracks extends TabActivity implements OnTouchListener,
}
setProgressValue(0);
clearProgressMessage();
showDialogSafely(DIALOG_PROGRESS);
dialogManager.showDialogSafely(DIALOG_PROGRESS);
if (sendToGoogleDialog.getSendToMyMaps()) {
if (!sendToGoogleDialog.getCreateNewMap()) {
Intent listIntent = new Intent(this, MyMapsList.class);
@@ -1232,43 +1184,20 @@ public class MyTracks extends TabActivity implements OnTouchListener,
}
}
/**
* Shows a dialog with the given message.
* Does it on the UI thread.
*
* @param success if true, displays an info icon/title, otherwise an error
* icon/title
* @param message resource string id
*/
public void showMessageDialog(final int message, final boolean success) {
runOnUiThread(new Runnable() {
public void run() {
AlertDialog dialog = null;
AlertDialog.Builder builder = new AlertDialog.Builder(MyTracks.this);
builder.setMessage(MyTracks.this.getString(message));
builder.setNegativeButton(MyTracks.this.getString(R.string.ok), null);
builder.setIcon(success ? android.R.drawable.ic_dialog_info :
android.R.drawable.ic_dialog_alert);
builder.setTitle(success ? R.string.success : R.string.error);
dialog = builder.create();
dialog.show();
}
});
}
/**
* Saves the track with the given id to the SD card.
*
* @param trackId The id of the track to be sent
*/
public void saveTrack(long trackId, TrackFileFormat format) {
showDialogSafely(DIALOG_WRITE_PROGRESS);
dialogManager.showDialogSafely(DIALOG_WRITE_PROGRESS);
final TrackWriter writer =
TrackWriterFactory.newWriter(this, providerUtils, trackId, format);
writer.setOnCompletion(new Runnable() {
public void run() {
dismissDialogSafely(DIALOG_WRITE_PROGRESS);
showMessageDialog(writer.getErrorMessage(), writer.wasSuccess());
dialogManager.dismissDialogSafely(DIALOG_WRITE_PROGRESS);
dialogManager.showMessageDialog(writer.getErrorMessage(),
writer.wasSuccess());
}
});
writer.writeTrackAsync();
@@ -1282,7 +1211,7 @@ public class MyTracks extends TabActivity implements OnTouchListener,
* @param trackId The id of the track to be sent
*/
public void sendTrack(long trackId, final TrackFileFormat format) {
showDialogSafely(DIALOG_WRITE_PROGRESS);
dialogManager.showDialogSafely(DIALOG_WRITE_PROGRESS);
final TrackWriter writer =
TrackWriterFactory.newWriter(this, providerUtils, trackId, format);
@@ -1294,9 +1223,10 @@ public class MyTracks extends TabActivity implements OnTouchListener,
writer.setDirectory(dir);
writer.setOnCompletion(new Runnable() {
public void run() {
dismissDialogSafely(DIALOG_WRITE_PROGRESS);
dialogManager.dismissDialogSafely(DIALOG_WRITE_PROGRESS);
if (!writer.wasSuccess()) {
showMessageDialog(writer.getErrorMessage(), writer.wasSuccess());
dialogManager.showMessageDialog(writer.getErrorMessage(),
writer.wasSuccess());
} else {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_SUBJECT,
@@ -1324,7 +1254,8 @@ public class MyTracks extends TabActivity implements OnTouchListener,
Toast.makeText(this, getMapsResultMessage(), Toast.LENGTH_LONG).show();
shareLinkToMyMap(sendToMyMapsMapId);
} else {
showDialogSafely(DialogManager.DIALOG_SEND_TO_GOOGLE_RESULT);
dialogManager.showDialogSafely(
DialogManager.DIALOG_SEND_TO_GOOGLE_RESULT);
}
}
@@ -1352,6 +1283,14 @@ public class MyTracks extends TabActivity implements OnTouchListener,
return accountChooser;
}
public ChartActivity getChartActivity() {
return chartActivity;
}
public void setChartActivity(ChartActivity chartActivity) {
this.chartActivity = chartActivity;
}
public String getSendToMyMapsMapId() {
return sendToMyMapsMapId;
}
@@ -27,15 +27,16 @@ public class TempFileCleaner {
private long currentTimeMillis;
public TempFileCleaner() {
currentTimeMillis = System.currentTimeMillis();
public static void clean() {
(new TempFileCleaner(System.currentTimeMillis())).cleanImpl();
}
public TempFileCleaner(long time) {
// @VisibleForTesting
TempFileCleaner(long time) {
currentTimeMillis = time;
}
public void clean() {
private void cleanImpl() {
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
return; // Can't do anything now.
@@ -54,15 +55,14 @@ public class TempFileCleaner {
+ "tmp"));
}
// VisibleForTesting
protected int cleanTmpDirectory(File dir) {
// @VisibleForTesting
int cleanTmpDirectory(File dir) {
if (!dir.exists()) {
return 0;
}
File[] list = dir.listFiles();
int count = 0;
long oldest = currentTimeMillis - 1000 * 3600;
for (File f : list) {
for (File f : dir.listFiles()) {
if (f.lastModified() < oldest) {
f.delete();
count++;
@@ -34,7 +34,7 @@ public class TempFileCleanerTest extends AndroidTestCase {
public void test_noDir() {
File dir = AndroidMock.createMock(File.class, "/no_file");
TempFileCleaner cleaner = new TempFileCleaner();
TempFileCleaner cleaner = new TempFileCleaner(0);
expect(dir.exists()).andStubReturn(false);
AndroidMock.replay(dir);
assertEquals(0, cleaner.cleanTmpDirectory(dir));
@@ -43,7 +43,7 @@ public class TempFileCleanerTest extends AndroidTestCase {
public void test_emptyDir() {
File dir = AndroidMock.createMock(File.class, "/no_file");
TempFileCleaner cleaner = new TempFileCleaner();
TempFileCleaner cleaner = new TempFileCleaner(0);
expect(dir.exists()).andStubReturn(true);
expect(dir.listFiles()).andStubReturn(new File[0]);
AndroidMock.replay(dir);