~singpolyma/cheogram-android

d420c34a3175cbd5ea59480cf39ba597368b086d — Stephen Paul Weber 5 months ago cbee11a
Allow exploring other channels on the same service as this channel
M src/main/java/eu/siacs/conversations/services/ChannelDiscoveryService.java => src/main/java/eu/siacs/conversations/services/ChannelDiscoveryService.java +20 -20
@@ 75,14 75,15 @@ public class ChannelDiscoveryService {
    void discover(
            @NonNull final String query,
            Method method,
            Map<Jid, Account> mucServices,
            OnChannelSearchResultsFound onChannelSearchResultsFound) {
        final List<Room> result = cache.getIfPresent(key(method, query));
        final List<Room> result = cache.getIfPresent(key(method, mucServices, query));
        if (result != null) {
            onChannelSearchResultsFound.onChannelSearchResultsFound(result);
            return;
        }
        if (method == Method.LOCAL_SERVER) {
            discoverChannelsLocalServers(query, onChannelSearchResultsFound);
            discoverChannelsLocalServers(query, mucServices, onChannelSearchResultsFound);
        } else {
            if (query.isEmpty()) {
                discoverChannelsJabberNetwork(onChannelSearchResultsFound);


@@ 110,7 111,7 @@ public class ChannelDiscoveryService {
                            logError(response);
                            return;
                        }
                        cache.put(key(Method.JABBER_NETWORK, ""), body.items);
                        cache.put(key(Method.JABBER_NETWORK, null, ""), body.items);
                        listener.onChannelSearchResultsFound(body.items);
                    }



@@ 149,7 150,7 @@ public class ChannelDiscoveryService {
                            logError(response);
                            return;
                        }
                        cache.put(key(Method.JABBER_NETWORK, query), body.result.items);
                        cache.put(key(Method.JABBER_NETWORK, null, query), body.result.items);
                        listener.onChannelSearchResultsFound(body.result.items);
                    }



@@ 167,18 168,18 @@ public class ChannelDiscoveryService {
    }

    private void discoverChannelsLocalServers(
            final String query, final OnChannelSearchResultsFound listener) {
        final Map<Jid, Account> localMucService = getLocalMucServices();
            final String query, Map<Jid, Account> mucServices, final OnChannelSearchResultsFound listener) {
        final Map<Jid, Account> localMucService = mucServices == null ? getLocalMucServices() : mucServices;
        Log.d(Config.LOGTAG, "checking with " + localMucService.size() + " muc services");
        if (localMucService.size() == 0) {
            listener.onChannelSearchResultsFound(Collections.emptyList());
            return;
        }
        if (!query.isEmpty()) {
            final List<Room> cached = cache.getIfPresent(key(Method.LOCAL_SERVER, ""));
            final List<Room> cached = cache.getIfPresent(key(Method.LOCAL_SERVER, mucServices, ""));
            if (cached != null) {
                final List<Room> results = copyMatching(cached, query);
                cache.put(key(Method.LOCAL_SERVER, query), results);
                cache.put(key(Method.LOCAL_SERVER, mucServices, query), results);
                listener.onChannelSearchResultsFound(results);
            }
        }


@@ 211,32 212,30 @@ public class ChannelDiscoveryService {
                                                    if (room != null) {
                                                        rooms.add(room);
                                                    }
                                                    if (queriesInFlight.decrementAndGet() <= 0) {
                                                        finishDiscoSearch(rooms, query, listener);
                                                    }
                                                } else {
                                                    queriesInFlight.decrementAndGet();
                                                }
                                                if (queriesInFlight.decrementAndGet() <= 0) {
                                                    finishDiscoSearch(rooms, query, mucServices, listener);
                                                }
                                            }
                                        });
                                        }, 20L);
                            }
                        }
                        if (queriesInFlight.decrementAndGet() <= 0) {
                            finishDiscoSearch(rooms, query, listener);
                            finishDiscoSearch(rooms, query, mucServices, listener);
                        }
                    });
        }
    }

    private void finishDiscoSearch(
            List<Room> rooms, String query, OnChannelSearchResultsFound listener) {
            List<Room> rooms, String query, Map<Jid, Account> mucServices, OnChannelSearchResultsFound listener) {
        Collections.sort(rooms);
        cache.put(key(Method.LOCAL_SERVER, ""), rooms);
        cache.put(key(Method.LOCAL_SERVER, mucServices, ""), rooms);
        if (query.isEmpty()) {
            listener.onChannelSearchResultsFound(rooms);
        } else {
            List<Room> results = copyMatching(rooms, query);
            cache.put(key(Method.LOCAL_SERVER, query), results);
            cache.put(key(Method.LOCAL_SERVER, mucServices, query), results);
            listener.onChannelSearchResultsFound(rooms);
        }
    }


@@ 270,8 269,9 @@ public class ChannelDiscoveryService {
        return localMucServices;
    }

    private static String key(Method method, String query) {
        return String.format("%s\00%s", method, query);
    private static String key(Method method, Map<Jid, Account> mucServices, String query) {
        final String servicesKey = mucServices == null ? "\00" : String.join("\00", mucServices.keySet());
        return String.format("%s\00%s\00%s", method, servicesKey, query);
    }

    private static void logError(final Response response) {

M src/main/java/eu/siacs/conversations/services/XmppConnectionService.java => src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +2 -2
@@ 1045,8 1045,8 @@ public class XmppConnectionService extends Service {
        mChannelDiscoveryService.initializeMuclumbusService();
    }

    public void discoverChannels(String query, ChannelDiscoveryService.Method method, ChannelDiscoveryService.OnChannelSearchResultsFound onChannelSearchResultsFound) {
        mChannelDiscoveryService.discover(Strings.nullToEmpty(query).trim(), method, onChannelSearchResultsFound);
    public void discoverChannels(String query, ChannelDiscoveryService.Method method, Map<Jid, Account> mucServices, ChannelDiscoveryService.OnChannelSearchResultsFound onChannelSearchResultsFound) {
        mChannelDiscoveryService.discover(Strings.nullToEmpty(query).trim(), method, mucServices, onChannelSearchResultsFound);
    }

    public boolean isDataSaverDisabled() {

M src/main/java/eu/siacs/conversations/ui/ChannelDiscoveryActivity.java => src/main/java/eu/siacs/conversations/ui/ChannelDiscoveryActivity.java +22 -7
@@ 23,6 23,7 @@ import androidx.databinding.DataBindingUtil;
import com.google.common.base.Strings;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;



@@ 52,7 53,9 @@ public class ChannelDiscoveryActivity extends XmppActivity implements MenuItem.O
    private MenuItem mMenuSearchView;
    private EditText mSearchEditText;

    private String[] pendingServices = null;
    private ChannelDiscoveryService.Method method = ChannelDiscoveryService.Method.LOCAL_SERVER;
    private HashMap<Jid, Account> mucServices = null;

    private boolean optedIn = false;



@@ 63,6 66,15 @@ public class ChannelDiscoveryActivity extends XmppActivity implements MenuItem.O

    @Override
    void onBackendConnected() {
        if (pendingServices != null) {
            mucServices = new HashMap<>();
            for (int i = 0; i < pendingServices.length; i += 2) {
                mucServices.put(Jid.of(pendingServices[i]), xmppConnectionService.findAccountByJid(Jid.of(pendingServices[i+1])));
            }
        }

        this.method = getMethod(this);

        if (optedIn || method == ChannelDiscoveryService.Method.LOCAL_SERVER) {
            final String query;
            if (mMenuSearchView != null && mMenuSearchView.isActionViewExpanded()) {


@@ 71,7 83,7 @@ public class ChannelDiscoveryActivity extends XmppActivity implements MenuItem.O
                query = mInitialSearchValue.peek();
            }
            toggleLoadingScreen();
            xmppConnectionService.discoverChannels(query, this.method, this);
            xmppConnectionService.discoverChannels(query, this.method, this.mucServices, this);
        }
    }



@@ 89,9 101,12 @@ public class ChannelDiscoveryActivity extends XmppActivity implements MenuItem.O
        if (search != null) {
            mInitialSearchValue.push(search);
        }

        pendingServices = getIntent().getStringArrayExtra("services");
    }

    private static ChannelDiscoveryService.Method getMethod(final Context c) {
    private ChannelDiscoveryService.Method getMethod(final Context c) {
        if (this.mucServices != null) return ChannelDiscoveryService.Method.LOCAL_SERVER;
        if ( Strings.isNullOrEmpty(Config.CHANNEL_DISCOVERY)) {
            return ChannelDiscoveryService.Method.LOCAL_SERVER;
        }


@@ 121,7 136,7 @@ public class ChannelDiscoveryActivity extends XmppActivity implements MenuItem.O
            mSearchEditText.append(initialSearchValue);
            mSearchEditText.requestFocus();
            if ((optedIn || method == ChannelDiscoveryService.Method.LOCAL_SERVER) && xmppConnectionService != null) {
                xmppConnectionService.discoverChannels(initialSearchValue, this.method, this);
                xmppConnectionService.discoverChannels(initialSearchValue, this.method, this.mucServices, this);
            }
        }
        mSearchEditText.setOnEditorActionListener(this);


@@ 146,7 161,7 @@ public class ChannelDiscoveryActivity extends XmppActivity implements MenuItem.O
        mSearchEditText.setText("");
        toggleLoadingScreen();
        if (optedIn || method == ChannelDiscoveryService.Method.LOCAL_SERVER) {
            xmppConnectionService.discoverChannels(null, this.method, this);
            xmppConnectionService.discoverChannels(null, this.method, this.mucServices, this);
        }
        return true;
    }


@@ 161,7 176,7 @@ public class ChannelDiscoveryActivity extends XmppActivity implements MenuItem.O
    public void onStart() {
        super.onStart();
        this.method = getMethod(this);
        if (!optedIn && method == ChannelDiscoveryService.Method.JABBER_NETWORK) {
        if (pendingServices == null && !optedIn && method == ChannelDiscoveryService.Method.JABBER_NETWORK) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle(R.string.channel_discovery_opt_in_title);
            builder.setMessage(Html.fromHtml(getString(R.string.channel_discover_opt_in_message)));


@@ 201,7 216,7 @@ public class ChannelDiscoveryActivity extends XmppActivity implements MenuItem.O
        preferences.edit().putBoolean(CHANNEL_DISCOVERY_OPT_IN, true).apply();
        optedIn = true;
        toggleLoadingScreen();
        xmppConnectionService.discoverChannels(null, this.method, this);
        xmppConnectionService.discoverChannels(null, this.method, this.mucServices, this);
    }

    @Override


@@ 209,7 224,7 @@ public class ChannelDiscoveryActivity extends XmppActivity implements MenuItem.O
        if (optedIn || method == ChannelDiscoveryService.Method.LOCAL_SERVER) {
            toggleLoadingScreen();
            SoftKeyboardUtils.hideSoftKeyboard(this);
            xmppConnectionService.discoverChannels(v.getText().toString(), this.method, this);
            xmppConnectionService.discoverChannels(v.getText().toString(), this.method, this.mucServices, this);
        }
        return true;
    }

M src/main/java/eu/siacs/conversations/ui/ConferenceDetailsActivity.java => src/main/java/eu/siacs/conversations/ui/ConferenceDetailsActivity.java +5 -0
@@ 229,6 229,11 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers
            intent.putExtra("uuid", mConversation.getUuid());
            startActivity(intent);
        });
        this.binding.relatedMucs.setOnClickListener(v -> {
            final Intent intent = new Intent(this, ChannelDiscoveryActivity.class);
            intent.putExtra("services", new String[]{ mConversation.getJid().getDomain().toEscapedString(), mConversation.getAccount().getJid().toEscapedString() });
            startActivity(intent);
        });
    }

    @Override

M src/main/res/layout/activity_muc_details.xml => src/main/res/layout/activity_muc_details.xml +21 -5
@@ 34,13 34,14 @@
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:padding="@dimen/card_padding_regular">
                        android:paddingVertical="@dimen/card_padding_regular"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="32dp">
                            android:layout_marginBottom="32dp"
                            android:paddingHorizontal="@dimen/card_padding_regular">

                            <com.google.android.material.imageview.ShapeableImageView
                                android:id="@+id/your_photo"


@@ 166,7 167,8 @@
                        <RelativeLayout
                            android:id="@+id/muc_settings"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content">
                            android:layout_height="wrap_content"
                            android:paddingHorizontal="@dimen/card_padding_regular">

                            <TextView
                                android:id="@+id/muc_conference_type"


@@ 198,6 200,7 @@
                            android:id="@+id/muc_info_more"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:paddingHorizontal="@dimen/card_padding_regular"
                            android:shrinkColumns="0"
                            android:stretchColumns="1"
                            android:visibility="gone">


@@ 226,12 229,24 @@

                        </TableLayout>

                        <Button
                            android:id="@+id/relatedMucs"
                            style="@style/Widget.Conversations.Button.Borderless"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="end"
                            android:minWidth="0dp"
                            android:layout_marginTop="32dp"
                            android:layout_marginRight="4dp"
                            android:text="Related Chats"
                            android:textColor="?attr/colorAccent" />

                        <TextView
                            android:id="@+id/jid"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="end"
                            android:layout_marginTop="32dp"
                            android:paddingHorizontal="@dimen/card_padding_regular"
                            android:textAppearance="@style/TextAppearance.Conversations.Caption"/>

                        <TextView


@@ 240,6 255,7 @@
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="end"
                            android:paddingHorizontal="@dimen/card_padding_regular"
                            android:textAppearance="@style/TextAppearance.Conversations.Caption"/>
                    </LinearLayout>
                </androidx.cardview.widget.CardView>