Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
yuliskov committed Apr 27, 2021
1 parent d002477 commit 7c764e0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.liskovsoft.sharedutils.helpers.Helpers;
import com.liskovsoft.sharedutils.helpers.MessageHelpers;
import com.liskovsoft.sharedutils.mylogger.Log;
import com.liskovsoft.sharedutils.prefs.GlobalPreferences;
import com.liskovsoft.smartyoutubetv2.common.app.presenters.base.BasePresenter;
import com.liskovsoft.smartyoutubetv2.common.app.views.SplashView;
import com.liskovsoft.smartyoutubetv2.common.app.views.ViewManager;
Expand Down Expand Up @@ -128,7 +127,7 @@ private void applyNewIntent(Intent intent) {
} else {
String searchText = IntentExtractor.extractSearchText(intent);

if (searchText != null || IntentExtractor.isVoiceCommand(intent)) {
if (searchText != null || IntentExtractor.isStartVoiceCommand(intent)) {
SearchPresenter searchPresenter = SearchPresenter.instance(getContext());
searchPresenter.startSearch(searchText);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.liskovsoft.smartyoutubetv2.common.utils;

import android.content.Intent;
import android.net.Uri;
import com.liskovsoft.sharedutils.helpers.Helpers;
import com.liskovsoft.sharedutils.querystringparser.UrlQueryString;
import com.liskovsoft.sharedutils.querystringparser.UrlQueryStringFactory;
Expand All @@ -24,6 +25,10 @@ public static String extractVideoId(Intent intent) {
return null;
}

if (extractVoiceQuery(intent.getData()) != null) {
return null;
}

// Don't Uri directly or you might get UnsupportedOperationException on some urls.
UrlQueryString parser = UrlQueryStringFactory.parse(intent.getData());
String videoId = parser.get(VIDEO_ID_KEY);
Expand All @@ -49,6 +54,12 @@ public static String extractSearchText(Intent intent) {
return null;
}

String voiceQuery = extractVoiceQuery(intent.getData());

if (voiceQuery != null) {
return voiceQuery;
}

// Don't Uri directly or you might get UnsupportedOperationException on some urls.
UrlQueryString parser = UrlQueryStringFactory.parse(intent.getData());

Expand Down Expand Up @@ -93,7 +104,14 @@ public static boolean isChannelUrl(Intent intent) {
&& Helpers.contains(new String[] {SUBSCRIPTIONS_URL, HISTORY_URL, RECOMMENDED_URL}, intent.getData().toString());
}

public static boolean isVoiceCommand(Intent intent) {
public static boolean isStartVoiceCommand(Intent intent) {
return intent != null && intent.getData() != null && intent.getData().toString().contains("launch=voice");
}

/**
* Example: https://www.youtube.com/tv?voice={"youtubeAssistantRequest":{"query":"Russian YouTube","queryIntent":"CgxTZWFyY2hJbnRlbnQSFAoFcXVlcnkSCxoJCgdSdXNzaWFuEiYKCGRvY190eXBlEhoaGAoWWU9VVFVCRV9ET0NfVFlQRV9WSURFTw==","youtubeAssistantParams":{"personalDataParams":{"showPersonalData":false}},"enablePrefetchLogging":true},"updateYoutubeSettings":{"enableSafetyMode":false,"enablePersonalResults":false},"hasEntityBar":false}&command_id=CWGIYL6nN8Gi3AP_5Y6wAQ&launch=voice&vq=Russian%20YouTube
*/
private static String extractVoiceQuery(Uri data) {
return Helpers.runMultiMatcher(data.toString(), ":{\"query\":\"([^\"]*)\"");
}
}

0 comments on commit 7c764e0

Please sign in to comment.