Differential D18212 Diff 53433 src/org/kde/kdeconnect/Plugins/SftpPlugin/AndroidFileSystemFactory.java
Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Plugins/SftpPlugin/AndroidFileSystemFactory.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.content.Context; | ||||
24 | import android.os.Build; | ||||
25 | | ||||
26 | import org.apache.sshd.common.Session; | ||||
27 | import org.apache.sshd.common.file.FileSystemFactory; | ||||
28 | import org.apache.sshd.common.file.FileSystemView; | ||||
29 | | ||||
30 | import java.util.HashMap; | ||||
31 | import java.util.List; | ||||
32 | import java.util.Map; | ||||
33 | | ||||
34 | class AndroidFileSystemFactory implements FileSystemFactory { | ||||
35 | final private Context context; | ||||
36 | final Map<String, String> roots; | ||||
37 | | ||||
38 | AndroidFileSystemFactory(Context context) { | ||||
39 | this.context = context; | ||||
40 | this.roots = new HashMap<>(); | ||||
41 | } | ||||
42 | | ||||
43 | void initRoots(List<SftpPlugin.StorageInfo> storageInfoList) { | ||||
44 | for (SftpPlugin.StorageInfo curStorageInfo : storageInfoList) { | ||||
45 | if (curStorageInfo.isFileUri()) { | ||||
46 | if (curStorageInfo.uri.getPath() != null){ | ||||
47 | roots.put(curStorageInfo.displayName, curStorageInfo.uri.getPath()); | ||||
48 | } | ||||
49 | } else if (curStorageInfo.isContentUri()){ | ||||
50 | roots.put(curStorageInfo.displayName, curStorageInfo.uri.toString()); | ||||
51 | } | ||||
52 | } | ||||
53 | } | ||||
54 | | ||||
55 | @Override | ||||
56 | public FileSystemView createFileSystemView(final Session username) { | ||||
57 | if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { | ||||
58 | if (roots.size() == 0) { | ||||
59 | throw new RuntimeException("roots cannot be empty"); | ||||
60 | } | ||||
61 | | ||||
62 | String[] rootsAsString = new String[roots.size()]; | ||||
63 | roots.keySet().toArray(rootsAsString); | ||||
64 | | ||||
65 | return new AndroidFileSystemView(roots, rootsAsString[0], username.getUsername(), context); | ||||
66 | } else { | ||||
67 | return new AndroidSafFileSystemView(roots, username.getUsername(), context); | ||||
68 | } | ||||
69 | } | ||||
70 | } |