make it easier to disable muclumbus in Config

This commit is contained in:
Daniel Gultsch 2022-10-12 14:47:02 +02:00
parent 716c804353
commit ab0ea7096e
3 changed files with 143 additions and 90 deletions

View file

@ -4,6 +4,7 @@ import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import com.google.common.base.Strings;
import com.google.common.cache.Cache; import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheBuilder;
@ -39,7 +40,6 @@ public class ChannelDiscoveryService {
private final XmppConnectionService service; private final XmppConnectionService service;
private MuclumbusService muclumbusService; private MuclumbusService muclumbusService;
private final Cache<String, List<Room>> cache; private final Cache<String, List<Room>> cache;
@ -50,16 +50,21 @@ public class ChannelDiscoveryService {
} }
void initializeMuclumbusService() { void initializeMuclumbusService() {
if (Strings.isNullOrEmpty(Config.CHANNEL_DISCOVERY)) {
this.muclumbusService = null;
return;
}
final OkHttpClient.Builder builder = HttpConnectionManager.OK_HTTP_CLIENT.newBuilder(); final OkHttpClient.Builder builder = HttpConnectionManager.OK_HTTP_CLIENT.newBuilder();
if (service.useTorToConnect()) { if (service.useTorToConnect()) {
builder.proxy(HttpConnectionManager.getProxy()); builder.proxy(HttpConnectionManager.getProxy());
} }
Retrofit retrofit = new Retrofit.Builder() final Retrofit retrofit =
.client(builder.build()) new Retrofit.Builder()
.baseUrl(Config.CHANNEL_DISCOVERY) .client(builder.build())
.addConverterFactory(GsonConverterFactory.create()) .baseUrl(Config.CHANNEL_DISCOVERY)
.callbackExecutor(Executors.newSingleThreadExecutor()) .addConverterFactory(GsonConverterFactory.create())
.build(); .callbackExecutor(Executors.newSingleThreadExecutor())
.build();
this.muclumbusService = retrofit.create(MuclumbusService.class); this.muclumbusService = retrofit.create(MuclumbusService.class);
} }
@ -67,7 +72,10 @@ public class ChannelDiscoveryService {
cache.invalidateAll(); cache.invalidateAll();
} }
void discover(@NonNull final String query, Method method, OnChannelSearchResultsFound onChannelSearchResultsFound) { void discover(
@NonNull final String query,
Method method,
OnChannelSearchResultsFound onChannelSearchResultsFound) {
final List<Room> result = cache.getIfPresent(key(method, query)); final List<Room> result = cache.getIfPresent(key(method, query));
if (result != null) { if (result != null) {
onChannelSearchResultsFound.onChannelSearchResultsFound(result); onChannelSearchResultsFound.onChannelSearchResultsFound(result);
@ -84,59 +92,82 @@ public class ChannelDiscoveryService {
} }
} }
private void discoverChannelsJabberNetwork(OnChannelSearchResultsFound listener) { private void discoverChannelsJabberNetwork(final OnChannelSearchResultsFound listener) {
Call<MuclumbusService.Rooms> call = muclumbusService.getRooms(1); if (muclumbusService == null) {
try { listener.onChannelSearchResultsFound(Collections.emptyList());
call.enqueue(new Callback<MuclumbusService.Rooms>() { return;
@Override
public void onResponse(@NonNull Call<MuclumbusService.Rooms> call, @NonNull Response<MuclumbusService.Rooms> response) {
final MuclumbusService.Rooms body = response.body();
if (body == null) {
listener.onChannelSearchResultsFound(Collections.emptyList());
logError(response);
return;
}
cache.put(key(Method.JABBER_NETWORK, ""), body.items);
listener.onChannelSearchResultsFound(body.items);
}
@Override
public void onFailure(@NonNull Call<MuclumbusService.Rooms> call, @NonNull Throwable throwable) {
Log.d(Config.LOGTAG, "Unable to query muclumbus on " + Config.CHANNEL_DISCOVERY, throwable);
listener.onChannelSearchResultsFound(Collections.emptyList());
}
});
} catch (Exception e) {
e.printStackTrace();
} }
final Call<MuclumbusService.Rooms> call = muclumbusService.getRooms(1);
call.enqueue(
new Callback<MuclumbusService.Rooms>() {
@Override
public void onResponse(
@NonNull Call<MuclumbusService.Rooms> call,
@NonNull Response<MuclumbusService.Rooms> response) {
final MuclumbusService.Rooms body = response.body();
if (body == null) {
listener.onChannelSearchResultsFound(Collections.emptyList());
logError(response);
return;
}
cache.put(key(Method.JABBER_NETWORK, ""), body.items);
listener.onChannelSearchResultsFound(body.items);
}
@Override
public void onFailure(
@NonNull Call<MuclumbusService.Rooms> call,
@NonNull Throwable throwable) {
Log.d(
Config.LOGTAG,
"Unable to query muclumbus on " + Config.CHANNEL_DISCOVERY,
throwable);
listener.onChannelSearchResultsFound(Collections.emptyList());
}
});
} }
private void discoverChannelsJabberNetwork(final String query, OnChannelSearchResultsFound listener) { private void discoverChannelsJabberNetwork(
MuclumbusService.SearchRequest searchRequest = new MuclumbusService.SearchRequest(query); final String query, final OnChannelSearchResultsFound listener) {
Call<MuclumbusService.SearchResult> searchResultCall = muclumbusService.search(searchRequest); if (muclumbusService == null) {
listener.onChannelSearchResultsFound(Collections.emptyList());
return;
}
final MuclumbusService.SearchRequest searchRequest =
new MuclumbusService.SearchRequest(query);
final Call<MuclumbusService.SearchResult> searchResultCall =
muclumbusService.search(searchRequest);
searchResultCall.enqueue(
new Callback<MuclumbusService.SearchResult>() {
@Override
public void onResponse(
@NonNull Call<MuclumbusService.SearchResult> call,
@NonNull Response<MuclumbusService.SearchResult> response) {
final MuclumbusService.SearchResult body = response.body();
if (body == null) {
listener.onChannelSearchResultsFound(Collections.emptyList());
logError(response);
return;
}
cache.put(key(Method.JABBER_NETWORK, query), body.result.items);
listener.onChannelSearchResultsFound(body.result.items);
}
searchResultCall.enqueue(new Callback<MuclumbusService.SearchResult>() { @Override
@Override public void onFailure(
public void onResponse(@NonNull Call<MuclumbusService.SearchResult> call, @NonNull Response<MuclumbusService.SearchResult> response) { @NonNull Call<MuclumbusService.SearchResult> call,
final MuclumbusService.SearchResult body = response.body(); @NonNull Throwable throwable) {
if (body == null) { Log.d(
listener.onChannelSearchResultsFound(Collections.emptyList()); Config.LOGTAG,
logError(response); "Unable to query muclumbus on " + Config.CHANNEL_DISCOVERY,
return; throwable);
} listener.onChannelSearchResultsFound(Collections.emptyList());
cache.put(key(Method.JABBER_NETWORK, query), body.result.items); }
listener.onChannelSearchResultsFound(body.result.items); });
}
@Override
public void onFailure(@NonNull Call<MuclumbusService.SearchResult> call, @NonNull Throwable throwable) {
Log.d(Config.LOGTAG, "Unable to query muclumbus on " + Config.CHANNEL_DISCOVERY, throwable);
listener.onChannelSearchResultsFound(Collections.emptyList());
}
});
} }
private void discoverChannelsLocalServers(final String query, final OnChannelSearchResultsFound listener) { private void discoverChannelsLocalServers(
final String query, final OnChannelSearchResultsFound listener) {
final Map<Jid, Account> localMucService = getLocalMucServices(); final Map<Jid, Account> localMucService = getLocalMucServices();
Log.d(Config.LOGTAG, "checking with " + localMucService.size() + " muc services"); Log.d(Config.LOGTAG, "checking with " + localMucService.size() + " muc services");
if (localMucService.size() == 0) { if (localMucService.size() == 0) {
@ -156,38 +187,49 @@ public class ChannelDiscoveryService {
for (Map.Entry<Jid, Account> entry : localMucService.entrySet()) { for (Map.Entry<Jid, Account> entry : localMucService.entrySet()) {
IqPacket itemsRequest = service.getIqGenerator().queryDiscoItems(entry.getKey()); IqPacket itemsRequest = service.getIqGenerator().queryDiscoItems(entry.getKey());
queriesInFlight.incrementAndGet(); queriesInFlight.incrementAndGet();
service.sendIqPacket(entry.getValue(), itemsRequest, (account, itemsResponse) -> { service.sendIqPacket(
if (itemsResponse.getType() == IqPacket.TYPE.RESULT) { entry.getValue(),
final List<Jid> items = IqParser.items(itemsResponse); itemsRequest,
for (Jid item : items) { (account, itemsResponse) -> {
IqPacket infoRequest = service.getIqGenerator().queryDiscoInfo(item); if (itemsResponse.getType() == IqPacket.TYPE.RESULT) {
queriesInFlight.incrementAndGet(); final List<Jid> items = IqParser.items(itemsResponse);
service.sendIqPacket(account, infoRequest, new OnIqPacketReceived() { for (Jid item : items) {
@Override IqPacket infoRequest =
public void onIqPacketReceived(Account account, IqPacket infoResponse) { service.getIqGenerator().queryDiscoInfo(item);
if (infoResponse.getType() == IqPacket.TYPE.RESULT) { queriesInFlight.incrementAndGet();
final Room room = IqParser.parseRoom(infoResponse); service.sendIqPacket(
if (room != null) { account,
rooms.add(room); infoRequest,
} new OnIqPacketReceived() {
if (queriesInFlight.decrementAndGet() <= 0) { @Override
finishDiscoSearch(rooms, query, listener); public void onIqPacketReceived(
} Account account, IqPacket infoResponse) {
} else { if (infoResponse.getType()
queriesInFlight.decrementAndGet(); == IqPacket.TYPE.RESULT) {
} final Room room =
IqParser.parseRoom(infoResponse);
if (room != null) {
rooms.add(room);
}
if (queriesInFlight.decrementAndGet() <= 0) {
finishDiscoSearch(rooms, query, listener);
}
} else {
queriesInFlight.decrementAndGet();
}
}
});
} }
}); }
} if (queriesInFlight.decrementAndGet() <= 0) {
} finishDiscoSearch(rooms, query, listener);
if (queriesInFlight.decrementAndGet() <= 0) { }
finishDiscoSearch(rooms, query, listener); });
}
});
} }
} }
private void finishDiscoSearch(List<Room> rooms, String query, OnChannelSearchResultsFound listener) { private void finishDiscoSearch(
List<Room> rooms, String query, OnChannelSearchResultsFound listener) {
Collections.sort(rooms); Collections.sort(rooms);
cache.put(key(Method.LOCAL_SERVER, ""), rooms); cache.put(key(Method.LOCAL_SERVER, ""), rooms);
if (query.isEmpty()) { if (query.isEmpty()) {
@ -241,7 +283,7 @@ public class ChannelDiscoveryService {
try { try {
Log.d(Config.LOGTAG, "error body=" + errorBody.string()); Log.d(Config.LOGTAG, "error body=" + errorBody.string());
} catch (IOException e) { } catch (IOException e) {
//ignored // ignored
} }
} }

View file

@ -20,6 +20,8 @@ import android.widget.Toast;
import androidx.databinding.DataBindingUtil; import androidx.databinding.DataBindingUtil;
import com.google.common.base.Strings;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@ -90,6 +92,9 @@ public class ChannelDiscoveryActivity extends XmppActivity implements MenuItem.O
} }
private static ChannelDiscoveryService.Method getMethod(final Context c) { private static ChannelDiscoveryService.Method getMethod(final Context c) {
if ( Strings.isNullOrEmpty(Config.CHANNEL_DISCOVERY)) {
return ChannelDiscoveryService.Method.LOCAL_SERVER;
}
if (QuickConversationsService.isQuicksy()) { if (QuickConversationsService.isQuicksy()) {
return ChannelDiscoveryService.Method.JABBER_NETWORK; return ChannelDiscoveryService.Method.JABBER_NETWORK;
} }

View file

@ -23,6 +23,8 @@ import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import com.google.common.base.Strings;
import java.io.File; import java.io.File;
import java.security.KeyStoreException; import java.security.KeyStoreException;
import java.util.ArrayList; import java.util.ArrayList;
@ -96,20 +98,24 @@ public class SettingsActivity extends XmppActivity implements OnSharedPreference
changeOmemoSettingSummary(); changeOmemoSettingSummary();
if (QuickConversationsService.isQuicksy()) { if (QuickConversationsService.isQuicksy()
final PreferenceCategory connectionOptions = || Strings.isNullOrEmpty(Config.CHANNEL_DISCOVERY)) {
(PreferenceCategory) mSettingsFragment.findPreference("connection_options");
final PreferenceCategory groupChats = final PreferenceCategory groupChats =
(PreferenceCategory) mSettingsFragment.findPreference("group_chats"); (PreferenceCategory) mSettingsFragment.findPreference("group_chats");
final Preference channelDiscoveryMethod = final Preference channelDiscoveryMethod =
mSettingsFragment.findPreference("channel_discovery_method"); mSettingsFragment.findPreference("channel_discovery_method");
if (groupChats != null && channelDiscoveryMethod != null) {
groupChats.removePreference(channelDiscoveryMethod);
}
}
if (QuickConversationsService.isQuicksy()) {
final PreferenceCategory connectionOptions =
(PreferenceCategory) mSettingsFragment.findPreference("connection_options");
PreferenceScreen expert = (PreferenceScreen) mSettingsFragment.findPreference("expert"); PreferenceScreen expert = (PreferenceScreen) mSettingsFragment.findPreference("expert");
if (connectionOptions != null) { if (connectionOptions != null) {
expert.removePreference(connectionOptions); expert.removePreference(connectionOptions);
} }
if (groupChats != null && channelDiscoveryMethod != null) {
groupChats.removePreference(channelDiscoveryMethod);
}
} }
PreferenceScreen mainPreferenceScreen = PreferenceScreen mainPreferenceScreen =