Kotlin *-*

This commit is contained in:
Jan-Lk Else 2016-11-25 06:59:16 +01:00
parent add76b7668
commit 27b34de2f7
5 changed files with 102 additions and 120 deletions

View File

@ -1,4 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 25
@ -9,7 +10,7 @@ android {
targetSdkVersion 25
versionCode 2
versionName "0.2 beta"
resConfigs "en", "nodpi"
resConfigs "en"
}
buildTypes {
debug {
@ -27,4 +28,5 @@ android {
dependencies {
compile 'com.android.support:appcompat-v7:25.0.1'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

View File

@ -1,102 +0,0 @@
package telegra.ph;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.*;
public class MainActivity extends AppCompatActivity {
private WebView webView;
private WebViewClient webViewClient;
private WebChromeClient webChromeClient;
private static final String TELEGRAPH = "http://telegra.ph/";
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
// Enable Javascript
webSettings.setJavaScriptEnabled(true);
// Allow File Access
webSettings.setAllowFileAccess(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
webSettings.setAllowFileAccessFromFileURLs(true);
}
// Add Database support
webSettings.setDatabaseEnabled(true);
webSettings.setDomStorageEnabled(true);
// Add Cache support
webSettings.setAppCacheEnabled(true);
// Set WebViewClient
webViewClient = new WebViewClient() {
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return urlAllowed(url);
}
@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return urlAllowed(request.getUrl().toString());
}
};
webView.setWebViewClient(webViewClient);
// Set WebChromeClient
webChromeClient = new WebChromeClient() {
};
webView.setWebChromeClient(webChromeClient);
// Check if app is opened to show special page
String urlToLoad = TELEGRAPH;
if (getIntent() != null && getIntent().getAction().equals(Intent.ACTION_VIEW) && getIntent().getDataString() != null && getIntent().getDataString().contains("telegra.ph"))
urlToLoad = getIntent().getDataString();
// Load URL
webView.loadUrl(urlToLoad);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.share:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TITLE, webView.getTitle());
shareIntent.putExtra(Intent.EXTRA_TEXT, webView.getUrl());
startActivity(Intent.createChooser(shareIntent, getString(R.string.share)));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// Extra methods
private boolean urlAllowed(String url) {
return url.contains("telegra.ph");
}
}

View File

@ -0,0 +1,91 @@
package telegra.ph
import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import android.webkit.*
class MainActivity : AppCompatActivity() {
private val TELEGRAPH = "http://telegra.ph/"
private val webView: WebView? by lazy { findViewById(R.id.webView) as WebView }
@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView?.settings?.apply {
// Enable Javascript
javaScriptEnabled = true
// Allow File Access
allowFileAccess = true
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
allowFileAccessFromFileURLs = true
}
// Add Database support
databaseEnabled = true
domStorageEnabled = true
// Add Cache support
setAppCacheEnabled(true)
}
// Set WebViewClient
webView?.setWebViewClient(object : WebViewClient() {
@SuppressWarnings("deprecation")
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
return urlAllowed(url)
}
@TargetApi(Build.VERSION_CODES.N)
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
return urlAllowed(request.url.toString())
}
})
// Set WebChromeClient
webView?.setWebChromeClient(object : WebChromeClient() {
})
// Check if app is opened to show special page
var urlToLoad = TELEGRAPH
if (intent.action == Intent.ACTION_VIEW && !intent.dataString.isNullOrBlank() && intent.dataString.contains("telegra.ph"))
urlToLoad = intent.dataString
// Load URL
webView?.loadUrl(urlToLoad)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
super.onCreateOptionsMenu(menu)
menuInflater.inflate(R.menu.activity_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.share -> {
val shareIntent = Intent()
shareIntent.action = Intent.ACTION_SEND
shareIntent.type = "text/plain"
shareIntent.putExtra(Intent.EXTRA_TITLE, webView?.title)
shareIntent.putExtra(Intent.EXTRA_TEXT, webView?.url)
startActivity(Intent.createChooser(shareIntent, getString(R.string.share)))
return true
}
else -> return super.onOptionsItemSelected(item)
}
}
// Extra methods
private fun urlAllowed(url: String) = url.contains("telegra.ph")
}

View File

@ -1,14 +1,18 @@
buildscript {
ext.kotlin_version = '1.0.5-2'
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0-alpha1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}

View File

@ -1,17 +1,4 @@
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
kotlin.incremental=true
org.gradle.parallel=true
org.gradle.configureondemand=true
org.gradle.jvmargs=-Xmx3072M