Initial commit

This commit is contained in:
2021-11-16 22:29:44 +01:00
commit bd221bb030
49 changed files with 1248 additions and 0 deletions

7
plugin/port_update/.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
.DS_Store
.dart_tool/
.packages
.pub/
build/

View File

@ -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: 18116933e77adc82f80866c928266a5b4f1ed645
channel: stable
project_type: plugin

View File

@ -0,0 +1,3 @@
## 0.0.1
* TODO: Describe initial release.

View File

@ -0,0 +1 @@
TODO: Add your license here.

View File

@ -0,0 +1,15 @@
# port_update
Bruh I need a library to call native code from foreground task
## Getting Started
This project is a starting point for a Flutter
[plug-in package](https://flutter.dev/developing-packages/),
a specialized package that includes platform-specific implementation code for
Android and/or iOS.
For help getting started with Flutter, view our
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.

View File

@ -0,0 +1,4 @@
include: package:flutter_lints/flutter.yaml
# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options

8
plugin/port_update/android/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures

View File

@ -0,0 +1,35 @@
group 'org.bytedream.port_update'
version '1.0'
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
}
}
rootProject.allprojects {
repositories {
google()
mavenCentral()
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 30
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
minSdkVersion 16
}
}

View File

@ -0,0 +1,3 @@
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true

View File

@ -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-6.7-all.zip

View File

@ -0,0 +1 @@
rootProject.name = 'port_update'

View File

@ -0,0 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.bytedream.port_update">
</manifest>

View File

@ -0,0 +1,136 @@
package org.bytedream.port_update;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import io.flutter.Log;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.EventChannel.StreamHandler;
/** PortUpdatePlugin */
public class PortUpdatePlugin implements FlutterPlugin, StreamHandler {
public static final String TAG = "PortUpdate";
public static final String CHANNEL = "port/stream";
private Context context;
private EventChannel channel;
private BroadcastReceiver batteryReceiver;
private BroadcastReceiver headphoneReceiver;
private int lastBatteryStatus;
private int lastHeadphoneStatus;
@Override
public void onAttachedToEngine(FlutterPluginBinding flutterPluginBinding) {
context = flutterPluginBinding.getApplicationContext();
channel = new EventChannel(flutterPluginBinding.getBinaryMessenger(), CHANNEL);
channel.setStreamHandler(this);
}
@Override
public void onDetachedFromEngine(FlutterPluginBinding binding) {
context = null;
channel.setStreamHandler(null);
}
@Override
public void onListen(Object o, EventChannel.EventSink eventSink) {
Log.w(TAG, "adding listener");
batteryReceiver = createBatteryReceiver(eventSink);
headphoneReceiver = createHeadphoneReceiver(eventSink);
context.registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
context.registerReceiver(headphoneReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
// lastBatteryStatus = getBatteryStatus(intent);
// lastHeadphoneStatus = getHeadphoneStatus(intent);
}
@Override
public void onCancel(Object o) {
Log.w(TAG, "canceling listener");
context.unregisterReceiver(batteryReceiver);
context.unregisterReceiver(headphoneReceiver);
}
private BroadcastReceiver createBatteryReceiver(EventChannel.EventSink eventSink) {
return new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int batteryStatus = getBatteryStatus(intent);
if (batteryStatus != lastBatteryStatus || batteryStatus == -1) {
lastBatteryStatus = batteryStatus;
eventSink.success(getBatteryAction(batteryStatus).value);
}
}
};
}
private int getBatteryStatus(Intent intent) {
return intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
}
private Action getBatteryAction(int batteryStatus) {
switch (batteryStatus) {
case BatteryManager.BATTERY_STATUS_CHARGING:
return Action.BATTERY_CHARGING;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
return Action.BATTERY_DISCHARGING;
case BatteryManager.BATTERY_STATUS_FULL:
return Action.BATTERY_FULL;
default:
return Action.UNKNOWN;
}
}
private BroadcastReceiver createHeadphoneReceiver(EventChannel.EventSink eventSink) {
return new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int headphoneStatus = intent.getIntExtra("state", -1);
if (headphoneStatus != lastHeadphoneStatus || headphoneStatus == -1) {
lastHeadphoneStatus = headphoneStatus;
eventSink.success(getHeadphoneAction(headphoneStatus).value);
}
}
};
}
private int getHeadphoneStatus(Intent intent) {
return intent.getIntExtra("state", -1);
}
private Action getHeadphoneAction(int headphoneStatus) {
switch (headphoneStatus) {
// unplugged
case 0:
return Action.HEADPHONE_DISCONNECTED;
// plugged in
case 1:
return Action.HEADPHONE_CONNECTED;
default:
return Action.UNKNOWN;
}
}
private enum Action {
UNKNOWN(0),
BATTERY_CHARGING(1),
BATTERY_DISCHARGING(2),
BATTERY_FULL(3),
HEADPHONE_CONNECTED(4),
HEADPHONE_DISCONNECTED(5);
public final int value;
Action(int value) {
this.value = value;
}
}
}

View File

@ -0,0 +1,3 @@
library port_update;
export 'src/port_update.dart' show PortUpdate, UpdateAction;

View File

@ -0,0 +1,18 @@
import 'package:flutter/services.dart';
enum UpdateAction {
unknown,
batteryCharging,
batteryDischarging,
batteryFull,
headphoneConnected,
headphoneDisconnected
}
class PortUpdate {
static const channel = "port/stream";
final _channel = const EventChannel(channel);
Stream<UpdateAction?> get stream => _channel.receiveBroadcastStream().map((event) => UpdateAction.values.elementAt(event));
}

View File

@ -0,0 +1,161 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
async:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.8.1"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
characters:
dependency: transitive
description:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
fake_async:
dependency: transitive
description:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
flutter:
dependency: "direct main"
description: flutter
source: sdk
version: "0.0.0"
flutter_lints:
dependency: "direct dev"
description:
name: flutter_lints
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
flutter_test:
dependency: "direct dev"
description: flutter
source: sdk
version: "0.0.0"
lints:
dependency: transitive
description:
name: lints
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
matcher:
dependency: transitive
description:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
sky_engine:
dependency: transitive
description: flutter
source: sdk
version: "0.0.99"
source_span:
dependency: transitive
description:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
stack_trace:
dependency: transitive
description:
name: stack_trace
url: "https://pub.dartlang.org"
source: hosted
version: "1.10.0"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.2"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
sdks:
dart: ">=2.12.0 <3.0.0"
flutter: ">=1.20.0"

View File

@ -0,0 +1,63 @@
name: port_update
description: Bruh I need a library to call native code from foreground task
version: 0.0.1
homepage:
environment:
sdk: ">=2.12.0 <3.0.0"
flutter: ">=1.20.0"
dependencies:
flutter:
sdk: flutter
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^1.0.0
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# This section identifies this Flutter project as a plugin project.
# The 'pluginClass' and Android 'package' identifiers should not ordinarily
# be modified. They are used by the tooling to maintain consistency when
# adding or updating assets for this project.
plugin:
platforms:
android:
package: org.bytedream.port_update
pluginClass: PortUpdatePlugin
# To add assets to your plugin package, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
#
# For details regarding assets in packages, see
# https://flutter.dev/assets-and-images/#from-packages
#
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
# To add custom fonts to your plugin package, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts in packages, see
# https://flutter.dev/custom-fonts/#from-packages