Fix copy and update indexes

This commit is contained in:
PavelRatushny 2017-07-31 17:10:47 +03:00
parent bbdafed84b
commit 316007b68f
2 changed files with 15 additions and 74 deletions

View file

@ -17,7 +17,7 @@ import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import net.osmand.IndexConstants; import net.osmand.IProgress;
import net.osmand.plus.OnDismissDialogFragmentListener; import net.osmand.plus.OnDismissDialogFragmentListener;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
@ -25,12 +25,9 @@ import net.osmand.plus.R;
import net.osmand.plus.base.BottomSheetDialogFragment; import net.osmand.plus.base.BottomSheetDialogFragment;
import net.osmand.plus.dashboard.DashChooseAppDirFragment; import net.osmand.plus.dashboard.DashChooseAppDirFragment;
import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadActivity;
import net.osmand.plus.download.DownloadIndexesThread;
import java.io.File; 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 { public class DataStoragePlaceDialogFragment extends BottomSheetDialogFragment {
@ -201,60 +198,9 @@ public class DataStoragePlaceDialogFragment extends BottomSheetDialogFragment {
return sz; return sz;
} }
public static void copyDirectory(File sourceDir, File destDir) throws IOException { private void updateDownloadIndexes() {
if (!destDir.exists()) { DownloadIndexesThread downloadIndexesThread = getMyApplication().getDownloadThread();
destDir.mkdirs(); downloadIndexesThread.runReloadIndexFilesSilent();
}
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 View.OnClickListener deviceMemoryOnClickListener = private View.OnClickListener deviceMemoryOnClickListener =
@ -262,6 +208,8 @@ public class DataStoragePlaceDialogFragment extends BottomSheetDialogFragment {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
saveFilesLocation(deviceStorageType, deviceStorage, getActivity()); saveFilesLocation(deviceStorageType, deviceStorage, getActivity());
getMyApplication().getResourceManager().checkAssets(IProgress.EMPTY_PROGRESS, true);
updateDownloadIndexes();
isInterestedInFirstTime = false; isInterestedInFirstTime = false;
dismiss(); dismiss();
} }
@ -272,6 +220,8 @@ public class DataStoragePlaceDialogFragment extends BottomSheetDialogFragment {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
saveFilesLocation(sharedStorageType, sharedStorage, getActivity()); saveFilesLocation(sharedStorageType, sharedStorage, getActivity());
getMyApplication().getResourceManager().checkAssets(IProgress.EMPTY_PROGRESS, true);
updateDownloadIndexes();
isInterestedInFirstTime = false; isInterestedInFirstTime = false;
dismiss(); dismiss();
} }
@ -282,6 +232,8 @@ public class DataStoragePlaceDialogFragment extends BottomSheetDialogFragment {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
boolean res = saveFilesLocation(cardStorageType, cardStorage, getActivity()); boolean res = saveFilesLocation(cardStorageType, cardStorage, getActivity());
getMyApplication().getResourceManager().checkAssets(IProgress.EMPTY_PROGRESS, true);
updateDownloadIndexes();
isInterestedInFirstTime = false; isInterestedInFirstTime = false;
if (res) { if (res) {
dismiss(); dismiss();
@ -292,19 +244,8 @@ public class DataStoragePlaceDialogFragment extends BottomSheetDialogFragment {
public boolean saveFilesLocation(int type, File selectedFile, Activity context) { public boolean saveFilesLocation(int type, File selectedFile, Activity context) {
boolean wr = OsmandSettings.isWritable(selectedFile); boolean wr = OsmandSettings.isWritable(selectedFile);
if (wr) { if (wr) {
File sourceStorage = ((OsmandApplication) getActivity().getApplication())
.getAppPath(IndexConstants.VOICE_INDEX_DIR);
((OsmandApplication) context.getApplication()) ((OsmandApplication) context.getApplication())
.setExternalStorageDirectory(type, selectedFile.getAbsolutePath()); .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(); reloadData();
} else { } else {
Toast.makeText(context, R.string.specified_directiory_not_writeable, Toast.makeText(context, R.string.specified_directiory_not_writeable,

View file

@ -350,7 +350,7 @@ public class ResourceManager {
public List<String> reloadIndexesOnStart(AppInitializer progress, List<String> warnings){ public List<String> reloadIndexesOnStart(AppInitializer progress, List<String> warnings){
close(); close();
// check we have some assets to copy to sdcard // check we have some assets to copy to sdcard
warnings.addAll(checkAssets(progress)); warnings.addAll(checkAssets(progress, false));
progress.notifyEvent(InitEvents.ASSETS_COPIED); progress.notifyEvent(InitEvents.ASSETS_COPIED);
reloadIndexes(progress, warnings); reloadIndexes(progress, warnings);
progress.notifyEvent(InitEvents.MAPS_INITIALIZED); progress.notifyEvent(InitEvents.MAPS_INITIALIZED);
@ -414,9 +414,9 @@ public class ResourceManager {
return warnings; return warnings;
} }
private List<String> checkAssets(IProgress progress) { public List<String> checkAssets(IProgress progress, boolean forceUpdate) {
String fv = Version.getFullVersion(context); 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); File applicationDataDir = context.getAppPath(null);
applicationDataDir.mkdirs(); applicationDataDir.mkdirs();
if (applicationDataDir.canWrite()) { if (applicationDataDir.canWrite()) {
@ -424,7 +424,7 @@ public class ResourceManager {
progress.startTask(context.getString(R.string.installing_new_resources), -1); progress.startTask(context.getString(R.string.installing_new_resources), -1);
AssetManager assetManager = context.getAssets(); AssetManager assetManager = context.getAssets();
boolean isFirstInstall = context.getSettings().PREVIOUS_INSTALLED_VERSION.get().equals(""); 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); context.getSettings().PREVIOUS_INSTALLED_VERSION.set(fv);
copyRegionsBoundaries(); copyRegionsBoundaries();
// see Issue #3381 // see Issue #3381