Fix kitkat issue with downloads

This commit is contained in:
vshcherb 2014-04-02 00:13:52 +02:00
parent 31777b8039
commit b2158f1a52
3 changed files with 141 additions and 24 deletions

View file

@ -998,23 +998,115 @@ public class OsmandSettings {
public File getExternalStorageDirectory() {
String defaultLocation = ctx.getExternalServiceAPI().getExternalStorageDirectory();
if(Build.VERSION.SDK_INT >= VERSION_DEFAULTLOCATION_CHANGED && !new File(defaultLocation, IndexConstants.APP_DIR).exists()) {
defaultLocation += "/Android/data/" + ctx.getPackageName();
}
return new File(settingsAPI.getString(globalPreferences, EXTERNAL_STORAGE_DIR,
defaultLocation));
}
public static final int VERSION_DEFAULTLOCATION_CHANGED = Integer.MAX_VALUE;
public static final int VERSION_DEFAULTLOCATION_CHANGED = 19;
public String getDefaultExternalStorageLocation() {
String defaultLocation = ctx.getExternalServiceAPI().getExternalStorageDirectory();
if(Build.VERSION.SDK_INT >= VERSION_DEFAULTLOCATION_CHANGED) {
defaultLocation += "/Android/data/" + ctx.getPackageName();
}
return defaultLocation;
}
private static List<String> getWritableSecondaryStorages() {
List<String> writableSecondaryStorage = new ArrayList<String>();
try {
String rawSecondaryStorage = System.getenv("SECONDARY_STORAGE");
if (rawSecondaryStorage != null && rawSecondaryStorage.trim().length() > 0) {
for (String secondaryPath : rawSecondaryStorage.split(":")) {
File testFile = new File(secondaryPath);
if (isWritable(testFile)) {
writableSecondaryStorage.add(secondaryPath);
}
}
}
} catch (RuntimeException e) {
e.printStackTrace();
}
return writableSecondaryStorage;
}
public String getMatchingExternalFilesDir(String dir) {
// only API 19 !!
try {
File[] externalFilesDirs = ctx.getExternalFilesDirs(null);
String rawSecondaryStorage = System.getenv("SECONDARY_STORAGE");
if (rawSecondaryStorage != null && rawSecondaryStorage.trim().length() > 0 && externalFilesDirs != null) {
for (String secondaryPath : rawSecondaryStorage.split(":")) {
if (dir.startsWith(secondaryPath)) {
for (File externFileDir : externalFilesDirs) {
if (externFileDir != null && externFileDir.getAbsolutePath().startsWith(secondaryPath)) {
return externFileDir.getAbsolutePath();
}
}
}
}
}
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public List<String> getWritableSecondaryStorageDirectorys() {
// only API 19 !!
// primary external storage directory
String primaryExternalStorageDirectory = getDefaultExternalStorageLocation();
// also creates directories, if they don't exist until now
File[] externalFilesDirs = ctx.getExternalFilesDirs(null);
List<String> writableSecondaryStorages = getWritableSecondaryStorages();
List<String> writableSecondaryStorageDirectory = new ArrayList<String>();
try {
boolean primaryExternalStorageFound = false;
if(externalFilesDirs != null) {
for (File externFileDir : externalFilesDirs) {
if (externFileDir != null) {
final String externalFilePath = externFileDir.getAbsolutePath();
if (externalFilePath.startsWith(primaryExternalStorageDirectory) && !primaryExternalStorageFound) {
// exclude primary external storage
// no special location is required
primaryExternalStorageFound = true;
} else {
// secondary storage
// check if special location is required
boolean specialPathRequired = true;
for (String writableSecondaryStorage : writableSecondaryStorages) {
if (externalFilePath.startsWith(writableSecondaryStorage)) {
// no special location required
writableSecondaryStorageDirectory.add(writableSecondaryStorage);
specialPathRequired = false;
break;
}
}
if (specialPathRequired == true) {
// special location required
writableSecondaryStorageDirectory.add(externalFilePath);
}
}
}
}
}
} catch (RuntimeException e) {
e.printStackTrace();
}
return writableSecondaryStorageDirectory;
}
public static boolean isWritable(File dirToTest) {
boolean isWriteable = false;
try {
File writeTestFile = File.createTempFile("osmand_", ".tmp", dirToTest);
isWriteable = writeTestFile.exists();
writeTestFile.delete();
} catch (IOException e) {
isWriteable = false;
}
return isWriteable;
}
public boolean setExternalStorageDirectory(String externalStorageDir) {
return settingsAPI.edit(globalPreferences).putString(EXTERNAL_STORAGE_DIR, externalStorageDir).commit();
}

View file

@ -190,24 +190,42 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity {
return true;
}
});
if(Build.VERSION.SDK_INT >= OsmandSettings.VERSION_DEFAULTLOCATION_CHANGED) {
if(!settings.getExternalStorageDirectory().getAbsolutePath().equals(settings.getDefaultExternalStorageLocation())) {
AccessibleAlertBuilder ab = new AccessibleAlertBuilder(this);
ab.setMessage(getString(R.string.android_19_location_disabled, settings.getExternalStorageDirectory()));
ab.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
copyFilesForAndroid19();
if (Build.VERSION.SDK_INT >= OsmandSettings.VERSION_DEFAULTLOCATION_CHANGED) {
final String currentStorage = settings.getExternalStorageDirectory().getAbsolutePath();
String primaryStorage = settings.getDefaultExternalStorageLocation();
if (!currentStorage.startsWith(primaryStorage)) {
// secondary storage
boolean currentDirectoryNotWritable = true;
for (String writeableDirectory : settings.getWritableSecondaryStorageDirectorys()) {
if (currentStorage.startsWith(writeableDirectory)) {
currentDirectoryNotWritable = false;
break;
}
});
ab.setNegativeButton(R.string.default_buttons_cancel, null);
ab.show();
}
}
}
if (currentDirectoryNotWritable) {
currentDirectoryNotWritable = !OsmandSettings.isWritable(settings.getExternalStorageDirectory());
}
if (currentDirectoryNotWritable) {
final String newLoc = settings.getMatchingExternalFilesDir(currentStorage);
if (newLoc != null && newLoc.length() != 0) {
AccessibleAlertBuilder ab = new AccessibleAlertBuilder(this);
ab.setMessage(getString(R.string.android_19_location_disabled,
settings.getExternalStorageDirectory()));
ab.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
copyFilesForAndroid19(newLoc);
}
});
ab.setNegativeButton(R.string.default_buttons_cancel, null);
ab.show();
}
}
}
}
}
private void copyFilesForAndroid19() {
final String newLoc = settings.getDefaultExternalStorageLocation();
private void copyFilesForAndroid19(final String newLoc) {
MoveFilesToDifferentDirectory task =
new MoveFilesToDifferentDirectory(DownloadIndexActivity.this,
new File(settings.getExternalStorageDirectory(), IndexConstants.APP_DIR),

View file

@ -6,6 +6,7 @@ import java.util.Locale;
import net.osmand.CallbackWithObject;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import android.app.Activity;
@ -13,6 +14,7 @@ import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Build;
public class SuggestExternalDirectoryDialog {
@ -22,9 +24,14 @@ public class SuggestExternalDirectoryDialog {
final boolean showOther = otherListener != null;
final OsmandApplication app = (OsmandApplication) a.getApplication();
Builder bld = new AlertDialog.Builder(a);
HashSet<String> externalMounts = getExternalMounts();
HashSet<String> externalMounts;
if(Build.VERSION.SDK_INT < OsmandSettings.VERSION_DEFAULTLOCATION_CHANGED) {
externalMounts = getExternalMounts();
} else {
externalMounts = new HashSet<String>(app.getSettings().getWritableSecondaryStorageDirectorys());
}
String apath = app.getSettings().getExternalStorageDirectory().getAbsolutePath();
externalMounts.add(app.getExternalServiceAPI().getExternalStorageDirectory());
externalMounts.add(app.getSettings().getDefaultExternalStorageLocation());
externalMounts.add(apath);
final String[] extMounts = new String[showOther ? externalMounts.size()+1 : externalMounts.size()];
externalMounts.toArray(extMounts);