From c89016e7427defd1f8a095d7aeb30292a42fad92 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Fri, 30 Jun 2017 10:21:51 +0200 Subject: Conversion to Kotlin --- app/build.gradle | 15 ++-- .../pacien/tincapp/activities/BaseActivity.java | 71 ------------------- .../org/pacien/tincapp/activities/BaseActivity.kt | 62 ++++++++++++++++ .../pacien/tincapp/activities/StartActivity.java | 82 ---------------------- .../org/pacien/tincapp/activities/StartActivity.kt | 82 ++++++++++++++++++++++ .../java/org/pacien/tincapp/commands/Command.java | 67 ------------------ .../java/org/pacien/tincapp/commands/Command.kt | 36 ++++++++++ .../java/org/pacien/tincapp/commands/Executor.java | 44 ------------ .../java/org/pacien/tincapp/commands/Executor.kt | 43 ++++++++++++ .../pacien/tincapp/commands/PermissionFixer.java | 34 --------- .../org/pacien/tincapp/commands/PermissionFixer.kt | 20 ++++++ .../java/org/pacien/tincapp/commands/Tinc.java | 60 ---------------- .../main/java/org/pacien/tincapp/commands/Tinc.kt | 49 +++++++++++++ .../java/org/pacien/tincapp/commands/Tincd.java | 28 -------- .../main/java/org/pacien/tincapp/commands/Tincd.kt | 25 +++++++ .../java/org/pacien/tincapp/context/AppInfo.java | 42 ----------- .../java/org/pacien/tincapp/context/AppInfo.kt | 32 +++++++++ .../java/org/pacien/tincapp/context/AppPaths.java | 75 -------------------- .../java/org/pacien/tincapp/context/AppPaths.kt | 43 ++++++++++++ .../org/pacien/tincapp/service/TincVpnService.java | 51 -------------- .../org/pacien/tincapp/service/TincVpnService.kt | 49 +++++++++++++ .../tincapp/service/VpnInterfaceConfigurator.java | 81 --------------------- .../tincapp/service/VpnInterfaceConfigurator.kt | 52 ++++++++++++++ .../tincapp/service/VpnServiceBuilderExtensions.kt | 45 ++++++++++++ .../java/org/pacien/tincapp/util/Function.java | 28 -------- 25 files changed, 545 insertions(+), 671 deletions(-) delete mode 100644 app/src/main/java/org/pacien/tincapp/activities/BaseActivity.java create mode 100644 app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt delete mode 100644 app/src/main/java/org/pacien/tincapp/activities/StartActivity.java create mode 100644 app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt delete mode 100644 app/src/main/java/org/pacien/tincapp/commands/Command.java create mode 100644 app/src/main/java/org/pacien/tincapp/commands/Command.kt delete mode 100644 app/src/main/java/org/pacien/tincapp/commands/Executor.java create mode 100644 app/src/main/java/org/pacien/tincapp/commands/Executor.kt delete mode 100644 app/src/main/java/org/pacien/tincapp/commands/PermissionFixer.java create mode 100644 app/src/main/java/org/pacien/tincapp/commands/PermissionFixer.kt delete mode 100644 app/src/main/java/org/pacien/tincapp/commands/Tinc.java create mode 100644 app/src/main/java/org/pacien/tincapp/commands/Tinc.kt delete mode 100644 app/src/main/java/org/pacien/tincapp/commands/Tincd.java create mode 100644 app/src/main/java/org/pacien/tincapp/commands/Tincd.kt delete mode 100644 app/src/main/java/org/pacien/tincapp/context/AppInfo.java create mode 100644 app/src/main/java/org/pacien/tincapp/context/AppInfo.kt delete mode 100644 app/src/main/java/org/pacien/tincapp/context/AppPaths.java create mode 100644 app/src/main/java/org/pacien/tincapp/context/AppPaths.kt delete mode 100644 app/src/main/java/org/pacien/tincapp/service/TincVpnService.java create mode 100644 app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt delete mode 100644 app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfigurator.java create mode 100644 app/src/main/java/org/pacien/tincapp/service/VpnInterfaceConfigurator.kt create mode 100644 app/src/main/java/org/pacien/tincapp/service/VpnServiceBuilderExtensions.kt delete mode 100644 app/src/main/java/org/pacien/tincapp/util/Function.java (limited to 'app') diff --git a/app/build.gradle b/app/build.gradle index 33514ea..840289d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,5 +1,6 @@ apply plugin: 'com.android.application' -apply plugin: 'me.tatarka.retrolambda' +apply plugin: 'kotlin-android' +apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 25 @@ -7,10 +8,9 @@ android { defaultConfig { applicationId "org.pacien.tincapp" minSdkVersion 21 - targetSdkVersion 25 + targetSdkVersion 21 versionCode 1 versionName "0.1-preview" - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { @@ -18,10 +18,6 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } - compileOptions { - sourceCompatibility JavaVersion.VERSION_1_8 - targetCompatibility JavaVersion.VERSION_1_8 - } externalNativeBuild { cmake { path "CMakeLists.txt" @@ -45,5 +41,8 @@ dependencies { exclude group: 'commons-logging', module: 'commons-logging' } - compile 'com.annimon:stream:1.1.5' + compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" +} +repositories { + mavenCentral() } diff --git a/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.java b/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.java deleted file mode 100644 index 0e6cb95..0000000 --- a/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.java +++ /dev/null @@ -1,71 +0,0 @@ -package org.pacien.tincapp.activities; - -import android.content.ClipData; -import android.content.ClipboardManager; -import android.content.Context; -import android.content.Intent; -import android.net.Uri; -import android.os.Bundle; -import android.support.annotation.StringRes; -import android.support.design.widget.Snackbar; -import android.support.v7.app.AlertDialog; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; -import android.view.Menu; -import android.view.MenuItem; -import android.view.ViewGroup; - -import org.pacien.tincapp.BuildConfig; -import org.pacien.tincapp.R; -import org.pacien.tincapp.context.AppInfo; - -/** - * @author pacien - */ -public abstract class BaseActivity extends AppCompatActivity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.base); - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); - setSupportActionBar(toolbar); - } - - @Override - public boolean onCreateOptionsMenu(Menu m) { - getMenuInflater().inflate(R.menu.menu_base, m); - return true; - } - - public void aboutDialog(MenuItem i) { - new AlertDialog.Builder(this) - .setTitle(BuildConfig.APPLICATION_ID) - .setMessage(getResources().getString(R.string.app_short_desc) + "\n\n" + - getResources().getString(R.string.app_copyright) + " " + - getResources().getString(R.string.app_license) + "\n\n" + - AppInfo.all(getResources())) - .setNeutralButton(R.string.action_open_project_website, (dialog, which) -> openWebsite(R.string.app_website_url)) - .setPositiveButton(R.string.action_close, (dialog, which) -> { /* nop */ }) - .show(); - } - - protected ViewGroup getContentView() { - return (ViewGroup) findViewById(R.id.main_content); - } - - protected void openWebsite(@StringRes int url) { - startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getResources().getString(url)))); - } - - protected void notify(@StringRes int msg) { - Snackbar.make(findViewById(R.id.activity_base), msg, Snackbar.LENGTH_LONG).show(); - } - - protected void copyIntoClipboard(String label, String str) { - ClipboardManager c = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); - c.setPrimaryClip(ClipData.newPlainText(label, str)); - notify(R.string.message_text_copied); - } - -} diff --git a/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt new file mode 100644 index 0000000..6070a0a --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt @@ -0,0 +1,62 @@ +package org.pacien.tincapp.activities + +import android.content.ClipData +import android.content.ClipboardManager +import android.content.Context +import android.content.Intent +import android.net.Uri +import android.os.Bundle +import android.support.annotation.StringRes +import android.support.design.widget.Snackbar +import android.support.v7.app.AlertDialog +import android.support.v7.app.AppCompatActivity +import android.view.Menu +import android.view.MenuItem +import kotlinx.android.synthetic.main.base.* +import org.pacien.tincapp.BuildConfig +import org.pacien.tincapp.R +import org.pacien.tincapp.context.AppInfo + +/** + * @author pacien + */ +abstract class BaseActivity : AppCompatActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.base) + setSupportActionBar(toolbar) + } + + override fun onCreateOptionsMenu(m: Menu): Boolean { + menuInflater.inflate(R.menu.menu_base, m) + return true + } + + fun aboutDialog(@Suppress("UNUSED_PARAMETER") i: MenuItem) { + AlertDialog.Builder(this) + .setTitle(BuildConfig.APPLICATION_ID) + .setMessage(resources.getString(R.string.app_short_desc) + "\n\n" + + resources.getString(R.string.app_copyright) + " " + + resources.getString(R.string.app_license) + "\n\n" + + AppInfo.all(resources)) + .setNeutralButton(R.string.action_open_project_website) { _, _ -> openWebsite(R.string.app_website_url) } + .setPositiveButton(R.string.action_close) { _, _ -> /* nop */ } + .show() + } + + protected fun openWebsite(@StringRes url: Int) { + startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(resources.getString(url)))) + } + + protected fun notify(@StringRes msg: Int) { + Snackbar.make(activity_base, msg, Snackbar.LENGTH_LONG).show() + } + + protected fun copyIntoClipboard(label: String, str: String) { + val c = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager + c.primaryClip = ClipData.newPlainText(label, str) + notify(R.string.message_text_copied) + } + +} diff --git a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.java b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.java deleted file mode 100644 index e469fa0..0000000 --- a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.java +++ /dev/null @@ -1,82 +0,0 @@ -package org.pacien.tincapp.activities; - -import android.annotation.SuppressLint; -import android.content.Intent; -import android.os.Bundle; -import android.support.v7.app.AlertDialog; -import android.view.View; -import android.view.ViewGroup; -import android.widget.EditText; -import android.widget.FrameLayout; - -import org.pacien.tincapp.R; -import org.pacien.tincapp.commands.PermissionFixer; -import org.pacien.tincapp.context.AppPaths; -import org.pacien.tincapp.service.TincVpnService; - -/** - * @author pacien - */ -public class StartActivity extends BaseActivity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getLayoutInflater().inflate(R.layout.page_start, getContentView()); - } - - @Override - protected void onActivityResult(int request, int result, Intent data) { - notify(result == RESULT_OK ? R.string.message_vpn_permissions_granted : R.string.message_vpn_permissions_denied); - } - - public void requestVpnPermission(View v) { - Intent askPermIntent = TincVpnService.prepare(this); - - if (askPermIntent != null) - startActivityForResult(askPermIntent, 0); - else - onActivityResult(0, RESULT_OK, null); - } - - public void startVpnDialog(View v) { - final EditText i = new EditText(this); - i.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)); - i.setHint(R.string.field_net_name); - - @SuppressLint("InflateParams") - ViewGroup vg = (ViewGroup) getLayoutInflater().inflate(R.layout.dialog_frame, null); - vg.addView(i); - - new AlertDialog.Builder(this) - .setTitle(R.string.title_connect_to_network) - .setView(vg) - .setPositiveButton(R.string.action_connect, (dialog, which) -> startVpn(i.getText().toString())) - .setNegativeButton(R.string.action_close, (dialog, which) -> { /* nop */ }) - .show(); - } - - public void confDirDialog(View v) { - String confDir = AppPaths.confDir(this).getPath(); - - new AlertDialog.Builder(this) - .setTitle(R.string.title_tinc_config_dir) - .setMessage(confDir) - .setNeutralButton(R.string.action_fix_perms, (dialog, which) -> fixPerms()) - .setNegativeButton(R.string.action_copy, - (dialog, which) -> copyIntoClipboard(getResources().getString(R.string.title_tinc_config_dir), confDir)) - .setPositiveButton(R.string.action_close, (dialog, which) -> { /* nop */ }) - .show(); - } - - private void startVpn(String netName) { - startService(new Intent(this, TincVpnService.class) - .putExtra(TincVpnService.INTENT_EXTRA_NET_NAME, netName)); - } - - private void fixPerms() { - boolean ok = PermissionFixer.makePrivateDirsPublic(getApplicationContext()); - notify(ok ? R.string.message_perms_fixed : R.string.message_perms_fix_failure); - } - -} diff --git a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt new file mode 100644 index 0000000..6d39a9d --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt @@ -0,0 +1,82 @@ +package org.pacien.tincapp.activities + +import android.annotation.SuppressLint +import android.app.Activity +import android.content.Intent +import android.net.VpnService +import android.os.Bundle +import android.support.v7.app.AlertDialog +import android.view.View +import android.view.ViewGroup +import android.widget.EditText +import android.widget.FrameLayout +import kotlinx.android.synthetic.main.base.* + +import org.pacien.tincapp.R +import org.pacien.tincapp.commands.PermissionFixer +import org.pacien.tincapp.context.AppPaths +import org.pacien.tincapp.service.TincVpnService + +/** + * @author pacien + */ +class StartActivity : BaseActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + layoutInflater.inflate(R.layout.page_start, main_content) + } + + override fun onActivityResult(request: Int, result: Int, data: Intent?) { + notify(if (result == Activity.RESULT_OK) R.string.message_vpn_permissions_granted else R.string.message_vpn_permissions_denied) + } + + fun requestVpnPermission(@Suppress("UNUSED_PARAMETER") v: View) { + val askPermIntent = VpnService.prepare(this) + + if (askPermIntent != null) + startActivityForResult(askPermIntent, 0) + else + onActivityResult(0, Activity.RESULT_OK, null) + } + + fun startVpnDialog(@Suppress("UNUSED_PARAMETER") v: View) { + val i = EditText(this) + i.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT) + i.setHint(R.string.field_net_name) + + @SuppressLint("InflateParams") + val vg = layoutInflater.inflate(R.layout.dialog_frame, null) as ViewGroup + vg.addView(i) + + AlertDialog.Builder(this) + .setTitle(R.string.title_connect_to_network) + .setView(vg) + .setPositiveButton(R.string.action_connect) { _, _ -> startVpn(i.text.toString()) } + .setNegativeButton(R.string.action_close) { _, _ -> /* nop */ } + .show() + } + + fun confDirDialog(@Suppress("UNUSED_PARAMETER") v: View) { + val confDir = AppPaths.confDir(this).path + + AlertDialog.Builder(this) + .setTitle(R.string.title_tinc_config_dir) + .setMessage(confDir) + .setNeutralButton(R.string.action_fix_perms) { _, _ -> fixPerms() } + .setNegativeButton(R.string.action_copy) { _, _ -> copyIntoClipboard(resources.getString(R.string.title_tinc_config_dir), confDir) } + .setPositiveButton(R.string.action_close) { _, _ -> /* nop */ } + .show() + } + + private fun startVpn(netName: String) { + startService(Intent(this, TincVpnService::class.java) + .putExtra(TincVpnService.INTENT_EXTRA_NET_NAME, netName)) + } + + private fun fixPerms() { + val ok = PermissionFixer.makePrivateDirsPublic(applicationContext) + notify(if (ok) R.string.message_perms_fixed else R.string.message_perms_fix_failure) + } + +} diff --git a/app/src/main/java/org/pacien/tincapp/commands/Command.java b/app/src/main/java/org/pacien/tincapp/commands/Command.java deleted file mode 100644 index 28ff226..0000000 --- a/app/src/main/java/org/pacien/tincapp/commands/Command.java +++ /dev/null @@ -1,67 +0,0 @@ -package org.pacien.tincapp.commands; - -import com.annimon.stream.Collectors; -import com.annimon.stream.Optional; -import com.annimon.stream.Stream; - -import java.util.Arrays; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; - -/** - * @author pacien - */ -class Command { - - static private class Option { - final String key; - final Optional val; - - Option(String key, String val) { - this.key = key; - this.val = Optional.ofNullable(val); - } - - @Override - public String toString() { - return val.isPresent() ? "--" + key + "=" + val.get() : "--" + key; - } - } - - final private String cmd; - final private List