Fix copy and update indexes
This commit is contained in:
parent
bbdafed84b
commit
316007b68f
2 changed files with 15 additions and 74 deletions
|
@ -17,7 +17,7 @@ import android.widget.ImageView;
|
|||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import net.osmand.IndexConstants;
|
||||
import net.osmand.IProgress;
|
||||
import net.osmand.plus.OnDismissDialogFragmentListener;
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.OsmandSettings;
|
||||
|
@ -25,12 +25,9 @@ import net.osmand.plus.R;
|
|||
import net.osmand.plus.base.BottomSheetDialogFragment;
|
||||
import net.osmand.plus.dashboard.DashChooseAppDirFragment;
|
||||
import net.osmand.plus.download.DownloadActivity;
|
||||
import net.osmand.plus.download.DownloadIndexesThread;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.FileChannel;
|
||||
|
||||
public class DataStoragePlaceDialogFragment extends BottomSheetDialogFragment {
|
||||
|
||||
|
@ -201,60 +198,9 @@ public class DataStoragePlaceDialogFragment extends BottomSheetDialogFragment {
|
|||
return sz;
|
||||
}
|
||||
|
||||
public static void copyDirectory(File sourceDir, File destDir) throws IOException {
|
||||
if (!destDir.exists()) {
|
||||
destDir.mkdirs();
|
||||
}
|
||||
|
||||
if (!sourceDir.exists()) {
|
||||
throw new IllegalArgumentException("sourceDir does not exist");
|
||||
}
|
||||
|
||||
if (sourceDir.isFile() || destDir.isFile()) {
|
||||
throw new IllegalArgumentException(
|
||||
"Either sourceDir or destDir is not a directory");
|
||||
}
|
||||
|
||||
copyDirectoryImpl(sourceDir, destDir);
|
||||
}
|
||||
|
||||
private static void copyDirectoryImpl(File sourceDir, File destDir) throws IOException {
|
||||
File[] items = sourceDir.listFiles();
|
||||
if (items != null && items.length > 0) {
|
||||
for (File anItem : items) {
|
||||
if (anItem.isDirectory()) {
|
||||
File newDir = new File(destDir, anItem.getName());
|
||||
newDir.mkdir();
|
||||
|
||||
copyDirectory(anItem, newDir);
|
||||
} else {
|
||||
File destFile = new File(destDir, anItem.getName());
|
||||
copySingleFile(anItem, destFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void copySingleFile(File sourceFile, File destFile) throws IOException {
|
||||
if (!destFile.exists()) {
|
||||
destFile.createNewFile();
|
||||
}
|
||||
|
||||
FileChannel sourceChannel = null;
|
||||
FileChannel destChannel = null;
|
||||
|
||||
try {
|
||||
sourceChannel = new FileInputStream(sourceFile).getChannel();
|
||||
destChannel = new FileOutputStream(destFile).getChannel();
|
||||
sourceChannel.transferTo(0, sourceChannel.size(), destChannel);
|
||||
} finally {
|
||||
if (sourceChannel != null) {
|
||||
sourceChannel.close();
|
||||
}
|
||||
if (destChannel != null) {
|
||||
destChannel.close();
|
||||
}
|
||||
}
|
||||
private void updateDownloadIndexes() {
|
||||
DownloadIndexesThread downloadIndexesThread = getMyApplication().getDownloadThread();
|
||||
downloadIndexesThread.runReloadIndexFilesSilent();
|
||||
}
|
||||
|
||||
private View.OnClickListener deviceMemoryOnClickListener =
|
||||
|
@ -262,6 +208,8 @@ public class DataStoragePlaceDialogFragment extends BottomSheetDialogFragment {
|
|||
@Override
|
||||
public void onClick(View v) {
|
||||
saveFilesLocation(deviceStorageType, deviceStorage, getActivity());
|
||||
getMyApplication().getResourceManager().checkAssets(IProgress.EMPTY_PROGRESS, true);
|
||||
updateDownloadIndexes();
|
||||
isInterestedInFirstTime = false;
|
||||
dismiss();
|
||||
}
|
||||
|
@ -272,6 +220,8 @@ public class DataStoragePlaceDialogFragment extends BottomSheetDialogFragment {
|
|||
@Override
|
||||
public void onClick(View v) {
|
||||
saveFilesLocation(sharedStorageType, sharedStorage, getActivity());
|
||||
getMyApplication().getResourceManager().checkAssets(IProgress.EMPTY_PROGRESS, true);
|
||||
updateDownloadIndexes();
|
||||
isInterestedInFirstTime = false;
|
||||
dismiss();
|
||||
}
|
||||
|
@ -282,6 +232,8 @@ public class DataStoragePlaceDialogFragment extends BottomSheetDialogFragment {
|
|||
@Override
|
||||
public void onClick(View v) {
|
||||
boolean res = saveFilesLocation(cardStorageType, cardStorage, getActivity());
|
||||
getMyApplication().getResourceManager().checkAssets(IProgress.EMPTY_PROGRESS, true);
|
||||
updateDownloadIndexes();
|
||||
isInterestedInFirstTime = false;
|
||||
if (res) {
|
||||
dismiss();
|
||||
|
@ -292,19 +244,8 @@ public class DataStoragePlaceDialogFragment extends BottomSheetDialogFragment {
|
|||
public boolean saveFilesLocation(int type, File selectedFile, Activity context) {
|
||||
boolean wr = OsmandSettings.isWritable(selectedFile);
|
||||
if (wr) {
|
||||
File sourceStorage = ((OsmandApplication) getActivity().getApplication())
|
||||
.getAppPath(IndexConstants.VOICE_INDEX_DIR);
|
||||
((OsmandApplication) context.getApplication())
|
||||
.setExternalStorageDirectory(type, selectedFile.getAbsolutePath());
|
||||
File destStorage = ((OsmandApplication) getActivity().getApplication())
|
||||
.getAppPath(IndexConstants.VOICE_INDEX_DIR);
|
||||
if (!destStorage.exists()) {
|
||||
try {
|
||||
copyDirectory(sourceStorage, destStorage);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
reloadData();
|
||||
} else {
|
||||
Toast.makeText(context, R.string.specified_directiory_not_writeable,
|
||||
|
|
|
@ -350,7 +350,7 @@ public class ResourceManager {
|
|||
public List<String> reloadIndexesOnStart(AppInitializer progress, List<String> warnings){
|
||||
close();
|
||||
// check we have some assets to copy to sdcard
|
||||
warnings.addAll(checkAssets(progress));
|
||||
warnings.addAll(checkAssets(progress, false));
|
||||
progress.notifyEvent(InitEvents.ASSETS_COPIED);
|
||||
reloadIndexes(progress, warnings);
|
||||
progress.notifyEvent(InitEvents.MAPS_INITIALIZED);
|
||||
|
@ -414,9 +414,9 @@ public class ResourceManager {
|
|||
return warnings;
|
||||
}
|
||||
|
||||
private List<String> checkAssets(IProgress progress) {
|
||||
public List<String> checkAssets(IProgress progress, boolean forceUpdate) {
|
||||
String fv = Version.getFullVersion(context);
|
||||
if (!fv.equalsIgnoreCase(context.getSettings().PREVIOUS_INSTALLED_VERSION.get())) {
|
||||
if (!fv.equalsIgnoreCase(context.getSettings().PREVIOUS_INSTALLED_VERSION.get()) || forceUpdate) {
|
||||
File applicationDataDir = context.getAppPath(null);
|
||||
applicationDataDir.mkdirs();
|
||||
if (applicationDataDir.canWrite()) {
|
||||
|
@ -424,7 +424,7 @@ public class ResourceManager {
|
|||
progress.startTask(context.getString(R.string.installing_new_resources), -1);
|
||||
AssetManager assetManager = context.getAssets();
|
||||
boolean isFirstInstall = context.getSettings().PREVIOUS_INSTALLED_VERSION.get().equals("");
|
||||
unpackBundledAssets(assetManager, applicationDataDir, progress, isFirstInstall);
|
||||
unpackBundledAssets(assetManager, applicationDataDir, progress, isFirstInstall || forceUpdate);
|
||||
context.getSettings().PREVIOUS_INSTALLED_VERSION.set(fv);
|
||||
copyRegionsBoundaries();
|
||||
// see Issue #3381
|
||||
|
|
Loading…
Reference in a new issue