Differential D18212 Diff 53433 src/org/kde/kdeconnect/Plugins/SftpPlugin/AndroidSafFileSystemView.java
Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Plugins/SftpPlugin/AndroidSafFileSystemView.java
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2018 Erik Duisters <e.duisters1@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | package org.kde.kdeconnect.Plugins.SftpPlugin; | ||||
22 | | ||||
23 | import android.annotation.TargetApi; | ||||
24 | import android.content.Context; | ||||
25 | import android.net.Uri; | ||||
26 | import android.provider.DocumentsContract; | ||||
27 | | ||||
28 | import org.apache.sshd.common.file.FileSystemView; | ||||
29 | import org.apache.sshd.common.file.SshFile; | ||||
30 | import org.apache.sshd.common.file.nativefs.NativeSshFile; | ||||
31 | | ||||
32 | import java.io.File; | ||||
33 | import java.util.ArrayList; | ||||
34 | import java.util.List; | ||||
35 | import java.util.Map; | ||||
36 | | ||||
37 | @TargetApi(21) | ||||
38 | public class AndroidSafFileSystemView implements FileSystemView { | ||||
39 | final String userName; | ||||
40 | final Context context; | ||||
41 | private final Map<String, String> roots; | ||||
42 | private final RootFile rootFile; | ||||
43 | | ||||
44 | AndroidSafFileSystemView(Map<String, String> roots, String userName, Context context) { | ||||
45 | this.roots = roots; | ||||
46 | this.userName = userName; | ||||
47 | this.context = context; | ||||
48 | this.rootFile = new RootFile( createFileList(), userName, true); | ||||
49 | } | ||||
50 | | ||||
51 | private List<SshFile> createFileList() { | ||||
52 | List<SshFile> list = new ArrayList<>(); | ||||
53 | for (Map.Entry<String, String> entry : roots.entrySet()) { | ||||
54 | String displayName = entry.getKey(); | ||||
55 | String uri = entry.getValue(); | ||||
56 | | ||||
57 | Uri treeUri = Uri.parse(uri); | ||||
58 | Uri documentUri = DocumentsContract.buildDocumentUriUsingTree(treeUri, DocumentsContract.getTreeDocumentId(treeUri)); | ||||
59 | list.add(createAndroidSafSshFile(null, documentUri, File.separatorChar + displayName)); | ||||
60 | } | ||||
61 | | ||||
62 | return list; | ||||
63 | } | ||||
64 | | ||||
65 | @Override | ||||
66 | public SshFile getFile(String file) { | ||||
67 | return getFile("/", file); | ||||
68 | } | ||||
69 | | ||||
70 | @Override | ||||
71 | public SshFile getFile(SshFile baseDir, String file) { | ||||
72 | return getFile(baseDir.getAbsolutePath(), file); | ||||
73 | } | ||||
74 | | ||||
75 | protected SshFile getFile(String dir, String file) { | ||||
76 | if (!dir.endsWith("/")) { | ||||
77 | dir = dir + "/"; | ||||
78 | } | ||||
79 | | ||||
80 | if (!file.startsWith("/")) { | ||||
81 | file = dir + file; | ||||
82 | } | ||||
83 | | ||||
84 | String filename = NativeSshFile.getPhysicalName("/", "/", file, false); | ||||
85 | | ||||
86 | if (filename.equals("/")) { | ||||
87 | return rootFile; | ||||
88 | } | ||||
89 | | ||||
90 | for (String root : roots.keySet()) { | ||||
91 | if (filename.indexOf(root) == 1) { | ||||
92 | String nameWithoutRoot = filename.substring(root.length() + 1); | ||||
93 | String pathOrUri = roots.get(root); | ||||
94 | | ||||
95 | Uri treeUri = Uri.parse(pathOrUri); | ||||
96 | if (nameWithoutRoot.isEmpty()) { | ||||
97 | //TreeDocument | ||||
98 | Uri documentUri = DocumentsContract.buildDocumentUriUsingTree(treeUri, DocumentsContract.getTreeDocumentId(treeUri)); | ||||
99 | | ||||
100 | return createAndroidSafSshFile(documentUri, documentUri, filename); | ||||
101 | } else { | ||||
102 | //ChildDocument, strip the leading / from nameWithoutRoot and append that to the treeDocumentId | ||||
103 | String treeDocumentId = DocumentsContract.getTreeDocumentId(treeUri); | ||||
104 | File nameWithoutRootFile = new File(nameWithoutRoot); | ||||
105 | String parentSuffix = nameWithoutRootFile.getParent(); | ||||
106 | String parentDocumentId = treeDocumentId + (parentSuffix.equals("/") ? "" : parentSuffix.substring(1)); | ||||
107 | | ||||
108 | Uri parentUri = DocumentsContract.buildDocumentUriUsingTree(treeUri, parentDocumentId); | ||||
109 | Uri documentUri = DocumentsContract.buildDocumentUriUsingTree(treeUri, treeDocumentId + nameWithoutRoot.substring(1)); | ||||
110 | | ||||
111 | return createAndroidSafSshFile(parentUri, documentUri, filename); | ||||
112 | } | ||||
113 | } | ||||
114 | } | ||||
115 | | ||||
116 | //It's a file under / but not one covered by any Tree | ||||
117 | return new RootFile(new ArrayList<>(0), userName, false); | ||||
118 | } | ||||
119 | | ||||
120 | public AndroidSafSshFile createAndroidSafSshFile(Uri parentUri, Uri documentUri, String virtualFilename) { | ||||
121 | return new AndroidSafSshFile(this, parentUri, documentUri, virtualFilename); | ||||
122 | } | ||||
123 | | ||||
124 | @Override | ||||
125 | public FileSystemView getNormalizedView() { | ||||
126 | return this; | ||||
127 | } | ||||
128 | } |