Skip to content

Commit

Permalink
SecureTime wrapper for RoughtimeClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
tajchert committed Jun 22, 2019
1 parent 8a600a1 commit f5a6e7d
Show file tree
Hide file tree
Showing 63 changed files with 4,295 additions and 39 deletions.
12 changes: 0 additions & 12 deletions app/src/main/java/pl/tajchert/securetime/MainActivity.kt

This file was deleted.

19 changes: 0 additions & 19 deletions app/src/main/res/layout/activity_main.xml

This file was deleted.

4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
repositories {
google()
jcenter()

}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0-beta04'
Expand All @@ -19,7 +19,7 @@ allprojects {
repositories {
google()
jcenter()

}
}

Expand Down
File renamed without changes.
21 changes: 17 additions & 4 deletions app/build.gradle → sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
compileSdkVersion 29
buildToolsVersion "29.0.0"
defaultConfig {
applicationId "pl.tajchert.securetime"
minSdkVersion 21
applicationId "pl.tajchert.securetime.sample"
minSdkVersion 26
targetSdkVersion 29
versionCode 1
versionName "1.0"
Expand All @@ -21,14 +21,27 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation project(':securetime')

implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.10'

implementation 'com.jakewharton.timber:timber:4.7.1'

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.core:core-ktx:1.0.2'

implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pl.tajchert.securetime">
package="pl.tajchert.securetime.sample">

<application
android:allowBackup="true"
Expand Down
84 changes: 84 additions & 0 deletions sample/src/main/java/pl/tajchert/securetime/sample/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package pl.tajchert.securetime.sample

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_main.*
import pl.tajchert.securetime.SecureTime
import timber.log.Timber
import java.util.concurrent.TimeUnit

class MainActivity : AppCompatActivity() {

val disposable = CompositeDisposable()

private var roughtimeHelper: SecureTime? = SecureTime(
host = "roughtime.cloudflare.com",
port = 2002,
serverPubKeyBase64 = "gD63hSj3ScS+wuOeGrubXlq35N1c5Lby/S+T7MNTjxo="
)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
buttonRefresh.setOnClickListener {
getCurrentTime()
}
}

override fun onResume() {
super.onResume()
getCurrentTime()
}

@SuppressLint("CheckResult")
private fun getCurrentTime() {
disposable.clear()

disposable.add(Observable.fromCallable {
if (roughtimeHelper == null) {
Timber.e("Error while initializing SecureTime - most likely serverPubKey")
throw RuntimeException("SecureTime is not initialized")
} else {
roughtimeHelper!!.getTime()
}
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe {
//TODO show processing indicator
Timber.d("Starting fetching time")

timeServer.text = "Server midpoint time\nX"
timeServerRadius.text = "Uncertainty radius X ms"
timeDiffMilliseconds.text = "Time diff X ms"
timeDiffSeconds.text = "Time diff X s"
}.doOnComplete {
//TODO hide pricessing indicator
Timber.d("Fetched completed")
}
.subscribe(
{ result ->
val midpoint = result.first
val radiousOfUncertanity = result.second
val localTimeDiffMilliseconds = result.third
val localTimeDiffSeconds = TimeUnit.MILLISECONDS.toSeconds(result.third)
//Use result for something
Timber.d("Result from server, midpoint: $midpoint, radius(ms): $radiousOfUncertanity, localDiff (ms): $localTimeDiffMilliseconds")
timeServer.text = "Server midpoint time:\n$midpoint"
timeServerRadius.text = "Uncertainty radius $radiousOfUncertanity ms"
timeDiffMilliseconds.text = "Time diff $localTimeDiffMilliseconds ms"
timeDiffSeconds.text = "Time diff $localTimeDiffSeconds s"
},
{
Timber.e(it)
Toast.makeText(MainActivity@ this, "Error: ${it.localizedMessage}", Toast.LENGTH_SHORT).show()
})
)
}
}
55 changes: 55 additions & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/timeServer"
android:text="Server midpoint time:\nX"
android:gravity="center"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/timeServerRadius"
android:text="Uncertainty radius X"
app:layout_constraintTop_toBottomOf="@+id/timeServer"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/timeDiffMilliseconds"
android:text="Time diff X ms"
app:layout_constraintTop_toBottomOf="@+id/timeServerRadius"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/timeDiffSeconds"
android:text="Time diff X s"
app:layout_constraintTop_toBottomOf="@+id/timeDiffMilliseconds"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>

<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonRefresh"
android:text="Refresh"
app:layout_constraintTop_toBottomOf="@+id/timeDiffSeconds"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions securetime/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
49 changes: 49 additions & 0 deletions securetime/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.0"


defaultConfig {
minSdkVersion 26
targetSdkVersion 29
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'io.netty:netty-all:4.1.36.Final'
implementation 'net.i2p.crypto:eddsa:0.3.0'

implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.10'

implementation 'com.jakewharton.timber:timber:4.7.1'

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.core:core-ktx:1.0.2'

testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Empty file added securetime/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions securetime/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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pl.tajchert.securetime

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("pl.tajchert.securetime.test", appContext.packageName)
}
}
8 changes: 8 additions & 0 deletions securetime/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pl.tajchert.securetime">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

</manifest>
Loading

0 comments on commit f5a6e7d

Please sign in to comment.