Skip to content

Commit

Permalink
Merge pull request #4 from loup-v/upgrade_project
Browse files Browse the repository at this point in the history
Upgrade to flutter 1.12 structure.
  • Loading branch information
lukaspili authored Feb 5, 2020
2 parents 360c3a8 + e579fc1 commit 461e8d2
Show file tree
Hide file tree
Showing 71 changed files with 919 additions and 677 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,5 @@

.packages
.pub/
pubspec.lock

build/

.idea/workspace.xml
22 changes: 11 additions & 11 deletions .idea/libraries/Dart_SDK.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions .idea/libraries/Flutter_Plugins.xml

This file was deleted.

2 changes: 1 addition & 1 deletion .idea/libraries/Flutter_for_Android.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions .idea/misc.xml

This file was deleted.

3 changes: 2 additions & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: 9f5ff2306bb3e30b2b98eee79cd231b1336f41f4
channel: stable

project_type: plugin
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.3.0

- Upgrade project structure to flutter v1.12
- Fix compilation warnings

## 0.2.3

- Remove deprecated use of platform API
Expand Down
10 changes: 5 additions & 5 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'io.intheloup.streamschannel'
version '1.0-SNAPSHOT'
group 'app.loup.streams_channel'
version '1.0'

buildscript {
repositories {
Expand All @@ -8,7 +8,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.android.tools.build:gradle:3.5.0'
}
}

Expand All @@ -22,11 +22,11 @@ rootProject.allprojects {
apply plugin: 'com.android.library'

android {
compileSdkVersion 27
compileSdkVersion 28

defaultConfig {
minSdkVersion 16
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
lintOptions {
disable 'InvalidPackage'
Expand Down
3 changes: 3 additions & 0 deletions android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true
5 changes: 5 additions & 0 deletions android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
2 changes: 1 addition & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.intheloup.streamschannel">
package="app.loup.streams_channel">
</manifest>
200 changes: 200 additions & 0 deletions android/src/main/java/app/loup/streams_channel/StreamsChannel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
// Copyright (c) 2018 Loup Inc.
// Licensed under Apache License v2.0

package app.loup.streams_channel;

import android.annotation.SuppressLint;
import android.util.Log;

import androidx.annotation.UiThread;

import java.nio.ByteBuffer;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.BinaryMessenger.BinaryMessageHandler;
import io.flutter.plugin.common.BinaryMessenger.BinaryReply;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodCodec;
import io.flutter.plugin.common.StandardMethodCodec;

/**
* Inspired from https://github.com/flutter/engine/blob/master/shell/platform/android/io/flutter/plugin/common/EventChannel.java
*/
public final class StreamsChannel {

public interface StreamHandlerFactory {
EventChannel.StreamHandler create(Object arguments);
}

private static final String TAG = "StreamsChannel#";

private final BinaryMessenger messenger;
private final String name;
private final MethodCodec codec;

public StreamsChannel(BinaryMessenger messenger, String name) {
this(messenger, name, StandardMethodCodec.INSTANCE);
}

public StreamsChannel(BinaryMessenger messenger, String name, MethodCodec codec) {
if (BuildConfig.DEBUG) {
if (messenger == null) {
Log.e(TAG, "Parameter messenger must not be null.");
}
if (name == null) {
Log.e(TAG, "Parameter name must not be null.");
}
if (codec == null) {
Log.e(TAG, "Parameter codec must not be null.");
}
}
this.messenger = messenger;
this.name = name;
this.codec = codec;
}

@UiThread
public void setStreamHandlerFactory(final StreamHandlerFactory factory) {
messenger.setMessageHandler(name, factory == null ? null : new IncomingStreamRequestHandler(factory));
}

private final class IncomingStreamRequestHandler implements BinaryMessageHandler {
private final StreamHandlerFactory factory;
private final ConcurrentHashMap<Integer, Stream> streams = new ConcurrentHashMap<>();

IncomingStreamRequestHandler(StreamHandlerFactory factory) {
this.factory = factory;
}

@Override
public void onMessage(ByteBuffer message, final BinaryReply reply) {
final MethodCall call = codec.decodeMethodCall(message);
final String[] methodParts = call.method.split("#");

if (methodParts.length != 2) {
reply.reply(null);
return;
}

final int id;
try {
id = Integer.parseInt(methodParts[1]);
} catch (NumberFormatException e) {
reply.reply(codec.encodeErrorEnvelope("error", e.getMessage(), null));
return;
}

final String method = methodParts[0];
switch (method) {
case "listen":
onListen(id, call.arguments, reply);
break;
case "cancel":
onCancel(id, call.arguments, reply);
break;
default:
reply.reply(null);
break;
}
}

private void onListen(int id, Object arguments, BinaryReply callback) {
final Stream stream = new Stream(new EventSinkImplementation(id), factory.create(arguments));
final Stream oldStream = streams.putIfAbsent(id, stream);

if (oldStream != null) {
// Repeated calls to onListen may happen during hot restart.
// We separate them with a call to onCancel.
try {
oldStream.handler.onCancel(null);
} catch (RuntimeException e) {
logError(id, "Failed to close existing event stream", e);
}
}

try {
stream.handler.onListen(arguments, stream.sink);
callback.reply(codec.encodeSuccessEnvelope(null));
} catch (RuntimeException e) {
streams.remove(id);
logError(id, "Failed to open event stream", e);
callback.reply(codec.encodeErrorEnvelope("error", e.getMessage(), null));
}
}

private void onCancel(int id, Object arguments, BinaryReply callback) {
final Stream oldStream = streams.remove(id);

if (oldStream != null) {
try {
oldStream.handler.onCancel(arguments);
callback.reply(codec.encodeSuccessEnvelope(null));
} catch (RuntimeException e) {
logError(id, "Failed to close event stream", e);
callback.reply(codec.encodeErrorEnvelope("error", e.getMessage(), null));
}
} else {
callback.reply(codec.encodeErrorEnvelope("error", "No active stream to cancel", null));
}
}

private void logError(int id, String message, Throwable e) {
Log.e(TAG + name, String.format("%s [id=%d]", message, id), e);
}

private final class EventSinkImplementation implements EventChannel.EventSink {

final int id;
final String name;
final AtomicBoolean hasEnded = new AtomicBoolean(false);

@SuppressLint("DefaultLocale")
private EventSinkImplementation(int id) {
this.id = id;
this.name = String.format("%s#%d", StreamsChannel.this.name, id);
}

@Override
@UiThread
public void success(Object event) {
if (hasEnded.get() || streams.get(id).sink != this) {
return;
}
StreamsChannel.this.messenger.send(name, codec.encodeSuccessEnvelope(event));
}

@Override
@UiThread
public void error(String errorCode, String errorMessage, Object errorDetails) {
if (hasEnded.get() || streams.get(id).sink != this) {
return;
}
StreamsChannel.this.messenger.send(
name,
codec.encodeErrorEnvelope(errorCode, errorMessage, errorDetails));
}

@Override
@UiThread
public void endOfStream() {
if (hasEnded.getAndSet(true) || streams.get(id).sink != this) {
return;
}
StreamsChannel.this.messenger.send(name, null);
}
}
}

private static class Stream {
final EventChannel.EventSink sink;
final EventChannel.StreamHandler handler;

private Stream(EventChannel.EventSink sink, EventChannel.StreamHandler handler) {
this.sink = sink;
this.handler = handler;
}
}
}
Loading

0 comments on commit 461e8d2

Please sign in to comment.