Skip to content

Commit

Permalink
open urls in external browser (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
pazos authored and Frenzie committed Mar 20, 2019
1 parent e080963 commit 98786ee
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions assets/android.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1554,6 +1554,20 @@ local function run(android_app_state)
end)
end

android.openLink = function(link)
return JNI:context(android.app.activity.vm, function(JNI)
local uri_string = JNI.env[0].NewStringUTF(JNI.env, link)
local result = JNI:callIntMethod(
android.app.activity.clazz,
"openLink",
"(Ljava/lang/String;)I",
uri_string
)
JNI.env[0].DeleteLocalRef(JNI.env, uri_string)
return result
end)
end

android.notification = function(message)
return JNI:context(android.app.activity.vm, function(JNI)
local text = JNI.env[0].NewStringUTF(JNI.env, message)
Expand Down
16 changes: 16 additions & 0 deletions src/org/koreader/launcher/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ public int download(final String url, final String name) {
return 0;
}

public int openLink(String url) {
return openWebPage(url) ? 0 : 1;
}

// ----------------------------------
private WifiManager getWifiManager() {
return (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
Expand All @@ -318,4 +322,16 @@ private void setFullscreenLayout() {
View.SYSTEM_UI_FLAG_LOW_PROFILE);
}
}

private boolean openWebPage(String url) {
Uri webpage = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
return true;
} else {
// cannot find a package able to open the page
return false;
}
}
}

0 comments on commit 98786ee

Please sign in to comment.