added Tor support to channel search
This commit is contained in:
parent
9db1c10f45
commit
6704db21fb
|
@ -7,12 +7,16 @@ import com.google.common.cache.CacheBuilder;
|
||||||
import com.google.common.cache.CacheLoader;
|
import com.google.common.cache.CacheLoader;
|
||||||
import com.google.common.cache.LoadingCache;
|
import com.google.common.cache.LoadingCache;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import eu.siacs.conversations.Config;
|
import eu.siacs.conversations.Config;
|
||||||
|
import eu.siacs.conversations.http.HttpConnectionManager;
|
||||||
import eu.siacs.conversations.http.services.MuclumbusService;
|
import eu.siacs.conversations.http.services.MuclumbusService;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
import retrofit2.Call;
|
import retrofit2.Call;
|
||||||
import retrofit2.Callback;
|
import retrofit2.Callback;
|
||||||
import retrofit2.Response;
|
import retrofit2.Response;
|
||||||
|
@ -24,19 +28,31 @@ public class ChannelDiscoveryService {
|
||||||
private final XmppConnectionService service;
|
private final XmppConnectionService service;
|
||||||
|
|
||||||
|
|
||||||
private final MuclumbusService muclumbusService;
|
private MuclumbusService muclumbusService;
|
||||||
|
|
||||||
private final Cache<String, List<MuclumbusService.Room>> cache;
|
private final Cache<String, List<MuclumbusService.Room>> cache;
|
||||||
|
|
||||||
public ChannelDiscoveryService(XmppConnectionService service) {
|
public ChannelDiscoveryService(XmppConnectionService service) {
|
||||||
this.service = service;
|
this.service = service;
|
||||||
|
this.cache = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initializeMuclumbusService() {
|
||||||
|
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
||||||
|
if (service.useTorToConnect()) {
|
||||||
|
try {
|
||||||
|
builder.proxy(HttpConnectionManager.getProxy());
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException("Unable to use Tor proxy", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
Retrofit retrofit = new Retrofit.Builder()
|
Retrofit retrofit = new Retrofit.Builder()
|
||||||
|
.client(builder.build())
|
||||||
.baseUrl(Config.CHANNEL_DISCOVERY)
|
.baseUrl(Config.CHANNEL_DISCOVERY)
|
||||||
.addConverterFactory(GsonConverterFactory.create())
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
.callbackExecutor(Executors.newSingleThreadExecutor())
|
.callbackExecutor(Executors.newSingleThreadExecutor())
|
||||||
.build();
|
.build();
|
||||||
this.muclumbusService = retrofit.create(MuclumbusService.class);
|
this.muclumbusService = retrofit.create(MuclumbusService.class);
|
||||||
this.cache = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES).build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void discover(String query, OnChannelSearchResultsFound onChannelSearchResultsFound) {
|
public void discover(String query, OnChannelSearchResultsFound onChannelSearchResultsFound) {
|
||||||
|
@ -70,7 +86,8 @@ public class ChannelDiscoveryService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Call<MuclumbusService.Rooms> call, Throwable throwable) {
|
public void onFailure(Call<MuclumbusService.Rooms> call, Throwable throwable) {
|
||||||
|
Log.d(Config.LOGTAG, "Unable to query muclumbus on "+Config.CHANNEL_DISCOVERY, throwable);
|
||||||
|
listener.onChannelSearchResultsFound(Collections.emptyList());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -95,7 +112,8 @@ public class ChannelDiscoveryService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Call<MuclumbusService.SearchResult> call, Throwable throwable) {
|
public void onFailure(Call<MuclumbusService.SearchResult> call, Throwable throwable) {
|
||||||
throwable.printStackTrace();
|
Log.d(Config.LOGTAG, "Unable to query muclumbus on "+Config.CHANNEL_DISCOVERY, throwable);
|
||||||
|
listener.onChannelSearchResultsFound(Collections.emptyList());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -805,6 +805,10 @@ public class XmppConnectionService extends Service {
|
||||||
return pingNow;
|
return pingNow;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void reinitializeMuclumbusService() {
|
||||||
|
mChannelDiscoveryService.initializeMuclumbusService();
|
||||||
|
}
|
||||||
|
|
||||||
public void discoverChannels(String query, ChannelDiscoveryService.OnChannelSearchResultsFound onChannelSearchResultsFound) {
|
public void discoverChannels(String query, ChannelDiscoveryService.OnChannelSearchResultsFound onChannelSearchResultsFound) {
|
||||||
mChannelDiscoveryService.discover(query, onChannelSearchResultsFound);
|
mChannelDiscoveryService.discover(query, onChannelSearchResultsFound);
|
||||||
}
|
}
|
||||||
|
@ -992,6 +996,7 @@ public class XmppConnectionService extends Service {
|
||||||
if (Compatibility.runsTwentySix()) {
|
if (Compatibility.runsTwentySix()) {
|
||||||
mNotificationService.initializeChannels();
|
mNotificationService.initializeChannels();
|
||||||
}
|
}
|
||||||
|
mChannelDiscoveryService.initializeMuclumbusService();
|
||||||
mForceDuringOnCreate.set(Compatibility.runsAndTargetsTwentySix(this));
|
mForceDuringOnCreate.set(Compatibility.runsAndTargetsTwentySix(this));
|
||||||
toggleForegroundService();
|
toggleForegroundService();
|
||||||
this.destroyed = false;
|
this.destroyed = false;
|
||||||
|
|
|
@ -384,6 +384,7 @@ public class SettingsActivity extends XmppActivity implements
|
||||||
reconnectAccounts();
|
reconnectAccounts();
|
||||||
} else if (name.equals("use_tor")) {
|
} else if (name.equals("use_tor")) {
|
||||||
reconnectAccounts();
|
reconnectAccounts();
|
||||||
|
xmppConnectionService.reinitializeMuclumbusService();
|
||||||
} else if (name.equals(AUTOMATIC_MESSAGE_DELETION)) {
|
} else if (name.equals(AUTOMATIC_MESSAGE_DELETION)) {
|
||||||
xmppConnectionService.expireOldMessages(true);
|
xmppConnectionService.expireOldMessages(true);
|
||||||
} else if (name.equals(THEME)) {
|
} else if (name.equals(THEME)) {
|
||||||
|
|
Loading…
Reference in a new issue