Search in sources :

Example 1 with SmallEntryItem

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

Aggregations

View (android.view.View)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 ConcurrentModificationException (java.util.ConcurrentModificationException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Plugin (org.kde.kdeconnect.Plugins.Plugin)1 CustomItem (org.kde.kdeconnect.UserInterface.List.CustomItem)1 ListAdapter (org.kde.kdeconnect.UserInterface.List.ListAdapter)1 PluginItem (org.kde.kdeconnect.UserInterface.List.PluginItem)1 SmallEntryItem (org.kde.kdeconnect.UserInterface.List.SmallEntryItem)1