Search in sources :

Example 1 with PairingDeviceItem

use of org.kde.kdeconnect.UserInterface.List.PairingDeviceItem 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)

Aggregations

Resources (android.content.res.Resources)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1 ArrayList (java.util.ArrayList)1 BackgroundService (org.kde.kdeconnect.BackgroundService)1 Device (org.kde.kdeconnect.Device)1 ListAdapter (org.kde.kdeconnect.UserInterface.List.ListAdapter)1 PairingDeviceItem (org.kde.kdeconnect.UserInterface.List.PairingDeviceItem)1 SectionItem (org.kde.kdeconnect.UserInterface.List.SectionItem)1