Use external storage directory display name rather than the actual path.

This commit is contained in:
Jimmy Shih
2013-06-21 16:42:50 -07:00
parent db124f4ca6
commit 9609010906
12 changed files with 59 additions and 55 deletions
+7 -7
View File
@@ -634,7 +634,7 @@ limitations under the License.
%1$d the number of tracks successfully exported. E.g., '1'. %1$d the number of tracks successfully exported. E.g., '1'.
%1$s the number of tracks to export. E.g., '2 tracks'. %1$s the number of tracks to export. E.g., '2 tracks'.
%2$s the directory. E.g., '/storage/sdcard0/MyTracks/kml/'."> %2$s the directory. E.g., '/MyTracks/kml'.">
Exported %1$d of %2$s to %3$s. Exported %1$d of %2$s to %3$s.
</string> </string>
<string name="export_external_storage_option" <string name="export_external_storage_option"
@@ -648,14 +648,14 @@ limitations under the License.
<string name="export_external_storage_progress_message" <string name="export_external_storage_progress_message"
translation_description="When exporting tracks to the external storage, the progress message. translation_description="When exporting tracks to the external storage, the progress message.
%1$s: the directory. E.g., '/storage/sdcard0/MyTracks/kml/'."> %1$s: the directory. E.g., '/MyTracks/kml'.">
Exporting to %1$s&#8230; Exporting to %1$s&#8230;
</string> </string>
<string name="export_external_storage_success" <string name="export_external_storage_success"
translation_description="When exporting tracks to the external storage, the success message. translation_description="When exporting tracks to the external storage, the success message.
%1$s the number of tracks. E.g., '2 tracks'. %1$s the number of tracks. E.g., '2 tracks'.
%2$s the directory. E.g., '/storage/sdcard0/MyTracks/kml/'."> %2$s the directory. E.g., '/MyTracks/kml'.">
Exported %1$s to %2$s. Exported %1$s to %2$s.
</string> </string>
<string name="export_google_drive" <string name="export_google_drive"
@@ -919,20 +919,20 @@ limitations under the License.
%1$d the number of files successfully imported. E.g., '2'. %1$d the number of files successfully imported. E.g., '2'.
%2$s the number of files in the directory. E.g., '2 files'. %2$s the number of files in the directory. E.g., '2 files'.
%3$s the directory. E.g., '/storage/sdcard0/MyTracks/kml/'."> %3$s the directory. E.g., '/MyTracks/kml'.">
Imported %1$d of %2$s from %3$s. Imported %1$d of %2$s from %3$s.
</string> </string>
<string name="import_no_directory" <string name="import_no_directory"
translation_description="When importing from the external storage, the error message when the translation_description="When importing from the external storage, the error message when the
directory doesn't exist. directory doesn't exist.
%1$s: the directory. E.g., '/storage/sdcard0/MyTracks/kml/'."> %1$s: the directory. E.g., '/MyTracks/kml'.">
%1$s does not exist. %1$s does not exist.
</string> </string>
<string name="import_progress_message" <string name="import_progress_message"
translation_description="When importing from the external storage, the progress message. translation_description="When importing from the external storage, the progress message.
%1$s: the directory. E.g., '/storage/sdcard0/MyTracks/kml/'."> %1$s: the directory. E.g., '/MyTracks/kml'.">
Importing from %1$s&#8230; Importing from %1$s&#8230;
</string> </string>
<string name="import_selection_option" <string name="import_selection_option"
@@ -951,7 +951,7 @@ limitations under the License.
translation_description="When importing from the external storage, the success message. translation_description="When importing from the external storage, the success message.
%1$s the number of files. E.g., '2 files'. %1$s the number of files. E.g., '2 files'.
%2$s the directory. E.g., '/storage/sdcard0/MyTracks/kml/'."> %2$s the directory. E.g., '/MyTracks/kml'.">
Imported %1$s from %2$s. Imported %1$s from %2$s.
</string> </string>
@@ -171,7 +171,7 @@ public class ExportDialogFragment extends DialogFragment {
*/ */
private void setExternalStorageOption(RadioButton radioButton, TrackFileFormat trackFileFormat) { private void setExternalStorageOption(RadioButton radioButton, TrackFileFormat trackFileFormat) {
radioButton.setText(getString(R.string.export_external_storage_option, trackFileFormat.name(), radioButton.setText(getString(R.string.export_external_storage_option, trackFileFormat.name(),
FileUtils.getDisplayDirectory(trackFileFormat.getExtension()))); FileUtils.getDirectoryDisplayName(trackFileFormat.getExtension())));
} }
/** /**
@@ -92,7 +92,7 @@ public class FileTypeDialogFragment extends DialogFragment {
for (int i = 0; i < choices.length; i++) { for (int i = 0; i < choices.length; i++) {
TrackFileFormat trackFileFormat = trackFileFormats[i]; TrackFileFormat trackFileFormat = trackFileFormats[i];
choices[i] = getString(optionId, trackFileFormat.name(), choices[i] = getString(optionId, trackFileFormat.name(),
FileUtils.getDisplayDirectory(trackFileFormat.getExtension())); FileUtils.getDirectoryDisplayName(trackFileFormat.getExtension()));
} }
return new AlertDialog.Builder(getActivity()).setNegativeButton(R.string.generic_cancel, null) return new AlertDialog.Builder(getActivity()).setNegativeButton(R.string.generic_cancel, null)
.setPositiveButton(R.string.generic_ok, new OnClickListener() { .setPositiveButton(R.string.generic_ok, new OnClickListener() {
@@ -86,8 +86,8 @@ class ExternalFileBackup {
* @param create whether to try creating the directory if it doesn't exist * @param create whether to try creating the directory if it doesn't exist
*/ */
private File getBackupsDirectory(boolean create) { private File getBackupsDirectory(boolean create) {
String dirName = FileUtils.buildExternalDirectoryPath(BACKUPS_SUBDIR); String directoryPath = FileUtils.getDirectoryPath(BACKUPS_SUBDIR);
final File dir = new File(dirName); final File dir = new File(directoryPath);
Log.d(Constants.TAG, "Dir: " + dir.getAbsolutePath()); Log.d(Constants.TAG, "Dir: " + dir.getAbsolutePath());
if (create) { if (create) {
// Try to create - if that fails, return null // Try to create - if that fails, return null
@@ -59,7 +59,7 @@ public class ImportActivity extends Activity {
private TrackFileFormat trackFileFormat; private TrackFileFormat trackFileFormat;
// the path on the external storage to import // the path on the external storage to import
private String path; private String directoryDisplayName;
// the number of files successfully imported // the number of files successfully imported
private int successCount; private int successCount;
@@ -86,10 +86,14 @@ public class ImportActivity extends Activity {
finish(); finish();
return; return;
} }
String directoryPath;
if (importAll) { if (importAll) {
path = FileUtils.buildExternalDirectoryPath(trackFileFormat.getExtension()); directoryDisplayName = FileUtils.getDirectoryDisplayName(
if (!FileUtils.isDirectory(new File(path))) { trackFileFormat.getExtension());
Toast.makeText(this, getString(R.string.import_no_directory, path), Toast.LENGTH_LONG) directoryPath = FileUtils.getDirectoryPath(trackFileFormat.getExtension());
if (!FileUtils.isDirectory(new File(directoryPath))) {
Toast.makeText(
this, getString(R.string.import_no_directory, directoryDisplayName), Toast.LENGTH_LONG)
.show(); .show();
finish(); finish();
return; return;
@@ -108,7 +112,8 @@ public class ImportActivity extends Activity {
finish(); finish();
return; return;
} }
path = data.getPath(); directoryDisplayName = data.getPath();
directoryPath = data.getPath();
} }
Object retained = getLastNonConfigurationInstance(); Object retained = getLastNonConfigurationInstance();
@@ -116,7 +121,7 @@ public class ImportActivity extends Activity {
importAsyncTask = (ImportAsyncTask) retained; importAsyncTask = (ImportAsyncTask) retained;
importAsyncTask.setActivity(this); importAsyncTask.setActivity(this);
} else { } else {
importAsyncTask = new ImportAsyncTask(this, importAll, trackFileFormat, path); importAsyncTask = new ImportAsyncTask(this, importAll, trackFileFormat, directoryPath);
importAsyncTask.execute(); importAsyncTask.execute();
} }
} }
@@ -139,7 +144,7 @@ public class ImportActivity extends Activity {
dialog.dismiss(); dialog.dismiss();
finish(); finish();
} }
}, path); }, directoryDisplayName);
return progressDialog; return progressDialog;
case DIALOG_RESULT_ID: case DIALOG_RESULT_ID:
final boolean success; final boolean success;
@@ -148,10 +153,10 @@ public class ImportActivity extends Activity {
.getQuantityString(R.plurals.files, totalCount, totalCount); .getQuantityString(R.plurals.files, totalCount, totalCount);
if (successCount == totalCount && totalCount > 0) { if (successCount == totalCount && totalCount > 0) {
success = true; success = true;
message = getString(R.string.import_success, totalFiles, path); message = getString(R.string.import_success, totalFiles, directoryDisplayName);
} else { } else {
success = false; success = false;
message = getString(R.string.import_error, successCount, totalFiles, path); message = getString(R.string.import_error, successCount, totalFiles, directoryDisplayName);
} }
return new AlertDialog.Builder(this).setCancelable(true).setIcon( return new AlertDialog.Builder(this).setCancelable(true).setIcon(
success ? android.R.drawable.ic_dialog_info : android.R.drawable.ic_dialog_alert) success ? android.R.drawable.ic_dialog_info : android.R.drawable.ic_dialog_alert)
@@ -51,7 +51,7 @@ public class SaveActivity extends Activity {
private TrackFileFormat trackFileFormat; private TrackFileFormat trackFileFormat;
private long[] trackIds; private long[] trackIds;
private boolean playTrack; private boolean playTrack;
private String directoryName; private String directoryDisplayName;
private SaveAsyncTask saveAsyncTask; private SaveAsyncTask saveAsyncTask;
private ProgressDialog progressDialog; private ProgressDialog progressDialog;
@@ -80,11 +80,14 @@ public class SaveActivity extends Activity {
return; return;
} }
directoryName = playTrack ? FileUtils.buildExternalDirectoryPath( directoryDisplayName = playTrack ? FileUtils.getDirectoryDisplayName(
trackFileFormat.getExtension(), FileUtils.TEMP_DIR) trackFileFormat.getExtension(), FileUtils.TEMP_DIR)
: FileUtils.buildExternalDirectoryPath(trackFileFormat.getExtension()); : FileUtils.getDirectoryDisplayName(trackFileFormat.getExtension());
File directory = new File(directoryName); String directoryPath = playTrack ? FileUtils.getDirectoryPath(
trackFileFormat.getExtension(), FileUtils.TEMP_DIR)
: FileUtils.getDirectoryPath(trackFileFormat.getExtension());
File directory = new File(directoryPath);
if (!FileUtils.ensureDirectoryExists(directory)) { if (!FileUtils.ensureDirectoryExists(directory)) {
Toast.makeText(this, R.string.external_storage_not_writable, Toast.LENGTH_LONG).show(); Toast.makeText(this, R.string.external_storage_not_writable, Toast.LENGTH_LONG).show();
finish(); finish();
@@ -120,7 +123,7 @@ public class SaveActivity extends Activity {
dialog.dismiss(); dialog.dismiss();
finish(); finish();
} }
}, directoryName); }, directoryDisplayName);
return progressDialog; return progressDialog;
case DIALOG_RESULT_ID: case DIALOG_RESULT_ID:
boolean success; boolean success;
@@ -129,11 +132,11 @@ public class SaveActivity extends Activity {
.getQuantityString(R.plurals.tracks, totalCount, totalCount); .getQuantityString(R.plurals.tracks, totalCount, totalCount);
if (successCount == totalCount && totalCount > 0) { if (successCount == totalCount && totalCount > 0) {
success = true; success = true;
message = getString(R.string.export_external_storage_success, totalTracks, directoryName); message = getString(R.string.export_external_storage_success, totalTracks, directoryDisplayName);
} else { } else {
success = false; success = false;
message = getString( message = getString(
R.string.export_external_storage_error, successCount, totalTracks, directoryName); R.string.export_external_storage_error, successCount, totalTracks, directoryDisplayName);
} }
AlertDialog.Builder builder = new AlertDialog.Builder(this).setCancelable(true).setIcon( AlertDialog.Builder builder = new AlertDialog.Builder(this).setCancelable(true).setIcon(
success ? android.R.drawable.ic_dialog_info : android.R.drawable.ic_dialog_alert) success ? android.R.drawable.ic_dialog_info : android.R.drawable.ic_dialog_alert)
@@ -91,7 +91,7 @@ public class RemoveTempFilesService extends Service {
} }
private void cleanTempDirectory(String name) { private void cleanTempDirectory(String name) {
cleanTempDirectory(new File(FileUtils.buildExternalDirectoryPath(name, FileUtils.TEMP_DIR))); cleanTempDirectory(new File(FileUtils.getDirectoryPath(name, FileUtils.TEMP_DIR)));
} }
/** /**
@@ -87,10 +87,11 @@ public class FileUtils {
} }
/** /**
* Gets the display directory. * Gets the directory display name.
*
* @param components the components * @param components the components
*/ */
public static String getDisplayDirectory(String... components) { public static String getDirectoryDisplayName(String... components) {
StringBuilder dirNameBuilder = new StringBuilder(); StringBuilder dirNameBuilder = new StringBuilder();
dirNameBuilder.append(File.separatorChar); dirNameBuilder.append(File.separatorChar);
dirNameBuilder.append(SDCARD_TOP_DIR); dirNameBuilder.append(SDCARD_TOP_DIR);
@@ -101,20 +102,14 @@ public class FileUtils {
return dirNameBuilder.toString(); return dirNameBuilder.toString();
} }
/** /**
* Builds a path inside the My Tracks directory in the SD card. * Gets the directory path.
* *
* @param components the path components inside the mytracks directory * @param components the components
* @return the full path to the destination
*/ */
public static String buildExternalDirectoryPath(String... components) { public static String getDirectoryPath(String... components) {
StringBuilder dirNameBuilder = new StringBuilder(); StringBuilder dirNameBuilder = new StringBuilder();
dirNameBuilder.append(Environment.getExternalStorageDirectory()); dirNameBuilder.append(Environment.getExternalStorageDirectory());
dirNameBuilder.append(File.separatorChar); dirNameBuilder.append(getDirectoryDisplayName(components));
dirNameBuilder.append(SDCARD_TOP_DIR);
for (String component : components) {
dirNameBuilder.append(File.separatorChar);
dirNameBuilder.append(component);
}
return dirNameBuilder.toString(); return dirNameBuilder.toString();
} }
@@ -29,13 +29,13 @@ import junit.framework.TestCase;
public class FileUtilsTest extends TestCase { public class FileUtilsTest extends TestCase {
/** /**
* Tests {@link FileUtils#buildExternalDirectoryPath(String...)}. * Tests {@link FileUtils#getDirectoryPath(String...)}.
*/ */
public void testBuildExternalDirectoryPath() { public void testBuildExternalDirectoryPath() {
String expectedName = Environment.getExternalStorageDirectory() + File.separator String expectedName = Environment.getExternalStorageDirectory() + File.separator
+ FileUtils.SDCARD_TOP_DIR + File.separator + "a" + File.separator + "b" + File.separator + FileUtils.SDCARD_TOP_DIR + File.separator + "a" + File.separator + "b" + File.separator
+ "c"; + "c";
String dirName = FileUtils.buildExternalDirectoryPath("a", "b", "c"); String dirName = FileUtils.getDirectoryPath("a", "b", "c");
assertEquals(expectedName, dirName); assertEquals(expectedName, dirName);
} }
@@ -500,7 +500,7 @@ public class EndToEndTestUtils {
* @param trackKind the kind of track * @param trackKind the kind of track
*/ */
public static void deleteExportedFiles(String trackKind) { public static void deleteExportedFiles(String trackKind) {
File[] allFiles = (new File(FileUtils.buildExternalDirectoryPath(trackKind.toLowerCase()))) File[] allFiles = (new File(FileUtils.getDirectoryPath(trackKind.toLowerCase())))
.listFiles(); .listFiles();
if (allFiles != null) { if (allFiles != null) {
for (File oneFile : allFiles) { for (File oneFile : allFiles) {
@@ -527,14 +527,14 @@ public class EndToEndTestUtils {
* @return files array of such kind of exported tracks * @return files array of such kind of exported tracks
*/ */
public static File[] getExportedFiles(final String trackKind) { public static File[] getExportedFiles(final String trackKind) {
String filePath = FileUtils.buildExternalDirectoryPath(trackKind); String directoryPath = FileUtils.getDirectoryPath(trackKind);
FileFilter filter = new FileFilter() { FileFilter filter = new FileFilter() {
@Override @Override
public boolean accept(File pathname) { public boolean accept(File pathname) {
return pathname.getName().indexOf("." + trackKind) > 0; return pathname.getName().indexOf("." + trackKind) > 0;
} }
}; };
return (new File(filePath)).listFiles(filter); return (new File(directoryPath)).listFiles(filter);
} }
/** /**
@@ -76,9 +76,9 @@ public class DeleteTest extends ActivityInstrumentationTestCase2<TrackListActivi
EndToEndTestUtils.getButtonOnScreen( EndToEndTestUtils.getButtonOnScreen(
EndToEndTestUtils.activityMytracks.getString(R.string.generic_ok), true, true); EndToEndTestUtils.activityMytracks.getString(R.string.generic_ok), true, true);
String totalTracks = activityMyTracks.getResources().getQuantityString(R.plurals.tracks, 0, 0); String totalTracks = activityMyTracks.getResources().getQuantityString(R.plurals.tracks, 0, 0);
String directoryName = FileUtils.buildExternalDirectoryPath(EndToEndTestUtils.GPX); String directoryDisplayName = FileUtils.getDirectoryDisplayName(EndToEndTestUtils.GPX);
String message = activityMyTracks.getString(R.string.export_external_storage_error, 0, String message = activityMyTracks.getString(R.string.export_external_storage_error, 0,
totalTracks, directoryName); totalTracks, directoryDisplayName);
assertTrue(EndToEndTestUtils.SOLO.waitForText( assertTrue(EndToEndTestUtils.SOLO.waitForText(
activityMyTracks.getString(R.string.external_storage_not_available), 1, activityMyTracks.getString(R.string.external_storage_not_available), 1,
EndToEndTestUtils.SHORT_WAIT_TIME) EndToEndTestUtils.SHORT_WAIT_TIME)
@@ -300,22 +300,23 @@ public class ExportAllAndImportAllTest extends ActivityInstrumentationTestCase2<
} }
private String getSaveSuccessMessage(int count, String type) { private String getSaveSuccessMessage(int count, String type) {
String tracks = activityMyTracks.getResources().getQuantityString(R.plurals.tracks, count, String tracks = activityMyTracks.getResources()
count); .getQuantityString(R.plurals.tracks, count, count);
String directoryName = FileUtils.buildExternalDirectoryPath(type); String directoryDisplayName = FileUtils.getDirectoryDisplayName(type);
return activityMyTracks.getString(R.string.export_external_storage_success, tracks, directoryName); return activityMyTracks.getString(
R.string.export_external_storage_success, tracks, directoryDisplayName);
} }
private String getImportSuccessMessage(int count, String type) { private String getImportSuccessMessage(int count, String type) {
String files = activityMyTracks.getResources().getQuantityString(R.plurals.files, count, count); String files = activityMyTracks.getResources().getQuantityString(R.plurals.files, count, count);
String directoryName = FileUtils.buildExternalDirectoryPath(type); String directoryDisplayName = FileUtils.getDirectoryDisplayName(type);
return activityMyTracks.getString(R.string.import_success, files, directoryName); return activityMyTracks.getString(R.string.import_success, files, directoryDisplayName);
} }
private String getImportErrorMessage(int count, int total, String type) { private String getImportErrorMessage(int count, int total, String type) {
String files = activityMyTracks.getResources().getQuantityString(R.plurals.files, total, total); String files = activityMyTracks.getResources().getQuantityString(R.plurals.files, total, total);
String directoryName = FileUtils.buildExternalDirectoryPath(type); String directoryDisplayName = FileUtils.getDirectoryDisplayName(type);
return activityMyTracks.getString(R.string.import_error, count, files, directoryName); return activityMyTracks.getString(R.string.import_error, count, files, directoryDisplayName);
} }
/** /**