Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit ea3d7ef

Browse files
authored
Merge pull request #8 from plgd-dev/androidsupport
support android
2 parents 07a4894 + 1df58fa commit ea3d7ef

37 files changed

Lines changed: 258 additions & 166 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,5 @@ app.*.symbols
115115

116116
# OCF Client
117117
**/ios/Frameworks/OCFClient.framework/*
118+
**/android/app/libs/*.jar
119+
**/android/app/libs/*.aar

android/app/build.gradle

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ apply plugin: 'com.android.application'
2525
apply plugin: 'kotlin-android'
2626
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
2727

28+
def keystoreProperties = new Properties()
29+
def keystorePropertiesFile = rootProject.file('key.properties')
30+
if (keystorePropertiesFile.exists()) {
31+
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
32+
}
33+
2834
android {
2935
compileSdkVersion 29
3036

@@ -37,7 +43,6 @@ android {
3743
}
3844

3945
defaultConfig {
40-
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
4146
applicationId "dev.plgd.client"
4247
minSdkVersion 16
4348
targetSdkVersion 29
@@ -46,11 +51,18 @@ android {
4651
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
4752
}
4853

54+
signingConfigs {
55+
release {
56+
keyAlias keystoreProperties['keyAlias']
57+
keyPassword keystoreProperties['keyPassword']
58+
storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
59+
storePassword keystoreProperties['storePassword']
60+
}
61+
}
62+
4963
buildTypes {
5064
release {
51-
// TODO: Add your own signing config for the release build.
52-
// Signing with the debug keys for now, so `flutter run --release` works.
53-
signingConfig signingConfigs.debug
65+
signingConfig signingConfigs.release
5466
}
5567
}
5668
}
@@ -68,6 +80,7 @@ repositories {
6880
dependencies {
6981
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
7082
implementation (name:'ocfclient', ext:'aar')
83+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1'
7184
testImplementation 'junit:junit:4.12'
7285
androidTestImplementation 'androidx.test:runner:1.1.1'
7386
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
22
package="dev.plgd.client">
3-
<!-- Flutter needs it to communicate with the running application
4-
to allow setting breakpoints, to provide hot reload, etc.
5-
-->
63
<uses-permission android:name="android.permission.INTERNET"/>
74
</manifest>

android/app/src/main/AndroidManifest.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
22
package="dev.plgd.client">
3-
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
4-
calls FlutterMain.startInitialization(this); in its onCreate method.
5-
In most cases you can leave this as-is, but you if you want to provide
6-
additional functionality it is fine to subclass or reimplement
7-
FlutterApplication and put your custom class here. -->
83
<application
94
android:name="io.flutter.app.FlutterApplication"
105
android:label="plgd"
@@ -21,8 +16,6 @@
2116
<category android:name="android.intent.category.LAUNCHER"/>
2217
</intent-filter>
2318
</activity>
24-
<!-- Don't delete the meta-data below.
25-
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
2619
<meta-data
2720
android:name="flutterEmbedding"
2821
android:value="2" />

android/app/src/main/kotlin/com/example/my_app/MainActivity.kt

Lines changed: 0 additions & 76 deletions
This file was deleted.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package dev.plgd.client
2+
3+
import androidx.annotation.NonNull
4+
import io.flutter.embedding.android.FlutterActivity
5+
import io.flutter.embedding.engine.FlutterEngine
6+
import io.flutter.plugin.common.MethodCall
7+
import io.flutter.plugins.GeneratedPluginRegistrant
8+
import io.flutter.plugin.common.MethodChannel
9+
import kotlinx.coroutines.CoroutineScope
10+
import kotlinx.coroutines.Dispatchers
11+
import kotlinx.coroutines.launch
12+
import kotlinx.coroutines.withContext
13+
14+
class MainActivity: FlutterActivity() {
15+
private val CHANNEL = "plgd.dev/client"
16+
private var sdkClient: ocfclient.Ocfclient_? = null;
17+
private val _mainScope = CoroutineScope(Dispatchers.Main)
18+
19+
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
20+
GeneratedPluginRegistrant.registerWith(flutterEngine)
21+
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
22+
when (call.method) {
23+
"initialize" -> this.initializeOCFClient(call, result)
24+
"discoverDevices" -> this.discoverDevices(call, result)
25+
"ownDevice" -> this.ownDevice(call, result)
26+
"setAccessForCloud" -> this.setAccessForCloud(call, result)
27+
"onboardDevice" -> this.onboardDevice(call, result)
28+
"disownDevice" -> this.disownDevice(call, result)
29+
else -> result.notImplemented()
30+
}
31+
}
32+
}
33+
34+
private fun initializeOCFClient(call: MethodCall, result: MethodChannel.Result) {
35+
if (sdkClient != null) {
36+
result.success(true)
37+
return
38+
}
39+
40+
var accessToken = call.argument<String>("accessToken");
41+
var cloudConfiguration = call.argument<String>("cloudConfiguration")
42+
_mainScope.launch {
43+
try {
44+
sdkClient = ocfclient.Ocfclient_()
45+
withContext(Dispatchers.IO) {
46+
sdkClient!!.initialize(accessToken, cloudConfiguration)
47+
}
48+
result.success(true)
49+
} catch (e: Exception) {
50+
sdkClient = null
51+
result.error("-1", e.message, "")
52+
}
53+
}
54+
}
55+
56+
private fun discoverDevices(call: MethodCall, result: MethodChannel.Result) {
57+
var discoveryTimeout = call.arguments<Int>()
58+
_mainScope.launch {
59+
try {
60+
var devices = withContext(Dispatchers.IO) {
61+
sdkClient!!.discover(discoveryTimeout.toLong())
62+
}
63+
result.success(devices)
64+
} catch (e: Exception) {
65+
result.error("-1", e.message, "")
66+
}
67+
}
68+
}
69+
70+
private fun ownDevice(call: MethodCall, result: MethodChannel.Result) {
71+
var deviceId = call.argument<String>("deviceID")
72+
var accessToken = call.argument<String>("accessToken")
73+
_mainScope.launch {
74+
try {
75+
var newDeviceId = withContext(Dispatchers.IO) {
76+
sdkClient!!.ownDevice(deviceId, accessToken)
77+
}
78+
result.success(newDeviceId)
79+
} catch (e: Exception) {
80+
result.error("-1", e.message, "")
81+
}
82+
}
83+
}
84+
85+
private fun setAccessForCloud(call: MethodCall, result: MethodChannel.Result) {
86+
var deviceId = call.argument<String>("deviceID")
87+
_mainScope.launch {
88+
try {
89+
withContext(Dispatchers.IO) {
90+
sdkClient!!.setAccessForCloud(deviceId)
91+
}
92+
result.success(true)
93+
} catch (e: Exception) {
94+
result.error("-1", e.message, "")
95+
}
96+
}
97+
}
98+
99+
private fun onboardDevice(call: MethodCall, result: MethodChannel.Result) {
100+
var deviceId = call.argument<String>("deviceID")
101+
var authCode = call.argument<String>("authCode")
102+
_mainScope.launch {
103+
try {
104+
withContext(Dispatchers.IO) {
105+
sdkClient!!.onboardDevice(deviceId, authCode)
106+
}
107+
result.success(true)
108+
} catch (e: Exception) {
109+
result.error("-1", e.message, "")
110+
}
111+
}
112+
}
113+
114+
private fun disownDevice(call: MethodCall, result: MethodChannel.Result) {
115+
var deviceId = call.argument<String>("deviceID")
116+
_mainScope.launch {
117+
try {
118+
withContext(Dispatchers.IO) {
119+
sdkClient!!.disownDevice(deviceId)
120+
}
121+
result.success(true)
122+
} catch (e: Exception) {
123+
result.error("-1", e.message, "")
124+
}
125+
}
126+
}
127+
}
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<!-- Modify this file to customize your launch splash screen -->
32
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
43
<item android:drawable="@android:color/white" />
5-
6-
<!-- You can insert your own image assets here -->
7-
<!-- <item>
4+
<item>
85
<bitmap
96
android:gravity="center"
10-
android:src="@mipmap/launch_image" />
11-
</item> -->
7+
android:src="@mipmap/launch_logo" />
8+
</item>
129
</layer-list>
File renamed without changes.
6.4 KB
Loading
File renamed without changes.

0 commit comments

Comments
 (0)