use of com.odysee.app.model.UrlSuggestion in project odysee-android by OdyseeTeam.
the class Lighthouse method autocomplete.
public static List<UrlSuggestion> autocomplete(String text) throws LbryRequestException, LbryResponseException {
if (autocompleteCache.containsKey(text)) {
return autocompleteCache.get(text);
}
List<UrlSuggestion> suggestions = new ArrayList<>();
Uri.Builder uriBuilder = Uri.parse(String.format("%s/autocomplete", CONNECTION_STRING)).buildUpon().appendQueryParameter("s", text);
Request request = new Request.Builder().url(uriBuilder.toString()).build();
OkHttpClient client = new OkHttpClient();
Response response = null;
ResponseBody responseBody = null;
try {
response = client.newCall(request).execute();
if (response.code() == 200) {
responseBody = response.body();
if (responseBody != null) {
JSONArray array = new JSONArray(responseBody.string());
for (int i = 0; i < array.length(); i++) {
String item = array.getString(i);
boolean isChannel = item.startsWith("@");
LbryUri uri = new LbryUri();
if (isChannel) {
uri.setChannelName(item);
} else {
uri.setStreamName(item);
}
UrlSuggestion suggestion = new UrlSuggestion(isChannel ? UrlSuggestion.TYPE_CHANNEL : UrlSuggestion.TYPE_FILE, item);
suggestion.setUri(uri);
suggestions.add(suggestion);
}
}
autocompleteCache.put(text, suggestions);
} else {
throw new LbryResponseException(response.message());
}
} catch (IOException ex) {
throw new LbryRequestException(String.format("autocomplete request for '%s' failed", text), ex);
} catch (JSONException ex) {
throw new LbryResponseException(String.format("the autocomplete response for '%s' could not be parsed", text), ex);
} finally {
if (responseBody != null) {
response.close();
}
}
return suggestions;
}
use of com.odysee.app.model.UrlSuggestion in project odysee-android by OdyseeTeam.
the class MainActivity method buildLbryUrlSuggestions.
private List<UrlSuggestion> buildLbryUrlSuggestions() {
List<UrlSuggestion> suggestions = new ArrayList<>();
suggestions.add(new UrlSuggestion(UrlSuggestion.TYPE_FILE, "What is LBRY?", LbryUri.tryParse("lbry://what#19b9c243bea0c45175e6a6027911abbad53e983e")));
suggestions.add(new UrlSuggestion(UrlSuggestion.TYPE_CHANNEL, "LBRYCast", LbryUri.tryParse("lbry://@lbrycast#4c29f8b013adea4d5cca1861fb2161d5089613ea")));
suggestions.add(new UrlSuggestion(UrlSuggestion.TYPE_CHANNEL, "The LBRY Channel", LbryUri.tryParse("lbry://@lbry#3fda836a92faaceedfe398225fb9b2ee2ed1f01a")));
for (UrlSuggestion suggestion : suggestions) {
suggestion.setUseTextAsDescription(true);
}
return suggestions;
}
use of com.odysee.app.model.UrlSuggestion in project odysee-android by OdyseeTeam.
the class MainActivity method buildDefaultSuggestions.
private List<UrlSuggestion> buildDefaultSuggestions(String text) {
List<UrlSuggestion> suggestions = new ArrayList<UrlSuggestion>();
if (LbryUri.PROTO_DEFAULT.equalsIgnoreCase(text)) {
loadDefaultSuggestionsForBlankUrl();
return recentUrlHistory != null ? recentUrlHistory : new ArrayList<>();
}
// First item is always search
if (!text.startsWith(LbryUri.PROTO_DEFAULT)) {
UrlSuggestion searchSuggestion = new UrlSuggestion(UrlSuggestion.TYPE_SEARCH, text);
suggestions.add(searchSuggestion);
}
if (!text.matches(LbryUri.REGEX_INVALID_URI)) {
boolean isUrlWithScheme = text.startsWith(LbryUri.PROTO_DEFAULT);
boolean isChannel = text.startsWith("@");
LbryUri uri = null;
if (isUrlWithScheme && text.length() > 7) {
try {
uri = LbryUri.parse(text);
isChannel = uri.isChannel();
} catch (LbryUriException ex) {
// pass
}
}
if (!isChannel) {
if (uri == null) {
uri = new LbryUri();
uri.setStreamName(text);
}
UrlSuggestion fileSuggestion = new UrlSuggestion(UrlSuggestion.TYPE_FILE, text);
fileSuggestion.setUri(uri);
suggestions.add(fileSuggestion);
}
if (text.indexOf(' ') == -1) {
// channels should not contain spaces
if (isChannel) {
if (uri == null) {
uri = new LbryUri();
uri.setChannelName(text);
}
UrlSuggestion suggestion = new UrlSuggestion(UrlSuggestion.TYPE_CHANNEL, text);
suggestion.setUri(uri);
suggestions.add(suggestion);
}
}
if (!isUrlWithScheme && !isChannel) {
UrlSuggestion suggestion = new UrlSuggestion(UrlSuggestion.TYPE_TAG, text);
suggestions.add(suggestion);
}
}
return suggestions;
}
use of com.odysee.app.model.UrlSuggestion in project odysee-android by OdyseeTeam.
the class MainActivity method loadDefaultSuggestionsForBlankUrl.
private void loadDefaultSuggestionsForBlankUrl() {
if (recentUrlHistory != null && recentUrlHistory.size() > 0) {
urlSuggestionListAdapter.addUrlSuggestions(recentUrlHistory);
}
FetchRecentUrlHistoryTask task = new FetchRecentUrlHistoryTask(DatabaseHelper.getInstance(), new FetchRecentUrlHistoryTask.FetchRecentUrlHistoryHandler() {
@Override
public void onSuccess(List<UrlSuggestion> recentHistory) {
List<UrlSuggestion> suggestions = new ArrayList<>(recentHistory);
List<UrlSuggestion> lbrySuggestions = buildLbryUrlSuggestions();
if (suggestions.size() < 10) {
for (int i = suggestions.size(), j = 0; i < 10 && j < lbrySuggestions.size(); i++, j++) {
suggestions.add(lbrySuggestions.get(j));
}
} else if (suggestions.size() == 0) {
suggestions.addAll(lbrySuggestions);
}
for (UrlSuggestion suggestion : suggestions) {
suggestion.setUseTextAsDescription(true);
}
recentUrlHistory = new ArrayList<>(suggestions);
urlSuggestionListAdapter.clear();
urlSuggestionListAdapter.addUrlSuggestions(recentUrlHistory);
List<String> urls = urlSuggestionListAdapter.getItemUrls();
resolveUrlSuggestions(urls);
}
});
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
use of com.odysee.app.model.UrlSuggestion in project odysee-android by OdyseeTeam.
the class DatabaseHelper method getRecentHistory.
// History items are essentially url suggestions
public static List<UrlSuggestion> getRecentHistory(SQLiteDatabase db) {
List<UrlSuggestion> suggestions = new ArrayList<>();
Cursor cursor = null;
try {
cursor = db.rawQuery(SQL_GET_RECENT_URL_HISTORY, null);
while (cursor.moveToNext()) {
UrlSuggestion suggestion = new UrlSuggestion();
suggestion.setText(cursor.getString(0));
suggestion.setUri(cursor.isNull(1) ? null : LbryUri.tryParse(cursor.getString(1)));
suggestion.setType(cursor.getInt(2));
suggestion.setTitleUrlOnly(true);
suggestions.add(suggestion);
}
} finally {
Helper.closeCursor(cursor);
}
return suggestions;
}
Aggregations