Search in sources :

Example 1 with BackgroundService

use of org.kde.kdeconnect.BackgroundService in project kdeconnect-android by KDE.

the class DeviceFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.activity_device, container, false);
    final String deviceId = getArguments().getString(ARG_DEVICE_ID);
    if (deviceId != null) {
        mDeviceId = deviceId;
    }
    setHasOptionsMenu(true);
    //Log.e("DeviceFragment", "device: " + deviceId);
    BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() {

        @Override
        public void onServiceStart(BackgroundService service) {
            device = service.getDevice(mDeviceId);
            if (device == null) {
                Log.e("DeviceFragment", "Trying to display a device fragment but the device is not present");
                mActivity.onDeviceSelected(null);
                return;
            }
            mActivity.getSupportActionBar().setTitle(device.getName());
            device.addPairingCallback(pairingCallback);
            device.addPluginsChangedListener(pluginsChangedListener);
            refreshUI();
        }
    });
    final Button pairButton = (Button) rootView.findViewById(R.id.pair_button);
    pairButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            pairButton.setVisibility(View.GONE);
            ((TextView) rootView.findViewById(R.id.pair_message)).setText("");
            rootView.findViewById(R.id.pair_progress).setVisibility(View.VISIBLE);
            BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() {

                @Override
                public void onServiceStart(BackgroundService service) {
                    device = service.getDevice(deviceId);
                    if (device == null)
                        return;
                    device.requestPairing();
                }
            });
        }
    });
    rootView.findViewById(R.id.accept_button).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() {

                @Override
                public void onServiceStart(BackgroundService service) {
                    if (device != null) {
                        device.acceptPairing();
                        rootView.findViewById(R.id.pairing_buttons).setVisibility(View.GONE);
                    }
                }
            });
        }
    });
    rootView.findViewById(R.id.reject_button).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() {

                @Override
                public void onServiceStart(BackgroundService service) {
                    if (device != null) {
                        //Remove listener so buttons don't show for a while before changing the view
                        device.removePluginsChangedListener(pluginsChangedListener);
                        device.removePairingCallback(pairingCallback);
                        device.rejectPairing();
                    }
                    mActivity.onDeviceSelected(null);
                }
            });
        }
    });
    return rootView;
}
Also used : BackgroundService(org.kde.kdeconnect.BackgroundService) Button(android.widget.Button) View(android.view.View) TextView(android.widget.TextView) ListView(android.widget.ListView)

Example 2 with BackgroundService

use of org.kde.kdeconnect.BackgroundService in project kdeconnect-android by KDE.

the class SettingsActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(this);
    setPreferenceScreen(preferenceScreen);
    if (getIntent().hasExtra("deviceId")) {
        deviceId = getIntent().getStringExtra("deviceId");
    }
    BackgroundService.RunCommand(getApplicationContext(), new BackgroundService.InstanceCallback() {

        @Override
        public void onServiceStart(BackgroundService service) {
            final Device device = service.getDevice(deviceId);
            if (device == null) {
                SettingsActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        SettingsActivity.this.finish();
                    }
                });
                return;
            }
            List<String> plugins = device.getSupportedPlugins();
            for (final String pluginKey : plugins) {
                PluginPreference pref = new PluginPreference(SettingsActivity.this, pluginKey, device);
                preferenceScreen.addPreference(pref);
            }
        }
    });
}
Also used : BackgroundService(org.kde.kdeconnect.BackgroundService) PreferenceScreen(android.preference.PreferenceScreen) Device(org.kde.kdeconnect.Device) List(java.util.List)

Example 3 with BackgroundService

use of org.kde.kdeconnect.BackgroundService in project kdeconnect-android by KDE.

the class PairingFragment method updateComputerList.

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 (listRefreshCalledThisFrame) {
                        // 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));
                    try {
                        Collection<Device> devices = service.getDevices().values();
                        final ArrayList<ListAdapter.Item> 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) {
                            //Remove connected devices section if empty
                            items.remove(items.size() - 1);
                        }
                        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) {
                            //Remove remembered devices section if empty
                            items.remove(items.size() - 1);
                        }
                        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;
                    }
                }
            });
        }
    });
}
Also used : PairingDeviceItem(org.kde.kdeconnect.UserInterface.List.PairingDeviceItem) BackgroundService(org.kde.kdeconnect.BackgroundService) Device(org.kde.kdeconnect.Device) ArrayList(java.util.ArrayList) TextView(android.widget.TextView) View(android.view.View) ListView(android.widget.ListView) SectionItem(org.kde.kdeconnect.UserInterface.List.SectionItem) MenuItem(android.view.MenuItem) PairingDeviceItem(org.kde.kdeconnect.UserInterface.List.PairingDeviceItem) SectionItem(org.kde.kdeconnect.UserInterface.List.SectionItem) ListView(android.widget.ListView) Resources(android.content.res.Resources) ListAdapter(org.kde.kdeconnect.UserInterface.List.ListAdapter)

Example 4 with BackgroundService

use of org.kde.kdeconnect.BackgroundService in project kdeconnect-android by KDE.

the class SendFileActivity method onActivityResult.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
        case Activity.RESULT_FIRST_USER:
            if (resultCode == RESULT_OK) {
                final ArrayList<Uri> uris = new ArrayList<>();
                Uri uri = data.getData();
                if (uri != null) {
                    uris.add(uri);
                }
                if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)) {
                    ClipData clipdata = data.getClipData();
                    if (clipdata != null) {
                        for (int i = 0; i < clipdata.getItemCount(); i++) {
                            uris.add(clipdata.getItemAt(i).getUri());
                        }
                    }
                }
                if (uris.isEmpty()) {
                    Log.w("SendFileActivity", "No files to send?");
                } else {
                    BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {

                        @Override
                        public void onServiceStart(BackgroundService service) {
                            Device device = service.getDevice(mDeviceId);
                            if (device == null) {
                                Log.e("SendFileActivity", "Device is null");
                                finish();
                                return;
                            }
                            SharePlugin.queuedSendUriList(getApplicationContext(), device, uris);
                        }
                    });
                }
            }
            finish();
            break;
        default:
            super.onActivityResult(requestCode, resultCode, data);
    }
}
Also used : BackgroundService(org.kde.kdeconnect.BackgroundService) Device(org.kde.kdeconnect.Device) ArrayList(java.util.ArrayList) Uri(android.net.Uri) ClipData(android.content.ClipData)

Example 5 with BackgroundService

use of org.kde.kdeconnect.BackgroundService in project kdeconnect-android by KDE.

the class LanLinkProvider method identityPackageReceived.

private void identityPackageReceived(final NetworkPackage identityPackage, final Socket socket, final LanLink.ConnectionStarted connectionStarted) {
    String myId = DeviceHelper.getDeviceId(context);
    final String deviceId = identityPackage.getString("deviceId");
    if (deviceId.equals(myId)) {
        Log.e("KDE/LanLinkProvider", "Somehow I'm connected to myself, ignoring. This should not happen.");
        return;
    }
    // If I'm the TCP server I will be the SSL client and viceversa.
    final boolean clientMode = (connectionStarted == LanLink.ConnectionStarted.Locally);
    // Add ssl handler if device uses new protocol
    try {
        if (identityPackage.getInt("protocolVersion") >= MIN_VERSION_WITH_SSL_SUPPORT) {
            SharedPreferences preferences = context.getSharedPreferences("trusted_devices", Context.MODE_PRIVATE);
            boolean isDeviceTrusted = preferences.getBoolean(deviceId, false);
            if (isDeviceTrusted && !SslHelper.isCertificateStored(context, deviceId)) {
                //Device paired with and old version, we can't use it as we lack the certificate
                BackgroundService.RunCommand(context, new BackgroundService.InstanceCallback() {

                    @Override
                    public void onServiceStart(BackgroundService service) {
                        Device device = service.getDevice(deviceId);
                        if (device == null)
                            return;
                        device.unpair();
                        //Retry as unpaired
                        identityPackageReceived(identityPackage, socket, connectionStarted);
                    }
                });
            }
            Log.i("KDE/LanLinkProvider", "Starting SSL handshake with " + identityPackage.getString("deviceName") + " trusted:" + isDeviceTrusted);
            final SSLSocket sslsocket = SslHelper.convertToSslSocket(context, socket, deviceId, isDeviceTrusted, clientMode);
            sslsocket.addHandshakeCompletedListener(new HandshakeCompletedListener() {

                @Override
                public void handshakeCompleted(HandshakeCompletedEvent event) {
                    String mode = clientMode ? "client" : "server";
                    try {
                        Certificate certificate = event.getPeerCertificates()[0];
                        identityPackage.set("certificate", Base64.encodeToString(certificate.getEncoded(), 0));
                        Log.i("KDE/LanLinkProvider", "Handshake as " + mode + " successful with " + identityPackage.getString("deviceName") + " secured with " + event.getCipherSuite());
                        addLink(identityPackage, sslsocket, connectionStarted);
                    } catch (Exception e) {
                        Log.e("KDE/LanLinkProvider", "Handshake as " + mode + " failed with " + identityPackage.getString("deviceName"));
                        e.printStackTrace();
                        BackgroundService.RunCommand(context, new BackgroundService.InstanceCallback() {

                            @Override
                            public void onServiceStart(BackgroundService service) {
                                Device device = service.getDevice(deviceId);
                                if (device == null)
                                    return;
                                device.unpair();
                            }
                        });
                    }
                }
            });
            //Handshake is blocking, so do it on another thread and free this thread to keep receiving new connection
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        sslsocket.startHandshake();
                    } catch (Exception e) {
                        Log.e("KDE/LanLinkProvider", "Handshake failed with " + identityPackage.getString("deviceName"));
                        e.printStackTrace();
                    //String[] ciphers = sslsocket.getSupportedCipherSuites();
                    //for (String cipher : ciphers) {
                    //    Log.i("SupportedCiphers","cipher: " + cipher);
                    //}
                    }
                }
            }).start();
        } else {
            addLink(identityPackage, socket, connectionStarted);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : BackgroundService(org.kde.kdeconnect.BackgroundService) SharedPreferences(android.content.SharedPreferences) Device(org.kde.kdeconnect.Device) SSLSocket(javax.net.ssl.SSLSocket) SocketException(java.net.SocketException) IOException(java.io.IOException) HandshakeCompletedListener(javax.net.ssl.HandshakeCompletedListener) HandshakeCompletedEvent(javax.net.ssl.HandshakeCompletedEvent) Certificate(java.security.cert.Certificate)

Aggregations

BackgroundService (org.kde.kdeconnect.BackgroundService)8 Device (org.kde.kdeconnect.Device)7 View (android.view.View)5 ListView (android.widget.ListView)4 ArrayList (java.util.ArrayList)4 AdapterView (android.widget.AdapterView)3 TextView (android.widget.TextView)3 ListAdapter (org.kde.kdeconnect.UserInterface.List.ListAdapter)3 SharedPreferences (android.content.SharedPreferences)2 Uri (android.net.Uri)2 SectionItem (org.kde.kdeconnect.UserInterface.List.SectionItem)2 ClipData (android.content.ClipData)1 Intent (android.content.Intent)1 Resources (android.content.res.Resources)1 Bundle (android.os.Bundle)1 PreferenceScreen (android.preference.PreferenceScreen)1 MenuItem (android.view.MenuItem)1 Button (android.widget.Button)1 SeekBar (android.widget.SeekBar)1 IOException (java.io.IOException)1