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.SyncedFolderProvider;
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.jobs.NCJobCreator;
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
@ -214,16 +216,51 @@ public class MainApp extends MultiDexApplication {
}
private void fixStoragePath() {
if (!PreferenceManager.getStoragePathFix(this) &&
appPrefs.getInt(WhatsNewActivity.KEY_LAST_SEEN_VERSION_CODE, 0) != 0) {
if (!PreferenceManager.getStoragePathFix(this)) {
StoragePoint[] storagePoints = DataStorageProvider.getInstance().getAvailableStoragePoints();
String storagePath = appPrefs.getString(Preferences.PreferenceKeys.STORAGE_PATH, "");
if (TextUtils.isEmpty(storagePath)) {
appPrefs.edit().putString(Preferences.PreferenceKeys.STORAGE_PATH,
Environment.getExternalStorageDirectory().getAbsolutePath()).commit();
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,
Environment.getExternalStorageDirectory().getAbsolutePath()).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);
} else {
appPrefs.edit().remove(PreferenceManager.PREF__KEYS_MIGRATION).commit();
PreferenceManager.setStoragePathFix(this, true);
}
PreferenceManager.setStoragePathFix(this, true);
} else {
appPrefs.edit().remove(PreferenceManager.PREF__KEYS_MIGRATION).commit();
PreferenceManager.setStoragePathFix(this, true);
}
}

View file

@ -21,6 +21,7 @@
package com.owncloud.android.datastorage;
import android.os.Build;
import android.os.Environment;
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.VDCStoragePointProvider;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
/**
@ -62,15 +66,59 @@ public class DataStorageProvider {
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
mCachedStoragePoints.add(new StoragePoint(MainApp.getAppContext().getFilesDir().getAbsolutePath(),
MainApp.getAppContext().getFilesDir().getAbsolutePath()));
storagePoint = new StoragePoint();
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.
if (isExternalStorageWritable()) {
mCachedStoragePoints.add(new StoragePoint(
MainApp.getAppContext().getExternalFilesDir(null).getAbsolutePath(),
MainApp.getAppContext().getExternalFilesDir(null).getAbsolutePath()));
storagePoint = new StoragePoint();
storagePoint.setPath(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()]);

View file

@ -25,19 +25,59 @@ package com.owncloud.android.datastorage;
* @author Bartosz Przybylski
*/
public class StoragePoint implements Comparable<StoragePoint> {
private String mDescription;
private String mPath;
public StoragePoint(String description, String path) {
mDescription = description;
mPath = path;
public enum StorageType {
INTERNAL, EXTERNAL
}
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 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
public int compareTo(StoragePoint another) {
return mPath.compareTo(another.getPath());
}
}

View file

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

View file

@ -1,22 +1,22 @@
/**
* Nextcloud Android client application
* Nextcloud Android client application
*
* @author Bartosz Przybylski
* Copyright (C) 2016 Nextcloud
* Copyright (C) 2016 Bartosz Przybylski
* @author Bartosz Przybylski
* Copyright (C) 2016 Nextcloud
* Copyright (C) 2016 Bartosz Przybylski
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
* You should have received a copy of the GNU Affero General Public
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.owncloud.android.datastorage.providers;
@ -43,7 +43,8 @@ public class SystemDefaultStoragePointProvider extends AbstractStoragePointProvi
final String defaultStringDesc = MainApp.getAppContext().getString(R.string.storage_description_default);
// Add private internal storage data directory.
result.add(new StoragePoint(defaultStringDesc,
MainApp.getAppContext().getFilesDir().getAbsolutePath()));
MainApp.getAppContext().getFilesDir().getAbsolutePath(), StoragePoint.StorageType.INTERNAL,
StoragePoint.PrivacyType.PRIVATE));
return result;
}

View file

@ -65,7 +65,7 @@ public class VDCStoragePointProvider extends AbstractCommandLineStoragePoint {
final String path = vdcLine[2];
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) {