fix(android): Use go/ links for keyboard search

This commit is contained in:
Darcy Wong 2020-07-10 17:42:15 +07:00
parent 4d5b3e5447
commit 21b85eb04e
4 changed files with 30 additions and 34 deletions

View file

@ -290,6 +290,22 @@ public final class KMManager {
return Tier.STABLE;
}
/**
* Extract KMEA major version #.# from VERSION_NAME
* @return String
*/
public static String getMajorVersion() {
// Regex needs to match the entire string
String appVersion = com.tavultesoft.kmea.BuildConfig.VERSION_NAME;
Pattern pattern = Pattern.compile("^(\\d+\\.\\d+)\\.\\d+.*");
Matcher matcher = pattern.matcher(appVersion);
if (matcher.matches() && matcher.groupCount() >= 1) {
appVersion = matcher.group(1);
}
return appVersion;
}
/**
* Extract KMEA version #.#.# from VERSION_NAME
* @return String

View file

@ -32,8 +32,8 @@ public class KMPBrowserActivity extends AppCompatActivity {
private WebView webView;
private static final String KMP_PRODUCTION_HOST = "https://keyman.com";
private static final String KMP_STAGING_HOST = "https://staging-keyman-com.azurewebsites.net";
private static final String KMP_SEARCH_URL_FORMATSTR = "%s/keyboards%s?embed=android&version=%s";
private static final String KMP_LANGUAGE_FORMATSTR = "/languages/%s";
private static final String KMP_DOWNLOAD_KEYBOARDS_FORMATSTR = "%s/go/android/%s/download-keyboards%s";
private static final String KMP_DOWNLOAD_KEYBOARDS_LANGUAGES = "/languages/%s";
private boolean isLoading = false;
private boolean didFinishLoading = false;
@ -108,9 +108,9 @@ public class KMPBrowserActivity extends AppCompatActivity {
KMP_PRODUCTION_HOST : KMP_STAGING_HOST;
// If language ID is provided, include it in the keyboard search
String languageID = getIntent().getStringExtra("languageCode");
String languageStr = (languageID != null) ? String.format(KMP_LANGUAGE_FORMATSTR, languageID) : "";
String appVersion = KMManager.getVersion();
String kmpSearchUrl = String.format(KMP_SEARCH_URL_FORMATSTR, host, languageStr, appVersion);
String languageStr = (languageID != null) ? String.format(KMP_DOWNLOAD_KEYBOARDS_LANGUAGES, languageID) : "";
String appMajorVersion = KMManager.getMajorVersion();
String kmpSearchUrl = String.format(KMP_DOWNLOAD_KEYBOARDS_FORMATSTR, host, appMajorVersion, languageStr);
webView.loadUrl(kmpSearchUrl);
}

View file

@ -266,9 +266,7 @@ public final class FileUtils {
}
/**
* Utility to parse a URL and determine if it's a valid keyman:<method>
* Currently, only "keyman" scheme with "download" path and query is supported.
* Legacy keyman:// protocol is deprecated and not supported.
* Utility to parse a URL and determine if it's a Keyman hosted keyboard download.
* @param u String of the URL
* @return boolean true if URL is a supported Keyman link
*/
@ -278,20 +276,13 @@ public final class FileUtils {
return ret;
}
String lowerU = u.toLowerCase();
Pattern pattern = Pattern.compile("^keyman:(\\w+)\\?(.+)");
Pattern pattern = Pattern.compile("^https://(staging-keyman-com.azurewebsites.net|keyman.com)/keyboard/download\\?(.+)");
Matcher matcher = pattern.matcher(lowerU);
// Check URL starts with "keyman"
if (matcher.matches() && (matcher.group(1) != null)) {
// For now, only handle "download"
switch (matcher.group(1).toLowerCase()) {
case "download":
if (matcher.group(2) != null) {
// Contains query
ret = true;
}
break;
default:
ret = false;
if (matcher.group(2) != null) {
// Contains query
ret = true;
}
}
return ret;

View file

@ -38,23 +38,12 @@ public class FileUtilsTest {
Assert.assertFalse(FileUtils.isKeymanLink(""));
// Valid Keyman links
Assert.assertTrue(FileUtils.isKeymanLink("keyman:download?keyboard"));
Assert.assertTrue(FileUtils.isKeymanLink("Keyman:Download?keyboard"));
// keyman:// invalid
Assert.assertFalse(FileUtils.isKeymanLink("keyman://keyboard"));
Assert.assertFalse(FileUtils.isKeymanLink("Keyman://keyboard"));
Assert.assertFalse(FileUtils.isKeymanLink("keyman:download//keyboard"));
Assert.assertFalse(FileUtils.isKeymanLink("keyman://download/keyboard"));
Assert.assertTrue(FileUtils.isKeymanLink("https://staging-keyman-com.azurewebsites.net/keyboard/download?id=malar_malayalam&platform=android&mode=standalone"));
Assert.assertTrue(FileUtils.isKeymanLink("https://keyman.com/keyboard/download?id=malar_malayalam&platform=android&mode=standalone"));
// link missing query
Assert.assertFalse(FileUtils.isKeymanLink("keyman:download?"));
// Other methods not supported
Assert.assertFalse(FileUtils.isKeymanLink("keyman:method//keyboard"));
Assert.assertFalse(FileUtils.isKeymanLink("keyman:method?keyboard"));
Assert.assertFalse(FileUtils.isKeymanLink("example:keyman?"));
Assert.assertFalse(FileUtils.isKeymanLink("https://staging-keyman-com.azurewebsites.net/keyboard/download"));
Assert.assertFalse(FileUtils.isKeymanLink("https://keyman.com/keyboard/download"));
}
@Test