Search in sources :

Example 1 with UrlSuggestion

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;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) ArrayList(java.util.ArrayList) Request(okhttp3.Request) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) LbryResponseException(com.odysee.app.exceptions.LbryResponseException) IOException(java.io.IOException) Uri(android.net.Uri) LbryRequestException(com.odysee.app.exceptions.LbryRequestException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) UrlSuggestion(com.odysee.app.model.UrlSuggestion)

Example 2 with UrlSuggestion

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;
}
Also used : ArrayList(java.util.ArrayList) UrlSuggestion(com.odysee.app.model.UrlSuggestion)

Example 3 with UrlSuggestion

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;
}
Also used : LbryUriException(com.odysee.app.exceptions.LbryUriException) ArrayList(java.util.ArrayList) LbryUri(com.odysee.app.utils.LbryUri) UrlSuggestion(com.odysee.app.model.UrlSuggestion)

Example 4 with UrlSuggestion

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);
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) FetchRecentUrlHistoryTask(com.odysee.app.tasks.localdata.FetchRecentUrlHistoryTask) UrlSuggestion(com.odysee.app.model.UrlSuggestion)

Example 5 with UrlSuggestion

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;
}
Also used : ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) UrlSuggestion(com.odysee.app.model.UrlSuggestion)

Aggregations

UrlSuggestion (com.odysee.app.model.UrlSuggestion)7 ArrayList (java.util.ArrayList)5 Cursor (android.database.Cursor)1 Uri (android.net.Uri)1 DatabaseHelper (com.odysee.app.data.DatabaseHelper)1 LbryRequestException (com.odysee.app.exceptions.LbryRequestException)1 LbryResponseException (com.odysee.app.exceptions.LbryResponseException)1 LbryUriException (com.odysee.app.exceptions.LbryUriException)1 FetchRecentUrlHistoryTask (com.odysee.app.tasks.localdata.FetchRecentUrlHistoryTask)1 SaveUrlHistoryTask (com.odysee.app.tasks.localdata.SaveUrlHistoryTask)1 LbryUri (com.odysee.app.utils.LbryUri)1 IOException (java.io.IOException)1 List (java.util.List)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 ResponseBody (okhttp3.ResponseBody)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1