diff --git a/src/org/kde/kdeconnect/Backends/LanBackend/LanLink.java b/src/org/kde/kdeconnect/Backends/LanBackend/LanLink.java index edddb2e5..5e6f39e2 100644 --- a/src/org/kde/kdeconnect/Backends/LanBackend/LanLink.java +++ b/src/org/kde/kdeconnect/Backends/LanBackend/LanLink.java @@ -1,301 +1,301 @@ /* * Copyright 2014 Albert Vaca Cintora * * 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 . */ package org.kde.kdeconnect.Backends.LanBackend; import android.content.Context; import android.util.Log; import org.json.JSONObject; import org.kde.kdeconnect.Backends.BaseLink; import org.kde.kdeconnect.Backends.BasePairingHandler; import org.kde.kdeconnect.Device; import org.kde.kdeconnect.Helpers.SecurityHelpers.RsaHelper; import org.kde.kdeconnect.Helpers.SecurityHelpers.SslHelper; import org.kde.kdeconnect.Helpers.StringsHelper; import org.kde.kdeconnect.NetworkPackage; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketTimeoutException; import java.nio.channels.NotYetConnectedException; import java.security.PublicKey; import javax.net.ssl.SSLSocket; public class LanLink extends BaseLink { public interface LinkDisconnectedCallback { void linkDisconnected(LanLink brokenLink); } public enum ConnectionStarted { - Locally, Remotely; - }; + Locally, Remotely + } private ConnectionStarted connectionSource; // If the other device sent me a broadcast, // I should not close the connection with it // because it's probably trying to find me and // potentially ask for pairing. private volatile Socket socket = null; private LinkDisconnectedCallback callback; @Override public void disconnect() { Log.i("LanLink/Disconnect","socket:"+ socket.hashCode()); try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } //Returns the old socket public Socket reset(final Socket newSocket, ConnectionStarted connectionSource) throws IOException { Socket oldSocket = socket; socket = newSocket; this.connectionSource = connectionSource; if (oldSocket != null) { oldSocket.close(); //This should cancel the readThread } //Log.e("LanLink", "Start listening"); //Create a thread to take care of incoming data for the new socket new Thread(new Runnable() { @Override public void run() { try { BufferedReader reader = new BufferedReader(new InputStreamReader(newSocket.getInputStream(), StringsHelper.UTF8)); while (true) { String packet; try { packet = reader.readLine(); } catch (SocketTimeoutException e) { continue; } if (packet == null) { throw new IOException("End of stream"); } if (packet.isEmpty()) { continue; } NetworkPackage np = NetworkPackage.unserialize(packet); receivedNetworkPackage(np); } } catch (Exception e) { Log.i("LanLink", "Socket closed: " + newSocket.hashCode() + ". Reason: " + e.getMessage()); try { Thread.sleep(300); } catch (InterruptedException ignored) {} // Wait a bit because we might receive a new socket meanwhile boolean thereIsaANewSocket = (newSocket != socket); if (!thereIsaANewSocket) { callback.linkDisconnected(LanLink.this); } } } }).start(); return oldSocket; } public LanLink(Context context, String deviceId, LanLinkProvider linkProvider, Socket socket, ConnectionStarted connectionSource) throws IOException { super(context, deviceId, linkProvider); callback = linkProvider; reset(socket, connectionSource); } @Override public String getName() { return "LanLink"; } @Override public BasePairingHandler getPairingHandler(Device device, BasePairingHandler.PairingHandlerCallback callback) { return new LanPairingHandler(device, callback); } //Blocking, do not call from main thread private boolean sendPackageInternal(NetworkPackage np, final Device.SendPackageStatusCallback callback, PublicKey key) { if (socket == null) { Log.e("KDE/sendPackage", "Not yet connected"); callback.onFailure(new NotYetConnectedException()); return false; } try { //Prepare socket for the payload final ServerSocket server; if (np.hasPayload()) { server = LanLinkProvider.openServerSocketOnFreePort(LanLinkProvider.PAYLOAD_TRANSFER_MIN_PORT); JSONObject payloadTransferInfo = new JSONObject(); payloadTransferInfo.put("port", server.getLocalPort()); np.setPayloadTransferInfo(payloadTransferInfo); } else { server = null; } //Encrypt if key provided if (key != null) { np = RsaHelper.encrypt(np, key); } //Log.e("LanLink/sendPackage", np.getType()); //Send body of the network package try { OutputStream writer = socket.getOutputStream(); writer.write(np.serialize().getBytes(StringsHelper.UTF8)); writer.flush(); } catch (Exception e) { disconnect(); //main socket is broken, disconnect throw e; } //Send payload if (server != null) { Socket payloadSocket = null; OutputStream outputStream = null; InputStream inputStream = null; try { //Wait a maximum of 10 seconds for the other end to establish a connection with our socket, close it afterwards server.setSoTimeout(10*1000); payloadSocket = server.accept(); //Convert to SSL if needed if (socket instanceof SSLSocket) { payloadSocket = SslHelper.convertToSslSocket(context, payloadSocket, getDeviceId(), true, false); } outputStream = payloadSocket.getOutputStream(); inputStream = np.getPayload(); Log.i("KDE/LanLink", "Beginning to send payload"); byte[] buffer = new byte[4096]; int bytesRead; long size = np.getPayloadSize(); long progress = 0; long timeSinceLastUpdate = -1; while ((bytesRead = inputStream.read(buffer)) != -1) { //Log.e("ok",""+bytesRead); progress += bytesRead; outputStream.write(buffer, 0, bytesRead); if (size > 0) { if (timeSinceLastUpdate + 500 < System.currentTimeMillis()) { //Report progress every half a second long percent = ((100 * progress) / size); callback.onProgressChanged((int) percent); timeSinceLastUpdate = System.currentTimeMillis(); } } } outputStream.flush(); outputStream.close(); Log.i("KDE/LanLink", "Finished sending payload ("+progress+" bytes written)"); } finally { try { server.close(); } catch (Exception e) { } try { payloadSocket.close(); } catch (Exception e) { } try { inputStream.close(); } catch (Exception e) { } try { outputStream.close(); } catch (Exception e) { } } } callback.onSuccess(); return true; } catch (Exception e) { if (callback != null) { callback.onFailure(e); } return false; } finally { //Make sure we close the payload stream, if any InputStream stream = np.getPayload(); try { stream.close(); } catch (Exception e) { } } } //Blocking, do not call from main thread @Override public boolean sendPackage(NetworkPackage np,Device.SendPackageStatusCallback callback) { return sendPackageInternal(np, callback, null); } //Blocking, do not call from main thread @Override public boolean sendPackageEncrypted(NetworkPackage np, Device.SendPackageStatusCallback callback, PublicKey key) { return sendPackageInternal(np, callback, key); } private void receivedNetworkPackage(NetworkPackage np) { if (np.getType().equals(NetworkPackage.PACKAGE_TYPE_ENCRYPTED)) { try { np = RsaHelper.decrypt(np, privateKey); } catch(Exception e) { e.printStackTrace(); Log.e("KDE/onPackageReceived","Exception decrypting the package"); } } if (np.hasPayloadTransferInfo()) { Socket payloadSocket = new Socket(); try { int tcpPort = np.getPayloadTransferInfo().getInt("port"); InetSocketAddress deviceAddress = (InetSocketAddress) socket.getRemoteSocketAddress(); payloadSocket.connect(new InetSocketAddress(deviceAddress.getAddress(), tcpPort)); // Use ssl if existing link is on ssl if (socket instanceof SSLSocket) { payloadSocket = SslHelper.convertToSslSocket(context, payloadSocket, getDeviceId(), true, true); } np.setPayload(payloadSocket.getInputStream(), np.getPayloadSize()); } catch (Exception e) { try { payloadSocket.close(); } catch(Exception ignored) { } e.printStackTrace(); Log.e("KDE/LanLink", "Exception connecting to payload remote socket"); } } packageReceived(np); } @Override public boolean linkShouldBeKeptAlive() { return true; //FIXME: Current implementation is broken, so for now we will keep links always established //We keep the remotely initiated connections, since the remotes require them if they want to request //pairing to us, or connections that are already paired. //return (connectionSource == ConnectionStarted.Remotely); } } diff --git a/src/org/kde/kdeconnect/Plugins/RemoteKeyboardPlugin/RemoteKeyboardService.java b/src/org/kde/kdeconnect/Plugins/RemoteKeyboardPlugin/RemoteKeyboardService.java index 3f2b3852..80c74278 100644 --- a/src/org/kde/kdeconnect/Plugins/RemoteKeyboardPlugin/RemoteKeyboardService.java +++ b/src/org/kde/kdeconnect/Plugins/RemoteKeyboardPlugin/RemoteKeyboardService.java @@ -1,233 +1,233 @@ /* * Copyright 2017 Holger Kaelberer * * 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 . */ package org.kde.kdeconnect.Plugins.RemoteKeyboardPlugin; import android.content.Context; import android.content.Intent; import android.inputmethodservice.InputMethodService; import android.inputmethodservice.Keyboard; import android.inputmethodservice.KeyboardView; import android.inputmethodservice.KeyboardView.OnKeyboardActionListener; import android.os.Handler; import android.util.Log; import android.view.View; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputMethodManager; import android.widget.Toast; import org.kde.kdeconnect.UserInterface.MaterialActivity; import org.kde.kdeconnect.UserInterface.PluginSettingsActivity; import org.kde.kdeconnect_tp.R; import java.util.ArrayList; import java.util.List; public class RemoteKeyboardService extends InputMethodService implements OnKeyboardActionListener { /** * Reference to our instance * null if this InputMethod is not currently selected. */ public static RemoteKeyboardService instance = null; /** * Whether input is currently accepted * Implies visible == true */ public boolean active = false; /** * Whether this InputMethod is currently visible. */ public boolean visible = false; KeyboardView inputView = null; - private final int StatusKeyIdx = 3; - private final int connectedIcon = R.drawable.ic_phonelink_white_36dp; - private final int disconnectedIcon = R.drawable.ic_phonelink_off_white_36dp; Handler handler; void updateInputView() { if (inputView == null) return; Keyboard currentKeyboard = inputView.getKeyboard(); List keys = currentKeyboard.getKeys(); boolean connected = RemoteKeyboardPlugin.isConnected(); // Log.d("RemoteKeyboardService", "Updating keyboard connection icon, connected=" + connected); - keys.get(StatusKeyIdx).icon = getResources().getDrawable(connected ? connectedIcon : disconnectedIcon); - inputView.invalidateKey(StatusKeyIdx); + int disconnectedIcon = R.drawable.ic_phonelink_off_white_36dp; + int connectedIcon = R.drawable.ic_phonelink_white_36dp; + int statusKeyIdx = 3; + keys.get(statusKeyIdx).icon = getResources().getDrawable(connected ? connectedIcon : disconnectedIcon); + inputView.invalidateKey(statusKeyIdx); } @Override public void onCreate() { super.onCreate(); active = false; visible = false; instance = this; handler = new Handler(); Log.d("RemoteKeyboardService", "Remote keyboard initialized"); } @Override public void onDestroy() { super.onDestroy(); instance = null; Log.d("RemoteKeyboardService", "Destroyed"); } @Override public void onInitializeInterface() { super.onInitializeInterface(); } @Override public View onCreateInputView() { // Log.d("RemoteKeyboardService", "onCreateInputView connected=" + RemoteKeyboardPlugin.isConnected()); inputView = new KeyboardView(this, null); inputView.setKeyboard(new Keyboard(this, R.xml.remotekeyboardplugin_keyboard)); inputView.setPreviewEnabled(false); inputView.setOnKeyboardActionListener(this); updateInputView(); return inputView; } @Override public void onStartInputView(EditorInfo attribute, boolean restarting) { // Log.d("RemoteKeyboardService", "onStartInputView"); super.onStartInputView(attribute, restarting); visible = true; ArrayList instances = RemoteKeyboardPlugin.acquireInstances(); try { for (RemoteKeyboardPlugin i : instances) i.notifyKeyboardState(true); } finally { RemoteKeyboardPlugin.releaseInstances(); } } @Override public void onFinishInputView(boolean finishingInput) { // Log.d("RemoteKeyboardService", "onFinishInputView"); super.onFinishInputView(finishingInput); visible = false; ArrayList instances = RemoteKeyboardPlugin.acquireInstances(); try { for (RemoteKeyboardPlugin i : instances) i.notifyKeyboardState(false); } finally { RemoteKeyboardPlugin.releaseInstances(); } } @Override public void onStartInput(EditorInfo attribute, boolean restarting) { // Log.d("RemoteKeyboardService", "onStartInput"); super.onStartInput(attribute, restarting); active = true; } @Override public void onFinishInput() { // Log.d("RemoteKeyboardService", "onFinishInput"); super.onFinishInput(); active = false; } @Override public void onPress(int primaryCode) { switch (primaryCode) { case 0: { // "hide keyboard" requestHideSelf(0); break; } case 1: { // "settings" ArrayList instances = RemoteKeyboardPlugin.acquireInstances(); try { if (instances.size() == 1) { // single instance of RemoteKeyboardPlugin -> access its settings RemoteKeyboardPlugin plugin = instances.get(0); if (plugin != null) { Intent intent = new Intent(this, PluginSettingsActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra("plugin_display_name", plugin.getDisplayName()); intent.putExtra("plugin_key", plugin.getPluginKey()); startActivity(intent); } } else { // != 1 instance of plugin -> show main activity view Intent intent = new Intent(this, MaterialActivity.class); intent.putExtra("forceOverview", true); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); if (instances.size() < 1) Toast.makeText(this, R.string.remotekeyboard_not_connected, Toast.LENGTH_SHORT).show(); else // instances.size() > 1 Toast.makeText(this, R.string.remotekeyboard_multiple_connections, Toast.LENGTH_SHORT).show(); } } finally { RemoteKeyboardPlugin.releaseInstances(); } break; } case 2: { // "keyboard" InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showInputMethodPicker(); break; } case 3: { // "connected"? if (RemoteKeyboardPlugin.isConnected()) Toast.makeText(this, R.string.remotekeyboard_connected, Toast.LENGTH_SHORT).show(); else Toast.makeText(this, R.string.remotekeyboard_not_connected, Toast.LENGTH_SHORT).show(); break; } } } @Override public void onKey(int primaryCode, int[] keyCodes) { } @Override public void onText(CharSequence text) { } @Override public void swipeRight() { } @Override public void swipeLeft() { } @Override public void swipeDown() { } @Override public void swipeUp() { } @Override public void onRelease(int primaryCode) { } } diff --git a/src/org/kde/kdeconnect/Plugins/SftpPlugin/SftpPlugin.java b/src/org/kde/kdeconnect/Plugins/SftpPlugin/SftpPlugin.java index 0ec83789..5bb30940 100644 --- a/src/org/kde/kdeconnect/Plugins/SftpPlugin/SftpPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/SftpPlugin/SftpPlugin.java @@ -1,149 +1,147 @@ /* * Copyright 2014 Samoilenko Yuri * * 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 . */ package org.kde.kdeconnect.Plugins.SftpPlugin; import android.Manifest; import android.os.Environment; import org.kde.kdeconnect.Helpers.StorageHelper; import org.kde.kdeconnect.NetworkPackage; import org.kde.kdeconnect.Plugins.Plugin; import org.kde.kdeconnect_tp.R; import java.io.File; import java.util.ArrayList; import java.util.List; public class SftpPlugin extends Plugin { public final static String PACKAGE_TYPE_SFTP = "kdeconnect.sftp"; public final static String PACKAGE_TYPE_SFTP_REQUEST = "kdeconnect.sftp.request"; private static final SimpleSftpServer server = new SimpleSftpServer(); - private int sftpPermissionExplanation = R.string.sftp_permission_explanation; - @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() { server.init(context, device); - permissionExplanation = sftpPermissionExplanation; + permissionExplanation = R.string.sftp_permission_explanation; return true; } @Override public void onDestroy() { server.stop(); } @Override public boolean onPackageReceived(NetworkPackage np) { if (np.getBoolean("startBrowsing")) { if (server.start()) { NetworkPackage np2 = new NetworkPackage(PACKAGE_TYPE_SFTP); np2.set("ip", server.getLocalIpAddress()); np2.set("port", server.getPort()); np2.set("user", SimpleSftpServer.USER); np2.set("password", server.getPassword()); //Kept for compatibility, in case "multiPaths" is not possible or the other end does not support it np2.set("path", Environment.getExternalStorageDirectory().getAbsolutePath()); List storageList = StorageHelper.getStorageList(); ArrayList paths = new ArrayList<>(); ArrayList pathNames = new ArrayList<>(); 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)); } String pathName = res.toString(); if (storage.readonly) { res.append(" "); res.append(context.getString(R.string.sftp_readonly)); } pathNames.add(res.toString()); //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)); } } } if (paths.size() > 0) { np2.set("multiPaths", paths); np2.set("pathNames", pathNames); } device.sendPackage(np2); return true; } } return false; } @Override public String[] getRequiredPermissions() { return new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}; } @Override public String[] getSupportedPackageTypes() { return new String[]{PACKAGE_TYPE_SFTP_REQUEST}; } @Override public String[] getOutgoingPackageTypes() { return new String[]{PACKAGE_TYPE_SFTP}; } } diff --git a/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java b/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java index 51d12bdf..5d2737e5 100644 --- a/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java +++ b/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java @@ -1,472 +1,470 @@ /* * Copyright 2014 Albert Vaca Cintora * * 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 . */ package org.kde.kdeconnect.Plugins.SharePlugin; import android.Manifest; import android.app.Activity; import android.app.DownloadManager; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.ClipboardManager; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.res.Resources; import android.database.Cursor; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.provider.MediaStore; import android.support.v4.app.NotificationCompat; import android.support.v4.app.TaskStackBuilder; import android.support.v4.content.ContextCompat; import android.support.v4.provider.DocumentFile; import android.util.Log; import android.widget.Toast; import org.kde.kdeconnect.Device; import org.kde.kdeconnect.Helpers.FilesHelper; import org.kde.kdeconnect.Helpers.MediaStoreHelper; import org.kde.kdeconnect.Helpers.NotificationHelper; import org.kde.kdeconnect.NetworkPackage; import org.kde.kdeconnect.Plugins.Plugin; import org.kde.kdeconnect.UserInterface.SettingsActivity; import org.kde.kdeconnect_tp.R; import java.io.File; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.util.ArrayList; public class SharePlugin extends Plugin { public final static String PACKAGE_TYPE_SHARE_REQUEST = "kdeconnect.share.request"; final static boolean openUrlsDirectly = true; - private int sharePermissionExplanation = R.string.share_optional_permission_explanation; - @Override public boolean onCreate() { - optionalPermissionExplanation = sharePermissionExplanation; + optionalPermissionExplanation = R.string.share_optional_permission_explanation; return true; } @Override public String getDisplayName() { return context.getResources().getString(R.string.pref_plugin_sharereceiver); } @Override public Drawable getIcon() { return ContextCompat.getDrawable(context, R.drawable.share_plugin_action); } @Override public String getDescription() { return context.getResources().getString(R.string.pref_plugin_sharereceiver_desc); } @Override public boolean hasMainActivity() { return true; } @Override public String getActionName() { return context.getString(R.string.send_files); } @Override public void startMainActivity(Activity parentActivity) { Intent intent = new Intent(parentActivity, SendFileActivity.class); intent.putExtra("deviceId", device.getDeviceId()); parentActivity.startActivity(intent); } @Override public boolean hasSettings() { return true; } @Override public boolean onPackageReceived(NetworkPackage np) { try { if (np.hasPayload()) { Log.i("SharePlugin", "hasPayload"); if (isPermissionGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { receiveFile(np); } else { Log.i("SharePlugin", "no Permission for Storage"); } } else if (np.has("text")) { Log.i("SharePlugin", "hasText"); receiveText(np); } else if (np.has("url")) { receiveUrl(np); } else { Log.e("SharePlugin", "Error: Nothing attached!"); } } catch (Exception e) { Log.e("SharePlugin", "Exception"); e.printStackTrace(); } return true; } private void receiveUrl(NetworkPackage np) { String url = np.getString("url"); Log.i("SharePlugin", "hasUrl: " + url); Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if (openUrlsDirectly) { context.startActivity(browserIntent); } else { Resources res = context.getResources(); TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); stackBuilder.addNextIntent(browserIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT ); Notification noti = new NotificationCompat.Builder(context) .setContentTitle(res.getString(R.string.received_url_title, device.getName())) .setContentText(res.getString(R.string.received_url_text, url)) .setContentIntent(resultPendingIntent) .setTicker(res.getString(R.string.received_url_title, device.getName())) .setSmallIcon(R.drawable.ic_notification) .setAutoCancel(true) .setDefaults(Notification.DEFAULT_ALL) .build(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationHelper.notifyCompat(notificationManager, (int) System.currentTimeMillis(), noti); } } private void receiveText(NetworkPackage np) { String text = np.getString("text"); if (Build.VERSION.SDK_INT >= 11) { ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); cm.setText(text); } else { android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); clipboard.setText(text); } Toast.makeText(context, R.string.shareplugin_text_saved, Toast.LENGTH_LONG).show(); } private void receiveFile(NetworkPackage np) { final InputStream input = np.getPayload(); final long fileLength = np.getPayloadSize(); final String originalFilename = np.getString("filename", Long.toString(System.currentTimeMillis())); //We need to check for already existing files only when storing in the default path. //User-defined paths use the new Storage Access Framework that already handles this. final boolean customDestination = ShareSettingsActivity.isCustomDestinationEnabled(context); final String defaultPath = ShareSettingsActivity.getDefaultDestinationDirectory().getAbsolutePath(); final String filename = customDestination ? originalFilename : FilesHelper.findNonExistingNameForNewFile(defaultPath, originalFilename); String displayName = FilesHelper.getFileNameWithoutExt(filename); final String mimeType = FilesHelper.getMimeTypeFromFile(filename); if ("*/*".equals(mimeType)) { displayName = filename; } final DocumentFile destinationFolderDocument = ShareSettingsActivity.getDestinationDirectory(context); final DocumentFile destinationDocument = destinationFolderDocument.createFile(mimeType, displayName); final OutputStream destinationOutput; try { destinationOutput = context.getContentResolver().openOutputStream(destinationDocument.getUri()); } catch (FileNotFoundException e) { e.printStackTrace(); return; } final Uri destinationUri = destinationDocument.getUri(); final ShareNotification notification = new ShareNotification(device, filename); notification.show(); new Thread(new Runnable() { @Override public void run() { try { byte data[] = new byte[4096]; long progress = 0, prevProgressPercentage = -1; int count; long lastUpdate = 0; while ((count = input.read(data)) >= 0) { progress += count; destinationOutput.write(data, 0, count); if (fileLength > 0) { if (progress >= fileLength) break; long progressPercentage = (progress * 100 / fileLength); if (progressPercentage != prevProgressPercentage && System.currentTimeMillis() - lastUpdate > 100) { prevProgressPercentage = progressPercentage; lastUpdate = System.currentTimeMillis(); notification.setProgress((int) progressPercentage); notification.show(); } } //else Log.e("SharePlugin", "Infinite loop? :D"); } destinationOutput.flush(); Log.i("SharePlugin", "Transfer finished: " + destinationUri.getPath()); //Update the notification and allow to open the file from it notification.setFinished(true); notification.setURI(destinationUri, mimeType); notification.show(); if (!customDestination && Build.VERSION.SDK_INT >= 12) { Log.i("SharePlugin", "Adding to downloads"); DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); manager.addCompletedDownload(destinationUri.getLastPathSegment(), device.getName(), true, mimeType, destinationUri.getPath(), fileLength, false); } else { //Make sure it is added to the Android Gallery anyway MediaStoreHelper.indexFile(context, destinationUri); } } catch (Exception e) { Log.e("SharePlugin", "Receiver thread exception"); e.printStackTrace(); notification.setFinished(false); notification.show(); } finally { try { destinationOutput.close(); } catch (Exception e) { } try { input.close(); } catch (Exception e) { } } } }).start(); } @Override public void startPreferencesActivity(SettingsActivity parentActivity) { Intent intent = new Intent(parentActivity, ShareSettingsActivity.class); intent.putExtra("plugin_display_name", getDisplayName()); intent.putExtra("plugin_key", getPluginKey()); parentActivity.startActivity(intent); } static void queuedSendUriList(Context context, final Device device, final ArrayList uriList) { //Read all the data early, as we only have permissions to do it while the activity is alive final ArrayList toSend = new ArrayList<>(); for (Uri uri : uriList) { toSend.add(uriToNetworkPackage(context, uri)); } //Callback that shows a progress notification final NotificationUpdateCallback notificationUpdateCallback = new NotificationUpdateCallback(context, device, toSend); //Do the sending in background new Thread(new Runnable() { @Override public void run() { //Actually send the files try { for (NetworkPackage np : toSend) { boolean success = device.sendPackageBlocking(np, notificationUpdateCallback); if (!success) { Log.e("SharePlugin", "Error sending files"); return; } } } catch (Exception e) { e.printStackTrace(); } } }).start(); } //Create the network package from the URI private static NetworkPackage uriToNetworkPackage(final Context context, final Uri uri) { try { ContentResolver cr = context.getContentResolver(); InputStream inputStream = cr.openInputStream(uri); NetworkPackage np = new NetworkPackage(PACKAGE_TYPE_SHARE_REQUEST); long size = -1; if (uri.getScheme().equals("file")) { // file:// is a non media uri, so we cannot query the ContentProvider np.set("filename", uri.getLastPathSegment()); try { size = new File(uri.getPath()).length(); } catch (Exception e) { Log.e("SendFileActivity", "Could not obtain file size"); e.printStackTrace(); } } else { // Probably a content:// uri, so we query the Media content provider Cursor cursor = null; try { String[] proj = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.SIZE, MediaStore.MediaColumns.DISPLAY_NAME}; cursor = cr.query(uri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); cursor.moveToFirst(); String path = cursor.getString(column_index); np.set("filename", Uri.parse(path).getLastPathSegment()); size = new File(path).length(); } catch (Exception unused) { Log.w("SendFileActivity", "Could not resolve media to a file, trying to get info as media"); try { int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME); cursor.moveToFirst(); String name = cursor.getString(column_index); np.set("filename", name); } catch (Exception e) { e.printStackTrace(); Log.e("SendFileActivity", "Could not obtain file name"); } try { int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE); cursor.moveToFirst(); //For some reason this size can differ from the actual file size! size = cursor.getInt(column_index); } catch (Exception e) { Log.e("SendFileActivity", "Could not obtain file size"); e.printStackTrace(); } } finally { try { cursor.close(); } catch (Exception e) { } } } np.setPayload(inputStream, size); return np; } catch (Exception e) { Log.e("SendFileActivity", "Exception sending files"); e.printStackTrace(); return null; } } public static void share(Intent intent, Device device) { Bundle extras = intent.getExtras(); if (extras != null) { if (extras.containsKey(Intent.EXTRA_STREAM)) { try { ArrayList uriList; if (!Intent.ACTION_SEND.equals(intent.getAction())) { uriList = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); } else { Uri uri = extras.getParcelable(Intent.EXTRA_STREAM); uriList = new ArrayList<>(); uriList.add(uri); } SharePlugin.queuedSendUriList(device.getContext(), device, uriList); } catch (Exception e) { Log.e("ShareActivity", "Exception"); e.printStackTrace(); } } else if (extras.containsKey(Intent.EXTRA_TEXT)) { String text = extras.getString(Intent.EXTRA_TEXT); String subject = extras.getString(Intent.EXTRA_SUBJECT); //Hack: Detect shared youtube videos, so we can open them in the browser instead of as text if (subject != null && subject.endsWith("YouTube")) { int index = text.indexOf(": http://youtu.be/"); if (index > 0) { text = text.substring(index + 2); //Skip ": " } } boolean isUrl; try { new URL(text); isUrl = true; } catch (Exception e) { isUrl = false; } NetworkPackage np = new NetworkPackage(SharePlugin.PACKAGE_TYPE_SHARE_REQUEST); if (isUrl) { np.set("url", text); } else { np.set("text", text); } device.sendPackage(np); } } } @Override public String[] getSupportedPackageTypes() { return new String[]{PACKAGE_TYPE_SHARE_REQUEST}; } @Override public String[] getOutgoingPackageTypes() { return new String[]{PACKAGE_TYPE_SHARE_REQUEST}; } @Override public String[] getOptionalPermissions() { return new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}; } } diff --git a/src/org/kde/kdeconnect/UserInterface/PairingFragment.java b/src/org/kde/kdeconnect/UserInterface/PairingFragment.java index 8d57e7de..23c08c20 100644 --- a/src/org/kde/kdeconnect/UserInterface/PairingFragment.java +++ b/src/org/kde/kdeconnect/UserInterface/PairingFragment.java @@ -1,295 +1,294 @@ /* * Copyright 2014 Albert Vaca Cintora * * 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 . */ package org.kde.kdeconnect.UserInterface; import android.app.Activity; import android.content.Intent; import android.content.res.Resources; import android.os.Bundle; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; import android.widget.TextView; import org.kde.kdeconnect.BackgroundService; import org.kde.kdeconnect.Device; import org.kde.kdeconnect.Helpers.NetworkHelper; import org.kde.kdeconnect.UserInterface.List.ListAdapter; import org.kde.kdeconnect.UserInterface.List.PairingDeviceItem; import org.kde.kdeconnect.UserInterface.List.SectionItem; import org.kde.kdeconnect_tp.R; import java.util.ArrayList; import java.util.Collection; /** * The view that the user will see when there are no devices paired, or when you choose "add a new device" from the sidebar. */ public class PairingFragment extends Fragment implements PairingDeviceItem.Callback { private static final int RESULT_PAIRING_SUCCESFUL = Activity.RESULT_FIRST_USER; private View rootView; - private View listRootView; private SwipeRefreshLayout mSwipeRefreshLayout; private MaterialActivity mActivity; boolean listRefreshCalledThisFrame = false; TextView headerText; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //Log.e("PairingFragmen", "OnCreateView"); mActivity.getSupportActionBar().setTitle(R.string.pairing_title); setHasOptionsMenu(true); rootView = inflater.inflate(R.layout.activity_refresh_list, container, false); - listRootView = rootView.findViewById(R.id.listView1); + View listRootView = rootView.findViewById(R.id.listView1); mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.refresh_list_layout); mSwipeRefreshLayout.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { updateComputerListAction(); } } ); headerText = new TextView(inflater.getContext()); headerText.setText(getString(R.string.pairing_description)); headerText.setPadding(0, (int) (16 * getResources().getDisplayMetrics().density), 0, (int) (12 * getResources().getDisplayMetrics().density)); ((ListView) listRootView).addHeaderView(headerText); return rootView; } @Override public void onAttach(Activity activity) { super.onAttach(activity); mActivity = ((MaterialActivity) getActivity()); } private void updateComputerListAction() { updateComputerList(); BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() { @Override public void onServiceStart(BackgroundService service) { service.onNetworkChange(); } }); mSwipeRefreshLayout.setRefreshing(true); new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(1500); } catch (InterruptedException ignored) { } mActivity.runOnUiThread(new Runnable() { @Override public void run() { mSwipeRefreshLayout.setRefreshing(false); } }); } }).start(); } private void updateComputerList() { BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() { @Override public void onServiceStart(final BackgroundService service) { mActivity.runOnUiThread(new Runnable() { @Override public void run() { if (!isAdded()) { //Fragment is not attached to an activity. We will crash if we try to do anything here. return; } if (listRefreshCalledThisFrame) { // This makes sure we don't try to call list.getFirstVisiblePosition() // twice per frame, because the second time the list hasn't been drawn // yet and it would always return 0. return; } listRefreshCalledThisFrame = true; headerText.setText(getString(NetworkHelper.isOnMobileNetwork(getContext()) ? R.string.on_data_message : R.string.pairing_description)); //Disable tap animation headerText.setOnClickListener(null); headerText.setOnLongClickListener(null); try { Collection devices = service.getDevices().values(); final ArrayList items = new ArrayList<>(); SectionItem section; Resources res = getResources(); section = new SectionItem(res.getString(R.string.category_not_paired_devices)); section.isSectionEmpty = true; items.add(section); for (Device device : devices) { if (device.isReachable() && !device.isPaired()) { items.add(new PairingDeviceItem(device, PairingFragment.this)); section.isSectionEmpty = false; } } section = new SectionItem(res.getString(R.string.category_connected_devices)); section.isSectionEmpty = true; items.add(section); for (Device device : devices) { if (device.isReachable() && device.isPaired()) { items.add(new PairingDeviceItem(device, PairingFragment.this)); section.isSectionEmpty = false; } } if (section.isSectionEmpty) { items.remove(items.size() - 1); //Remove connected devices section if empty } section = new SectionItem(res.getString(R.string.category_remembered_devices)); section.isSectionEmpty = true; items.add(section); for (Device device : devices) { if (!device.isReachable() && device.isPaired()) { items.add(new PairingDeviceItem(device, PairingFragment.this)); section.isSectionEmpty = false; } } if (section.isSectionEmpty) { items.remove(items.size() - 1); //Remove remembered devices section if empty } final ListView list = (ListView) rootView.findViewById(R.id.listView1); //Store current scroll int index = list.getFirstVisiblePosition(); View v = list.getChildAt(0); int top = (v == null) ? 0 : (v.getTop() - list.getPaddingTop()); list.setAdapter(new ListAdapter(mActivity, items)); //Restore scroll list.setSelectionFromTop(index, top); } catch (IllegalStateException e) { e.printStackTrace(); //Ignore: The activity was closed while we were trying to update it } finally { listRefreshCalledThisFrame = false; } } }); } }); } @Override public void onStart() { super.onStart(); BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() { @Override public void onServiceStart(BackgroundService service) { service.addDeviceListChangedCallback("PairingFragment", new BackgroundService.DeviceListChangedCallback() { @Override public void onDeviceListChanged() { updateComputerList(); } }); } }); updateComputerList(); } @Override public void onStop() { super.onStop(); mSwipeRefreshLayout.setEnabled(false); BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() { @Override public void onServiceStart(BackgroundService service) { service.removeDeviceListChangedCallback("PairingFragment"); } }); } @Override public void pairingClicked(Device device) { mActivity.onDeviceSelected(device.getDeviceId(), !device.isPaired() || !device.isReachable()); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case RESULT_PAIRING_SUCCESFUL: if (resultCode == 1) { String deviceId = data.getStringExtra("deviceId"); mActivity.onDeviceSelected(deviceId); } break; default: super.onActivityResult(requestCode, resultCode, data); } } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.pairing, menu); } @Override public boolean onOptionsItemSelected(final MenuItem item) { switch (item.getItemId()) { case R.id.menu_refresh: updateComputerListAction(); break; case R.id.menu_rename: mActivity.renameDevice(); break; case R.id.menu_custom_device_list: startActivity(new Intent(mActivity, CustomDevicesActivity.class)); break; default: break; } return true; } }