Răsfoiți Sursa

base for updated documents provider
common place to create client

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>

tobiasKaminsky 6 ani în urmă
părinte
comite
5f315ef682

+ 1 - 1
scripts/analysis/lint-results.txt

@@ -1,2 +1,2 @@
 DO NOT TOUCH; GENERATED BY DRONE
-      <span class="mdl-layout-title">Lint Report: 83 warnings</span>
+      <span class="mdl-layout-title">Lint Report: 82 warnings</span>

+ 34 - 1
src/main/AndroidManifest.xml

@@ -178,7 +178,40 @@
             android:enabled="true"
             android:exported="true"
             android:label="@string/sync_string_files"
-            android:syncable="true" />
+            android:syncable="true">
+            <path-permission
+                android:pathPrefix="/shares"
+                android:readPermission="false"
+                android:writePermission="false" />
+            <path-permission
+                android:pathPrefix="/capabilities"
+                android:readPermission="false"
+                android:writePermission="false" />
+            <path-permission
+                android:pathPrefix="/uploads"
+                android:readPermission="false"
+                android:writePermission="false" />
+            <path-permission
+                android:pathPrefix="/synced_folders"
+                android:readPermission="false"
+                android:writePermission="false" />
+            <path-permission
+                android:pathPrefix="/external_links"
+                android:readPermission="false"
+                android:writePermission="false" />
+            <path-permission
+                android:pathPrefix="/arbitrary_data"
+                android:readPermission="false"
+                android:writePermission="false" />
+            <path-permission
+                android:pathPrefix="/virtual"
+                android:readPermission="false"
+                android:writePermission="false" />
+            <path-permission
+                android:pathPrefix="/filesystem"
+                android:readPermission="false"
+                android:writePermission="false" />
+        </provider>
 
         <provider
             android:name=".providers.UsersAndGroupsSearchProvider"

+ 79 - 38
src/main/java/com/owncloud/android/providers/DocumentsStorageProvider.java

@@ -1,5 +1,5 @@
-/**
- *   nextCloud Android client application
+/*
+ *   Nextcloud Android client application
  *
  *   @author Bartosz Przybylski
  *   Copyright (C) 2016  Bartosz Przybylski <bart.p.pl@gmail.com>
@@ -21,6 +21,8 @@
 package com.owncloud.android.providers;
 
 import android.accounts.Account;
+import android.accounts.AuthenticatorException;
+import android.accounts.OperationCanceledException;
 import android.annotation.SuppressLint;
 import android.annotation.TargetApi;
 import android.content.ContentResolver;
@@ -46,7 +48,11 @@ import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.files.services.FileDownloader;
+import com.owncloud.android.lib.common.OwnCloudAccount;
+import com.owncloud.android.lib.common.OwnCloudClient;
+import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
+import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.operations.SynchronizeFileOperation;
 import com.owncloud.android.ui.activity.ConflictsResolveActivity;
 import com.owncloud.android.ui.activity.Preferences;
@@ -57,6 +63,7 @@ import org.nextcloud.providers.cursors.RootCursor;
 
 import java.io.File;
 import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -67,18 +74,19 @@ public class DocumentsStorageProvider extends DocumentsProvider {
 
     private static final String TAG = "DocumentsStorageProvider";
 
-    private FileDataStorageManager mCurrentStorageManager;
-    private static Map<Long, FileDataStorageManager> mRootIdToStorageManager;
+    private FileDataStorageManager currentStorageManager;
+    private Map<Long, FileDataStorageManager> rootIdToStorageManager;
+    private OwnCloudClient client;
 
     @Override
     public Cursor queryRoots(String[] projection) throws FileNotFoundException {
 
         SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(MainApp.getAppContext());
-        if (appPrefs.getString(Preferences.PREFERENCE_LOCK, "").equals(Preferences.LOCK_PASSCODE) ||
-                appPrefs.getString(Preferences.PREFERENCE_LOCK, "").equals(Preferences.LOCK_DEVICE_CREDENTIALS)) {
-            return new FileCursor(new String[]{});
+        if (Preferences.LOCK_PASSCODE.equals(appPrefs.getString(Preferences.PREFERENCE_LOCK, "")) ||
+            Preferences.LOCK_DEVICE_CREDENTIALS.equals(appPrefs.getString(Preferences.PREFERENCE_LOCK, ""))) {
+            return new FileCursor();
         }
-        
+
         initiateStorageMap();
 
         final RootCursor result = new RootCursor(projection);
@@ -96,16 +104,21 @@ public class DocumentsStorageProvider extends DocumentsProvider {
         updateCurrentStorageManagerIfNeeded(docId);
 
         final FileCursor result = new FileCursor(projection);
-        if (mCurrentStorageManager == null) {
-            for(long key : mRootIdToStorageManager.keySet()) {
-                if (mRootIdToStorageManager.get(key).getFileById(docId) != null) {
-                    mCurrentStorageManager = mRootIdToStorageManager.get(key);
+        if (currentStorageManager == null) {
+
+            for (Map.Entry<Long, FileDataStorageManager> entry : rootIdToStorageManager.entrySet()) {
+                if (entry.getValue().getFileById(docId) != null) {
+                    currentStorageManager = entry.getValue();
                     break;
                 }
             }
         }
 
-        OCFile file = mCurrentStorageManager.getFileById(docId);
+        if (currentStorageManager == null) {
+            throw new FileNotFoundException("File with " + documentId + " not found");
+        }
+
+        OCFile file = currentStorageManager.getFileById(docId);
         if (file != null) {
             result.addFile(file);
         }
@@ -114,16 +127,15 @@ public class DocumentsStorageProvider extends DocumentsProvider {
     }
 
     @Override
-    public Cursor queryChildDocuments(String parentDocumentId, String[] projection, String sortOrder)
-            throws FileNotFoundException {
+    public Cursor queryChildDocuments(String parentDocumentId, String[] projection, String sortOrder) {
 
         final long folderId = Long.parseLong(parentDocumentId);
         updateCurrentStorageManagerIfNeeded(folderId);
 
         final FileCursor result = new FileCursor(projection);
 
-        final OCFile browsedDir = mCurrentStorageManager.getFileById(folderId);
-        for (OCFile file : mCurrentStorageManager.getFolderContent(browsedDir, false)) {
+        final OCFile browsedDir = currentStorageManager.getFileById(folderId);
+        for (OCFile file : currentStorageManager.getFolderContent(browsedDir, false)) {
             result.addFile(file);
         }
 
@@ -137,11 +149,19 @@ public class DocumentsStorageProvider extends DocumentsProvider {
         final long docId = Long.parseLong(documentId);
         updateCurrentStorageManagerIfNeeded(docId);
 
-        OCFile file = mCurrentStorageManager.getFileById(docId);
+        OCFile file = currentStorageManager.getFileById(docId);
 
-        Account account = mCurrentStorageManager.getAccount();
+        if (file == null) {
+            throw new FileNotFoundException("File with id " + documentId + " not found!");
+        }
+
+        Account account = currentStorageManager.getAccount();
         Context context = getContext();
 
+        if (context == null) {
+            throw new FileNotFoundException("Context may not be null!");
+        }
+
         if (!file.isDown()) {
 
             Intent i = new Intent(getContext(), FileDownloader.class);
@@ -155,10 +175,13 @@ public class DocumentsStorageProvider extends DocumentsProvider {
 
             do {
                 if (!waitOrGetCancelled(cancellationSignal)) {
-                    return null;
+                    throw new FileNotFoundException("File with id " + documentId + " not found!");
                 }
-                file = mCurrentStorageManager.getFileById(docId);
+                file = currentStorageManager.getFileById(docId);
 
+                if (file == null) {
+                    throw new FileNotFoundException("File with id " + documentId + " not found!");
+                }
             } while (!file.isDown());
         } else {
             OCFile finalFile = file;
@@ -219,7 +242,11 @@ public class DocumentsStorageProvider extends DocumentsProvider {
         long docId = Long.parseLong(documentId);
         updateCurrentStorageManagerIfNeeded(docId);
 
-        OCFile file = mCurrentStorageManager.getFileById(docId);
+        OCFile file = currentStorageManager.getFileById(docId);
+
+        if (file == null) {
+            throw new FileNotFoundException("File with id " + documentId + " not found!");
+        }
 
         File realFile = new File(file.getStoragePath());
 
@@ -230,10 +257,10 @@ public class DocumentsStorageProvider extends DocumentsProvider {
     }
 
     @Override
-    public Cursor querySearchDocuments(String rootId, String query, String[] projection) throws FileNotFoundException {
+    public Cursor querySearchDocuments(String rootId, String query, String[] projection) {
         updateCurrentStorageManagerIfNeeded(rootId);
 
-        OCFile root = mCurrentStorageManager.getFileByPath("/");
+        OCFile root = currentStorageManager.getFileByPath("/");
         FileCursor result = new FileCursor(projection);
 
         for (OCFile f : findFiles(root, query)) {
@@ -245,7 +272,7 @@ public class DocumentsStorageProvider extends DocumentsProvider {
 
     @SuppressLint("LongLogTag")
     private void updateCurrentStorageManagerIfNeeded(long docId) {
-        if (mRootIdToStorageManager == null) {
+        if (rootIdToStorageManager == null) {
             try {
                 queryRoots(FileCursor.DEFAULT_DOCUMENT_PROJECTION);
             } catch (FileNotFoundException e) {
@@ -253,32 +280,46 @@ public class DocumentsStorageProvider extends DocumentsProvider {
             }
         }
 
-        if (mCurrentStorageManager == null ||
-                (mRootIdToStorageManager.containsKey(docId) &&
-                        mCurrentStorageManager != mRootIdToStorageManager.get(docId))) {
-            mCurrentStorageManager = mRootIdToStorageManager.get(docId);
+        if (currentStorageManager == null ||
+            rootIdToStorageManager.containsKey(docId) && currentStorageManager != rootIdToStorageManager.get(docId)) {
+            currentStorageManager = rootIdToStorageManager.get(docId);
+        }
+
+        try {
+            Account account = currentStorageManager.getAccount();
+            OwnCloudAccount ocAccount = new OwnCloudAccount(account, MainApp.getAppContext());
+            client = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, getContext());
+        } catch (OperationCanceledException | IOException | AuthenticatorException |
+            com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
+            Log_OC.e(TAG, "Failed to set client", e);
         }
     }
 
     private void updateCurrentStorageManagerIfNeeded(String rootId) {
-        for (FileDataStorageManager data : mRootIdToStorageManager.values()) {
+        for (FileDataStorageManager data : rootIdToStorageManager.values()) {
             if (data.getAccount().name.equals(rootId)) {
-                mCurrentStorageManager = data;
+                currentStorageManager = data;
             }
         }
     }
 
-    private void initiateStorageMap() {
+    @SuppressLint("UseSparseArrays")
+    private void initiateStorageMap() throws FileNotFoundException {
 
-        mRootIdToStorageManager = new HashMap<>();
+        rootIdToStorageManager = new HashMap<>();
+
+        Context context = getContext();
+
+        if (context == null) {
+            throw new FileNotFoundException("Context may not be null!");
+        }
 
-        ContentResolver contentResolver = getContext().getContentResolver();
+        ContentResolver contentResolver = context.getContentResolver();
 
         for (Account account : AccountUtils.getAccounts(getContext())) {
-            final FileDataStorageManager storageManager =
-                    new FileDataStorageManager(account, contentResolver);
+            final FileDataStorageManager storageManager = new FileDataStorageManager(account, contentResolver);
             final OCFile rootDir = storageManager.getFileByPath("/");
-            mRootIdToStorageManager.put(rootDir.getFileId(), storageManager);
+            rootIdToStorageManager.put(rootDir.getFileId(), storageManager);
         }
     }
 
@@ -294,7 +335,7 @@ public class DocumentsStorageProvider extends DocumentsProvider {
 
     List<OCFile> findFiles(OCFile root, String query) {
         List<OCFile> result = new ArrayList<>();
-        for (OCFile f : mCurrentStorageManager.getFolderContent(root, false)) {
+        for (OCFile f : currentStorageManager.getFolderContent(root, false)) {
             if (f.isFolder()) {
                 result.addAll(findFiles(f, query));
             } else if (f.getFileName().contains(query)) {

+ 24 - 8
src/main/java/com/owncloud/android/providers/FileContentProvider.java

@@ -96,7 +96,7 @@ public class FileContentProvider extends ContentProvider {
 
     @Override
     public int delete(@NonNull Uri uri, String where, String[] whereArgs) {
-        if (isCallerNotAllowed()) {
+        if (isCallerNotAllowed(uri)) {
             return -1;
         }
 
@@ -114,7 +114,7 @@ public class FileContentProvider extends ContentProvider {
     }
 
     private int delete(SQLiteDatabase db, Uri uri, String where, String... whereArgs) {
-        if (isCallerNotAllowed()) {
+        if (isCallerNotAllowed(uri)) {
             return -1;
         }
 
@@ -248,7 +248,7 @@ public class FileContentProvider extends ContentProvider {
 
     @Override
     public Uri insert(@NonNull Uri uri, ContentValues values) {
-        if (isCallerNotAllowed()) {
+        if (isCallerNotAllowed(uri)) {
             return null;
         }
 
@@ -476,7 +476,7 @@ public class FileContentProvider extends ContentProvider {
                 break;
 
             default:
-                if (isCallerNotAllowed()) {
+                if (isCallerNotAllowed(uri)) {
                     return null;
                 }
         }
@@ -643,7 +643,7 @@ public class FileContentProvider extends ContentProvider {
     @Override
     public int update(@NonNull Uri uri, ContentValues values, String selection, String[] selectionArgs) {
 
-        if (isCallerNotAllowed()) {
+        if (isCallerNotAllowed(uri)) {
             return -1;
         }
 
@@ -1023,9 +1023,25 @@ public class FileContentProvider extends ContentProvider {
         }
     }
 
-    private boolean isCallerNotAllowed() {
-        String callingPackage = mContext.getPackageManager().getNameForUid(Binder.getCallingUid());
-        return callingPackage == null || !callingPackage.equals(mContext.getPackageName());
+    private boolean isCallerNotAllowed(Uri uri) {
+        switch (mUriMatcher.match(uri)) {
+            case SHARES:
+            case CAPABILITIES:
+            case UPLOADS:
+            case SYNCED_FOLDERS:
+            case EXTERNAL_LINKS:
+            case ARBITRARY_DATA:
+            case VIRTUAL:
+            case FILESYSTEM:
+                String callingPackage = mContext.getPackageManager().getNameForUid(Binder.getCallingUid());
+                return callingPackage == null || !callingPackage.equals(mContext.getPackageName());
+
+            case ROOT_DIRECTORY:
+            case SINGLE_FILE:
+            case DIRECTORY:
+            default:
+                return false;
+        }
     }
 
     class DataBaseHelper extends SQLiteOpenHelper {