forked from nullxception/boorusphere
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use pigeon to generate platform channels
- Loading branch information
1 parent
3e74911
commit 65d1792
Showing
9 changed files
with
227 additions
and
23 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
android/app/src/main/kotlin/io/chaldeaprjkt/boorusphere/AndroidStorageUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.chaldeaprjkt.boorusphere | ||
|
||
import StorageUtil | ||
import android.os.Environment | ||
|
||
class AndroidStorageUtil : StorageUtil { | ||
override fun getStoragePath(): String { | ||
val file = Environment.getExternalStorageDirectory() | ||
return file.absolutePath | ||
} | ||
|
||
override fun getDownloadPath(): String { | ||
val file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) | ||
return file.absolutePath | ||
} | ||
} |
23 changes: 4 additions & 19 deletions
23
android/app/src/main/kotlin/io/chaldeaprjkt/boorusphere/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,13 @@ | ||
package io.chaldeaprjkt.boorusphere | ||
|
||
import android.os.Environment | ||
import androidx.annotation.NonNull | ||
import StorageUtil | ||
import io.flutter.embedding.android.FlutterActivity | ||
import io.flutter.embedding.engine.FlutterEngine | ||
import io.flutter.plugin.common.MethodChannel | ||
|
||
class MainActivity : FlutterActivity() { | ||
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { | ||
override fun configureFlutterEngine(flutterEngine: FlutterEngine) { | ||
super.configureFlutterEngine(flutterEngine) | ||
val channelPath = "${BuildConfig.APPLICATION_ID}/path" | ||
MethodChannel( | ||
flutterEngine.dartExecutor.binaryMessenger, | ||
channelPath | ||
).setMethodCallHandler { call, result -> | ||
if (call.method == "getDownload") { | ||
result.success(downloadPath.absolutePath) | ||
} else { | ||
result.notImplemented() | ||
} | ||
} | ||
} | ||
|
||
@Suppress("DEPRECATION") | ||
private val downloadPath get() = | ||
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) | ||
StorageUtil.setUp(flutterEngine.dartExecutor.binaryMessenger, AndroidStorageUtil()) | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
android/app/src/main/kotlin/io/chaldeaprjkt/boorusphere/pigeon/StorageUtil.pi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Autogenerated from Pigeon (v9.2.5), do not edit directly. | ||
// See also: https://pub.dev/packages/pigeon | ||
|
||
|
||
import android.util.Log | ||
import io.flutter.plugin.common.BasicMessageChannel | ||
import io.flutter.plugin.common.BinaryMessenger | ||
import io.flutter.plugin.common.MessageCodec | ||
import io.flutter.plugin.common.StandardMessageCodec | ||
import java.io.ByteArrayOutputStream | ||
import java.nio.ByteBuffer | ||
|
||
private fun wrapResult(result: Any?): List<Any?> { | ||
return listOf(result) | ||
} | ||
|
||
private fun wrapError(exception: Throwable): List<Any?> { | ||
if (exception is FlutterError) { | ||
return listOf( | ||
exception.code, | ||
exception.message, | ||
exception.details | ||
) | ||
} else { | ||
return listOf( | ||
exception.javaClass.simpleName, | ||
exception.toString(), | ||
"Cause: " + exception.cause + ", Stacktrace: " + Log.getStackTraceString(exception) | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* Error class for passing custom error details to Flutter via a thrown PlatformException. | ||
* @property code The error code. | ||
* @property message The error message. | ||
* @property details The error details. Must be a datatype supported by the api codec. | ||
*/ | ||
class FlutterError ( | ||
val code: String, | ||
override val message: String? = null, | ||
val details: Any? = null | ||
) : Throwable() | ||
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */ | ||
interface StorageUtil { | ||
fun getStoragePath(): String | ||
fun getDownloadPath(): String | ||
|
||
companion object { | ||
/** The codec used by StorageUtil. */ | ||
val codec: MessageCodec<Any?> by lazy { | ||
StandardMessageCodec() | ||
} | ||
/** Sets up an instance of `StorageUtil` to handle messages through the `binaryMessenger`. */ | ||
@Suppress("UNCHECKED_CAST") | ||
fun setUp(binaryMessenger: BinaryMessenger, api: StorageUtil?) { | ||
run { | ||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.StorageUtil.getStoragePath", codec) | ||
if (api != null) { | ||
channel.setMessageHandler { _, reply -> | ||
var wrapped: List<Any?> | ||
try { | ||
wrapped = listOf<Any?>(api.getStoragePath()) | ||
} catch (exception: Throwable) { | ||
wrapped = wrapError(exception) | ||
} | ||
reply.reply(wrapped) | ||
} | ||
} else { | ||
channel.setMessageHandler(null) | ||
} | ||
} | ||
run { | ||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.StorageUtil.getDownloadPath", codec) | ||
if (api != null) { | ||
channel.setMessageHandler { _, reply -> | ||
var wrapped: List<Any?> | ||
try { | ||
wrapped = listOf<Any?>(api.getDownloadPath()) | ||
} catch (exception: Throwable) { | ||
wrapped = wrapError(exception) | ||
} | ||
reply.reply(wrapped) | ||
} | ||
} else { | ||
channel.setMessageHandler(null) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Autogenerated from Pigeon (v9.2.5), do not edit directly. | ||
// See also: https://pub.dev/packages/pigeon | ||
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import | ||
|
||
import 'dart:async'; | ||
import 'dart:typed_data' show Float64List, Int32List, Int64List, Uint8List; | ||
|
||
import 'package:flutter/foundation.dart' show ReadBuffer, WriteBuffer; | ||
import 'package:flutter/services.dart'; | ||
|
||
class StorageUtil { | ||
/// Constructor for [StorageUtil]. The [binaryMessenger] named argument is | ||
/// available for dependency injection. If it is left null, the default | ||
/// BinaryMessenger will be used which routes to the host platform. | ||
StorageUtil({BinaryMessenger? binaryMessenger}) | ||
: _binaryMessenger = binaryMessenger; | ||
final BinaryMessenger? _binaryMessenger; | ||
|
||
static const MessageCodec<Object?> codec = StandardMessageCodec(); | ||
|
||
Future<String> getStoragePath() async { | ||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>( | ||
'dev.flutter.pigeon.StorageUtil.getStoragePath', codec, | ||
binaryMessenger: _binaryMessenger); | ||
final List<Object?>? replyList = | ||
await channel.send(null) as List<Object?>?; | ||
if (replyList == null) { | ||
throw PlatformException( | ||
code: 'channel-error', | ||
message: 'Unable to establish connection on channel.', | ||
); | ||
} else if (replyList.length > 1) { | ||
throw PlatformException( | ||
code: replyList[0]! as String, | ||
message: replyList[1] as String?, | ||
details: replyList[2], | ||
); | ||
} else if (replyList[0] == null) { | ||
throw PlatformException( | ||
code: 'null-error', | ||
message: 'Host platform returned null value for non-null return value.', | ||
); | ||
} else { | ||
return (replyList[0] as String?)!; | ||
} | ||
} | ||
|
||
Future<String> getDownloadPath() async { | ||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>( | ||
'dev.flutter.pigeon.StorageUtil.getDownloadPath', codec, | ||
binaryMessenger: _binaryMessenger); | ||
final List<Object?>? replyList = | ||
await channel.send(null) as List<Object?>?; | ||
if (replyList == null) { | ||
throw PlatformException( | ||
code: 'channel-error', | ||
message: 'Unable to establish connection on channel.', | ||
); | ||
} else if (replyList.length > 1) { | ||
throw PlatformException( | ||
code: replyList[0]! as String, | ||
message: replyList[1] as String?, | ||
details: replyList[2], | ||
); | ||
} else if (replyList[0] == null) { | ||
throw PlatformException( | ||
code: 'null-error', | ||
message: 'Host platform returned null value for non-null return value.', | ||
); | ||
} else { | ||
return (replyList[0] as String?)!; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import 'package:pigeon/pigeon.dart'; | ||
|
||
@ConfigurePigeon( | ||
PigeonOptions( | ||
dartOut: 'lib/pigeon/storage_util.pi.dart', | ||
kotlinOut: | ||
'android/app/src/main/kotlin/io/chaldeaprjkt/boorusphere/pigeon/StorageUtil.pi.kt', | ||
kotlinOptions: KotlinOptions(errorClassName: 'StorageUtilException'), | ||
), | ||
) | ||
@HostApi() | ||
abstract class StorageUtil { | ||
String getStoragePath(); | ||
String getDownloadPath(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters