Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/androidx no dependence #606

Merged
merged 4 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 8 additions & 10 deletions kraken/android/src/main/java/com/openkraken/kraken/Kraken.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public String getUrl() {
public void _handleMethodCall(MethodCall call, MethodChannel.Result result) {
if (this.handler != null) {
this.handler.onMethodCall(call, result);
} else {
result.error("No handler found.", null, null);
}
}

Expand All @@ -59,11 +61,9 @@ public void invokeMethod(final String method, final Object arguments) {
public void run() {
if (flutterEngine != null) {
PluginRegistry pluginRegistry = flutterEngine.getPlugins();
if (pluginRegistry != null) {
KrakenPlugin krakenSDKPlugin = (KrakenPlugin) pluginRegistry.get(KrakenPlugin.class);
if (krakenSDKPlugin != null && krakenSDKPlugin.channel != null) {
krakenSDKPlugin.channel.invokeMethod(method, arguments);
}
KrakenPlugin krakenSDKPlugin = (KrakenPlugin) pluginRegistry.get(KrakenPlugin.class);
if (krakenSDKPlugin != null && krakenSDKPlugin.channel != null) {
krakenSDKPlugin.channel.invokeMethod(method, arguments);
}
}
}
Expand All @@ -73,11 +73,9 @@ public void run() {
public void reload() {
if (flutterEngine != null) {
PluginRegistry pluginRegistry = flutterEngine.getPlugins();
if (pluginRegistry != null) {
KrakenPlugin krakenSDKPlugin = (KrakenPlugin) pluginRegistry.get(KrakenPlugin.class);
if (krakenSDKPlugin != null) {
krakenSDKPlugin.reload();
}
KrakenPlugin krakenSDKPlugin = (KrakenPlugin) pluginRegistry.get(KrakenPlugin.class);
if (krakenSDKPlugin != null) {
krakenSDKPlugin.reload();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.openkraken.kraken;

import androidx.annotation.NonNull;

import android.content.Context;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
Expand All @@ -11,7 +10,7 @@
import io.flutter.plugin.common.PluginRegistry.Registrar;

/**
* KrakenBundlePlugin
* KrakenPlugin
*/
public class KrakenPlugin implements FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android
Expand All @@ -20,6 +19,8 @@ public class KrakenPlugin implements FlutterPlugin, MethodCallHandler {
/// when the Flutter Engine is detached from the Activity
public MethodChannel channel;
private FlutterEngine flutterEngine;
private Context mContext;
private Kraken mKraken;

// This static function is optional and equivalent to onAttachedToEngine. It supports the old
// pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
Expand All @@ -33,11 +34,13 @@ public class KrakenPlugin implements FlutterPlugin, MethodCallHandler {
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "kraken");
KrakenPlugin plugin = new KrakenPlugin();
plugin.mContext = registrar.context();
channel.setMethodCallHandler(plugin);
}

@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
public void onAttachedToEngine(FlutterPluginBinding flutterPluginBinding) {
mContext = flutterPluginBinding.getApplicationContext();
channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "kraken");
flutterEngine = flutterPluginBinding.getFlutterEngine();
channel.setMethodCallHandler(this);
Expand All @@ -49,33 +52,55 @@ public void reload() {
}
}

@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
Kraken kraken = Kraken.get(flutterEngine);
if (kraken == null) {
result.notImplemented();
return;
}
Kraken getKraken() {
if (mKraken == null) {
mKraken = Kraken.get(flutterEngine);
}
return mKraken;
}

if (call.method.equals("getUrl")) {
@Override
public void onMethodCall(MethodCall call, Result result) {
switch (call.method) {
case "getUrl": {
Kraken kraken = getKraken();
result.success(kraken == null ? "" : kraken.getUrl());
} else if (call.method.equals("invokeMethod")) {
String method = call.argument("method");
Object args = call.argument("args");
assert method != null;
MethodCall callWrap = new MethodCall(method, args);
kraken._handleMethodCall(callWrap, result);
} else {
break;
}

case "invokeMethod": {
Kraken kraken = getKraken();
if (kraken != null) {
String method = call.argument("method");
Object args = call.argument("args");
assert method != null;
MethodCall callWrap = new MethodCall(method, args);
kraken._handleMethodCall(callWrap, result);
} else {
result.error("Kraken instance not found.", null, null);
}
break;
}

case "getTemporaryDirectory":
result.success(getTemporaryDirectory());
break;

default:
result.notImplemented();
}
}

@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
public void onDetachedFromEngine(FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
Kraken kraken = Kraken.get(flutterEngine);
if (kraken == null) return;
kraken.destroy();
flutterEngine = null;
}

private String getTemporaryDirectory() {
return mContext.getCacheDir().getPath() + "/Kraken";
}
}
2 changes: 0 additions & 2 deletions kraken/example/macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import jsc
import kraken
import kraken_devtools
import kraken_websocket
import path_provider_macos
import shared_preferences_macos

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
Expand All @@ -19,6 +18,5 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
KrakenPlugin.register(with: registry.registrar(forPlugin: "KrakenPlugin"))
KrakenDevtoolsPlugin.register(with: registry.registrar(forPlugin: "KrakenDevtoolsPlugin"))
KrakenWebsocketPlugin.register(with: registry.registrar(forPlugin: "KrakenWebsocketPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
}
2 changes: 0 additions & 2 deletions kraken/example/macos/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@
"${BUILT_PRODUCTS_DIR}/kraken_devtools/kraken_devtools.framework",
"${PODS_ROOT}/../Flutter/ephemeral/.symlinks/plugins/kraken_websocket/macos/libkraken_websocket_jsc.dylib",
"${BUILT_PRODUCTS_DIR}/kraken_websocket/kraken_websocket.framework",
"${BUILT_PRODUCTS_DIR}/path_provider_macos/path_provider_macos.framework",
"${BUILT_PRODUCTS_DIR}/shared_preferences_macos/shared_preferences_macos.framework",
);
name = "[CP] Embed Pods Frameworks";
Expand All @@ -286,7 +285,6 @@
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/kraken_devtools.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/libkraken_websocket_jsc.dylib",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/kraken_websocket.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/path_provider_macos.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/shared_preferences_macos.framework",
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
7 changes: 7 additions & 0 deletions kraken/ios/Classes/KrakenPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,16 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
Kraken* krakenInstance = [Kraken instanceByBinaryMessenger: [self.registrar messenger]];
FlutterMethodCall* callWrap = [FlutterMethodCall methodCallWithMethodName: call.arguments[@"method"] arguments: call.arguments[@"args"]];
[krakenInstance _handleMethodCall:callWrap result:result];
} else if ([@"getTemporaryDirectory" isEqualToString: call.method]) {
result([self getTemporaryDirectory]);
} else {
result(FlutterMethodNotImplemented);
}

- (NSString*) getTemporaryDirectory {
NSArray<NSString *>* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
return paths.firstObject;
}
}

@end
18 changes: 13 additions & 5 deletions kraken/lib/src/foundation/environment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@
* Author: Kraken Team.
*/

import 'dart:io';

import 'package:path_provider/path_provider.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

String? _krakenTemporaryPath;
Future<String> getKrakenTemporaryPath() async {
if (_krakenTemporaryPath == null) {
Directory temporaryDirectory = await getTemporaryDirectory();
_krakenTemporaryPath = temporaryDirectory.path + '/Kraken';
String? temporaryDirectory = await getKrakenMethodChannel()
.invokeMethod<String>('getTemporaryDirectory');
if (temporaryDirectory == null) {
throw FlutterError('Can\'t get temporary directory from native side.');
}
_krakenTemporaryPath = temporaryDirectory;
}
return _krakenTemporaryPath!;
}

MethodChannel _methodChannel = const MethodChannel('kraken');
MethodChannel getKrakenMethodChannel() {
return _methodChannel;
}
3 changes: 3 additions & 0 deletions kraken/lib/src/launcher/bundle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ abstract class KrakenBundle {
static Future<KrakenBundle> getBundle(String path, { String? contentOverride, required int contextId }) async {
KrakenBundle bundle;

if (kDebugMode) {
print('Kraken getting bundle for contextId: $contextId, path: $path');
}
if (contentOverride != null && contentOverride.isNotEmpty) {
bundle = RawBundle(contentOverride, Uri.parse(path));
} else {
Expand Down
6 changes: 4 additions & 2 deletions kraken/lib/src/module/method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter/foundation.dart';
import 'package:kraken/kraken.dart';
import 'package:kraken/src/module/module_manager.dart';

import 'module_manager.dart';

typedef MethodCallCallback = Future<dynamic> Function(String method, dynamic arguments);
const String METHOD_CHANNEL_NOT_INITIALIZED = 'MethodChannel not initialized.';
Expand Down Expand Up @@ -92,7 +93,8 @@ class KrakenJavaScriptChannel extends KrakenMethodChannel {
class KrakenNativeChannel extends KrakenMethodChannel {
// Flutter method channel used to communicate with public SDK API
// Only works when integration wieh public SDK API
static final MethodChannel _nativeChannel = MethodChannel('kraken')

static final MethodChannel _nativeChannel = getKrakenMethodChannel()
..setMethodCallHandler((call) async {
String method = call.method;
KrakenController? controller = KrakenController.getControllerOfJSContextId(0);
Expand Down
3 changes: 1 addition & 2 deletions kraken/lib/src/module/navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ class NavigatorModule extends BaseModule {
NavigatorModule(ModuleManager moduleManager) : super(moduleManager);

@override
void dispose() {
}
void dispose() {}

@override
String invoke(String method, dynamic params, callback) {
Expand Down
7 changes: 7 additions & 0 deletions kraken/macos/Classes/KrakenPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,16 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
Kraken* krakenInstance = [Kraken instanceByBinaryMessenger: [self.registrar messenger]];
FlutterMethodCall* callWrap = [FlutterMethodCall methodCallWithMethodName: call.arguments[@"method"] arguments: call.arguments[@"args"]];
[krakenInstance _handleMethodCall:callWrap result:result];
} else if ([@"getTemporaryDirectory" isEqualToString: call.method]) {
result([self getTemporaryDirectory]);
} else {
result(FlutterMethodNotImplemented);
}
}

- (NSString*) getTemporaryDirectory {
NSArray<NSString *>* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
return paths.firstObject;
}

@end
18 changes: 8 additions & 10 deletions kraken/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ environment:
dependencies:
flutter:
sdk: flutter
path: ^1.8.0
meta: ^1.3.0
ffi: ^1.0.0
connectivity: ^3.0.4
shared_preferences: ^2.0.5
device_info: ^2.0.1 # Only support iOS and Android.
path_provider: ^2.0.2
vector_math: ^2.1.0
characters: ^1.1.0
vibration: ^1.7.4-nullsafety.0
path: ^1.8.0 # Pure dart module.
meta: ^1.3.0 # Pure dart module.
ffi: ^1.0.0 # Pure dart module.
connectivity: ^3.0.6 # No AndroidX used.
shared_preferences: ^2.0.6 # No AndroidX used.
device_info: ^2.0.1 # Only support iOS and Android, no AndroidX used.
vector_math: ^2.1.0 # Pure dart module.
vibration: ^1.7.4-nullsafety.0 # No AndroidX used, but can be optional.

dev_dependencies:
flutter_test:
Expand Down
9 changes: 7 additions & 2 deletions kraken/test/kraken_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';
import 'package:flutter/widgets.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:kraken/foundation.dart';

import 'foundation.dart' as foundation;
import 'local_http_server.dart';
Expand All @@ -20,8 +21,12 @@ void main() {

// Work around with path_provider.
Directory tempDirectory = Directory('./temp');
const MethodChannel channel = MethodChannel('plugins.flutter.io/path_provider');
channel.setMockMethodCallHandler((MethodCall methodCall) async => tempDirectory.path);
getKrakenMethodChannel().setMockMethodCallHandler((MethodCall methodCall) async {
if (methodCall.method == 'getTemporaryDirectory') {
return tempDirectory.path;
}
throw FlutterError('Not implemented for method ${methodCall.method}.');
});

// Start tests.
foundation.main();
Expand Down
2 changes: 1 addition & 1 deletion kraken/test/src/foundation/environment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ void main() {
group('environment', () {
test('getKrakenTemporaryPath()', () async {
String tempPath = await getKrakenTemporaryPath();
expect(tempPath, './temp/Kraken');
expect(tempPath, './temp');
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import Foundation

import connectivity_macos
import kraken
import path_provider_macos
import shared_preferences_macos

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
ConnectivityPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlugin"))
KrakenPlugin.register(with: registry.registrar(forPlugin: "KrakenPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
}