From 845e85fb24c1890c9186251dc6fe75524b0a9ac4 Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Thu, 11 Jan 2018 20:18:05 +0100 Subject: [PATCH 01/11] Most improvements Signed-off-by: Mario Danic --- .../com/owncloud/android/utils/PushUtils.java | 4 +- .../java/com/owncloud/android/MainApp.java | 4 +- .../owncloud/android/datamodel/OCFile.java | 42 +++++++++++-------- .../datastorage/DataStorageProvider.java | 30 ++++++------- .../SystemDefaultStoragePointProvider.java | 16 ++----- .../android/ui/activity/Preferences.java | 5 ++- .../ui/helpers/FileOperationsHelper.java | 20 ++++++++- src/main/res/values/arrays.xml | 32 ++++++++++++++ src/main/res/xml/exposed_filepaths.xml | 1 + .../com/owncloud/android/utils/PushUtils.java | 4 +- 10 files changed, 103 insertions(+), 55 deletions(-) create mode 100644 src/main/res/values/arrays.xml diff --git a/src/gplay/java/com/owncloud/android/utils/PushUtils.java b/src/gplay/java/com/owncloud/android/utils/PushUtils.java index bd6ded231111..9cb2f927ef8a 100644 --- a/src/gplay/java/com/owncloud/android/utils/PushUtils.java +++ b/src/gplay/java/com/owncloud/android/utils/PushUtils.java @@ -99,7 +99,7 @@ public static String bytesToHex(byte[] bytes) { } private static int generateRsa2048KeyPair() { - String keyPath = MainApp.getStoragePath() + File.separator + MainApp.getDataFolder() + File.separator + String keyPath = MainApp.getAppContext().getFilesDir().getAbsolutePath() + File.separator + MainApp.getDataFolder() + File.separator + KEYPAIR_FOLDER; String privateKeyPath = keyPath + File.separator + KEYPAIR_FILE_NAME + KEYPAIR_PRIV_EXTENSION; @@ -271,7 +271,7 @@ public static void pushRegistrationToServer() { } public static Key readKeyFromFile(boolean readPublicKey) { - String keyPath = MainApp.getStoragePath() + File.separator + MainApp.getDataFolder() + File.separator + String keyPath = MainApp.getAppContext().getFilesDir().getAbsolutePath() + File.separator + MainApp.getDataFolder() + File.separator + KEYPAIR_FOLDER; String privateKeyPath = keyPath + File.separator + KEYPAIR_FILE_NAME + KEYPAIR_PRIV_EXTENSION; diff --git a/src/main/java/com/owncloud/android/MainApp.java b/src/main/java/com/owncloud/android/MainApp.java index 2fa65ec6b7b9..c9edeef95423 100644 --- a/src/main/java/com/owncloud/android/MainApp.java +++ b/src/main/java/com/owncloud/android/MainApp.java @@ -115,8 +115,8 @@ public void onCreate() { SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); - MainApp.storagePath = appPrefs.getString(Preferences.PreferenceKeys.STORAGE_PATH, Environment. - getExternalStorageDirectory().getAbsolutePath()); + MainApp.storagePath = appPrefs.getString(Preferences.PreferenceKeys.STORAGE_PATH, + getApplicationContext().getFilesDir().getAbsolutePath()); boolean isSamlAuth = AUTH_ON.equals(getString(R.string.auth_method_saml_web_sso)); diff --git a/src/main/java/com/owncloud/android/datamodel/OCFile.java b/src/main/java/com/owncloud/android/datamodel/OCFile.java index 2e60fced69fa..c65359cd83c5 100644 --- a/src/main/java/com/owncloud/android/datamodel/OCFile.java +++ b/src/main/java/com/owncloud/android/datamodel/OCFile.java @@ -28,7 +28,9 @@ import android.net.Uri; import android.os.Parcel; import android.os.Parcelable; +import android.support.v4.content.FileProvider; +import com.owncloud.android.R; import com.owncloud.android.lib.common.network.WebdavUtils; import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.utils.MimeType; @@ -316,6 +318,19 @@ public Uri getStorageUri() { return mLocalUri; } + + public Uri getLegacyExposedFileUri(Context context) { + if (mLocalPath == null || mLocalPath.length() == 0) { + return null; + } + + if (mExposedFileUri == null) { + return Uri.parse(ContentResolver.SCHEME_FILE + "://" + WebdavUtils.encodePath(mLocalPath)); + } + + return mExposedFileUri; + + } /* Partly disabled because not all apps understand paths that we get via this method for now */ @@ -324,25 +339,16 @@ public Uri getExposedFileUri(Context context) { return null; } if (mExposedFileUri == null) { - /*if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { - // TODO - use FileProvider with any Android version, with deeper testing -> 2.2.0 - mExposedFileUri = Uri.parse( - ContentResolver.SCHEME_FILE + "://" + WebdavUtils.encodePath(mLocalPath) - ); - } else { - // Use the FileProvider to get a content URI - try { - mExposedFileUri = FileProvider.getUriForFile( - context, - context.getString(R.string.file_provider_authority), - new File(mLocalPath) - ); - } catch (IllegalArgumentException e) { - Log_OC.e(TAG, "File can't be exported"); - } + try { + mExposedFileUri = FileProvider.getUriForFile( + context, + context.getString(R.string.file_provider_authority), + new File(mLocalPath)); + } catch (IllegalArgumentException ex) { + // Could not share file using FileProvider URI scheme. + // Fall back to legacy URI parsing. + getLegacyExposedFileUri(context); } - }*/ - return Uri.parse(ContentResolver.SCHEME_FILE + "://" + WebdavUtils.encodePath(mLocalPath)); } return mExposedFileUri; diff --git a/src/main/java/com/owncloud/android/datastorage/DataStorageProvider.java b/src/main/java/com/owncloud/android/datastorage/DataStorageProvider.java index 0ee3d23e2f24..632ede992974 100644 --- a/src/main/java/com/owncloud/android/datastorage/DataStorageProvider.java +++ b/src/main/java/com/owncloud/android/datastorage/DataStorageProvider.java @@ -21,7 +21,7 @@ package com.owncloud.android.datastorage; -import android.os.Build; +import android.os.Environment; import com.owncloud.android.MainApp; import com.owncloud.android.datastorage.providers.EnvironmentStoragePointProvider; @@ -31,7 +31,6 @@ import com.owncloud.android.datastorage.providers.SystemDefaultStoragePointProvider; import com.owncloud.android.datastorage.providers.VDCStoragePointProvider; -import java.io.File; import java.util.Vector; /** @@ -63,18 +62,14 @@ public StoragePoint[] getAvailableStoragePoints() { return mCachedStoragePoints.toArray(new StoragePoint[mCachedStoragePoints.size()]); } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - for (File f : MainApp.getAppContext().getExternalMediaDirs()) { - if (f != null) { - mCachedStoragePoints.add(new StoragePoint(f.getAbsolutePath(), f.getAbsolutePath())); - } - } - } else { - for (IStoragePointProvider p : mStorageProviders) { - if (p.canProvideStoragePoints()) { - mCachedStoragePoints.addAll(p.getAvailableStoragePoint()); - } - } + // Add internal storage directory + mCachedStoragePoints.add(new StoragePoint(MainApp.getAppContext().getFilesDir().getAbsolutePath(), + MainApp.getAppContext().getFilesDir().getAbsolutePath())); + + // Add external storage directory if available. + if (isExternalStorageWritable()) { + mCachedStoragePoints.add(new StoragePoint(MainApp.getAppContext().getExternalFilesDir(null).getAbsolutePath(), + MainApp.getAppContext().getExternalFilesDir(null).getAbsolutePath())); } return mCachedStoragePoints.toArray(new StoragePoint[mCachedStoragePoints.size()]); @@ -97,4 +92,11 @@ public void addStoragePointProvider(IStoragePointProvider provider) { public void removeStoragePointProvider(IStoragePointProvider provider) { mStorageProviders.remove(provider); } + + /* Checks if external storage is available for read and write */ + public boolean isExternalStorageWritable() { + String state = Environment.getExternalStorageState(); + return Environment.MEDIA_MOUNTED.equals(state); + } + } 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 4a06ed7c965e..4fc32acca4f2 100644 --- a/src/main/java/com/owncloud/android/datastorage/providers/SystemDefaultStoragePointProvider.java +++ b/src/main/java/com/owncloud/android/datastorage/providers/SystemDefaultStoragePointProvider.java @@ -25,11 +25,8 @@ import com.owncloud.android.R; import com.owncloud.android.datastorage.StoragePoint; -import java.io.File; import java.util.Vector; -import static android.os.Environment.getExternalStorageDirectory; - /** * @author Bartosz Przybylski */ @@ -44,16 +41,9 @@ public Vector getAvailableStoragePoint() { Vector result = new Vector<>(); final String defaultStringDesc = MainApp.getAppContext().getString(R.string.storage_description_default); - File path; - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { - path = MainApp.getAppContext().getExternalMediaDirs()[0]; - } else { - path = getExternalStorageDirectory(); - } - - if (path != null && path.canWrite()) { - result.add(new StoragePoint(defaultStringDesc, path.getAbsolutePath())); - } + // Add private internal storage data directory. + result.add(new StoragePoint(defaultStringDesc, + MainApp.getAppContext().getFilesDir().getAbsolutePath())); return result; } diff --git a/src/main/java/com/owncloud/android/ui/activity/Preferences.java b/src/main/java/com/owncloud/android/ui/activity/Preferences.java index 361253e9e333..1330072f78b2 100644 --- a/src/main/java/com/owncloud/android/ui/activity/Preferences.java +++ b/src/main/java/com/owncloud/android/ui/activity/Preferences.java @@ -1017,8 +1017,9 @@ private void saveStoragePath(String newStoragePath) { private void loadStoragePath() { SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); - mStoragePath = appPrefs.getString(PreferenceKeys.STORAGE_PATH, Environment.getExternalStorageDirectory() - .getAbsolutePath()); + // Load storage path from shared preferences. Use private internal storage by default. + mStoragePath = appPrefs.getString(PreferenceKeys.STORAGE_PATH, + getApplicationContext().getFilesDir().getAbsolutePath()); String storageDescription = DataStorageProvider.getInstance().getStorageDescriptionByPath(mStoragePath); mPrefStoragePath.setSummary(storageDescription); } diff --git a/src/main/java/com/owncloud/android/ui/helpers/FileOperationsHelper.java b/src/main/java/com/owncloud/android/ui/helpers/FileOperationsHelper.java index e8eb02aa99df..4bd5ea775630 100755 --- a/src/main/java/com/owncloud/android/ui/helpers/FileOperationsHelper.java +++ b/src/main/java/com/owncloud/android/ui/helpers/FileOperationsHelper.java @@ -72,6 +72,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.regex.Matcher; @@ -197,6 +198,20 @@ public void openFile(OCFile file) { if (file != null) { String storagePath = file.getStoragePath(); + String[] officeExtensions = MainApp.getAppContext().getResources().getStringArray(R.array + .ms_office_extensions); + + Uri fileUri; + + if (file.getFileName().contains(".") && + Arrays.asList(officeExtensions).contains(file.getFileName().substring(file.getFileName(). + lastIndexOf(".") + 1, file.getFileName().length())) && + !file.getStoragePath().startsWith(MainApp.getAppContext().getFilesDir().getAbsolutePath())) { + fileUri = file.getLegacyExposedFileUri(mFileActivity); + } else { + fileUri = file.getExposedFileUri(mFileActivity); + } + Intent openFileWithIntent = null; int lastIndexOfDot = storagePath.lastIndexOf('.'); if (lastIndexOfDot >= 0) { @@ -204,8 +219,9 @@ public void openFile(OCFile file) { String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExt); if (guessedMimeType != null) { openFileWithIntent = new Intent(Intent.ACTION_VIEW); + openFileWithIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); openFileWithIntent.setDataAndType( - file.getExposedFileUri(mFileActivity), + fileUri, guessedMimeType ); } @@ -218,7 +234,7 @@ public void openFile(OCFile file) { if (openFileWithIntent == null) { openFileWithIntent = new Intent(Intent.ACTION_VIEW); openFileWithIntent.setDataAndType( - file.getExposedFileUri(mFileActivity), + fileUri, file.getMimetype() ); } diff --git a/src/main/res/values/arrays.xml b/src/main/res/values/arrays.xml new file mode 100644 index 000000000000..a8f68d627c70 --- /dev/null +++ b/src/main/res/values/arrays.xml @@ -0,0 +1,32 @@ + + + + doc + dot + docx + dotx + docm + dotm + xls + xlt + xla + xlsx + xltx + xlsm + xltm + xlam + xlsb + ppt + pot + pps + ppa + pptx + potx + ppsx + ppam + pptm + potm + ppsm + mdb + + \ No newline at end of file diff --git a/src/main/res/xml/exposed_filepaths.xml b/src/main/res/xml/exposed_filepaths.xml index 9a47df8a0bc6..154694811d50 100644 --- a/src/main/res/xml/exposed_filepaths.xml +++ b/src/main/res/xml/exposed_filepaths.xml @@ -1,5 +1,6 @@ + diff --git a/src/modified/java/com/owncloud/android/utils/PushUtils.java b/src/modified/java/com/owncloud/android/utils/PushUtils.java index 1a86197718f5..4e53c88ca079 100644 --- a/src/modified/java/com/owncloud/android/utils/PushUtils.java +++ b/src/modified/java/com/owncloud/android/utils/PushUtils.java @@ -99,7 +99,7 @@ public static String bytesToHex(byte[] bytes) { } private static int generateRsa2048KeyPair() { - String keyPath = MainApp.getStoragePath() + File.separator + MainApp.getDataFolder() + File.separator + String keyPath = MainApp.getAppContext().getFilesDir().getAbsolutePath() + File.separator + MainApp.getDataFolder() + File.separator + KEYPAIR_FOLDER; String privateKeyPath = keyPath + File.separator + KEYPAIR_FILE_NAME + KEYPAIR_PRIV_EXTENSION; @@ -267,7 +267,7 @@ public static void pushRegistrationToServer() { } public static Key readKeyFromFile(boolean readPublicKey) { - String keyPath = MainApp.getStoragePath() + File.separator + MainApp.getDataFolder() + File.separator + String keyPath = MainApp.getAppContext().getFilesDir().getAbsolutePath() + File.separator + MainApp.getDataFolder() + File.separator + KEYPAIR_FOLDER; String privateKeyPath = keyPath + File.separator + KEYPAIR_FILE_NAME + KEYPAIR_PRIV_EXTENSION; From d56f53bb1159efa88b043ac5cf94a6444df42d41 Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Thu, 11 Jan 2018 20:19:28 +0100 Subject: [PATCH 02/11] Fix uploads Signed-off-by: Mario Danic --- .../com/owncloud/android/providers/FileContentProvider.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/owncloud/android/providers/FileContentProvider.java b/src/main/java/com/owncloud/android/providers/FileContentProvider.java index 49a4afa1e4f1..0970a845f2cd 100644 --- a/src/main/java/com/owncloud/android/providers/FileContentProvider.java +++ b/src/main/java/com/owncloud/android/providers/FileContentProvider.java @@ -621,8 +621,8 @@ private Cursor query( // if both are null, let them pass to query if (selectionArgs == null && selection != null) { - selectionArgs = new String[]{selection}; selection = "(?)"; + selectionArgs = new String[]{selection}; } sqlQuery.setStrict(true); From ee7d4ed56c276961a9b74c4a827d07834ffd8b2e Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Thu, 11 Jan 2018 20:55:16 +0100 Subject: [PATCH 03/11] Implement keys migration Signed-off-by: Mario Danic --- .../com/owncloud/android/utils/PushUtils.java | 1 + .../java/com/owncloud/android/MainApp.java | 27 ++++++++++++++++++- .../android/db/PreferenceManager.java | 10 +++++++ .../com/owncloud/android/utils/PushUtils.java | 1 + 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/gplay/java/com/owncloud/android/utils/PushUtils.java b/src/gplay/java/com/owncloud/android/utils/PushUtils.java index 9cb2f927ef8a..9ab7d6065849 100644 --- a/src/gplay/java/com/owncloud/android/utils/PushUtils.java +++ b/src/gplay/java/com/owncloud/android/utils/PushUtils.java @@ -99,6 +99,7 @@ public static String bytesToHex(byte[] bytes) { } private static int generateRsa2048KeyPair() { + MainApp.migratePushKeys(); String keyPath = MainApp.getAppContext().getFilesDir().getAbsolutePath() + File.separator + MainApp.getDataFolder() + File.separator + KEYPAIR_FOLDER; diff --git a/src/main/java/com/owncloud/android/MainApp.java b/src/main/java/com/owncloud/android/MainApp.java index c9edeef95423..885c7f690f1c 100644 --- a/src/main/java/com/owncloud/android/MainApp.java +++ b/src/main/java/com/owncloud/android/MainApp.java @@ -33,7 +33,6 @@ import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; -import android.os.Environment; import android.os.StrictMode; import android.support.annotation.StringRes; import android.support.multidex.MultiDexApplication; @@ -66,6 +65,7 @@ import com.owncloud.android.utils.PermissionUtil; import com.owncloud.android.utils.ReceiversHelper; +import java.io.File; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; @@ -238,6 +238,31 @@ public static void initAutoUpload() { } } + public static void migratePushKeys() { + Context context = getAppContext(); + + if (!PreferenceManager.getKeysMigration(context)) { + String oldKeyPath = getStoragePath() + File.separator + MainApp.getDataFolder() + File.separator + "nc-keypair"; + File oldPrivateKey = new File(oldKeyPath + File.separator + "push_key.priv"); + File oldPublicKey = new File(oldKeyPath + File.separator + "push_key.pub"); + + String keyPath = getAppContext().getFilesDir().getAbsolutePath() + + File.separator + MainApp.getDataFolder() + File.separator + "nc-keypair"; + File privateKey = new File(keyPath + File.separator + "push_key.priv"); + File publicKey = new File(keyPath + File.separator + "push_key.pub"); + + if (oldPrivateKey.exists() && oldPublicKey.exists()) { + if (oldPrivateKey.renameTo(privateKey) && oldPublicKey.renameTo(publicKey)) { + PreferenceManager.setKeysMigration(context, true); + } else { + PreferenceManager.setKeysMigration(context, false); + } + } else { + PreferenceManager.setKeysMigration(context, true); + } + } + } + public static void notificationChannels() { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O && getAppContext() != null) { Context context = getAppContext(); diff --git a/src/main/java/com/owncloud/android/db/PreferenceManager.java b/src/main/java/com/owncloud/android/db/PreferenceManager.java index b3f5a3cd3c9f..ae590dd808e9 100644 --- a/src/main/java/com/owncloud/android/db/PreferenceManager.java +++ b/src/main/java/com/owncloud/android/db/PreferenceManager.java @@ -53,6 +53,7 @@ public abstract class PreferenceManager { private static final String PREF__INSTANT_VIDEO_UPLOAD_PATH_USE_SUBFOLDERS = "instant_video_upload_path_use_subfolders"; private static final String PREF__LEGACY_CLEAN = "legacyClean"; + private static final String PREF__KEYS_MIGRATION = "keysMigration"; private static final String PREF__AUTO_UPLOAD_UPDATE_PATH = "autoUploadPathUpdate"; private static final String PREF__PUSH_TOKEN = "pushToken"; private static final String PREF__AUTO_UPLOAD_SPLIT_OUT = "autoUploadEntriesSplitOut"; @@ -286,6 +287,11 @@ public static boolean getLegacyClean(Context context) { return getDefaultSharedPreferences(context).getBoolean(PREF__LEGACY_CLEAN, false); } + public static boolean getKeysMigration(Context context) { + return getDefaultSharedPreferences(context).getBoolean(PREF__KEYS_MIGRATION, false); + } + + /** * Gets the auto upload paths flag last set. * @@ -316,6 +322,10 @@ public static void setLegacyClean(Context context, boolean legacyClean) { saveBooleanPreference(context, PREF__LEGACY_CLEAN, legacyClean); } + public static void setKeysMigration(Context context, boolean keysMigration) { + saveBooleanPreference(context, PREF__KEYS_MIGRATION, keysMigration); + } + public static void setAutoUploadInit(Context context, boolean autoUploadInit) { saveBooleanPreference(context, PREF__AUTO_UPLOAD_INIT, autoUploadInit); } diff --git a/src/modified/java/com/owncloud/android/utils/PushUtils.java b/src/modified/java/com/owncloud/android/utils/PushUtils.java index 4e53c88ca079..b80cb7155760 100644 --- a/src/modified/java/com/owncloud/android/utils/PushUtils.java +++ b/src/modified/java/com/owncloud/android/utils/PushUtils.java @@ -99,6 +99,7 @@ public static String bytesToHex(byte[] bytes) { } private static int generateRsa2048KeyPair() { + MainApp.migratePushKeys(); String keyPath = MainApp.getAppContext().getFilesDir().getAbsolutePath() + File.separator + MainApp.getDataFolder() + File.separator + KEYPAIR_FOLDER; From 4c6a98d8af9888a815295a5032abf1165c089528 Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Thu, 11 Jan 2018 20:57:47 +0100 Subject: [PATCH 04/11] Add new line Signed-off-by: Mario Danic --- src/main/res/values/arrays.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/res/values/arrays.xml b/src/main/res/values/arrays.xml index a8f68d627c70..4853d401e4e1 100644 --- a/src/main/res/values/arrays.xml +++ b/src/main/res/values/arrays.xml @@ -29,4 +29,4 @@ ppsm mdb - \ No newline at end of file + From 66711f215c59c91571137e431d65e0bfec37699f Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Fri, 12 Jan 2018 00:45:10 +0100 Subject: [PATCH 05/11] Fix code review issues Signed-off-by: Mario Danic --- .../java/com/owncloud/android/MainApp.java | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/owncloud/android/MainApp.java b/src/main/java/com/owncloud/android/MainApp.java index 885c7f690f1c..98a552a3ed7b 100644 --- a/src/main/java/com/owncloud/android/MainApp.java +++ b/src/main/java/com/owncloud/android/MainApp.java @@ -243,22 +243,28 @@ public static void migratePushKeys() { if (!PreferenceManager.getKeysMigration(context)) { String oldKeyPath = getStoragePath() + File.separator + MainApp.getDataFolder() + File.separator + "nc-keypair"; - File oldPrivateKey = new File(oldKeyPath + File.separator + "push_key.priv"); - File oldPublicKey = new File(oldKeyPath + File.separator + "push_key.pub"); + File oldPrivateKey = new File(oldKeyPath, "push_key.priv"); + File oldPublicKey = new File(oldKeyPath, "push_key.pub"); String keyPath = getAppContext().getFilesDir().getAbsolutePath() + File.separator + MainApp.getDataFolder() + File.separator + "nc-keypair"; - File privateKey = new File(keyPath + File.separator + "push_key.priv"); - File publicKey = new File(keyPath + File.separator + "push_key.pub"); + File privateKey = new File(keyPath, "push_key.priv"); + File publicKey = new File(keyPath, "push_key.pub"); - if (oldPrivateKey.exists() && oldPublicKey.exists()) { - if (oldPrivateKey.renameTo(privateKey) && oldPublicKey.renameTo(publicKey)) { + if ((privateKey.exists() && publicKey.exists()) || (!oldPrivateKey.exists() && !oldPublicKey.exists())) { + PreferenceManager.setKeysMigration(context, true); + } else { + if (oldPrivateKey.exists()) { + oldPrivateKey.renameTo(privateKey); + } + + if (oldPublicKey.exists()) { + oldPublicKey.renameTo(publicKey); + } + + if (privateKey.exists() && publicKey.exists()) { PreferenceManager.setKeysMigration(context, true); - } else { - PreferenceManager.setKeysMigration(context, false); } - } else { - PreferenceManager.setKeysMigration(context, true); } } } From 45f7cb1cc70fe71beef99686c83cf953b166d151 Mon Sep 17 00:00:00 2001 From: tobiaskaminsky Date: Fri, 12 Jan 2018 17:43:32 +0100 Subject: [PATCH 06/11] linebreaks Signed-off-by: tobiaskaminsky --- .../java/com/owncloud/android/utils/PushUtils.java | 14 +++++++------- src/main/java/com/owncloud/android/MainApp.java | 3 ++- .../android/datastorage/DataStorageProvider.java | 3 ++- .../java/com/owncloud/android/utils/PushUtils.java | 14 +++++++------- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/gplay/java/com/owncloud/android/utils/PushUtils.java b/src/gplay/java/com/owncloud/android/utils/PushUtils.java index 9ab7d6065849..ac2d78ae551e 100644 --- a/src/gplay/java/com/owncloud/android/utils/PushUtils.java +++ b/src/gplay/java/com/owncloud/android/utils/PushUtils.java @@ -1,4 +1,4 @@ -/** +/* * Nextcloud Android client application * * @author Mario Danic @@ -100,8 +100,8 @@ public static String bytesToHex(byte[] bytes) { private static int generateRsa2048KeyPair() { MainApp.migratePushKeys(); - String keyPath = MainApp.getAppContext().getFilesDir().getAbsolutePath() + File.separator + MainApp.getDataFolder() + File.separator - + KEYPAIR_FOLDER; + String keyPath = MainApp.getAppContext().getFilesDir().getAbsolutePath() + File.separator + + MainApp.getDataFolder() + File.separator + KEYPAIR_FOLDER; String privateKeyPath = keyPath + File.separator + KEYPAIR_FILE_NAME + KEYPAIR_PRIV_EXTENSION; String publicKeyPath = keyPath + File.separator + KEYPAIR_FILE_NAME + KEYPAIR_PUB_EXTENSION; @@ -228,8 +228,8 @@ public static void pushRegistrationToServer() { publicKey, context.getResources().getString(R.string.push_server_url)); - RemoteOperationResult remoteOperationResult = registerAccountDeviceForNotificationsOperation. - execute(mClient); + RemoteOperationResult remoteOperationResult = + registerAccountDeviceForNotificationsOperation.execute(mClient); if (remoteOperationResult.isSuccess()) { PushResponse pushResponse = remoteOperationResult.getPushResponseData(); @@ -272,8 +272,8 @@ public static void pushRegistrationToServer() { } public static Key readKeyFromFile(boolean readPublicKey) { - String keyPath = MainApp.getAppContext().getFilesDir().getAbsolutePath() + File.separator + MainApp.getDataFolder() + File.separator - + KEYPAIR_FOLDER; + String keyPath = MainApp.getAppContext().getFilesDir().getAbsolutePath() + File.separator + + MainApp.getDataFolder() + File.separator + KEYPAIR_FOLDER; String privateKeyPath = keyPath + File.separator + KEYPAIR_FILE_NAME + KEYPAIR_PRIV_EXTENSION; String publicKeyPath = keyPath + File.separator + KEYPAIR_FILE_NAME + KEYPAIR_PUB_EXTENSION; diff --git a/src/main/java/com/owncloud/android/MainApp.java b/src/main/java/com/owncloud/android/MainApp.java index 98a552a3ed7b..63e1da5ba116 100644 --- a/src/main/java/com/owncloud/android/MainApp.java +++ b/src/main/java/com/owncloud/android/MainApp.java @@ -242,7 +242,8 @@ public static void migratePushKeys() { Context context = getAppContext(); if (!PreferenceManager.getKeysMigration(context)) { - String oldKeyPath = getStoragePath() + File.separator + MainApp.getDataFolder() + File.separator + "nc-keypair"; + String oldKeyPath = getStoragePath() + File.separator + MainApp.getDataFolder() + + File.separator + "nc-keypair"; File oldPrivateKey = new File(oldKeyPath, "push_key.priv"); File oldPublicKey = new File(oldKeyPath, "push_key.pub"); diff --git a/src/main/java/com/owncloud/android/datastorage/DataStorageProvider.java b/src/main/java/com/owncloud/android/datastorage/DataStorageProvider.java index 632ede992974..13805e1579f1 100644 --- a/src/main/java/com/owncloud/android/datastorage/DataStorageProvider.java +++ b/src/main/java/com/owncloud/android/datastorage/DataStorageProvider.java @@ -68,7 +68,8 @@ public StoragePoint[] getAvailableStoragePoints() { // Add external storage directory if available. if (isExternalStorageWritable()) { - mCachedStoragePoints.add(new StoragePoint(MainApp.getAppContext().getExternalFilesDir(null).getAbsolutePath(), + mCachedStoragePoints.add(new StoragePoint( + MainApp.getAppContext().getExternalFilesDir(null).getAbsolutePath(), MainApp.getAppContext().getExternalFilesDir(null).getAbsolutePath())); } diff --git a/src/modified/java/com/owncloud/android/utils/PushUtils.java b/src/modified/java/com/owncloud/android/utils/PushUtils.java index b80cb7155760..6386318606b1 100644 --- a/src/modified/java/com/owncloud/android/utils/PushUtils.java +++ b/src/modified/java/com/owncloud/android/utils/PushUtils.java @@ -1,4 +1,4 @@ -/** +/* * Nextcloud Android client application * * @author Mario Danic @@ -100,8 +100,8 @@ public static String bytesToHex(byte[] bytes) { private static int generateRsa2048KeyPair() { MainApp.migratePushKeys(); - String keyPath = MainApp.getAppContext().getFilesDir().getAbsolutePath() + File.separator + MainApp.getDataFolder() + File.separator - + KEYPAIR_FOLDER; + String keyPath = MainApp.getAppContext().getFilesDir().getAbsolutePath() + File.separator + + MainApp.getDataFolder() + File.separator + KEYPAIR_FOLDER; String privateKeyPath = keyPath + File.separator + KEYPAIR_FILE_NAME + KEYPAIR_PRIV_EXTENSION; String publicKeyPath = keyPath + File.separator + KEYPAIR_FILE_NAME + KEYPAIR_PUB_EXTENSION; @@ -228,8 +228,8 @@ public static void pushRegistrationToServer() { publicKey, context.getResources().getString(R.string.push_server_url)); - RemoteOperationResult remoteOperationResult = registerAccountDeviceForNotificationsOperation. - execute(mClient); + RemoteOperationResult remoteOperationResult = + registerAccountDeviceForNotificationsOperation.execute(mClient); if (remoteOperationResult.isSuccess()) { PushResponse pushResponse = remoteOperationResult.getPushResponseData(); @@ -268,8 +268,8 @@ public static void pushRegistrationToServer() { } public static Key readKeyFromFile(boolean readPublicKey) { - String keyPath = MainApp.getAppContext().getFilesDir().getAbsolutePath() + File.separator + MainApp.getDataFolder() + File.separator - + KEYPAIR_FOLDER; + String keyPath = MainApp.getAppContext().getFilesDir().getAbsolutePath() + File.separator + + MainApp.getDataFolder() + File.separator + KEYPAIR_FOLDER; String privateKeyPath = keyPath + File.separator + KEYPAIR_FILE_NAME + KEYPAIR_PRIV_EXTENSION; String publicKeyPath = keyPath + File.separator + KEYPAIR_FILE_NAME + KEYPAIR_PUB_EXTENSION; From f626f58378ce3e67ed89ec881c0b9ced5c1bdc0d Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Fri, 12 Jan 2018 19:08:57 +0100 Subject: [PATCH 07/11] Attempt to fix moves Signed-off-by: Mario Danic --- build.gradle | 1 + .../java/com/owncloud/android/MainApp.java | 46 ++++++++++++++----- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/build.gradle b/build.gradle index 6cde77f62768..689034b5ff47 100644 --- a/build.gradle +++ b/build.gradle @@ -246,6 +246,7 @@ dependencies { //androidTestImplementation "com.android.support:support-annotations:${supportLibraryVersion}" implementation 'org.jetbrains:annotations:15.0' + implementation 'org.lukhnos:nnio:0.2' androidTestCompile 'tools.fastlane:screengrab:1.0.0' } diff --git a/src/main/java/com/owncloud/android/MainApp.java b/src/main/java/com/owncloud/android/MainApp.java index 63e1da5ba116..49cfbbd48813 100644 --- a/src/main/java/com/owncloud/android/MainApp.java +++ b/src/main/java/com/owncloud/android/MainApp.java @@ -38,6 +38,7 @@ import android.support.multidex.MultiDexApplication; import android.support.v4.util.Pair; import android.support.v7.app.AlertDialog; +import android.util.Log; import android.view.WindowManager; import com.evernote.android.job.JobManager; @@ -65,7 +66,13 @@ import com.owncloud.android.utils.PermissionUtil; import com.owncloud.android.utils.ReceiversHelper; +import org.lukhnos.nnio.file.Files; +import org.lukhnos.nnio.file.Path; +import org.lukhnos.nnio.file.Paths; +import org.lukhnos.nnio.file.StandardCopyOption; + import java.io.File; +import java.io.IOException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; @@ -242,28 +249,43 @@ public static void migratePushKeys() { Context context = getAppContext(); if (!PreferenceManager.getKeysMigration(context)) { - String oldKeyPath = getStoragePath() + File.separator + MainApp.getDataFolder() + - File.separator + "nc-keypair"; - File oldPrivateKey = new File(oldKeyPath, "push_key.priv"); - File oldPublicKey = new File(oldKeyPath, "push_key.pub"); + String oldKeyPath = getStoragePath() + File.separator + MainApp.getDataFolder() + File.separator + "nc-keypair"; + File oldPrivateKeyFile = new File(oldKeyPath, "push_key.priv"); + File oldPublicKeyFile = new File(oldKeyPath, "push_key.pub"); + + Path oldPrivateKeyPath = Paths.get(oldPrivateKeyFile.toURI()); + Path oldPublicKeyPath = Paths.get(oldPublicKeyFile.toURI()); String keyPath = getAppContext().getFilesDir().getAbsolutePath() + File.separator + MainApp.getDataFolder() + File.separator + "nc-keypair"; - File privateKey = new File(keyPath, "push_key.priv"); - File publicKey = new File(keyPath, "push_key.pub"); + File privateKeyFile = new File(keyPath, "push_key.priv"); + File publicKeyFile = new File(keyPath, "push_key.pub"); + + Path privateKeyPath = Paths.get(privateKeyFile.toURI()); + Path publicKeyPath = Paths.get(publicKeyFile.toURI()); - if ((privateKey.exists() && publicKey.exists()) || (!oldPrivateKey.exists() && !oldPublicKey.exists())) { + + if ((privateKeyFile.exists() && publicKeyFile.exists()) || + (!oldPrivateKeyFile.exists() && !oldPublicKeyFile.exists())) { PreferenceManager.setKeysMigration(context, true); } else { - if (oldPrivateKey.exists()) { - oldPrivateKey.renameTo(privateKey); + if (oldPrivateKeyFile.exists()) { + try { + Files.move(oldPrivateKeyPath, privateKeyPath, StandardCopyOption.ATOMIC_MOVE); + } catch (IOException e) { + Log.e(TAG, "Failed to move old private key to new location"); + } } - if (oldPublicKey.exists()) { - oldPublicKey.renameTo(publicKey); + if (oldPublicKeyFile.exists()) { + try { + Files.move(oldPublicKeyPath, publicKeyPath, StandardCopyOption.ATOMIC_MOVE); + } catch (IOException e) { + Log.e(TAG, "Failed to move old public key to new location"); + } } - if (privateKey.exists() && publicKey.exists()) { + if (privateKeyFile.exists() && publicKeyFile.exists()) { PreferenceManager.setKeysMigration(context, true); } } From ffb33a3ed8d92d2801c36788112369be362feb08 Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Fri, 12 Jan 2018 19:49:31 +0100 Subject: [PATCH 08/11] Remove double dep Signed-off-by: Mario Danic --- build.gradle | 1 - 1 file changed, 1 deletion(-) diff --git a/build.gradle b/build.gradle index 689034b5ff47..6cde77f62768 100644 --- a/build.gradle +++ b/build.gradle @@ -246,7 +246,6 @@ dependencies { //androidTestImplementation "com.android.support:support-annotations:${supportLibraryVersion}" implementation 'org.jetbrains:annotations:15.0' - implementation 'org.lukhnos:nnio:0.2' androidTestCompile 'tools.fastlane:screengrab:1.0.0' } From 1c8d46f9fffd683a4992188ca92c198a266f9e97 Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Fri, 12 Jan 2018 21:21:50 +0100 Subject: [PATCH 09/11] Revert change Signed-off-by: Mario Danic --- .../com/owncloud/android/providers/FileContentProvider.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/owncloud/android/providers/FileContentProvider.java b/src/main/java/com/owncloud/android/providers/FileContentProvider.java index 0970a845f2cd..49a4afa1e4f1 100644 --- a/src/main/java/com/owncloud/android/providers/FileContentProvider.java +++ b/src/main/java/com/owncloud/android/providers/FileContentProvider.java @@ -621,8 +621,8 @@ private Cursor query( // if both are null, let them pass to query if (selectionArgs == null && selection != null) { - selection = "(?)"; selectionArgs = new String[]{selection}; + selection = "(?)"; } sqlQuery.setStrict(true); From 88498e4071c5606b28d84e2bd7d65de95b458cbf Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Mon, 15 Jan 2018 12:40:15 +0100 Subject: [PATCH 10/11] use copy & delete instead of move Signed-off-by: tobiasKaminsky --- .../com/owncloud/android/utils/PushUtils.java | 43 ++++++++++++++- .../java/com/owncloud/android/MainApp.java | 55 ------------------- .../android/utils/FileStorageUtils.java | 8 +++ 3 files changed, 50 insertions(+), 56 deletions(-) diff --git a/src/gplay/java/com/owncloud/android/utils/PushUtils.java b/src/gplay/java/com/owncloud/android/utils/PushUtils.java index ac2d78ae551e..6fc3e16e0e14 100644 --- a/src/gplay/java/com/owncloud/android/utils/PushUtils.java +++ b/src/gplay/java/com/owncloud/android/utils/PushUtils.java @@ -26,6 +26,7 @@ import android.content.Context; import android.text.TextUtils; import android.util.Base64; +import android.util.Log; import com.google.gson.Gson; import com.owncloud.android.MainApp; @@ -99,7 +100,7 @@ public static String bytesToHex(byte[] bytes) { } private static int generateRsa2048KeyPair() { - MainApp.migratePushKeys(); + migratePushKeys(); String keyPath = MainApp.getAppContext().getFilesDir().getAbsolutePath() + File.separator + MainApp.getDataFolder() + File.separator + KEYPAIR_FOLDER; @@ -335,4 +336,44 @@ private static int saveKeyToFile(Key key, String path) { return -1; } + + public static void migratePushKeys() { + Context context = MainApp.getAppContext(); + + if (!PreferenceManager.getKeysMigration(context)) { + String oldKeyPath = MainApp.getStoragePath() + File.separator + MainApp.getDataFolder() + + File.separator + "nc-keypair"; + File oldPrivateKeyFile = new File(oldKeyPath, "push_key.priv"); + File oldPublicKeyFile = new File(oldKeyPath, "push_key.pub"); + + String keyPath = context.getDir("nc-keypair", Context.MODE_PRIVATE).getAbsolutePath(); + File privateKeyFile = new File(keyPath, "push_key.priv"); + File publicKeyFile = new File(keyPath, "push_key.pub"); + + if ((privateKeyFile.exists() && publicKeyFile.exists()) || + (!oldPrivateKeyFile.exists() && !oldPublicKeyFile.exists())) { + PreferenceManager.setKeysMigration(context, true); + } else { + if (oldPrivateKeyFile.exists()) { + try { + FileStorageUtils.moveFile(oldPrivateKeyFile, privateKeyFile); + } catch (IOException e) { + Log.e(TAG, "Failed to move old private key to new location"); + } + } + + if (oldPublicKeyFile.exists()) { + try { + FileStorageUtils.moveFile(oldPublicKeyFile, publicKeyFile); + } catch (IOException e) { + Log.e(TAG, "Failed to move old public key to new location"); + } + } + + if (privateKeyFile.exists() && publicKeyFile.exists()) { + PreferenceManager.setKeysMigration(context, true); + } + } + } + } } diff --git a/src/main/java/com/owncloud/android/MainApp.java b/src/main/java/com/owncloud/android/MainApp.java index 49cfbbd48813..31fddcab5794 100644 --- a/src/main/java/com/owncloud/android/MainApp.java +++ b/src/main/java/com/owncloud/android/MainApp.java @@ -38,7 +38,6 @@ import android.support.multidex.MultiDexApplication; import android.support.v4.util.Pair; import android.support.v7.app.AlertDialog; -import android.util.Log; import android.view.WindowManager; import com.evernote.android.job.JobManager; @@ -66,13 +65,6 @@ import com.owncloud.android.utils.PermissionUtil; import com.owncloud.android.utils.ReceiversHelper; -import org.lukhnos.nnio.file.Files; -import org.lukhnos.nnio.file.Path; -import org.lukhnos.nnio.file.Paths; -import org.lukhnos.nnio.file.StandardCopyOption; - -import java.io.File; -import java.io.IOException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; @@ -245,53 +237,6 @@ public static void initAutoUpload() { } } - public static void migratePushKeys() { - Context context = getAppContext(); - - if (!PreferenceManager.getKeysMigration(context)) { - String oldKeyPath = getStoragePath() + File.separator + MainApp.getDataFolder() + File.separator + "nc-keypair"; - File oldPrivateKeyFile = new File(oldKeyPath, "push_key.priv"); - File oldPublicKeyFile = new File(oldKeyPath, "push_key.pub"); - - Path oldPrivateKeyPath = Paths.get(oldPrivateKeyFile.toURI()); - Path oldPublicKeyPath = Paths.get(oldPublicKeyFile.toURI()); - - String keyPath = getAppContext().getFilesDir().getAbsolutePath() + - File.separator + MainApp.getDataFolder() + File.separator + "nc-keypair"; - File privateKeyFile = new File(keyPath, "push_key.priv"); - File publicKeyFile = new File(keyPath, "push_key.pub"); - - Path privateKeyPath = Paths.get(privateKeyFile.toURI()); - Path publicKeyPath = Paths.get(publicKeyFile.toURI()); - - - if ((privateKeyFile.exists() && publicKeyFile.exists()) || - (!oldPrivateKeyFile.exists() && !oldPublicKeyFile.exists())) { - PreferenceManager.setKeysMigration(context, true); - } else { - if (oldPrivateKeyFile.exists()) { - try { - Files.move(oldPrivateKeyPath, privateKeyPath, StandardCopyOption.ATOMIC_MOVE); - } catch (IOException e) { - Log.e(TAG, "Failed to move old private key to new location"); - } - } - - if (oldPublicKeyFile.exists()) { - try { - Files.move(oldPublicKeyPath, publicKeyPath, StandardCopyOption.ATOMIC_MOVE); - } catch (IOException e) { - Log.e(TAG, "Failed to move old public key to new location"); - } - } - - if (privateKeyFile.exists() && publicKeyFile.exists()) { - PreferenceManager.setKeysMigration(context, true); - } - } - } - } - public static void notificationChannels() { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O && getAppContext() != null) { Context context = getAppContext(); diff --git a/src/main/java/com/owncloud/android/utils/FileStorageUtils.java b/src/main/java/com/owncloud/android/utils/FileStorageUtils.java index 288981573b30..fdc564cd984d 100644 --- a/src/main/java/com/owncloud/android/utils/FileStorageUtils.java +++ b/src/main/java/com/owncloud/android/utils/FileStorageUtils.java @@ -355,6 +355,14 @@ public static boolean copyFile(File src, File target) { return ret; } + public static boolean moveFile(File sourceFile, File targetFile) throws IOException { + if (copyFile(sourceFile, targetFile)) { + return sourceFile.delete(); + } else { + return false; + } + } + public static void deleteRecursively(File file, FileDataStorageManager storageManager) { if (file.isDirectory()) { for (File child : file.listFiles()) { From 59fafc7b3d86c2a1c3b1725e0b42b634eafd7f34 Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Mon, 15 Jan 2018 13:24:07 +0100 Subject: [PATCH 11/11] function is private fix modified, but will soon be removed Signed-off-by: tobiasKaminsky --- src/gplay/java/com/owncloud/android/utils/PushUtils.java | 2 +- src/modified/java/com/owncloud/android/utils/PushUtils.java | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/gplay/java/com/owncloud/android/utils/PushUtils.java b/src/gplay/java/com/owncloud/android/utils/PushUtils.java index 6fc3e16e0e14..99ae49b15a87 100644 --- a/src/gplay/java/com/owncloud/android/utils/PushUtils.java +++ b/src/gplay/java/com/owncloud/android/utils/PushUtils.java @@ -337,7 +337,7 @@ private static int saveKeyToFile(Key key, String path) { return -1; } - public static void migratePushKeys() { + private static void migratePushKeys() { Context context = MainApp.getAppContext(); if (!PreferenceManager.getKeysMigration(context)) { diff --git a/src/modified/java/com/owncloud/android/utils/PushUtils.java b/src/modified/java/com/owncloud/android/utils/PushUtils.java index 6386318606b1..2a848c9b1f09 100644 --- a/src/modified/java/com/owncloud/android/utils/PushUtils.java +++ b/src/modified/java/com/owncloud/android/utils/PushUtils.java @@ -99,7 +99,7 @@ public static String bytesToHex(byte[] bytes) { } private static int generateRsa2048KeyPair() { - MainApp.migratePushKeys(); + migratePushKeys(); String keyPath = MainApp.getAppContext().getFilesDir().getAbsolutePath() + File.separator + MainApp.getDataFolder() + File.separator + KEYPAIR_FOLDER; @@ -331,4 +331,8 @@ private static int saveKeyToFile(Key key, String path) { return -1; } + + private static void migratePushKeys() { + + } }