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

Commit

Permalink
Suggestions bar implementation (#724)
Browse files Browse the repository at this point in the history
* Suggestions bar implementation

* Fixed row click issue.
  • Loading branch information
keianhzo authored and MortimerGoro committed Nov 8, 2018
1 parent e900283 commit fe5bacd
Show file tree
Hide file tree
Showing 38 changed files with 1,444 additions and 478 deletions.
24 changes: 18 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -230,19 +230,32 @@ configurations {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')

// Common
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:27.1.1'
implementation 'com.google.vr:sdk-audio:1.170.0'

// Android Components
implementation "com.github.mozilla:mozillaspeechlibrary:1.0.6"
implementation "org.mozilla.components:service-telemetry:${rootProject.ext.androidComponents['version']}"
implementation "org.mozilla.components:browser-errorpages:${rootProject.ext.androidComponents['version']}"
implementation "org.mozilla.components:browser-search:${rootProject.ext.androidComponents['version']}"
implementation "org.mozilla.components:browser-domains:${rootProject.ext.androidComponents['version']}"
implementation "org.mozilla.components:ui-autocomplete:${rootProject.ext.androidComponents['version']}"

// Testing
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

// Daydream
googlevrImplementation 'com.google.vr:sdk-base:1.170.0'
googlevrFlatImplementation 'com.google.vr:sdk-base:1.170.0'

// ODG
svrImplementation fileTree(dir: "${project.rootDir}/third_party/svr/", include: ['*.jar'])
implementation 'com.android.support:design:27.1.1'
implementation 'com.google.vr:sdk-audio:1.170.0'
implementation "org.mozilla.components:service-telemetry:${rootProject.ext.androidComponents['version']}"
implementation "org.mozilla.components:browser-errorpages:${rootProject.ext.androidComponents['version']}"
implementation "com.github.mozilla:mozillaspeechlibrary:1.0.6"
}

if (findProject(':servo')) {
Expand All @@ -266,7 +279,6 @@ if (findProject(':geckoview-local')) {
dependencies {
// To see what the latest geckoview-nightly version is go here:
// https://maven.mozilla.org/?prefix=maven2/org/mozilla/geckoview/geckoview-nightly-armeabi-v7a/

armImplementation "org.mozilla.geckoview:geckoview-nightly-armeabi-v7a:${rootProject.ext.geckoNightly['version']}"
x86Implementation "org.mozilla.geckoview:geckoview-nightly-x86:${rootProject.ext.geckoNightly['version']}"
}
Expand Down
16 changes: 12 additions & 4 deletions app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
import org.mozilla.vrbrowser.browser.SettingsStore;
import org.mozilla.vrbrowser.crashreporting.CrashReporterService;
import org.mozilla.vrbrowser.crashreporting.GlobalExceptionHandler;
import org.mozilla.vrbrowser.geolocation.GeolocationWrapper;
import org.mozilla.vrbrowser.input.MotionEventGenerator;
import org.mozilla.vrbrowser.search.SearchEngine;
import org.mozilla.vrbrowser.search.SearchEngineWrapper;
import org.mozilla.vrbrowser.telemetry.TelemetryWrapper;
import org.mozilla.vrbrowser.ui.OffscreenDisplay;
import org.mozilla.vrbrowser.ui.widgets.BrowserWidget;
Expand Down Expand Up @@ -116,6 +117,7 @@ public void run() {
private Thread mUiThread;
private LinkedList<Pair<Object, Float>> mBrightnessQueue;
private Pair<Object, Float> mCurrentBrightness;
private SearchEngineWrapper mSearchEngineWrapper;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -129,6 +131,7 @@ protected void onCreate(Bundle savedInstanceState) {

Bundle extras = getIntent() != null ? getIntent().getExtras() : null;
SessionStore.get().setContext(this, extras);
SessionStore.get().registerListeners();

// Create broadcast receiver for getting crash messages from crash process
IntentFilter intentFilter = new IntentFilter();
Expand Down Expand Up @@ -170,7 +173,11 @@ protected void onCreate(Bundle savedInstanceState) {
queueRunnable(() -> setTemporaryFilePath(tempPath));
initializeWorld();

SearchEngine.get(this).update();
// Setup the search engine
mSearchEngineWrapper = SearchEngineWrapper.get(this);
mSearchEngineWrapper.registerForUpdates();

GeolocationWrapper.update(this);
}

protected void initializeWorld() {
Expand Down Expand Up @@ -244,6 +251,7 @@ protected void onResume() {
protected void onDestroy() {
// Unregister the crash service broadcast receiver
unregisterReceiver(mCrashReceiver);
mSearchEngineWrapper.unregisterForUpdates();

for (Widget widget: mWidgets.values()) {
widget.releaseWidget();
Expand All @@ -259,7 +267,7 @@ protected void onDestroy() {
mPermissionDelegate.release();
}

SessionStore.get().clearListeners();
SessionStore.get().unregisterListeners();
super.onDestroy();
}

Expand Down Expand Up @@ -483,7 +491,7 @@ void handleAudioPose(float qx, float qy, float qz, float qw, float px, float py,
mAudioEngine.setPose(qx, qy, qz, qw, px, py, pz);

// https://developers.google.com/vr/reference/android/com/google/vr/sdk/audio/GvrAudioEngine.html#resume()
// The update method must be called from the main thread at a regular rate.
// The initialize method must be called from the main thread at a regular rate.
runOnUiThread(mAudioUpdateRunnable);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
package org.mozilla.vrbrowser.browser;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Rect;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
Expand All @@ -27,9 +29,9 @@
import org.mozilla.vrbrowser.BuildConfig;
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.crashreporting.CrashReporterService;
import org.mozilla.vrbrowser.geolocation.GeolocationData;
import org.mozilla.vrbrowser.telemetry.TelemetryWrapper;
import org.mozilla.vrbrowser.utils.InternalPages;
import org.mozilla.vrbrowser.utils.ValueHolder;

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -47,11 +49,13 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import static org.mozilla.vrbrowser.utils.ServoUtils.*;
import static org.mozilla.vrbrowser.utils.ServoUtils.createServoSession;
import static org.mozilla.vrbrowser.utils.ServoUtils.isInstanceOfServoSession;
import static org.mozilla.vrbrowser.utils.ServoUtils.isServoAvailable;

public class SessionStore implements GeckoSession.NavigationDelegate, GeckoSession.ProgressDelegate,
GeckoSession.ContentDelegate, GeckoSession.TextInputDelegate, GeckoSession.TrackingProtectionDelegate,
GeckoSession.PromptDelegate {
GeckoSession.PromptDelegate, SharedPreferences.OnSharedPreferenceChangeListener {

private static SessionStore mInstance;
private static final String LOGTAG = "VRB";
Expand Down Expand Up @@ -110,26 +114,37 @@ class State {
private int mPreviousSessionId = SessionStore.NO_SESSION_ID;
private String mRegion;
private Context mContext;
private SharedPreferences mPrefs;

private SessionStore() {
mSessions = new LinkedHashMap<>();
mSessionsStack = new ArrayDeque<>();
mPrivateSessionsStack = new ArrayDeque<>();
}

public void registerListeners() {
mNavigationListeners = new LinkedList<>();
mProgressListeners = new LinkedList<>();
mContentListeners = new LinkedList<>();
mSessionChangeListeners = new LinkedList<>();
mTextInputListeners = new LinkedList<>();
mPromptListeners = new LinkedList<>();

mSessions = new LinkedHashMap<>();
mSessionsStack = new ArrayDeque<>();
mPrivateSessionsStack = new ArrayDeque<>();
if (mPrefs != null) {
mPrefs.registerOnSharedPreferenceChangeListener(this);
}
}

public void clearListeners() {
public void unregisterListeners() {
mNavigationListeners.clear();
mProgressListeners.clear();
mContentListeners.clear();
mSessionChangeListeners.clear();
mTextInputListeners.clear();

if (mPrefs != null) {
mPrefs.unregisterOnSharedPreferenceChangeListener(this);
}
}

public void setContext(Context aContext, Bundle aExtras) {
Expand Down Expand Up @@ -157,6 +172,7 @@ public void setContext(Context aContext, Bundle aExtras) {
}

mContext = aContext;
mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
}

public void dumpAllState(Integer sessionId) {
Expand Down Expand Up @@ -441,7 +457,7 @@ public void setRegion(String aRegion) {
Log.d(LOGTAG, "SessionStore setRegion: " + aRegion);
mRegion = aRegion != null ? aRegion.toLowerCase() : "worldwide";

// There is a region update and the home is already loaded
// There is a region initialize and the home is already loaded
if (mCurrentSession != null && isHomeUri(getCurrentUri())) {
mCurrentSession.loadUri("javascript:window.location.replace('" + getHomeUri() + "');");
}
Expand Down Expand Up @@ -1173,4 +1189,16 @@ public void onFilePrompt(GeckoSession session, String title, int type, String[]
public GeckoResult<AllowOrDeny> onPopupRequest(final GeckoSession session, final String targetUri) {
return GeckoResult.fromValue(AllowOrDeny.DENY);
}

// SharedPreferences.OnSharedPreferenceChangeListener

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (mContext != null) {
if (key == mContext.getString(R.string.settings_key_geolocation_data)) {
GeolocationData data = GeolocationData.parse(sharedPreferences.getString(key, null));
setRegion(data.getCountryCode());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.mozilla.vrbrowser.geolocation;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.json.JSONObject;

import java.util.function.Function;

import cz.msebera.android.httpclient.Header;

public class GeolocationClient {

private static final int RETRY_SLEEP = 5 * 1000;

private static AsyncHttpClient client = new AsyncHttpClient();

public static void getGeolocation(String aQuery, int retries, Function success, Function error) {
client.cancelAllRequests(true);
client.setMaxRetriesAndTimeout(retries, RETRY_SLEEP);
client.get(aQuery, null, new JsonHttpResponseHandler("ISO-8859-1") {

@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
success.apply(GeolocationData.parse(response.toString()));
}

@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
error.apply(errorResponse);
}

});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.mozilla.vrbrowser.geolocation;

import android.support.annotation.NonNull;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

/**
* Class representing a Geolocation success response (HTTP 200)
*/
public class GeolocationData {

private static final String LOGTAG = "VRB";

private JSONObject mData;

private GeolocationData(JSONObject data) {
mData = data;
}

@NonNull
public static GeolocationData create(JSONObject data) {
return new GeolocationData(data);
}

@NonNull
public static GeolocationData parse(String aGeolocationJson) {
try {
return GeolocationData.create(new JSONObject(aGeolocationJson));

} catch (JSONException e) {
Log.e(LOGTAG, "Error parsing geolocation data: " + e.getLocalizedMessage());
return null;
}
}

public String getCountryCode() {
return mData.optString("country_code", "");
}

public String getCountryName() {
return mData.optString("country_name", "");
}

@Override
public String toString() {
return mData.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.mozilla.vrbrowser.geolocation;

import android.content.Context;
import android.support.annotation.NonNull;

import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.browser.SettingsStore;

public class GeolocationWrapper {

private static final int MAX_RETRIES = 2;
private static final int RETRY_SLEEP = 5 * 1000;

public static void update(final @NonNull Context aContext) {
String endpoint = aContext.getString(R.string.geolocation_api_url);
update(aContext, endpoint, 0, MAX_RETRIES);
}

private static void update(final @NonNull Context aContext,
final @NonNull String endPoint,
final int retryCount,
final int maxRetries) {
if (retryCount <= maxRetries - 1) {
GeolocationClient.getGeolocation(
endPoint,
MAX_RETRIES,
(data) -> {
if (data == null) {
if (retryCount <= maxRetries) {
ThreadUtils.postDelayedToUiThread(() ->
update(aContext, endPoint, retryCount + 1, maxRetries),
RETRY_SLEEP);
}

} else {
SettingsStore.getInstance(aContext).setGeolocationData(data.toString());
}
return null;
},
(error) -> {
if (retryCount <= maxRetries) {
ThreadUtils.postDelayedToUiThread(() ->
update(aContext, endPoint, retryCount + 1, maxRetries),
RETRY_SLEEP);
}
return null;
});
}
}

}
Loading

0 comments on commit fe5bacd

Please sign in to comment.