diff --git a/src/main/java/com/owncloud/android/MainApp.java b/src/main/java/com/owncloud/android/MainApp.java index 079d338688..ce72321e7f 100644 --- a/src/main/java/com/owncloud/android/MainApp.java +++ b/src/main/java/com/owncloud/android/MainApp.java @@ -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); } } diff --git a/src/main/java/com/owncloud/android/datastorage/DataStorageProvider.java b/src/main/java/com/owncloud/android/datastorage/DataStorageProvider.java index 13805e1579..d1b19647cc 100644 --- a/src/main/java/com/owncloud/android/datastorage/DataStorageProvider.java +++ b/src/main/java/com/owncloud/android/datastorage/DataStorageProvider.java @@ -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 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()]); diff --git a/src/main/java/com/owncloud/android/datastorage/StoragePoint.java b/src/main/java/com/owncloud/android/datastorage/StoragePoint.java index b3083d7676..db201da106 100644 --- a/src/main/java/com/owncloud/android/datastorage/StoragePoint.java +++ b/src/main/java/com/owncloud/android/datastorage/StoragePoint.java @@ -25,19 +25,59 @@ package com.owncloud.android.datastorage; * @author Bartosz Przybylski */ public class StoragePoint implements Comparable { - 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()); } + + } diff --git a/src/main/java/com/owncloud/android/datastorage/providers/EnvironmentStoragePointProvider.java b/src/main/java/com/owncloud/android/datastorage/providers/EnvironmentStoragePointProvider.java index 8daffc6fce..039720d3d0 100644 --- a/src/main/java/com/owncloud/android/datastorage/providers/EnvironmentStoragePointProvider.java +++ b/src/main/java/com/owncloud/android/datastorage/providers/EnvironmentStoragePointProvider.java @@ -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)); } } } diff --git a/src/main/java/com/owncloud/android/datastorage/providers/HardcodedStoragePointProvider.java b/src/main/java/com/owncloud/android/datastorage/providers/HardcodedStoragePointProvider.java index 0398d7de5e..d6deebad84 100644 --- a/src/main/java/com/owncloud/android/datastorage/providers/HardcodedStoragePointProvider.java +++ b/src/main/java/com/owncloud/android/datastorage/providers/HardcodedStoragePointProvider.java @@ -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)); } } diff --git a/src/main/java/com/owncloud/android/datastorage/providers/MountCommandStoragePointProvider.java b/src/main/java/com/owncloud/android/datastorage/providers/MountCommandStoragePointProvider.java index be7dc23356..c0fe761511 100644 --- a/src/main/java/com/owncloud/android/datastorage/providers/MountCommandStoragePointProvider.java +++ b/src/main/java/com/owncloud/android/datastorage/providers/MountCommandStoragePointProvider.java @@ -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)); } } diff --git a/src/main/java/com/owncloud/android/datastorage/providers/SystemDefaultStoragePointProvider.java b/src/main/java/com/owncloud/android/datastorage/providers/SystemDefaultStoragePointProvider.java index 4fc32acca4..cbaee1a1b8 100644 --- a/src/main/java/com/owncloud/android/datastorage/providers/SystemDefaultStoragePointProvider.java +++ b/src/main/java/com/owncloud/android/datastorage/providers/SystemDefaultStoragePointProvider.java @@ -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 . + * You should have received a copy of the GNU Affero General Public + * License along with this program. If not, see . */ 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; } diff --git a/src/main/java/com/owncloud/android/datastorage/providers/VDCStoragePointProvider.java b/src/main/java/com/owncloud/android/datastorage/providers/VDCStoragePointProvider.java index 200996ff2f..5be6c78432 100644 --- a/src/main/java/com/owncloud/android/datastorage/providers/VDCStoragePointProvider.java +++ b/src/main/java/com/owncloud/android/datastorage/providers/VDCStoragePointProvider.java @@ -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) {