2014-11-16 23:14:06 -08:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Samoilenko Yuri <kinnalru@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License or (at your option) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-01-06 19:30:55 +04:00
|
|
|
package org.kde.kdeconnect.Plugins.SftpPlugin;
|
|
|
|
|
2017-05-31 15:51:07 +02:00
|
|
|
import android.Manifest;
|
2018-11-07 16:05:53 +01:00
|
|
|
import android.os.Build;
|
2014-01-06 19:30:55 +04:00
|
|
|
import android.os.Environment;
|
|
|
|
|
2014-10-10 15:49:27 -07:00
|
|
|
import org.kde.kdeconnect.Helpers.StorageHelper;
|
2018-03-04 11:31:37 +01:00
|
|
|
import org.kde.kdeconnect.NetworkPacket;
|
2014-01-06 19:30:55 +04:00
|
|
|
import org.kde.kdeconnect.Plugins.Plugin;
|
|
|
|
import org.kde.kdeconnect_tp.R;
|
|
|
|
|
2014-10-10 15:49:27 -07:00
|
|
|
import java.io.File;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2014-01-06 19:30:55 +04:00
|
|
|
public class SftpPlugin extends Plugin {
|
|
|
|
|
2018-10-26 23:53:58 +02:00
|
|
|
private final static String PACKET_TYPE_SFTP = "kdeconnect.sftp";
|
|
|
|
private final static String PACKET_TYPE_SFTP_REQUEST = "kdeconnect.sftp.request";
|
2016-05-31 17:19:39 +02:00
|
|
|
|
2014-01-07 17:40:21 +04:00
|
|
|
private static final SimpleSftpServer server = new SimpleSftpServer();
|
2014-01-06 19:30:55 +04:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDisplayName() {
|
|
|
|
return context.getResources().getString(R.string.pref_plugin_sftp);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDescription() {
|
|
|
|
return context.getResources().getString(R.string.pref_plugin_sftp_desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreate() {
|
2018-03-03 16:26:33 +01:00
|
|
|
permissionExplanation = R.string.sftp_permission_explanation;
|
2018-08-31 21:12:04 +02:00
|
|
|
try {
|
|
|
|
server.init(context, device);
|
|
|
|
return true;
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-06 19:30:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
2014-01-07 17:40:21 +04:00
|
|
|
server.stop();
|
2014-01-06 19:30:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-04 11:31:37 +01:00
|
|
|
public boolean onPacketReceived(NetworkPacket np) {
|
2014-02-14 13:13:17 +01:00
|
|
|
|
2014-01-06 19:30:55 +04:00
|
|
|
if (np.getBoolean("startBrowsing")) {
|
2014-01-07 17:40:21 +04:00
|
|
|
if (server.start()) {
|
2014-10-10 15:49:27 -07:00
|
|
|
|
2018-03-04 11:31:37 +01:00
|
|
|
NetworkPacket np2 = new NetworkPacket(PACKET_TYPE_SFTP);
|
2014-10-10 15:49:27 -07:00
|
|
|
|
2014-01-07 17:40:21 +04:00
|
|
|
np2.set("ip", server.getLocalIpAddress());
|
2016-12-11 13:31:17 +01:00
|
|
|
np2.set("port", server.getPort());
|
|
|
|
np2.set("user", SimpleSftpServer.USER);
|
|
|
|
np2.set("password", server.getPassword());
|
2014-10-10 15:49:27 -07:00
|
|
|
|
2015-03-15 17:52:14 -07:00
|
|
|
//Kept for compatibility, in case "multiPaths" is not possible or the other end does not support it
|
2014-01-07 17:40:21 +04:00
|
|
|
np2.set("path", Environment.getExternalStorageDirectory().getAbsolutePath());
|
2014-10-10 15:49:27 -07:00
|
|
|
|
2017-05-31 15:51:07 +02:00
|
|
|
List<StorageHelper.StorageInfo> storageList = StorageHelper.getStorageList();
|
|
|
|
ArrayList<String> paths = new ArrayList<>();
|
|
|
|
ArrayList<String> pathNames = new ArrayList<>();
|
2014-10-10 15:49:27 -07:00
|
|
|
|
2017-05-31 15:51:07 +02:00
|
|
|
for (StorageHelper.StorageInfo storage : storageList) {
|
|
|
|
paths.add(storage.path);
|
|
|
|
StringBuilder res = new StringBuilder();
|
2014-10-10 15:49:27 -07:00
|
|
|
|
2017-05-31 15:51:07 +02:00
|
|
|
if (storageList.size() > 1) {
|
|
|
|
if (!storage.removable) {
|
|
|
|
res.append(context.getString(R.string.sftp_internal_storage));
|
|
|
|
} else if (storage.number > 1) {
|
|
|
|
res.append(context.getString(R.string.sftp_sdcard_num, storage.number));
|
2014-10-10 15:49:27 -07:00
|
|
|
} else {
|
2017-05-31 15:51:07 +02:00
|
|
|
res.append(context.getString(R.string.sftp_sdcard));
|
2014-10-10 15:49:27 -07:00
|
|
|
}
|
2017-05-31 15:51:07 +02:00
|
|
|
} else {
|
|
|
|
res.append(context.getString(R.string.sftp_all_files));
|
2014-10-10 15:49:27 -07:00
|
|
|
}
|
2017-05-31 15:51:07 +02:00
|
|
|
String pathName = res.toString();
|
|
|
|
if (storage.readonly) {
|
|
|
|
res.append(" ");
|
|
|
|
res.append(context.getString(R.string.sftp_readonly));
|
|
|
|
}
|
|
|
|
pathNames.add(res.toString());
|
2014-10-10 15:49:27 -07:00
|
|
|
|
2017-05-31 15:51:07 +02:00
|
|
|
//Shortcut for users that only want to browse camera pictures
|
|
|
|
String dcim = storage.path + "/DCIM/Camera";
|
|
|
|
if (new File(dcim).exists()) {
|
|
|
|
paths.add(dcim);
|
|
|
|
if (storageList.size() > 1) {
|
|
|
|
pathNames.add(context.getString(R.string.sftp_camera) + "(" + pathName + ")");
|
|
|
|
} else {
|
|
|
|
pathNames.add(context.getString(R.string.sftp_camera));
|
|
|
|
}
|
2014-11-22 17:01:21 -08:00
|
|
|
}
|
2014-10-10 15:49:27 -07:00
|
|
|
}
|
|
|
|
|
2017-05-31 15:51:07 +02:00
|
|
|
if (paths.size() > 0) {
|
|
|
|
np2.set("multiPaths", paths);
|
|
|
|
np2.set("pathNames", pathNames);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-04 11:31:37 +01:00
|
|
|
device.sendPacket(np2);
|
2014-10-10 15:49:27 -07:00
|
|
|
|
2014-01-06 19:30:55 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-31 15:51:07 +02:00
|
|
|
@Override
|
|
|
|
public String[] getRequiredPermissions() {
|
2018-11-07 16:05:53 +01:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
|
|
|
|
return new String[]{Manifest.permission.READ_EXTERNAL_STORAGE};
|
|
|
|
} else {
|
|
|
|
return new String[0];
|
|
|
|
}
|
2017-05-31 15:51:07 +02:00
|
|
|
}
|
|
|
|
|
2015-09-08 14:54:04 -07:00
|
|
|
@Override
|
2018-03-04 11:31:37 +01:00
|
|
|
public String[] getSupportedPacketTypes() {
|
|
|
|
return new String[]{PACKET_TYPE_SFTP_REQUEST};
|
2015-09-08 14:54:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-04 11:31:37 +01:00
|
|
|
public String[] getOutgoingPacketTypes() {
|
|
|
|
return new String[]{PACKET_TYPE_SFTP};
|
2015-09-08 14:54:04 -07:00
|
|
|
}
|
|
|
|
|
2014-01-06 19:30:55 +04:00
|
|
|
}
|