Lots of storage changes

Signed-off-by: Mario Danic <mario@lovelyhq.com>
This commit is contained in:
Mario Danic 2018-01-22 11:55:12 +01:00
parent ec13dc8234
commit c1a72a4b4b
8 changed files with 161 additions and 35 deletions

View file

@ -52,6 +52,8 @@ import com.owncloud.android.datamodel.MediaProvider;
import com.owncloud.android.datamodel.SyncedFolder; import com.owncloud.android.datamodel.SyncedFolder;
import com.owncloud.android.datamodel.SyncedFolderProvider; import com.owncloud.android.datamodel.SyncedFolderProvider;
import com.owncloud.android.datamodel.ThumbnailsCacheManager; import com.owncloud.android.datamodel.ThumbnailsCacheManager;
import com.owncloud.android.datastorage.DataStorageProvider;
import com.owncloud.android.datastorage.StoragePoint;
import com.owncloud.android.db.PreferenceManager; import com.owncloud.android.db.PreferenceManager;
import com.owncloud.android.jobs.NCJobCreator; import com.owncloud.android.jobs.NCJobCreator;
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory; import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
@ -214,16 +216,51 @@ public class MainApp extends MultiDexApplication {
} }
private void fixStoragePath() { private void fixStoragePath() {
if (!PreferenceManager.getStoragePathFix(this) && if (!PreferenceManager.getStoragePathFix(this)) {
appPrefs.getInt(WhatsNewActivity.KEY_LAST_SEEN_VERSION_CODE, 0) != 0) { StoragePoint[] storagePoints = DataStorageProvider.getInstance().getAvailableStoragePoints();
String storagePath = appPrefs.getString(Preferences.PreferenceKeys.STORAGE_PATH, ""); String storagePath = appPrefs.getString(Preferences.PreferenceKeys.STORAGE_PATH, "");
if (TextUtils.isEmpty(storagePath)) { if (TextUtils.isEmpty(storagePath)) {
if (appPrefs.getInt(WhatsNewActivity.KEY_LAST_SEEN_VERSION_CODE, 0) != 0) {
/*
We already used the app, but no storage is set - fix that! :)
*/
appPrefs.edit().putString(Preferences.PreferenceKeys.STORAGE_PATH, appPrefs.edit().putString(Preferences.PreferenceKeys.STORAGE_PATH,
Environment.getExternalStorageDirectory().getAbsolutePath()).commit(); Environment.getExternalStorageDirectory().getAbsolutePath()).commit();
appPrefs.edit().remove(PreferenceManager.PREF__KEYS_MIGRATION).commit(); appPrefs.edit().remove(PreferenceManager.PREF__KEYS_MIGRATION).commit();
} else {
// find internal storage path that's indexable
boolean set = false;
for (StoragePoint storagePoint : storagePoints) {
if (storagePoint.getStorageType().equals(StoragePoint.StorageType.INTERNAL) &&
storagePoint.getPrivacyType().equals(StoragePoint.PrivacyType.PUBLIC)) {
appPrefs.edit().putString(Preferences.PreferenceKeys.STORAGE_PATH,
storagePoint.getPath()).commit();
appPrefs.edit().remove(PreferenceManager.PREF__KEYS_MIGRATION).commit();
set = true;
break;
}
}
if (!set) {
for (StoragePoint storagePoint : storagePoints) {
if (storagePoint.getPrivacyType().equals(StoragePoint.PrivacyType.PUBLIC)) {
appPrefs.edit().putString(Preferences.PreferenceKeys.STORAGE_PATH,
Environment.getExternalStorageDirectory().getAbsolutePath()).commit();
appPrefs.edit().remove(PreferenceManager.PREF__KEYS_MIGRATION).commit();
set = true;
break;
}
}
}
} }
PreferenceManager.setStoragePathFix(this, true); PreferenceManager.setStoragePathFix(this, true);
} else { } else {
appPrefs.edit().remove(PreferenceManager.PREF__KEYS_MIGRATION).commit();
PreferenceManager.setStoragePathFix(this, true);
}
} else {
appPrefs.edit().remove(PreferenceManager.PREF__KEYS_MIGRATION).commit();
PreferenceManager.setStoragePathFix(this, true); PreferenceManager.setStoragePathFix(this, true);
} }
} }

View file

@ -21,6 +21,7 @@
package com.owncloud.android.datastorage; package com.owncloud.android.datastorage;
import android.os.Build;
import android.os.Environment; import android.os.Environment;
import com.owncloud.android.MainApp; import com.owncloud.android.MainApp;
@ -31,6 +32,9 @@ import com.owncloud.android.datastorage.providers.MountCommandStoragePointProvid
import com.owncloud.android.datastorage.providers.SystemDefaultStoragePointProvider; import com.owncloud.android.datastorage.providers.SystemDefaultStoragePointProvider;
import com.owncloud.android.datastorage.providers.VDCStoragePointProvider; import com.owncloud.android.datastorage.providers.VDCStoragePointProvider;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector; import java.util.Vector;
/** /**
@ -62,15 +66,59 @@ public class DataStorageProvider {
return mCachedStoragePoints.toArray(new StoragePoint[mCachedStoragePoints.size()]); return mCachedStoragePoints.toArray(new StoragePoint[mCachedStoragePoints.size()]);
} }
List<String> paths = new ArrayList<>();
StoragePoint storagePoint;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
for (File f : MainApp.getAppContext().getExternalMediaDirs()) {
if (f != null && !paths.contains(f.getAbsolutePath())) {
storagePoint = new StoragePoint();
storagePoint.setPath(f.getAbsolutePath());
storagePoint.setDescription(f.getAbsolutePath());
storagePoint.setPrivacyType(StoragePoint.PrivacyType.PUBLIC);
if (f.getAbsolutePath().startsWith("/storage/emulated/0")) {
storagePoint.setStorageType(StoragePoint.StorageType.INTERNAL);
mCachedStoragePoints.add(storagePoint);
} else {
storagePoint.setStorageType(StoragePoint.StorageType.EXTERNAL);
if (isExternalStorageWritable()) {
mCachedStoragePoints.add(storagePoint);
}
}
}
}
} else {
for (IStoragePointProvider p : mStorageProviders) {
if (p.canProvideStoragePoints()) {
mCachedStoragePoints.addAll(p.getAvailableStoragePoint());
}
}
for (int i = 0; i < mCachedStoragePoints.size(); i++) {
paths.add(mCachedStoragePoints.get(i).getPath());
}
}
// Now we go add private ones
// Add internal storage directory // Add internal storage directory
mCachedStoragePoints.add(new StoragePoint(MainApp.getAppContext().getFilesDir().getAbsolutePath(), storagePoint = new StoragePoint();
MainApp.getAppContext().getFilesDir().getAbsolutePath())); storagePoint.setDescription(MainApp.getAppContext().getFilesDir().getAbsolutePath());
storagePoint.setPath(MainApp.getAppContext().getFilesDir().getAbsolutePath());
storagePoint.setPrivacyType(StoragePoint.PrivacyType.PRIVATE);
storagePoint.setStorageType(StoragePoint.StorageType.INTERNAL);
if (!paths.contains(MainApp.getAppContext().getFilesDir().getAbsolutePath())) {
mCachedStoragePoints.add(storagePoint);
}
// Add external storage directory if available. // Add external storage directory if available.
if (isExternalStorageWritable()) { if (isExternalStorageWritable()) {
mCachedStoragePoints.add(new StoragePoint( storagePoint = new StoragePoint();
MainApp.getAppContext().getExternalFilesDir(null).getAbsolutePath(), storagePoint.setPath(MainApp.getAppContext().getExternalFilesDir(null).getAbsolutePath());
MainApp.getAppContext().getExternalFilesDir(null).getAbsolutePath())); storagePoint.setDescription(MainApp.getAppContext().getExternalFilesDir(null).getAbsolutePath());
storagePoint.setPrivacyType(StoragePoint.PrivacyType.PRIVATE);
storagePoint.setStorageType(StoragePoint.StorageType.EXTERNAL);
if (!paths.contains(MainApp.getAppContext().getExternalFilesDir(null).getAbsolutePath())) {
mCachedStoragePoints.add(storagePoint);
}
} }
return mCachedStoragePoints.toArray(new StoragePoint[mCachedStoragePoints.size()]); return mCachedStoragePoints.toArray(new StoragePoint[mCachedStoragePoints.size()]);

View file

@ -25,19 +25,59 @@ package com.owncloud.android.datastorage;
* @author Bartosz Przybylski * @author Bartosz Przybylski
*/ */
public class StoragePoint implements Comparable<StoragePoint> { public class StoragePoint implements Comparable<StoragePoint> {
private String mDescription; public enum StorageType {
private String mPath; INTERNAL, EXTERNAL
public StoragePoint(String description, String path) {
mDescription = description;
mPath = path;
} }
public enum PrivacyType {
PRIVATE, PUBLIC
}
private String mDescription;
private String mPath;
private StorageType mStorageType;
private PrivacyType mPrivacyType;
public StoragePoint() {
}
public StoragePoint(String mDescription, String mPath, StorageType mStorageType, PrivacyType privacyType) {
this.mDescription = mDescription;
this.mPath = mPath;
this.mStorageType = mStorageType;
this.mPrivacyType = privacyType;
}
public StorageType getStorageType() {
return mStorageType;
}
public PrivacyType getPrivacyType() {
return mPrivacyType;
}
public String getPath() { return mPath; } public String getPath() { return mPath; }
public String getDescription() { return mDescription; } public String getDescription() { return mDescription; }
public void setDescription(String description) {
this.mDescription = description;
}
public void setPath(String path) {
this.mPath = path;
}
public void setStorageType(StorageType storageType) {
this.mStorageType = storageType;
}
public void setPrivacyType(PrivacyType privacyType) {
this.mPrivacyType = privacyType;
}
@Override @Override
public int compareTo(StoragePoint another) { public int compareTo(StoragePoint another) {
return mPath.compareTo(another.getPath()); return mPath.compareTo(another.getPath());
} }
} }

View file

@ -53,7 +53,7 @@ public class EnvironmentStoragePointProvider extends AbstractStoragePointProvide
if (env != null) { if (env != null) {
for (String p : env.split(":")) { for (String p : env.split(":")) {
if (canBeAddedToAvailableList(result, p)) { if (canBeAddedToAvailableList(result, p)) {
result.add(new StoragePoint(p, p)); result.add(new StoragePoint(p, p, StoragePoint.StorageType.EXTERNAL, StoragePoint.PrivacyType.PUBLIC));
} }
} }
} }

View file

@ -49,7 +49,7 @@ public class HardcodedStoragePointProvider extends AbstractStoragePointProvider
for (String s : PATHS) { for (String s : PATHS) {
if (canBeAddedToAvailableList(result, s)) { if (canBeAddedToAvailableList(result, s)) {
result.add(new StoragePoint(s, s)); result.add(new StoragePoint(s, s, StoragePoint.StorageType.EXTERNAL, StoragePoint.PrivacyType.PUBLIC));
} }
} }

View file

@ -47,7 +47,7 @@ public class MountCommandStoragePointProvider extends AbstractCommandLineStorage
for (String p : getPotentialPaths(getCommandLineResult())) { for (String p : getPotentialPaths(getCommandLineResult())) {
if (canBeAddedToAvailableList(result, p)) { if (canBeAddedToAvailableList(result, p)) {
result.add(new StoragePoint(p, p)); result.add(new StoragePoint(p, p, StoragePoint.StorageType.EXTERNAL, StoragePoint.PrivacyType.PUBLIC));
} }
} }

View file

@ -43,7 +43,8 @@ public class SystemDefaultStoragePointProvider extends AbstractStoragePointProvi
final String defaultStringDesc = MainApp.getAppContext().getString(R.string.storage_description_default); final String defaultStringDesc = MainApp.getAppContext().getString(R.string.storage_description_default);
// Add private internal storage data directory. // Add private internal storage data directory.
result.add(new StoragePoint(defaultStringDesc, result.add(new StoragePoint(defaultStringDesc,
MainApp.getAppContext().getFilesDir().getAbsolutePath())); MainApp.getAppContext().getFilesDir().getAbsolutePath(), StoragePoint.StorageType.INTERNAL,
StoragePoint.PrivacyType.PRIVATE));
return result; return result;
} }

View file

@ -65,7 +65,7 @@ public class VDCStoragePointProvider extends AbstractCommandLineStoragePoint {
final String path = vdcLine[2]; final String path = vdcLine[2];
if (canBeAddedToAvailableList(result, path)) { if (canBeAddedToAvailableList(result, path)) {
result.add(new StoragePoint(description, path)); result.add(new StoragePoint(description, path, StoragePoint.StorageType.EXTERNAL, StoragePoint.PrivacyType.PRIVATE));
} }
} catch (NumberFormatException e) { } catch (NumberFormatException e) {