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$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.
</string>
<string name="export_external_storage_option"
@@ -648,14 +648,14 @@ limitations under the License.
<string name="export_external_storage_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;
</string>
<string name="export_external_storage_success"
translation_description="When exporting tracks to the external storage, the success message.
%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.
</string>
<string name="export_google_drive"
@@ -919,20 +919,20 @@ limitations under the License.
%1$d the number of files successfully imported. E.g., '2'.
%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.
</string>
<string name="import_no_directory"
translation_description="When importing from the external storage, the error message when the
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.
</string>
<string name="import_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;
</string>
<string name="import_selection_option"
@@ -951,7 +951,7 @@ limitations under the License.
translation_description="When importing from the external storage, the success message.
%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.
</string>
@@ -171,7 +171,7 @@ public class ExportDialogFragment extends DialogFragment {
*/
private void setExternalStorageOption(RadioButton radioButton, TrackFileFormat trackFileFormat) {
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++) {
TrackFileFormat trackFileFormat = trackFileFormats[i];
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)
.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
*/
private File getBackupsDirectory(boolean create) {
String dirName = FileUtils.buildExternalDirectoryPath(BACKUPS_SUBDIR);
final File dir = new File(dirName);
String directoryPath = FileUtils.getDirectoryPath(BACKUPS_SUBDIR);
final File dir = new File(directoryPath);
Log.d(Constants.TAG, "Dir: " + dir.getAbsolutePath());
if (create) {
// Try to create - if that fails, return null
@@ -59,7 +59,7 @@ public class ImportActivity extends Activity {
private TrackFileFormat trackFileFormat;
// the path on the external storage to import
private String path;
private String directoryDisplayName;
// the number of files successfully imported
private int successCount;
@@ -86,10 +86,14 @@ public class ImportActivity extends Activity {
finish();
return;
}
String directoryPath;
if (importAll) {
path = FileUtils.buildExternalDirectoryPath(trackFileFormat.getExtension());
if (!FileUtils.isDirectory(new File(path))) {
Toast.makeText(this, getString(R.string.import_no_directory, path), Toast.LENGTH_LONG)
directoryDisplayName = FileUtils.getDirectoryDisplayName(
trackFileFormat.getExtension());
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();
finish();
return;
@@ -108,7 +112,8 @@ public class ImportActivity extends Activity {
finish();
return;
}
path = data.getPath();
directoryDisplayName = data.getPath();
directoryPath = data.getPath();
}
Object retained = getLastNonConfigurationInstance();
@@ -116,7 +121,7 @@ public class ImportActivity extends Activity {
importAsyncTask = (ImportAsyncTask) retained;
importAsyncTask.setActivity(this);
} else {
importAsyncTask = new ImportAsyncTask(this, importAll, trackFileFormat, path);
importAsyncTask = new ImportAsyncTask(this, importAll, trackFileFormat, directoryPath);
importAsyncTask.execute();
}
}
@@ -139,7 +144,7 @@ public class ImportActivity extends Activity {
dialog.dismiss();
finish();
}
}, path);
}, directoryDisplayName);
return progressDialog;
case DIALOG_RESULT_ID:
final boolean success;
@@ -148,10 +153,10 @@ public class ImportActivity extends Activity {
.getQuantityString(R.plurals.files, totalCount, totalCount);
if (successCount == totalCount && totalCount > 0) {
success = true;
message = getString(R.string.import_success, totalFiles, path);
message = getString(R.string.import_success, totalFiles, directoryDisplayName);
} else {
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(
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 long[] trackIds;
private boolean playTrack;
private String directoryName;
private String directoryDisplayName;
private SaveAsyncTask saveAsyncTask;
private ProgressDialog progressDialog;
@@ -80,11 +80,14 @@ public class SaveActivity extends Activity {
return;
}
directoryName = playTrack ? FileUtils.buildExternalDirectoryPath(
directoryDisplayName = playTrack ? FileUtils.getDirectoryDisplayName(
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)) {
Toast.makeText(this, R.string.external_storage_not_writable, Toast.LENGTH_LONG).show();
finish();
@@ -120,7 +123,7 @@ public class SaveActivity extends Activity {
dialog.dismiss();
finish();
}
}, directoryName);
}, directoryDisplayName);
return progressDialog;
case DIALOG_RESULT_ID:
boolean success;
@@ -129,11 +132,11 @@ public class SaveActivity extends Activity {
.getQuantityString(R.plurals.tracks, totalCount, totalCount);
if (successCount == totalCount && totalCount > 0) {
success = true;
message = getString(R.string.export_external_storage_success, totalTracks, directoryName);
message = getString(R.string.export_external_storage_success, totalTracks, directoryDisplayName);
} else {
success = false;
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(
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) {
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
*/
public static String getDisplayDirectory(String... components) {
public static String getDirectoryDisplayName(String... components) {
StringBuilder dirNameBuilder = new StringBuilder();
dirNameBuilder.append(File.separatorChar);
dirNameBuilder.append(SDCARD_TOP_DIR);
@@ -101,20 +102,14 @@ public class FileUtils {
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
* @return the full path to the destination
* @param components the components
*/
public static String buildExternalDirectoryPath(String... components) {
public static String getDirectoryPath(String... components) {
StringBuilder dirNameBuilder = new StringBuilder();
dirNameBuilder.append(Environment.getExternalStorageDirectory());
dirNameBuilder.append(File.separatorChar);
dirNameBuilder.append(SDCARD_TOP_DIR);
for (String component : components) {
dirNameBuilder.append(File.separatorChar);
dirNameBuilder.append(component);
}
dirNameBuilder.append(getDirectoryDisplayName(components));
return dirNameBuilder.toString();
}
@@ -29,13 +29,13 @@ import junit.framework.TestCase;
public class FileUtilsTest extends TestCase {
/**
* Tests {@link FileUtils#buildExternalDirectoryPath(String...)}.
* Tests {@link FileUtils#getDirectoryPath(String...)}.
*/
public void testBuildExternalDirectoryPath() {
String expectedName = Environment.getExternalStorageDirectory() + File.separator
+ FileUtils.SDCARD_TOP_DIR + File.separator + "a" + File.separator + "b" + File.separator
+ "c";
String dirName = FileUtils.buildExternalDirectoryPath("a", "b", "c");
String dirName = FileUtils.getDirectoryPath("a", "b", "c");
assertEquals(expectedName, dirName);
}
@@ -500,7 +500,7 @@ public class EndToEndTestUtils {
* @param trackKind the kind of track
*/
public static void deleteExportedFiles(String trackKind) {
File[] allFiles = (new File(FileUtils.buildExternalDirectoryPath(trackKind.toLowerCase())))
File[] allFiles = (new File(FileUtils.getDirectoryPath(trackKind.toLowerCase())))
.listFiles();
if (allFiles != null) {
for (File oneFile : allFiles) {
@@ -527,14 +527,14 @@ public class EndToEndTestUtils {
* @return files array of such kind of exported tracks
*/
public static File[] getExportedFiles(final String trackKind) {
String filePath = FileUtils.buildExternalDirectoryPath(trackKind);
String directoryPath = FileUtils.getDirectoryPath(trackKind);
FileFilter filter = new FileFilter() {
@Override
public boolean accept(File pathname) {
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.activityMytracks.getString(R.string.generic_ok), true, true);
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,
totalTracks, directoryName);
totalTracks, directoryDisplayName);
assertTrue(EndToEndTestUtils.SOLO.waitForText(
activityMyTracks.getString(R.string.external_storage_not_available), 1,
EndToEndTestUtils.SHORT_WAIT_TIME)
@@ -300,22 +300,23 @@ public class ExportAllAndImportAllTest extends ActivityInstrumentationTestCase2<
}
private String getSaveSuccessMessage(int count, String type) {
String tracks = activityMyTracks.getResources().getQuantityString(R.plurals.tracks, count,
count);
String directoryName = FileUtils.buildExternalDirectoryPath(type);
return activityMyTracks.getString(R.string.export_external_storage_success, tracks, directoryName);
String tracks = activityMyTracks.getResources()
.getQuantityString(R.plurals.tracks, count, count);
String directoryDisplayName = FileUtils.getDirectoryDisplayName(type);
return activityMyTracks.getString(
R.string.export_external_storage_success, tracks, directoryDisplayName);
}
private String getImportSuccessMessage(int count, String type) {
String files = activityMyTracks.getResources().getQuantityString(R.plurals.files, count, count);
String directoryName = FileUtils.buildExternalDirectoryPath(type);
return activityMyTracks.getString(R.string.import_success, files, directoryName);
String directoryDisplayName = FileUtils.getDirectoryDisplayName(type);
return activityMyTracks.getString(R.string.import_success, files, directoryDisplayName);
}
private String getImportErrorMessage(int count, int total, String type) {
String files = activityMyTracks.getResources().getQuantityString(R.plurals.files, total, total);
String directoryName = FileUtils.buildExternalDirectoryPath(type);
return activityMyTracks.getString(R.string.import_error, count, files, directoryName);
String directoryDisplayName = FileUtils.getDirectoryDisplayName(type);
return activityMyTracks.getString(R.string.import_error, count, files, directoryDisplayName);
}
/**