mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-11 13:33:07 +02:00
Code cleanup: add getExtension and getName in FileUtils.java
This commit is contained in:
@@ -178,8 +178,8 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
try {
|
try {
|
||||||
TrackImporter trackImporter;
|
TrackImporter trackImporter;
|
||||||
if (trackFileFormat == TrackFileFormat.KML) {
|
if (trackFileFormat == TrackFileFormat.KML) {
|
||||||
String name = file.getName();
|
String extension = FileUtils.getExtension(file.getName());
|
||||||
if (name.endsWith("." + TrackFileFormat.KML.getExtension())) {
|
if (TrackFileFormat.KML.getExtension().equals(extension)) {
|
||||||
trackImporter = new KmlFileTrackImporter(context, -1L);
|
trackImporter = new KmlFileTrackImporter(context, -1L);
|
||||||
} else {
|
} else {
|
||||||
MyTracksProviderUtils myTracksProviderUtils = MyTracksProviderUtils.Factory.get(context);
|
MyTracksProviderUtils myTracksProviderUtils = MyTracksProviderUtils.Factory.get(context);
|
||||||
@@ -221,15 +221,15 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
if (candidates != null) {
|
if (candidates != null) {
|
||||||
for (File candidate : candidates) {
|
for (File candidate : candidates) {
|
||||||
if (!FileUtils.isDirectory(candidate)) {
|
if (!FileUtils.isDirectory(candidate)) {
|
||||||
String name = candidate.getName();
|
String extension = FileUtils.getExtension(candidate.getName());
|
||||||
if (trackFileFormat == TrackFileFormat.KML && (
|
if (trackFileFormat == TrackFileFormat.KML && (
|
||||||
name.endsWith("." + TrackFileFormat.KML.getExtension())
|
TrackFileFormat.KML.getExtension().equals(extension)
|
||||||
|| name.endsWith("." + KmzTrackExporter.KMZ_EXTENSION))) {
|
|| KmzTrackExporter.KMZ_EXTENSION.equals(extension))) {
|
||||||
files.add(candidate);
|
files.add(candidate);
|
||||||
} else if (trackFileFormat == TrackFileFormat.GPX
|
} else if (trackFileFormat == TrackFileFormat.GPX
|
||||||
&& name.endsWith("." + TrackFileFormat.GPX.getExtension())) {
|
&& TrackFileFormat.GPX.getExtension().equals(extension)) {
|
||||||
files.add(candidate);
|
files.add(candidate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import com.google.android.apps.mytracks.io.file.importer.KmlFileTrackImporter;
|
|||||||
import com.google.android.apps.mytracks.io.file.importer.KmzTrackImporter;
|
import com.google.android.apps.mytracks.io.file.importer.KmzTrackImporter;
|
||||||
import com.google.android.apps.mytracks.io.file.importer.TrackImporter;
|
import com.google.android.apps.mytracks.io.file.importer.TrackImporter;
|
||||||
import com.google.android.apps.mytracks.io.sendtogoogle.SendToGoogleUtils;
|
import com.google.android.apps.mytracks.io.sendtogoogle.SendToGoogleUtils;
|
||||||
|
import com.google.android.apps.mytracks.util.FileUtils;
|
||||||
import com.google.android.apps.mytracks.util.PreferencesUtils;
|
import com.google.android.apps.mytracks.util.PreferencesUtils;
|
||||||
import com.google.android.gms.auth.GoogleAuthException;
|
import com.google.android.gms.auth.GoogleAuthException;
|
||||||
import com.google.android.gms.auth.UserRecoverableAuthException;
|
import com.google.android.gms.auth.UserRecoverableAuthException;
|
||||||
@@ -491,7 +492,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
File updatedDriveFile;
|
File updatedDriveFile;
|
||||||
String trackName = getTrackName(driveFile);
|
String trackName = FileUtils.getName(driveFile.getTitle());
|
||||||
if (!updatedTrack.getName().equals(trackName)) {
|
if (!updatedTrack.getName().equals(trackName)) {
|
||||||
updatedTrack.setName(trackName);
|
updatedTrack.setName(trackName);
|
||||||
|
|
||||||
@@ -568,20 +569,6 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the track name from the drive file title.
|
|
||||||
*
|
|
||||||
* @param driveFile the drive file
|
|
||||||
*/
|
|
||||||
private String getTrackName(File driveFile) {
|
|
||||||
String title = driveFile.getTitle();
|
|
||||||
int index = title.lastIndexOf('.');
|
|
||||||
if (index == -1) {
|
|
||||||
return title;
|
|
||||||
}
|
|
||||||
return title.substring(0, index);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes a drive file.
|
* Deletes a drive file.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -452,7 +452,6 @@ public class SyncUtils {
|
|||||||
public static java.io.File getTempFile(
|
public static java.io.File getTempFile(
|
||||||
Context context, MyTracksProviderUtils myTracksProviderUtils, Track track, boolean useKmz)
|
Context context, MyTracksProviderUtils myTracksProviderUtils, Track track, boolean useKmz)
|
||||||
throws FileNotFoundException {
|
throws FileNotFoundException {
|
||||||
String extension = useKmz ? KmzTrackExporter.KMZ_EXTENSION : TrackFileFormat.KML.getExtension();
|
|
||||||
java.io.File directory = new java.io.File(context.getCacheDir(), FileUtils.TEMP_FILES_DIR);
|
java.io.File directory = new java.io.File(context.getCacheDir(), FileUtils.TEMP_FILES_DIR);
|
||||||
|
|
||||||
if (!FileUtils.ensureDirectoryExists(directory)) {
|
if (!FileUtils.ensureDirectoryExists(directory)) {
|
||||||
@@ -465,6 +464,7 @@ public class SyncUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Track[] tracks = new Track[] { track };
|
Track[] tracks = new Track[] { track };
|
||||||
|
String extension = useKmz ? KmzTrackExporter.KMZ_EXTENSION : TrackFileFormat.KML.getExtension();
|
||||||
java.io.File file = new java.io.File(
|
java.io.File file = new java.io.File(
|
||||||
directory, FileUtils.buildUniqueFileName(directory, track.getName(), extension));
|
directory, FileUtils.buildUniqueFileName(directory, track.getName(), extension));
|
||||||
FileTrackExporter fileTrackExporter = new FileTrackExporter(
|
FileTrackExporter fileTrackExporter = new FileTrackExporter(
|
||||||
|
|||||||
@@ -144,6 +144,32 @@ public class FileUtils {
|
|||||||
return buildUniqueFileName(directory, fileBaseName, extension, 0);
|
return buildUniqueFileName(directory, fileBaseName, extension, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the name from a file name, without the extension.
|
||||||
|
*
|
||||||
|
* @param fileName the file name
|
||||||
|
*/
|
||||||
|
public static String getName(String fileName) {
|
||||||
|
int index = fileName.lastIndexOf('.');
|
||||||
|
if (index == -1) {
|
||||||
|
return fileName;
|
||||||
|
}
|
||||||
|
return fileName.substring(0, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the extension from a file name. Returns null if there is no extension.
|
||||||
|
*
|
||||||
|
* @param fileName the file name
|
||||||
|
*/
|
||||||
|
public static String getExtension(String fileName) {
|
||||||
|
int index = fileName.lastIndexOf('.');
|
||||||
|
if (index == -1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return fileName.substring(index + 1);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a filename with the given base and the given extension, possibly
|
* Builds a filename with the given base and the given extension, possibly
|
||||||
* adding a suffix to ensure the file doesn't exist.
|
* adding a suffix to ensure the file doesn't exist.
|
||||||
|
|||||||
Reference in New Issue
Block a user