Get temp dir refactoring

This commit is contained in:
Dima-1 2020-09-24 17:04:07 +03:00
parent 915dc9800b
commit 2581bcb4e5
4 changed files with 18 additions and 24 deletions

View file

@ -205,10 +205,7 @@ public class FileUtils {
if (!src.exists()) { if (!src.exists()) {
return null; return null;
} }
File tempDir = app.getAppPath(IndexConstants.TEMP_DIR); File tempDir = getTempDir(app);
if (!tempDir.exists()) {
tempDir.mkdirs();
}
File dest = new File(tempDir, src.getName()); File dest = new File(tempDir, src.getName());
try { try {
Algorithms.fileCopy(src, dest); Algorithms.fileCopy(src, dest);
@ -218,6 +215,14 @@ public class FileUtils {
return dest; return dest;
} }
public static File getTempDir(OsmandApplication app) {
File tempDir = app.getAppPath(IndexConstants.TEMP_DIR);
if (!tempDir.exists()) {
tempDir.mkdirs();
}
return tempDir;
}
public interface RenameCallback { public interface RenameCallback {
void renamedTo(File file); void renamedTo(File file);
} }

View file

@ -26,6 +26,7 @@ import com.google.gson.reflect.TypeToken;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.CallbackWithObject; import net.osmand.CallbackWithObject;
import net.osmand.FileUtils;
import net.osmand.GPXUtilities; import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.GPXTrackAnalysis; import net.osmand.GPXUtilities.GPXTrackAnalysis;
@ -2350,11 +2351,8 @@ public class OsmandAidlApi {
} }
private int copyFileImpl(String fileName, byte[] filePartData, long startTime, boolean done, String destinationDir) { private int copyFileImpl(String fileName, byte[] filePartData, long startTime, boolean done, String destinationDir) {
File file = app.getAppPath(IndexConstants.TEMP_DIR + fileName); File tempDir = FileUtils.getTempDir(app);
File tempDir = app.getAppPath(IndexConstants.TEMP_DIR); File file = new File(tempDir, fileName);
if (!tempDir.exists()) {
tempDir.mkdirs();
}
File destFile = app.getAppPath(destinationDir + fileName); File destFile = app.getAppPath(destinationDir + fileName);
long currentTime = System.currentTimeMillis(); long currentTime = System.currentTimeMillis();
try { try {

View file

@ -21,6 +21,7 @@ import androidx.fragment.app.FragmentManager;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.CallbackWithObject; import net.osmand.CallbackWithObject;
import net.osmand.FileUtils;
import net.osmand.GPXUtilities; import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.WptPt; import net.osmand.GPXUtilities.WptPt;
@ -715,17 +716,14 @@ public class ImportHelper {
@Override @Override
protected String doInBackground(Void... voids) { protected String doInBackground(Void... voids) {
File tempDir = app.getAppPath(IndexConstants.TEMP_DIR); File tempDir = FileUtils.getTempDir(app);
if (!tempDir.exists()) {
tempDir.mkdirs();
}
File dest = new File(tempDir, name); File dest = new File(tempDir, name);
return copyFile(app, dest, uri, true); return copyFile(app, dest, uri, true);
} }
@Override @Override
protected void onPostExecute(String error) { protected void onPostExecute(String error) {
File tempDir = app.getAppPath(IndexConstants.TEMP_DIR); File tempDir = FileUtils.getTempDir(app);
final File file = new File(tempDir, name); final File file = new File(tempDir, name);
if (error == null && file.exists()) { if (error == null && file.exists()) {
app.getSettingsHelper().collectSettings(file, latestChanges, version, new SettingsCollectListener() { app.getSettingsHelper().collectSettings(file, latestChanges, version, new SettingsCollectListener() {

View file

@ -21,6 +21,7 @@ import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.FileUtils;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
@ -329,7 +330,7 @@ public class ExportProfileBottomSheet extends BasePreferenceBottomSheet {
if (app != null) { if (app != null) {
exportingProfile = true; exportingProfile = true;
showExportProgressDialog(); showExportProgressDialog();
File tempDir = getTempDir(); File tempDir = FileUtils.getTempDir(app);
String fileName = profile.toHumanString(); String fileName = profile.toHumanString();
app.getSettingsHelper().exportSettings(tempDir, fileName, getSettingsExportListener(), prepareSettingsItemsForExport(), true); app.getSettingsHelper().exportSettings(tempDir, fileName, getSettingsExportListener(), prepareSettingsItemsForExport(), true);
} }
@ -391,19 +392,11 @@ public class ExportProfileBottomSheet extends BasePreferenceBottomSheet {
} }
private File getExportFile() { private File getExportFile() {
File tempDir = getTempDir(); File tempDir = FileUtils.getTempDir(app);
String fileName = profile.toHumanString(); String fileName = profile.toHumanString();
return new File(tempDir, fileName + IndexConstants.OSMAND_SETTINGS_FILE_EXT); return new File(tempDir, fileName + IndexConstants.OSMAND_SETTINGS_FILE_EXT);
} }
private File getTempDir() {
File tempDir = app.getAppPath(IndexConstants.TEMP_DIR);
if (!tempDir.exists()) {
tempDir.mkdirs();
}
return tempDir;
}
private void shareProfile(@NonNull File file, @NonNull ApplicationMode profile) { private void shareProfile(@NonNull File file, @NonNull ApplicationMode profile) {
try { try {
final Intent sendIntent = new Intent(); final Intent sendIntent = new Intent();