This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for bookmarks and history in the awesome bar (#1425)
* Added support for bookmarks and history in the awesome bar * Awesome bar icon style updates As requested per design here: #962 (comment)
- Loading branch information
1 parent
0bb0c9e
commit 6ccab7a
Showing
14 changed files
with
370 additions
and
168 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
app/src/common/shared/org/mozilla/vrbrowser/browser/HistoryStore.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.vrbrowser.browser | ||
|
||
import android.content.Context | ||
import android.os.Handler | ||
import android.os.Looper | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.future.future | ||
import mozilla.components.concept.storage.VisitType | ||
import org.mozilla.vrbrowser.VRBrowserApplication | ||
import java.util.concurrent.CompletableFuture | ||
|
||
class HistoryStore constructor(val context: Context) { | ||
private var listeners = ArrayList<HistoryListener>() | ||
private val storage = (context.applicationContext as VRBrowserApplication).places.history | ||
|
||
interface HistoryListener { | ||
fun onHistoryUpdated() | ||
} | ||
|
||
fun addListener(aListener: HistoryListener) { | ||
if (!listeners.contains(aListener)) { | ||
listeners.add(aListener) | ||
} | ||
} | ||
|
||
fun removeListener(aListener: HistoryListener) { | ||
listeners.remove(aListener) | ||
} | ||
|
||
fun removeAllListeners() { | ||
listeners.clear() | ||
} | ||
|
||
fun getHistory(): CompletableFuture<List<String>?> = GlobalScope.future { | ||
storage.getVisited() | ||
} | ||
|
||
fun addHistory(aURL: String, visitType: VisitType) = GlobalScope.future { | ||
storage.recordVisit(aURL, visitType) | ||
notifyListeners() | ||
} | ||
|
||
fun deleteHistory(aUrl: String, timestamp: Long) = GlobalScope.future { | ||
storage.deleteVisit(aUrl, timestamp) | ||
notifyListeners() | ||
} | ||
|
||
fun isInHistory(aURL: String): CompletableFuture<Boolean> = GlobalScope.future { | ||
storage.getVisited(listOf(aURL)) != null | ||
} | ||
|
||
private fun notifyListeners() { | ||
if (listeners.size > 0) { | ||
val listenersCopy = ArrayList(listeners) | ||
Handler(Looper.getMainLooper()).post { | ||
for (listener in listenersCopy) { | ||
listener.onHistoryUpdated() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
app/src/common/shared/org/mozilla/vrbrowser/search/suggestions/SuggestionsProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package org.mozilla.vrbrowser.search.suggestions; | ||
|
||
import android.content.Context; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import org.mozilla.vrbrowser.browser.SessionStore; | ||
import org.mozilla.vrbrowser.search.SearchEngineWrapper; | ||
import org.mozilla.vrbrowser.ui.widgets.SuggestionsWidget.SuggestionItem; | ||
import org.mozilla.vrbrowser.ui.widgets.SuggestionsWidget.SuggestionItem.Type; | ||
import org.mozilla.vrbrowser.utils.UrlUtils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class SuggestionsProvider { | ||
|
||
public class DefaultSuggestionsComparator implements Comparator { | ||
|
||
public int compare(Object obj1, Object obj2) { | ||
SuggestionItem suggestion1 = (SuggestionItem)obj1; | ||
SuggestionItem suggestion2 = (SuggestionItem)obj2; | ||
if (suggestion1.type == Type.SUGGESTION && suggestion2.type == Type.SUGGESTION) { | ||
return 0; | ||
|
||
} else if (suggestion1.type == suggestion2.type) { | ||
if (mFilterText != null) { | ||
if (suggestion1.title != null && suggestion2.title != null) | ||
return suggestion1.title.toLowerCase().indexOf(mFilterText) - suggestion2.title.toLowerCase().indexOf(mFilterText); | ||
return suggestion1.url.toLowerCase().indexOf(mFilterText) - suggestion2.url.indexOf(mFilterText); | ||
|
||
} else { | ||
return suggestion1.url.compareTo(suggestion2.url); | ||
} | ||
|
||
} else { | ||
return suggestion1.type.ordinal() - suggestion2.type.ordinal(); | ||
} | ||
} | ||
} | ||
|
||
private Context mContext; | ||
private SearchEngineWrapper mSearchEngineWrapper; | ||
private String mText; | ||
private String mFilterText; | ||
private Comparator mComparator; | ||
|
||
public SuggestionsProvider(Context context) { | ||
mContext = context; | ||
mSearchEngineWrapper = SearchEngineWrapper.get(mContext); | ||
mFilterText = ""; | ||
mComparator = new DefaultSuggestionsComparator(); | ||
} | ||
|
||
private String getSearchURLOrDomain(String text) { | ||
if (UrlUtils.isDomain(text)) { | ||
return text; | ||
|
||
} else { | ||
return mSearchEngineWrapper.getSearchURL(text); | ||
} | ||
} | ||
|
||
public void setFilterText(String text) { | ||
mFilterText = text.toLowerCase(); | ||
} | ||
|
||
public void setText(String text) { mText = text; } | ||
|
||
public void setComparator(Comparator comparator) { | ||
mComparator = comparator; | ||
} | ||
|
||
public CompletableFuture<List<SuggestionItem>> getBookmarkSuggestions(@NonNull List<SuggestionItem> items) { | ||
CompletableFuture future = new CompletableFuture(); | ||
SessionStore.get().getBookmarkStore().getBookmarks().thenAcceptAsync((bookmarks) -> { | ||
bookmarks.stream(). | ||
filter(b -> b.getUrl().toLowerCase().contains(mFilterText) || | ||
b.getTitle().toLowerCase().contains(mFilterText)) | ||
.forEach(b -> items.add(SuggestionItem.create( | ||
b.getTitle(), | ||
b.getUrl(), | ||
null, | ||
Type.BOOKMARK | ||
))); | ||
if (mComparator != null) | ||
items.sort(mComparator); | ||
future.complete(items); | ||
}); | ||
|
||
return future; | ||
} | ||
|
||
public CompletableFuture<List<SuggestionItem>> getHistorySuggestions(@NonNull final List<SuggestionItem> items) { | ||
CompletableFuture future = new CompletableFuture(); | ||
SessionStore.get().getHistoryStore().getHistory().thenAcceptAsync((history) -> { | ||
history.stream() | ||
.filter(h -> | ||
h.toLowerCase().contains(mFilterText)) | ||
.forEach(h -> items.add(SuggestionItem.create( | ||
h, | ||
h, | ||
null, | ||
Type.HISTORY | ||
))); | ||
if (mComparator != null) | ||
items.sort(mComparator); | ||
future.complete(items); | ||
}); | ||
|
||
return future; | ||
} | ||
|
||
public CompletableFuture<List<SuggestionItem>> getSearchEngineSuggestions(@NonNull final List<SuggestionItem> items) { | ||
CompletableFuture future = new CompletableFuture(); | ||
|
||
// Completion from browser-domains | ||
if (!mText.equals(mFilterText)) { | ||
items.add(SuggestionItem.create( | ||
mText, | ||
getSearchURLOrDomain(mText), | ||
null, | ||
Type.COMPLETION | ||
)); | ||
} | ||
|
||
// Original text | ||
items.add(SuggestionItem.create( | ||
mFilterText, | ||
getSearchURLOrDomain(mFilterText), | ||
null, | ||
Type.SUGGESTION | ||
)); | ||
|
||
// Suggestions | ||
mSearchEngineWrapper.getSuggestions(mFilterText).thenAcceptAsync((suggestions) -> { | ||
suggestions.forEach(s -> { | ||
String url = mSearchEngineWrapper.getSearchURL(s); | ||
items.add(SuggestionItem.create( | ||
s, | ||
url, | ||
null, | ||
Type.SUGGESTION | ||
)); | ||
}); | ||
if (mComparator != null) | ||
items.sort(mComparator); | ||
future.complete(items); | ||
}); | ||
|
||
return future; | ||
} | ||
|
||
public CompletableFuture<List<SuggestionItem>> getSuggestions() { | ||
return CompletableFuture.supplyAsync(() -> new ArrayList<SuggestionItem>()) | ||
.thenComposeAsync(this::getSearchEngineSuggestions) | ||
.thenComposeAsync(this::getBookmarkSuggestions) | ||
.thenComposeAsync(this::getHistorySuggestions); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.