Skip to content

Commit

Permalink
Add Gradle/ Kotlin/ Android frontend (#213)
Browse files Browse the repository at this point in the history
- 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
kalikiana authored Dec 18, 2018
1 parent dea3d53 commit da81c91
Show file tree
Hide file tree
Showing 25 changed files with 796 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,34 @@ jobs:
command: |
cd _build
make install
android:
docker:
- image: circleci/android:api-28-alpha
environment:
- JVM_OPTS: -Xmx3200m
steps:
- checkout
- restore_cache:
key: jars-{{ checksum "build.gradle" }}-{{ checksum "app/build.gradle" }}
- run:
name: Download dependencies
command: ./gradlew androidDependencies
- save_cache:
key: jars-{{ checksum "build.gradle" }}-{{ checksum "app/build.gradle" }}
paths:
- ~/.gradle
- run:
name: Run unit tests
command: ./gradlew lint test
- store_artifacts:
path: app/build/reports
destination: reports
- store_test_results:
path: app/build/test-results

workflows:
version: 2
ci:
jobs:
- build
- android
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ parts
stage
prime
*.snap

*.iml
.gradle
/local.properties
.idea
.DS_Store
/build
/captures
.externalNativeBuild
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ you can install the **latest stable** version of Midori

> **Spoilers:** For those more adventurous types out there, trying out the preview of the next version is only the switch of a channel away.
# Installing Midori on Android

You can opt-in for the [beta release on the Play Store](https://play.google.com/apps/testing/org.midori_browser.midori).

# Building from source

**Requirements**
Expand Down Expand Up @@ -247,6 +251,18 @@ This will automatically request a **review from other developers** who can then
* **fortress**: user of an ancient release like 0.4.3 as found on Raspberry Pie, Debian, Ubuntu
* **katze, sokoke, tabby**: legacy API names and coincidentally cat breeds

# Midori for Android

The easiest way to build, develop and test Midori on Android is with [Android Studio](https://developer.android.com/studio/#downloads) ([snap](https://snapcraft.io/android-studio)).

When working with the command line, setting `JAVA_HOME` is paramount:

export JAVA_HOME=/snap/android-studio/current/android-studio/jre/

Afterwards you can run commands like so:

./gradlew lint test

# Midori for Windows

## For Linux developers
Expand Down
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
38 changes: 38 additions & 0 deletions app/build.gradle
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'
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
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
31 changes: 31 additions & 0 deletions app/src/main/AndroidManifest.xml
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 app/src/main/java/org/midori_browser/midori/BrowserActivity.kt
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 app/src/main/java/org/midori_browser/midori/WebChromeClient.kt
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 app/src/main/java/org/midori_browser/midori/WebViewClient.kt
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
}
}
Loading

0 comments on commit da81c91

Please sign in to comment.