浏览代码

Allows deep linking to open files

Signed-off-by: Evans Jahja <evansjahja13@gmail.com>
Evans Jahja 5 年之前
父节点
当前提交
0467cc37ca

+ 13 - 0
src/main/AndroidManifest.xml

@@ -117,6 +117,19 @@
             <intent-filter>
                 <action android:name="android.intent.action.SEARCH" />
             </intent-filter>
+            <intent-filter>
+                <action android:name="android.intent.action.VIEW" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <category android:name="android.intent.category.BROWSABLE" />
+                <data android:scheme="http"
+                    android:host="*"
+                    android:pathPattern="/.*index.php/f/.*"
+                    />
+                <data android:scheme="https"
+                      android:host="*"
+                      android:pathPattern="/.*index.php/f/.*"
+                    />
+            </intent-filter>
             <meta-data android:name="android.app.searchable"
                 android:resource="@xml/users_and_groups_searchable"/>
         </activity>

+ 85 - 14
src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -135,6 +135,8 @@ import java.io.File;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import javax.annotation.Nullable;
 import javax.inject.Inject;
@@ -176,6 +178,8 @@ public class FileDisplayActivity extends FileActivity
     public static final String TAG_PUBLIC_LINK = "PUBLIC_LINK";
     public static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
     public static final String KEY_FILE_ID = "KEY_FILE_ID";
+    public static final String KEY_ACCOUNT = "KEY_ACCOUNT";
+
 
     private static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
     private static final String KEY_SYNC_IN_PROGRESS = "SYNC_IN_PROGRESS";
@@ -2620,27 +2624,51 @@ public class FileDisplayActivity extends FileActivity
     private void handleOpenFileViaIntent(Intent intent) {
         showLoadingDialog(getString(R.string.retrieving_file));
 
-        String accountName = intent.getStringExtra("KEY_ACCOUNT");
+        String userName = intent.getStringExtra(KEY_ACCOUNT);
+        String fileId = intent.getStringExtra(KEY_FILE_ID);
 
-        Account newAccount;
-        if (accountName == null) {
-            newAccount = getAccount();
-        } else {
-            newAccount = getUserAccountManager().getAccountByName(accountName);
+        if (userName == null && fileId == null && intent.getData() != null) {
+            // Handle intent coming from URI
 
-            if (newAccount == null) {
+            Pattern pattern  = Pattern.compile("(.*)/index\\.php/([f])/([0-9]+)$");
+            Matcher matcher = pattern.matcher(intent.getData().toString());
+            if (matcher.matches()) {
+                String uri = matcher.group(1);
+                if ("f".equals(matcher.group(2))) {
+                    fileId = matcher.group(3);
+                    findAccountAndOpenFile(uri, fileId);
+                    return;
+                }
+            } else {
                 dismissLoadingDialog();
-                DisplayUtils.showSnackMessage(this, getString(R.string.associated_account_not_found));
+                DisplayUtils.showSnackMessage(this, getString(R.string.invalid_url));
                 return;
             }
+        }
+        openFile(userName, fileId);
 
-            setAccount(newAccount, false);
-            updateAccountList();
+    }
+    private void openFile(String userName, String fileId) {
+        Optional<User> optionalNewUser;
+        User user;
+
+        if (userName == null) {
+            optionalNewUser = getUser();
+        } else {
+            optionalNewUser = getUserAccountManager().getUser(userName);
         }
 
-        String fileId = String.valueOf(intent.getStringExtra(KEY_FILE_ID));
+        if (optionalNewUser.isPresent()) {
+            user = optionalNewUser.get();
+            setUser(user);
+            updateAccountList();
+        } else {
+            dismissLoadingDialog();
+            DisplayUtils.showSnackMessage(this, getString(R.string.associated_account_not_found));
+            return;
+        }
 
-        if ("null".equals(fileId)) {
+        if (fileId == null) {
             dismissLoadingDialog();
             DisplayUtils.showSnackMessage(this, getString(R.string.error_retrieving_file));
             return;
@@ -2649,13 +2677,56 @@ public class FileDisplayActivity extends FileActivity
         FileDataStorageManager storageManager = getStorageManager();
 
         if (storageManager == null) {
-            storageManager = new FileDataStorageManager(newAccount, getContentResolver());
+            storageManager = new FileDataStorageManager(user.toPlatformAccount(), getContentResolver());
         }
 
-        FetchRemoteFileTask fetchRemoteFileTask = new FetchRemoteFileTask(newAccount,
+        FetchRemoteFileTask fetchRemoteFileTask = new FetchRemoteFileTask(user.toPlatformAccount(),
                                                                           fileId,
                                                                           storageManager,
                                                                           this);
         fetchRemoteFileTask.execute();
+
+    }
+
+    private void findAccountAndOpenFile(String uri, String fileId) {
+
+        ArrayList<User> validUsers = new ArrayList<>();
+
+        for (User user : getUserAccountManager().getAllUsers()) {
+            if (user.getServer().getUri().toString().equals(uri)) {
+                validUsers.add(user);
+            }
+        }
+
+        if (validUsers.size() == 0) {
+            dismissLoadingDialog();
+            DisplayUtils.showSnackMessage(this, getString(R.string.associated_account_not_found));
+            return;
+        }
+
+        if (validUsers.size() == 1) {
+            openFile(validUsers.get(0).getAccountName(), fileId);
+            return;
+        }
+
+        ArrayList<String> validUserNames = new ArrayList<>();
+
+        for (User user : validUsers) {
+            validUserNames.add(user.getAccountName());
+        }
+
+        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
+        builder
+            .setTitle(R.string.common_choose_account)
+            .setItems(validUserNames.toArray(new CharSequence[validUserNames.size()]),
+                      new DialogInterface.OnClickListener() {
+                public void onClick(DialogInterface dialog, int which) {
+                    openFile(validUsers.get(which).getAccountName(), fileId);
+                    showLoadingDialog(getString(R.string.retrieving_file));
+                }
+            });
+        AlertDialog dialog = builder.create();
+        dismissLoadingDialog();
+        dialog.show();
     }
 }

+ 2 - 0
src/main/res/values/strings.xml

@@ -925,4 +925,6 @@
     <string name="sync_not_enough_space_dialog_action_free_space">Free up space</string>
     <string name="sync_not_enough_space_dialog_placeholder">%1$s is %2$s, but there is only %3$s available on device.</string>
     <string name="sync_not_enough_space_dialog_title">Not enough space</string>
+
+    <string name="invalid_url">Invalid URL</string>
 </resources>