Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 5e0c591

Browse files
authored
Add support for GWE enabled MozillaSpeechLibrary (#2896)
1 parent 140209a commit 5e0c591

9 files changed

Lines changed: 138 additions & 64 deletions

File tree

app/src/common/shared/org/mozilla/vrbrowser/browser/engine/EngineProvider.kt

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
package org.mozilla.vrbrowser.browser.engine
66

77
import android.content.Context
8-
import mozilla.components.concept.fetch.Client
9-
import org.mozilla.geckoview.ContentBlocking
10-
import org.mozilla.geckoview.GeckoRuntime
11-
import org.mozilla.geckoview.GeckoRuntimeSettings
12-
import org.mozilla.geckoview.WebExtension
8+
import org.mozilla.geckoview.*
139
import org.mozilla.vrbrowser.BuildConfig
1410
import org.mozilla.vrbrowser.browser.SettingsStore
1511
import org.mozilla.vrbrowser.crashreporting.CrashReporterService
@@ -19,6 +15,7 @@ object EngineProvider {
1915
private val WEB_EXTENSIONS = arrayOf("webcompat_vimeo", "webcompat_youtube")
2016

2117
private var runtime: GeckoRuntime? = null
18+
private var executor: GeckoWebExecutor? = null
2219

2320
@Synchronized
2421
fun getOrCreateRuntime(context: Context): GeckoRuntime {
@@ -61,8 +58,20 @@ object EngineProvider {
6158
return runtime!!
6259
}
6360

64-
fun createClient(context: Context): Client {
65-
val runtime = getOrCreateRuntime(context)
66-
return GeckoViewFetchClient(context, runtime)
61+
fun createGeckoWebExecutor(context: Context): GeckoWebExecutor {
62+
return GeckoWebExecutor(getOrCreateRuntime(context))
6763
}
64+
65+
fun getDefaultGeckoWebExecutor(context: Context): GeckoWebExecutor {
66+
if (executor == null) {
67+
executor = createGeckoWebExecutor(context)
68+
}
69+
70+
return executor!!
71+
}
72+
73+
fun createClient(context: Context): GeckoViewFetchClient {
74+
return GeckoViewFetchClient(context)
75+
}
76+
6877
}

app/src/common/shared/org/mozilla/vrbrowser/browser/engine/GeckoViewFetchClient.kt

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ package org.mozilla.vrbrowser.browser.engine
66

77
import android.content.Context
88
import androidx.annotation.VisibleForTesting
9-
import mozilla.components.concept.fetch.Client
10-
import mozilla.components.concept.fetch.Headers
11-
import mozilla.components.concept.fetch.MutableHeaders
12-
import mozilla.components.concept.fetch.Request
13-
import mozilla.components.concept.fetch.Response
14-
15-
import org.mozilla.geckoview.GeckoRuntime
9+
import mozilla.components.concept.fetch.*
1610
import org.mozilla.geckoview.GeckoWebExecutor
1711
import org.mozilla.geckoview.WebRequest
1812
import org.mozilla.geckoview.WebRequest.CACHE_MODE_DEFAULT
@@ -30,12 +24,11 @@ import java.util.concurrent.TimeoutException
3024
*/
3125
class GeckoViewFetchClient(
3226
context: Context,
33-
runtime: GeckoRuntime = GeckoRuntime.getDefault(context),
3427
private val maxReadTimeOut: Pair<Long, TimeUnit> = Pair(MAX_READ_TIMEOUT_MINUTES, TimeUnit.MINUTES)
3528
) : Client() {
3629

3730
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
38-
internal var executor: GeckoWebExecutor = GeckoWebExecutor(runtime)
31+
internal var executor: GeckoWebExecutor = EngineProvider.createGeckoWebExecutor(context)
3932

4033
@Throws(IOException::class)
4134
override fun fetch(request: Request): Response {
Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,63 @@
11
package org.mozilla.vrbrowser.geolocation;
22

3-
import com.loopj.android.http.AsyncHttpClient;
4-
import com.loopj.android.http.JsonHttpResponseHandler;
3+
import android.os.Handler;
4+
import android.os.Looper;
5+
6+
import androidx.annotation.NonNull;
57

68
import org.json.JSONObject;
9+
import org.mozilla.geckoview.GeckoWebExecutor;
10+
import org.mozilla.geckoview.WebRequest;
711

12+
import java.io.ByteArrayOutputStream;
13+
import java.nio.charset.StandardCharsets;
814
import java.util.function.Function;
915

10-
import cz.msebera.android.httpclient.Header;
11-
12-
public class GeolocationClient {
13-
14-
private static final int RETRY_SLEEP = 5 * 1000;
15-
16-
private static AsyncHttpClient client = new AsyncHttpClient();
17-
18-
public static void getGeolocation(String aQuery, int retries, Function success, Function error) {
19-
client.cancelAllRequests(true);
20-
client.setMaxRetriesAndTimeout(retries, RETRY_SLEEP);
21-
client.get(aQuery, null, new JsonHttpResponseHandler("ISO-8859-1") {
22-
23-
@Override
24-
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
25-
success.apply(GeolocationData.parse(response.toString()));
26-
}
27-
28-
@Override
29-
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
30-
error.apply(errorResponse);
31-
}
32-
16+
class GeolocationClient {
17+
18+
static void getGeolocation(@NonNull GeckoWebExecutor executor, String aQuery, Function<GeolocationData, Void> success, Function<String, Void> error) {
19+
new Handler(Looper.getMainLooper()).post(() -> {
20+
WebRequest request = new WebRequest.Builder(aQuery)
21+
.method("GET")
22+
.build();
23+
24+
executor.fetch(request).then(webResponse -> {
25+
String body;
26+
if (webResponse != null) {
27+
if (webResponse.body != null) {
28+
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
29+
int nRead;
30+
byte[] data = new byte[1024];
31+
while ((nRead = webResponse.body.read(data, 0, data.length)) != -1) {
32+
buffer.write(data, 0, nRead);
33+
}
34+
body = new String(buffer.toByteArray(), StandardCharsets.UTF_8);
35+
36+
if (webResponse.statusCode == 200) {
37+
JSONObject reader = new JSONObject(body);
38+
success.apply(GeolocationData.parse(reader.toString()));
39+
40+
} else {
41+
// called when response HTTP status is not "200"
42+
error.apply(String.format("Network Error: %s", webResponse.statusCode));
43+
}
44+
45+
} else {
46+
// WebResponse body is null
47+
error.apply("Response body is null");
48+
}
49+
50+
} else {
51+
// WebResponse is null
52+
error.apply("Response is null");
53+
}
54+
return null;
55+
56+
}).exceptionally(throwable -> {
57+
// Exception happened
58+
error.apply(String.format("Unknown network Error: %s", String.format("An exception happened during the request: %s", throwable.getMessage())));
59+
return null;
60+
});
3361
});
3462
}
3563
}

app/src/common/shared/org/mozilla/vrbrowser/geolocation/GeolocationWrapper.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
import android.content.Context;
44

5+
import androidx.annotation.NonNull;
6+
57
import org.mozilla.gecko.util.ThreadUtils;
68
import org.mozilla.vrbrowser.R;
79
import org.mozilla.vrbrowser.browser.SettingsStore;
8-
9-
import androidx.annotation.NonNull;
10+
import org.mozilla.vrbrowser.browser.engine.EngineProvider;
1011

1112
public class GeolocationWrapper {
1213

@@ -24,8 +25,8 @@ private static void update(final @NonNull Context aContext,
2425
final int maxRetries) {
2526
if (retryCount <= maxRetries - 1) {
2627
GeolocationClient.getGeolocation(
28+
EngineProvider.INSTANCE.getDefaultGeckoWebExecutor(aContext),
2729
endPoint,
28-
MAX_RETRIES,
2930
(data) -> {
3031
if (data == null) {
3132
if (retryCount <= maxRetries) {

app/src/common/shared/org/mozilla/vrbrowser/search/SearchEngineWrapper.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.mozilla.vrbrowser.R;
1414
import org.mozilla.vrbrowser.VRBrowserApplication;
1515
import org.mozilla.vrbrowser.browser.SettingsStore;
16+
import org.mozilla.vrbrowser.browser.engine.EngineProvider;
1617
import org.mozilla.vrbrowser.geolocation.GeolocationData;
1718
import org.mozilla.vrbrowser.search.suggestions.SuggestionsClient;
1819
import org.mozilla.vrbrowser.utils.SystemUtils;
@@ -118,7 +119,10 @@ public CompletableFuture<List<String>> getSuggestions(String aQuery) {
118119
// TODO: Use mSuggestionsClient.getSuggestions when fixed in browser-search.
119120
String query = getSuggestionURL(aQuery);
120121
mUIThreadExecutor.execute(() ->
121-
SuggestionsClient.getSuggestions(mSearchEngine, query).thenAcceptAsync(future::complete));
122+
SuggestionsClient.getSuggestions(
123+
EngineProvider.INSTANCE.getDefaultGeckoWebExecutor(mContext),
124+
mSearchEngine,
125+
query).thenAcceptAsync(future::complete));
122126

123127
return future;
124128
}

app/src/common/shared/org/mozilla/vrbrowser/search/suggestions/SuggestionsClient.java

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,67 @@
11
package org.mozilla.vrbrowser.search.suggestions;
22

3-
import com.loopj.android.http.AsyncHttpClient;
4-
import com.loopj.android.http.TextHttpResponseHandler;
3+
import android.os.Handler;
4+
import android.os.Looper;
55

6+
import androidx.annotation.NonNull;
7+
8+
import org.mozilla.geckoview.GeckoWebExecutor;
9+
import org.mozilla.geckoview.WebRequest;
10+
11+
import java.io.ByteArrayOutputStream;
12+
import java.nio.charset.StandardCharsets;
613
import java.util.List;
714
import java.util.concurrent.CompletableFuture;
815

9-
import cz.msebera.android.httpclient.Header;
1016
import mozilla.components.browser.search.SearchEngine;
1117

1218
public class SuggestionsClient {
1319

14-
private static AsyncHttpClient client = new AsyncHttpClient();
1520

16-
public static CompletableFuture<List<String>> getSuggestions(SearchEngine mEngine, String aQuery) {
21+
public static CompletableFuture<List<String>> getSuggestions(@NonNull GeckoWebExecutor executor, SearchEngine mEngine, String aQuery) {
1722
final CompletableFuture<List<String>> future = new CompletableFuture<>();
18-
client.cancelAllRequests(true);
19-
client.get(aQuery, null, new TextHttpResponseHandler("ISO-8859-1") {
20-
@Override
21-
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
22-
future.completeExceptionally(throwable);
23-
}
24-
25-
@Override
26-
public void onSuccess(int statusCode, Header[] headers, String responseString) {
27-
future.complete(SuggestionParser.selectResponseParser(mEngine).apply(responseString));
28-
}
23+
24+
new Handler(Looper.getMainLooper()).post(() -> {
25+
WebRequest request = new WebRequest.Builder(aQuery)
26+
.method("GET")
27+
.build();
28+
29+
executor.fetch(request).then(webResponse -> {
30+
String body;
31+
if (webResponse != null) {
32+
if (webResponse.body != null) {
33+
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
34+
int nRead;
35+
byte[] data = new byte[1024];
36+
while ((nRead = webResponse.body.read(data, 0, data.length)) != -1) {
37+
buffer.write(data, 0, nRead);
38+
}
39+
body = new String(buffer.toByteArray(), StandardCharsets.UTF_8);
40+
41+
if (webResponse.statusCode == 200) {
42+
future.complete(SuggestionParser.selectResponseParser(mEngine).apply(body));
43+
44+
} else {
45+
// called when response HTTP status is not "200"
46+
future.completeExceptionally(new Throwable(String.format("Network Error: %s", webResponse.statusCode)));
47+
}
48+
49+
} else {
50+
// WebResponse body is null
51+
future.completeExceptionally(new Throwable("Response body is null"));
52+
}
53+
54+
} else {
55+
// WebResponse is null
56+
future.completeExceptionally(new Throwable("Response is null"));
57+
}
58+
return null;
59+
60+
}).exceptionally(throwable -> {
61+
// Exception happened
62+
future.completeExceptionally(new Throwable(String.format("Unknown network Error: %s", String.format("An exception happened during the request: %s", throwable.getMessage()))));
63+
return null;
64+
});
2965
});
3066

3167
return future;

app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/dialogs/VoiceSearchWidget.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.mozilla.gecko.util.ThreadUtils;
2929
import org.mozilla.vrbrowser.R;
3030
import org.mozilla.vrbrowser.browser.SettingsStore;
31+
import org.mozilla.vrbrowser.browser.engine.EngineProvider;
3132
import org.mozilla.vrbrowser.browser.engine.SessionStore;
3233
import org.mozilla.vrbrowser.databinding.VoiceSearchDialogBinding;
3334
import org.mozilla.vrbrowser.ui.widgets.WidgetManagerDelegate;
@@ -86,6 +87,7 @@ private void initialize(Context aContext) {
8687
mWidgetManager.addPermissionListener(this);
8788

8889
mMozillaSpeechService = MozillaSpeechService.getInstance();
90+
mMozillaSpeechService.setGeckoWebExecutor(EngineProvider.INSTANCE.createGeckoWebExecutor(getContext()));
8991
mMozillaSpeechService.setProductTag(getContext().getString(R.string.voice_app_id));
9092

9193
mSearchingAnimation = new RotateAnimation(0, 360f,

app/src/main/res/values/non_L10n.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@
183183
<string name="keyboard_nl_NL_popup_i" translatable="false">ìíiïîįī</string>
184184

185185
<!-- SEARCH ENGINES -->
186-
<string name="geolocation_api_url" translatable="false">https://location.services.mozilla.com/v1/country</string>
186+
<!-- The MLS policy has been updated and now we can't use it without an API key anymore -->
187+
<string name="geolocation_api_url" translatable="false">https://location.services.mozilla.com/v1/country?key=fff72d56-b040-4205-9a11-82feda9d83a3</string>
187188
<!-- Google -->
188189
<string name="search_google_params" translatable="false">q=%1$s&amp;client=firefox-b-o</string>
189190
<string name="search_google_us_params" translatable="false">q=%1$s&amp;client=firefox-b-1-o</string>

versions.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ versions.android_components = "28.0.1"
3232
// that we depend on directly for its rustlog package, and it's important
3333
// that it be kept insync with the version used by android-components above.
3434
versions.mozilla_appservices = "0.48.2"
35-
versions.mozilla_speech = "1.0.10"
35+
versions.mozilla_speech = "1.0.11"
3636
versions.openwnn = "1.3.7"
3737
versions.google_vr = "1.190.0"
3838
versions.room = "2.2.0"

0 commit comments

Comments
 (0)