Browse Source

Most improvements

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 7 years ago
parent
commit
845e85fb24

+ 2 - 2
src/gplay/java/com/owncloud/android/utils/PushUtils.java

@@ -99,7 +99,7 @@ public class PushUtils {
     }
     
     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 class PushUtils {
     }
 
     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;

+ 2 - 2
src/main/java/com/owncloud/android/MainApp.java

@@ -115,8 +115,8 @@ public class MainApp extends MultiDexApplication {
 
         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));
 

+ 24 - 18
src/main/java/com/owncloud/android/datamodel/OCFile.java

@@ -28,7 +28,9 @@ import android.content.Context;
 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 class OCFile implements Parcelable, Comparable<OCFile> {
         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 class OCFile implements Parcelable, Comparable<OCFile> {
             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;

+ 16 - 14
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.MountCommandStoragePointProvid
 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 class DataStorageProvider {
             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 class DataStorageProvider {
     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);
+    }
+
 }

+ 3 - 13
src/main/java/com/owncloud/android/datastorage/providers/SystemDefaultStoragePointProvider.java

@@ -25,11 +25,8 @@ import com.owncloud.android.MainApp;
 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 class SystemDefaultStoragePointProvider extends AbstractStoragePointProvi
         Vector<StoragePoint> 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;
     }

+ 3 - 2
src/main/java/com/owncloud/android/ui/activity/Preferences.java

@@ -1017,8 +1017,9 @@ public class Preferences extends PreferenceActivity
     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);
     }

+ 18 - 2
src/main/java/com/owncloud/android/ui/helpers/FileOperationsHelper.java

@@ -72,6 +72,7 @@ import java.io.FileInputStream;
 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 class FileOperationsHelper {
         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 class FileOperationsHelper {
                 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 class FileOperationsHelper {
             if (openFileWithIntent == null) {
                 openFileWithIntent = new Intent(Intent.ACTION_VIEW);
                 openFileWithIntent.setDataAndType(
-                        file.getExposedFileUri(mFileActivity),
+                        fileUri,
                         file.getMimetype()
                 );
             }

+ 32 - 0
src/main/res/values/arrays.xml

@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <array name="ms_office_extensions">
+        <item>doc</item>
+        <item>dot</item>
+        <item>docx</item>
+        <item>dotx</item>
+        <item>docm</item>
+        <item>dotm</item>
+        <item>xls</item>
+        <item>xlt</item>
+        <item>xla</item>
+        <item>xlsx</item>
+        <item>xltx</item>
+        <item>xlsm</item>
+        <item>xltm</item>
+        <item>xlam</item>
+        <item>xlsb</item>
+        <item>ppt</item>
+        <item>pot</item>
+        <item>pps</item>
+        <item>ppa</item>
+        <item>pptx</item>
+        <item>potx</item>
+        <item>ppsx</item>
+        <item>ppam</item>
+        <item>pptm</item>
+        <item>potm</item>
+        <item>ppsm</item>
+        <item>mdb</item>
+    </array>
+</resources>

+ 1 - 0
src/main/res/xml/exposed_filepaths.xml

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <paths>
+    <files-path name="user_files_internal" path="nextcloud/"/>
     <files-path
         path="log/"
         name="log"/>

+ 2 - 2
src/modified/java/com/owncloud/android/utils/PushUtils.java

@@ -99,7 +99,7 @@ public class PushUtils {
     }
 
     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 class PushUtils {
     }
 
     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;