Search in sources :

Example 1 with EntryItem

use of org.kde.kdeconnect.UserInterface.List.EntryItem in project kdeconnect-android by KDE.

the class ShareActivity method updateComputerList.

private void updateComputerList() {
    final Intent intent = getIntent();
    String action = intent.getAction();
    if (!Intent.ACTION_SEND.equals(action) && !Intent.ACTION_SEND_MULTIPLE.equals(action)) {
        finish();
        return;
    }
    BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {

        @Override
        public void onServiceStart(final BackgroundService service) {
            Collection<Device> devices = service.getDevices().values();
            final ArrayList<Device> devicesList = new ArrayList<>();
            final ArrayList<ListAdapter.Item> items = new ArrayList<>();
            items.add(new SectionItem(getString(R.string.share_to)));
            for (Device d : devices) {
                if (d.isReachable() && d.isPaired()) {
                    devicesList.add(d);
                    items.add(new EntryItem(d.getName()));
                }
            }
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    ListView list = (ListView) findViewById(R.id.listView1);
                    list.setAdapter(new ListAdapter(ShareActivity.this, items));
                    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            //NOTE: -1 because of the title!
                            Device device = devicesList.get(i - 1);
                            Bundle extras = intent.getExtras();
                            if (extras != null) {
                                if (extras.containsKey(Intent.EXTRA_STREAM)) {
                                    try {
                                        ArrayList<Uri> 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(getApplicationContext(), 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) {
                                            //Skip ": "
                                            text = text.substring(index + 2);
                                        }
                                    }
                                    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);
                                }
                            }
                            finish();
                        }
                    });
                }
            });
        }
    });
}
Also used : BackgroundService(org.kde.kdeconnect.BackgroundService) ArrayList(java.util.ArrayList) Uri(android.net.Uri) URL(java.net.URL) SectionItem(org.kde.kdeconnect.UserInterface.List.SectionItem) ListView(android.widget.ListView) ListAdapter(org.kde.kdeconnect.UserInterface.List.ListAdapter) EntryItem(org.kde.kdeconnect.UserInterface.List.EntryItem) Device(org.kde.kdeconnect.Device) Bundle(android.os.Bundle) Intent(android.content.Intent) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) NetworkPackage(org.kde.kdeconnect.NetworkPackage) Collection(java.util.Collection) AdapterView(android.widget.AdapterView)

Aggregations

Intent (android.content.Intent)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 ListView (android.widget.ListView)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 BackgroundService (org.kde.kdeconnect.BackgroundService)1 Device (org.kde.kdeconnect.Device)1 NetworkPackage (org.kde.kdeconnect.NetworkPackage)1 EntryItem (org.kde.kdeconnect.UserInterface.List.EntryItem)1 ListAdapter (org.kde.kdeconnect.UserInterface.List.ListAdapter)1 SectionItem (org.kde.kdeconnect.UserInterface.List.SectionItem)1