Compare commits

...
This repository has been archived on 2024-07-21. You can view files and clone it, but cannot push or open issues or pull requests.

4 commits
main ... notifs

Author SHA1 Message Date
Matyáš Caras
2d2faf5a59 feat(notifikace): přenést background notifikaci do main.dart
+ přidat autostart
2022-11-21 17:23:49 +01:00
Matyáš Caras
5583dec569 chore(flutter): přejít na flutter wrapper 2022-11-21 17:20:07 +01:00
Matyáš Caras
6bab6db1b2
chore: synchronizace z větve main 2022-11-21 16:18:13 +01:00
Matyáš Caras
1dcad3c716 fix: zachování zapnutí skrz flutter_background 2022-11-17 13:34:18 +01:00
15 changed files with 228 additions and 7 deletions

1
.flutter Submodule

@ -0,0 +1 @@
Subproject commit 52b3dc25f6471c27b2144594abb11c741cb88f57

4
.gitmodules vendored Normal file
View file

@ -0,0 +1,4 @@
[submodule ".flutter"]
path = .flutter
url = https://github.com/flutter/flutter.git
branch = stable

3
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"dart.flutterSdkPath": ".flutter",
}

View file

@ -1,3 +1,6 @@
# 1.5.0
- Android: přidat permanentní oznámení pro zabránění ukončení procesu
- přejít na flutter wrapper
# 1.4.2
- aktualizace knihovny flutter_local_notifications
- lepší podpora pro Android 13

View file

@ -1,6 +1,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cz.hernikplays.opencanteen">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<application
android:label="OpenCanteen"
android:name="${applicationName}"
@ -35,5 +38,18 @@
<meta-data
android:name="flutterEmbedding"
android:value="2" />
<!-- Autostart -->
<receiver
android:enabled="true"
android:exported="true"
android:name="cz.hernikplays.opencanteen.BootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>

View file

@ -4,3 +4,13 @@ import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() {
}
class BootReceiver: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
val i = Intent(context, MainActivity::class.java)
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(i)
}
}
}

113
flutterw Executable file
View file

@ -0,0 +1,113 @@
#!/usr/bin/env sh
##############################################################################
##
## Flutter start up script for UN*X
## Version: v1.3.1
## Date: 2022-11-21 16:41:48
##
## Use this flutter wrapper to bundle Flutter within your project to make
## sure everybody builds with the same version.
##
## Read about the install and uninstall process in the README on GitHub
## https://github.com/passsy/flutter_wrapper
##
## Inspired by gradle-wrapper.
##
##############################################################################
echoerr() { echo "$@" 1>&2; }
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ]; do
ls=$(ls -ld "$PRG")
link=$(expr "$ls" : '.*-> \(.*\)$')
if expr "$link" : '/.*' >/dev/null; then
PRG="$link"
else
PRG=$(dirname "$PRG")"/$link"
fi
done
SAVED="$(pwd)"
cd "$(dirname "$PRG")/" >/dev/null
APP_HOME="$(pwd -P)"
cd "$SAVED" >/dev/null
FLUTTER_SUBMODULE_NAME='.flutter'
GIT_HOME=$(git -C "${APP_HOME}" rev-parse --show-toplevel)
FLUTTER_DIR="${GIT_HOME}/${FLUTTER_SUBMODULE_NAME}"
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
cd "$(dirname "$0")"
fi
# Fix not initialized flutter submodule
if [ ! -f "${FLUTTER_DIR}/bin/flutter" ]; then
echoerr "$FLUTTER_SUBMODULE_NAME submodule not initialized. Initializing..."
git submodule update --init "${FLUTTER_DIR}"
fi
# Detect detach HEAD and fix it. commands like upgrade expect a valid branch, not a detached HEAD
FLUTTER_SYMBOLIC_REF=$(git -C "${FLUTTER_DIR}" symbolic-ref -q HEAD)
if [ -z "${FLUTTER_SYMBOLIC_REF}" ]; then
FLUTTER_REV=$(git -C "${FLUTTER_DIR}" rev-parse HEAD)
FLUTTER_CHANNEL=$(git -C "${GIT_HOME}" config -f .gitmodules submodule.${FLUTTER_SUBMODULE_NAME}.branch)
if [ -z "${FLUTTER_CHANNEL}" ]; then
echoerr "Warning: Submodule '$FLUTTER_SUBMODULE_NAME' doesn't point to an official Flutter channel \
(one of stable|beta|dev|master). './flutterw upgrade' will fail without a channel."
echoerr "Fix this by adding a specific channel with:"
echoerr " - './flutterw channel <channel>' or"
echoerr " - Add 'branch = <channel>' to '$FLUTTER_SUBMODULE_NAME' submodule in .gitmodules"
else
echoerr "Fixing detached HEAD: '$FLUTTER_SUBMODULE_NAME' submodule points to a specific commit $FLUTTER_REV, not channel '$FLUTTER_CHANNEL' (as defined in .gitmodules)."
# Make sure channel is fetched
# Remove old channel branch because it might be moved to an unrelated commit where fast-forward pull isn't possible
git -C "${FLUTTER_DIR}" branch -q -D "${FLUTTER_CHANNEL}" 2> /dev/null || true
git -C "${FLUTTER_DIR}" fetch -q origin
# bind current HEAD to channel defined in .gitmodules
git -C "${FLUTTER_DIR}" checkout -q -b "${FLUTTER_CHANNEL}" "${FLUTTER_REV}"
git -C "${FLUTTER_DIR}" branch -q -u "origin/${FLUTTER_CHANNEL}" "${FLUTTER_CHANNEL}"
echoerr "Fixed! Migrated to channel '$FLUTTER_CHANNEL' while staying at commit $FLUTTER_REV. './flutterw upgrade' now works without problems!"
git -C "${FLUTTER_DIR}" status -bs
fi
fi
# Wrapper tasks done, call flutter binary with all args
set -e
"$FLUTTER_DIR/bin/flutter" "$@"
set +e
# Post flutterw tasks. exit code from /bin/flutterw will be used as final exit
FLUTTER_EXIT_STATUS=$?
if [ ${FLUTTER_EXIT_STATUS} -eq 0 ]; then
# ./flutterw channel CHANNEL
if echo "$@" | grep -q "channel"; then
if [ -n "$2" ]; then
# make sure .gitmodules is updated as well
CHANNEL=${2} # second arg
git config -f "${GIT_HOME}/.gitmodules" "submodule.${FLUTTER_SUBMODULE_NAME}.branch" "${CHANNEL}"
# makes sure nobody forgets to do commit all changed files
git add "${GIT_HOME}/.gitmodules"
git add "${FLUTTER_DIR}"
fi
fi
# ./flutterw upgrade
if echo "$@" | grep -q "upgrade"; then
# makes sure nobody forgets to do commit the changed submodule
git add "${FLUTTER_DIR}"
# flutter packages get runs automatically. Stage those changes as well
if [ -f pubspec.lock ]; then
git add pubspec.lock
fi
fi
fi
exit ${FLUTTER_EXIT_STATUS}

View file

@ -168,4 +168,6 @@ abstract class Languages {
String get error;
String get needRemember;
String get wakeLock;
}

View file

@ -237,4 +237,8 @@ class LanguageCz extends Languages {
@override
String get review => "Ohodnotit aplikaci";
@override
String get wakeLock =>
"Toto oznámení slouží k tomu, aby nebyl proces spontánně ukončen.";
}

View file

@ -235,4 +235,8 @@ class LanguageEn extends Languages {
@override
String get review => "Review the app";
@override
String get wakeLock =>
"This notification is used to hopefully block unexpected process kill.";
}

View file

@ -3,6 +3,7 @@ import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_background/flutter_background.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_native_timezone/flutter_native_timezone.dart';
@ -45,14 +46,18 @@ final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
void oznamitPredem(SharedPreferences prefs, tz.Location l) async {
String title;
String notif;
String locale = Intl.getCurrentLocale();
debugPrint(locale);
switch (locale) {
case "cs_CZ":
title = LanguageCz().lunchNotif;
notif = LanguageCz().wakeLock;
break;
default:
notif = LanguageEn().wakeLock;
title = LanguageEn().lunchNotif;
break;
}
/*if (prefs.getBool("offline") ?? false) {
@ -73,6 +78,7 @@ void oznamitPredem(SharedPreferences prefs, tz.Location l) async {
TimeOfDay(hour: kdy.hour, minute: kdy.minute),
);
if (cas.isBefore(DateTime.now())) return;
// data o oznámení
const AndroidNotificationDetails androidSpec =
AndroidNotificationDetails('predobedem', 'Oznámení před obědem',
@ -82,6 +88,21 @@ void oznamitPredem(SharedPreferences prefs, tz.Location l) async {
styleInformation: BigTextStyleInformation(''),
ticker: 'today meal');
// blokovat vypnutí
if (Platform.isAndroid) {
// ! TODO: OTESTOVAT, JESTLI FUNGUJE IMPORT NA IOSu
var androidConfig = FlutterBackgroundAndroidConfig(
notificationTitle: "OpenCanteen",
notificationText: notif,
notificationImportance: AndroidNotificationImportance.Default,
notificationIcon: const AndroidResource(
name: 'notif_icon', defType: 'drawable'),
enableWifiLock: true);
bool success =
await FlutterBackground.initialize(androidConfig: androidConfig);
if (success) await FlutterBackground.enableBackgroundExecution();
}
// naplánovat
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
@ -94,6 +115,7 @@ void oznamitPredem(SharedPreferences prefs, tz.Location l) async {
UILocalNotificationDateInterpretation.absoluteTime);
} on StateError catch (_) {
// nenalezeno
debugPrint("Nenalezeno");
}
}
// }
@ -106,11 +128,6 @@ void main() async {
var l = tz.getLocation(await FlutterNativeTimezone.getLocalTimezone());
tz.setLocalLocation(l);
var prefs = await SharedPreferences.getInstance();
if (prefs.getBool("oznamit") ?? false) {
oznamitPredem(prefs, l);
}
// nastavit oznámení
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('notif_icon');
@ -120,6 +137,11 @@ void main() async {
);
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
var prefs = await SharedPreferences.getInstance();
if (prefs.getBool("oznamit") ?? false) {
oznamitPredem(prefs, l);
}
// spustit aplikaci
runApp(const MyApp());
}

View file

@ -378,6 +378,22 @@ class _JidelnicekPageState extends State<JidelnicekPage> {
void nactiNastaveni() async {
var prefs = await SharedPreferences.getInstance();
_skipWeekend = prefs.getBool("skip") ?? false;
// if (prefs.getBool("oznamit") ?? false) {
// if (!mounted) return;
// if (Platform.isAndroid) {
// // ! TODO: OTESTOVAT, JESTLI FUNGUJE IMPORT NA IOSu
// var androidConfig = FlutterBackgroundAndroidConfig(
// notificationTitle: "OpenCanteen",
// notificationText: Languages.of(context)!.wakeLock,
// notificationImportance: AndroidNotificationImportance.Default,
// notificationIcon:
// const AndroidResource(name: 'notif_icon', defType: 'drawable'),
// enableWifiLock: true);
// bool success =
// await FlutterBackground.initialize(androidConfig: androidConfig);
// if (success) await FlutterBackground.enableBackgroundExecution();
// }
// }
if (!mounted) return;
kontrolaTyden(context);
}

View file

@ -2,6 +2,7 @@ import 'dart:io';
import 'package:canteenlib/canteenlib.dart';
import 'package:flutter/material.dart';
import 'package:flutter_background/flutter_background.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_native_timezone/flutter_native_timezone.dart';
import 'package:path_provider/path_provider.dart';
@ -235,9 +236,23 @@ class _NastaveniState extends State<Nastaveni> {
var l =
tz.getLocation(await FlutterNativeTimezone.getLocalTimezone());
if (!mounted) return;
if (Platform.isAndroid) {
// ! TODO: OTESTOVAT, JESTLI FUNGUJE IMPORT NA IOSu
var androidConfig = FlutterBackgroundAndroidConfig(
notificationTitle: "OpenCanteen",
notificationText: Languages.of(context)!.wakeLock,
notificationImportance: AndroidNotificationImportance.Default,
notificationIcon: const AndroidResource(
name: 'notif_icon', defType: 'drawable'),
enableWifiLock: true);
bool success = await FlutterBackground.initialize(
androidConfig: androidConfig);
if (success) await FlutterBackground.enableBackgroundExecution();
}
await widget.n.zonedSchedule(
// Vytvoří nové oznámení pro daný čas a datum
0,
// ignore: use_build_context_synchronously
Languages.of(context)!.lunchNotif,
"${jidlo.varianta} - ${jidlo.nazev}",
tz.TZDateTime.from(den, l),

View file

@ -111,6 +111,13 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_background:
dependency: "direct main"
description:
name: flutter_background
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
flutter_launcher_icons:
dependency: "direct dev"
description:

View file

@ -6,7 +6,7 @@ publish_to: 'none'
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
version: 1.4.2+22
version: 1.4.3+23
environment:
sdk: ">=2.16.1 <3.0.0"
@ -27,6 +27,7 @@ dependencies:
flutter_native_timezone: ^2.0.0
intl: ^0.17.0
package_info_plus: ^1.4.3+1
flutter_background: ^1.1.0
dev_dependencies:
flutter_lints: ^2.0.1