Search in sources :

Example 1 with ListAdapter

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

the class DeviceFragment method refreshUI.

void refreshUI() {
    if (device == null || rootView == null) {
        return;
    }
    //Once in-app, there is no point in keep displaying the notification if any
    device.hidePairingNotification();
    mActivity.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            if (device.isPairRequestedByPeer()) {
                ((TextView) rootView.findViewById(R.id.pair_message)).setText(R.string.pair_requested);
                rootView.findViewById(R.id.pair_progress).setVisibility(View.GONE);
                rootView.findViewById(R.id.pair_button).setVisibility(View.GONE);
                rootView.findViewById(R.id.pair_request).setVisibility(View.VISIBLE);
            } else {
                boolean paired = device.isPaired();
                boolean reachable = device.isReachable();
                boolean onData = NetworkHelper.isOnMobileNetwork(getContext());
                rootView.findViewById(R.id.pairing_buttons).setVisibility(paired ? View.GONE : View.VISIBLE);
                rootView.findViewById(R.id.not_reachable_message).setVisibility((paired && !reachable && !onData) ? View.VISIBLE : View.GONE);
                rootView.findViewById(R.id.on_data_message).setVisibility((paired && !reachable && onData) ? View.VISIBLE : View.GONE);
                try {
                    ArrayList<ListAdapter.Item> items = new ArrayList<>();
                    //Plugins button list
                    final Collection<Plugin> plugins = device.getLoadedPlugins().values();
                    for (final Plugin p : plugins) {
                        if (!p.hasMainActivity())
                            continue;
                        if (p.displayInContextMenu())
                            continue;
                        items.add(new PluginItem(p, new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                p.startMainActivity(mActivity);
                            }
                        }));
                    }
                    //Failed plugins List
                    final ConcurrentHashMap<String, Plugin> failed = device.getFailedPlugins();
                    if (!failed.isEmpty()) {
                        if (errorHeader == null) {
                            errorHeader = new TextView(mActivity);
                            errorHeader.setPadding(0, ((int) (28 * getResources().getDisplayMetrics().density)), 0, ((int) (8 * getResources().getDisplayMetrics().density)));
                            errorHeader.setOnClickListener(null);
                            errorHeader.setOnLongClickListener(null);
                            errorHeader.setText(getResources().getString(R.string.plugins_failed_to_load));
                        }
                        items.add(new CustomItem(errorHeader));
                        for (Map.Entry<String, Plugin> entry : failed.entrySet()) {
                            String pluginKey = entry.getKey();
                            final Plugin plugin = entry.getValue();
                            if (plugin == null) {
                                items.add(new SmallEntryItem(pluginKey));
                            } else {
                                items.add(new SmallEntryItem(plugin.getDisplayName(), new View.OnClickListener() {

                                    @Override
                                    public void onClick(View v) {
                                        plugin.getErrorDialog(mActivity).show();
                                    }
                                }));
                            }
                        }
                    }
                    //Plugins without permissions List
                    final ConcurrentHashMap<String, Plugin> permissionsNeeded = device.getPluginsWithoutPermissions();
                    if (!permissionsNeeded.isEmpty()) {
                        if (noPermissionsHeader == null) {
                            noPermissionsHeader = new TextView(mActivity);
                            noPermissionsHeader.setPadding(0, ((int) (28 * getResources().getDisplayMetrics().density)), 0, ((int) (8 * getResources().getDisplayMetrics().density)));
                            noPermissionsHeader.setOnClickListener(null);
                            noPermissionsHeader.setOnLongClickListener(null);
                            noPermissionsHeader.setText(getResources().getString(R.string.plugins_need_permission));
                        }
                        items.add(new CustomItem(noPermissionsHeader));
                        for (Map.Entry<String, Plugin> entry : permissionsNeeded.entrySet()) {
                            String pluginKey = entry.getKey();
                            final Plugin plugin = entry.getValue();
                            if (plugin == null) {
                                items.add(new SmallEntryItem(pluginKey));
                            } else {
                                items.add(new SmallEntryItem(plugin.getDisplayName(), new View.OnClickListener() {

                                    @Override
                                    public void onClick(View v) {
                                        plugin.getPermissionExplanationDialog(mActivity).show();
                                    }
                                }));
                            }
                        }
                    }
                    ListView buttonsList = (ListView) rootView.findViewById(R.id.buttons_list);
                    ListAdapter adapter = new ListAdapter(mActivity, items);
                    buttonsList.setAdapter(adapter);
                    mActivity.invalidateOptionsMenu();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                //Ignore: The activity was closed while we were trying to update it
                } catch (ConcurrentModificationException e) {
                    Log.e("DeviceActivity", "ConcurrentModificationException");
                    //Try again
                    this.run();
                }
            }
        }
    });
}
Also used : CustomItem(org.kde.kdeconnect.UserInterface.List.CustomItem) ConcurrentModificationException(java.util.ConcurrentModificationException) ArrayList(java.util.ArrayList) View(android.view.View) TextView(android.widget.TextView) ListView(android.widget.ListView) SmallEntryItem(org.kde.kdeconnect.UserInterface.List.SmallEntryItem) ListView(android.widget.ListView) Collection(java.util.Collection) TextView(android.widget.TextView) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PluginItem(org.kde.kdeconnect.UserInterface.List.PluginItem) ListAdapter(org.kde.kdeconnect.UserInterface.List.ListAdapter) Plugin(org.kde.kdeconnect.Plugins.Plugin)

Example 2 with ListAdapter

use of org.kde.kdeconnect.UserInterface.List.ListAdapter 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 3 with ListAdapter

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

the class RunCommandActivity method updateView.

private void updateView() {
    BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {

        @Override
        public void onServiceStart(final BackgroundService service) {
            final Device device = service.getDevice(deviceId);
            final RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class);
            if (plugin == null) {
                Log.e("RunCommandActivity", "device has no runcommand plugin!");
                return;
            }
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    ListView view = (ListView) findViewById(R.id.listView1);
                    final ArrayList<ListAdapter.Item> commandItems = new ArrayList<>();
                    for (JSONObject obj : plugin.getCommandList()) {
                        try {
                            commandItems.add(new CommandEntry(obj.getString("name"), obj.getString("command"), obj.getString("key")));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    Collections.sort(commandItems, new Comparator<ListAdapter.Item>() {

                        @Override
                        public int compare(ListAdapter.Item lhs, ListAdapter.Item rhs) {
                            String lName = ((CommandEntry) lhs).getName();
                            String rName = ((CommandEntry) rhs).getName();
                            return lName.compareTo(rName);
                        }
                    });
                    ListAdapter adapter = new ListAdapter(RunCommandActivity.this, commandItems);
                    view.setAdapter(adapter);
                    view.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            CommandEntry entry = (CommandEntry) commandItems.get(i);
                            plugin.runCommand(entry.getKey());
                        }
                    });
                }
            });
        }
    });
}
Also used : BackgroundService(org.kde.kdeconnect.BackgroundService) Device(org.kde.kdeconnect.Device) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) ListView(android.widget.ListView) JSONObject(org.json.JSONObject) AdapterView(android.widget.AdapterView) ListAdapter(org.kde.kdeconnect.UserInterface.List.ListAdapter)

Example 4 with ListAdapter

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

View (android.view.View)4 ListView (android.widget.ListView)4 ArrayList (java.util.ArrayList)4 ListAdapter (org.kde.kdeconnect.UserInterface.List.ListAdapter)4 BackgroundService (org.kde.kdeconnect.BackgroundService)3 Device (org.kde.kdeconnect.Device)3 AdapterView (android.widget.AdapterView)2 TextView (android.widget.TextView)2 Collection (java.util.Collection)2 SectionItem (org.kde.kdeconnect.UserInterface.List.SectionItem)2 Intent (android.content.Intent)1 Resources (android.content.res.Resources)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 MenuItem (android.view.MenuItem)1 URL (java.net.URL)1 ConcurrentModificationException (java.util.ConcurrentModificationException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1