-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Gradle/ Kotlin/ Android frontend (#213)
- Native Android activity - Uses system WebView component - Urlbar with search capability, autohiding when scrolling - Share button to pass URL to other apps - CircleCI configuration Fixes: #203
- Loading branch information
Showing
25 changed files
with
796 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,12 @@ parts | |
stage | ||
prime | ||
*.snap | ||
|
||
*.iml | ||
.gradle | ||
/local.properties | ||
.idea | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
apply plugin: 'com.android.application' | ||
|
||
apply plugin: 'kotlin-android' | ||
|
||
apply plugin: 'kotlin-android-extensions' | ||
|
||
android { | ||
compileSdkVersion 28 | ||
defaultConfig { | ||
applicationId "org.midori_browser.midori" | ||
minSdkVersion 15 | ||
targetSdkVersion 28 | ||
versionCode 3 | ||
versionName "7.0" | ||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | ||
resConfigs "en" | ||
} | ||
buildTypes { | ||
debug { | ||
ext.alwaysUpdateBuildId = false | ||
} | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" | ||
implementation 'com.android.support:appcompat-v7:28.0.0' | ||
implementation 'com.android.support.constraint:constraint-layout:1.1.3' | ||
implementation 'com.android.support:design:28.0.0' | ||
testImplementation 'junit:junit:4.12' | ||
androidTestImplementation 'com.android.support.test:runner:1.0.2' | ||
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" package="org.midori_browser.midori"> | ||
<uses-permission android:name="android.permission.INTERNET"/> | ||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> | ||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> | ||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> | ||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:usesCleartextTraffic="true" | ||
android:label="@string/appName" | ||
android:icon="@drawable/ic_midori" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme" tools:targetApi="m"> | ||
<meta-data android:name="android.webkit.WebView.EnableSafeBrowsing" | ||
android:value="false"/> | ||
<meta-data android:name="android.webkit.WebView.MetricsOptOut" | ||
android:value="true"/> | ||
<activity android:name=".BrowserActivity" | ||
android:configChanges="orientation|screenSize|keyboardHidden"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN"/> | ||
<action android:name="android.intent.action.VIEW"/> | ||
<category android:name="android.intent.category.LAUNCHER"/> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
126 changes: 126 additions & 0 deletions
126
app/src/main/java/org/midori_browser/midori/BrowserActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package org.midori_browser.midori | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Build | ||
import android.os.Bundle | ||
import android.support.annotation.RequiresApi | ||
import android.support.v7.app.AppCompatActivity | ||
import android.text.Editable | ||
import android.text.TextWatcher | ||
import android.view.KeyEvent | ||
import android.view.Menu | ||
import android.view.MenuItem | ||
import android.view.inputmethod.EditorInfo | ||
import android.view.inputmethod.InputMethodManager | ||
import android.webkit.WebSettings | ||
import android.widget.AdapterView | ||
import android.widget.ArrayAdapter | ||
import kotlinx.android.synthetic.main.activity_browser.* | ||
|
||
class BrowserActivity : AppCompatActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_browser) | ||
setSupportActionBar(findViewById(R.id.toolbar)) | ||
|
||
val webSettings = webView.settings | ||
webSettings.javaScriptEnabled = true | ||
webSettings.userAgentString += " " + getString(R.string.userAgentVersion) | ||
webSettings.databaseEnabled = true | ||
webSettings.setAppCacheEnabled(true) | ||
webSettings.domStorageEnabled = true | ||
@RequiresApi(Build.VERSION_CODES.LOLLIPOP) | ||
webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE | ||
webView.webViewClient = WebViewClient(this) | ||
webView.webChromeClient = WebChromeClient(this) | ||
|
||
|
||
val openTabs = (getSharedPreferences("config", Context.MODE_PRIVATE).getString( | ||
"openTabs", null | ||
) ?: getString(R.string.appWebsite)).split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() | ||
webView.loadUrl(openTabs.first()) | ||
|
||
val adapter = ArrayAdapter<String>( | ||
this, | ||
android.R.layout.simple_spinner_dropdown_item, completion | ||
) | ||
urlBar.setAdapter(adapter) | ||
urlBar.onItemClickListener = AdapterView.OnItemClickListener { parent, _, position, _ -> | ||
val im = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager | ||
im.hideSoftInputFromWindow(parent.applicationWindowToken, 0) | ||
loadUrlOrSearch(parent.getItemAtPosition(position).toString()) | ||
} | ||
urlBar.setOnEditorActionListener() { v, actionId, event -> | ||
if ((event != null && event.keyCode == KeyEvent.KEYCODE_ENTER) || actionId == EditorInfo.IME_ACTION_DONE) { | ||
val im = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager | ||
im.hideSoftInputFromWindow(v.applicationWindowToken, 0) | ||
loadUrlOrSearch(urlBar.text.toString()) | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
urlBar.addTextChangedListener(object : TextWatcher { | ||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} | ||
|
||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {} | ||
|
||
override fun afterTextChanged(s: Editable?) { | ||
} | ||
}) | ||
} | ||
|
||
private fun loadUrlOrSearch(text: String) { | ||
webView.loadUrl(magicUri(text) ?: uriForSearch(text)) | ||
} | ||
|
||
fun magicUri(text: String): String? { | ||
if (" " in text) { | ||
return null | ||
} else if (text.startsWith("http")) { | ||
return text | ||
} else if (text == "localhost" || "." in text) { | ||
return "http://" + text | ||
} | ||
return null | ||
} | ||
|
||
val locationEntrySearch = "https://duckduckgo.com/?q=%s" | ||
fun uriForSearch(keywords: String? = null, search: String? = null): String { | ||
val uri = search ?: locationEntrySearch | ||
val escaped = Uri.encode(keywords ?: "", ":/") | ||
// Allow DuckDuckGo to distinguish Midori and in turn share revenue | ||
if (uri == "https://duckduckgo.com/?q=%s") { | ||
return "https://duckduckgo.com/?q=$escaped&t=midori" | ||
} else if ("%s" in uri) { | ||
return uri.format(escaped) | ||
} | ||
return uri + escaped | ||
|
||
} | ||
|
||
val completion = listOf("www.midori-browser.org", "example.com", "duckduckgo.com") | ||
|
||
override fun onCreateOptionsMenu(menu: Menu?): Boolean { | ||
menuInflater.inflate(R.menu.app_menu, menu) | ||
return true | ||
} | ||
|
||
override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) { | ||
R.id.actionShare -> { | ||
val share = Intent().apply { | ||
action = Intent.ACTION_VIEW | ||
data = Uri.parse(webView.url) | ||
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP | ||
} | ||
startActivity(Intent.createChooser(share, getString(R.string.actionShare))) | ||
true | ||
} | ||
else -> { | ||
super.onOptionsItemSelected(item) | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
app/src/main/java/org/midori_browser/midori/WebChromeClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.midori_browser.midori | ||
|
||
import android.os.Message | ||
import android.support.v7.widget.Toolbar | ||
import android.webkit.GeolocationPermissions | ||
import android.webkit.WebChromeClient | ||
import android.webkit.WebView | ||
import kotlinx.android.synthetic.main.activity_browser.* | ||
|
||
class WebChromeClient constructor(val activity: BrowserActivity) : WebChromeClient() { | ||
override fun onProgressChanged(view: WebView?, newProgress: Int) { | ||
activity.loadingProgress.progress = newProgress * 1000 | ||
} | ||
|
||
override fun onReceivedTitle(view: WebView?, title: String?) { | ||
val toolbar = activity.findViewById<Toolbar>(R.id.toolbar) | ||
toolbar.title = title | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/org/midori_browser/midori/WebViewClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.midori_browser.midori | ||
|
||
import android.content.Intent | ||
import android.net.Uri | ||
import android.webkit.WebView | ||
import android.webkit.WebViewClient | ||
import kotlinx.android.synthetic.main.activity_browser.* | ||
|
||
class WebViewClient(val activity: BrowserActivity) : WebViewClient() { | ||
|
||
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean { | ||
if (url != null && url.startsWith("http")) { | ||
activity.urlBar.setText(url) | ||
return false | ||
} | ||
|
||
activity.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url))) | ||
return true | ||
} | ||
} |
Oops, something went wrong.