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;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.Environment;
|
|
|
|
import android.widget.Button;
|
|
|
|
|
2014-10-10 15:49:27 -07:00
|
|
|
import org.kde.kdeconnect.Helpers.StorageHelper;
|
2014-01-06 19:30:55 +04:00
|
|
|
import org.kde.kdeconnect.NetworkPackage;
|
|
|
|
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 {
|
|
|
|
|
2014-01-07 17:40:21 +04:00
|
|
|
private static final SimpleSftpServer server = new SimpleSftpServer();
|
2014-01-06 19:30:55 +04:00
|
|
|
|
|
|
|
@Override
|
2014-01-07 17:40:21 +04:00
|
|
|
public String getPluginName() {return "plugin_sftp";}
|
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 Drawable getIcon() {
|
|
|
|
return context.getResources().getDrawable(R.drawable.icon);
|
|
|
|
}
|
|
|
|
|
2014-09-16 15:45:31 +02:00
|
|
|
@Override
|
|
|
|
public boolean hasSettings() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-06 19:30:55 +04:00
|
|
|
@Override
|
2014-10-10 15:49:27 -07:00
|
|
|
public boolean isEnabledByDefault() { return true; }
|
2014-01-06 19:30:55 +04:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreate() {
|
2014-01-16 09:51:32 +04:00
|
|
|
server.init(context, device);
|
2014-01-06 19:30:55 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
2014-01-07 17:40:21 +04:00
|
|
|
server.stop();
|
2014-01-06 19:30:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPackageReceived(NetworkPackage np) {
|
|
|
|
if (!np.getType().equals(NetworkPackage.PACKAGE_TYPE_SFTP)) return false;
|
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
|
|
|
|
2014-01-06 19:30:55 +04:00
|
|
|
NetworkPackage np2 = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_SFTP);
|
2014-10-10 15:49:27 -07:00
|
|
|
|
2014-01-07 17:40:21 +04:00
|
|
|
np2.set("ip", server.getLocalIpAddress());
|
|
|
|
np2.set("port", server.port);
|
|
|
|
np2.set("user", server.passwordAuth.getUser());
|
|
|
|
np2.set("password", server.passwordAuth.getPassword());
|
2014-10-10 15:49:27 -07:00
|
|
|
|
|
|
|
//Kept for compatibility, but new desktop clients will read "multiPaths" instead,
|
|
|
|
// that supports devices with more than one external storage
|
2014-01-07 17:40:21 +04:00
|
|
|
np2.set("path", Environment.getExternalStorageDirectory().getAbsolutePath());
|
2014-10-10 15:49:27 -07:00
|
|
|
|
|
|
|
List<StorageHelper.StorageInfo> storageList = StorageHelper.getStorageList();
|
|
|
|
ArrayList<String> paths = new ArrayList<String>();
|
|
|
|
ArrayList<String> pathNames = new ArrayList<String>();
|
|
|
|
|
|
|
|
for (StorageHelper.StorageInfo storage : storageList) {
|
|
|
|
paths.add(storage.path);
|
|
|
|
StringBuilder res = new StringBuilder();
|
|
|
|
|
|
|
|
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));
|
|
|
|
} else {
|
|
|
|
res.append(context.getString(R.string.sftp_sdcard));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res.append(context.getString(R.string.sftp_all_files));
|
|
|
|
}
|
2014-11-22 17:01:21 -08:00
|
|
|
String pathName = res.toString();
|
2014-10-10 15:49:27 -07:00
|
|
|
if (storage.readonly) {
|
|
|
|
res.append(" ");
|
|
|
|
res.append(context.getString(R.string.sftp_readonly));
|
|
|
|
}
|
|
|
|
pathNames.add(res.toString());
|
|
|
|
|
2014-11-22 17:01:21 -08: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-10-10 15:49:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
np2.set("multiPaths", paths);
|
|
|
|
np2.set("pathNames", pathNames);
|
|
|
|
|
2014-01-06 19:30:55 +04:00
|
|
|
device.sendPackage(np2);
|
2014-10-10 15:49:27 -07:00
|
|
|
|
2014-01-06 19:30:55 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-10-10 15:49:27 -07:00
|
|
|
public AlertDialog getErrorDialog(Activity deviceActivity) { return null; }
|
2014-01-06 19:30:55 +04:00
|
|
|
|
|
|
|
@Override
|
2014-10-10 15:49:27 -07:00
|
|
|
public Button getInterfaceButton(Activity activity) { return null; }
|
2014-01-06 19:30:55 +04:00
|
|
|
|
|
|
|
}
|