Skip to content

Commit

Permalink
Added app updater for Android
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinHaendel committed Sep 12, 2021
1 parent abef997 commit b753434
Show file tree
Hide file tree
Showing 11 changed files with 343 additions and 6 deletions.
10 changes: 10 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@
<meta-data
android:name="flutterEmbedding"
android:value="2" />

<provider
android:name="sk.fourq.otaupdate.OtaUpdateFileProvider"
android:authorities="${applicationId}.ota_update_provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
</application>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.INTERNET" />
Expand Down
4 changes: 4 additions & 0 deletions android/app/src/main/res/xml/filepaths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_download" path="Download"/>
</paths>
11 changes: 10 additions & 1 deletion assets/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,14 @@
"Color validation without guarantee": "Farbliche Validierung ohne Gewähr",

"Outdated app version": "Veraltete App-Version",
"Your app version is outdated, so certificate checking and color validation have been disabled. To re-enable these features, you need to update the app.": "Deine App-Version ist veraltet, daher wurde das Prüfen von Pässen und die Farbvalidierung deaktiviert. Um diese Funktionen wieder zu aktivieren, musst du die App aktualisieren."
"Your app version is outdated, so certificate checking and color validation have been disabled. To re-enable these features, you need to update the app.": "Deine App-Version ist veraltet, daher wurde das Prüfen von Pässen und die Farbvalidierung deaktiviert. Um diese Funktionen wieder zu aktivieren, musst du die App aktualisieren.",

"Update available": "Update verfügbar",
"A new version of the app is available. Would you like to update now?": "Eine neue Version der App ist verfügbar. Möchtest du jetzt aktualisieren?",
"New version: {} (Last update: {})": "Neue Version: {} (Stand: {})",
"Visit our website if you want to update manually": "Besuche unsere Webseite, wenn du manuell aktualisieren möchtest",
"Not now": "Nicht jetzt",
"Download update": "Update herunterladen",
"Downloading...": "Lade herunter...",
"An error occurred while downloading the update. Please try again later.": "Beim Herunterladen des Updates ist ein Fehler aufgetreten. Bitte versuche es später erneut."
}
11 changes: 10 additions & 1 deletion assets/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,14 @@
"Color validation without guarantee": "Color validation without guarantee",

"Outdated app version": "Outdated app version",
"Your app version is outdated, so certificate checking and color validation have been disabled. To re-enable these features, you need to update the app.": "Your app version is outdated, so certificate checking and color validation have been disabled. To re-enable these features, you need to update the app."
"Your app version is outdated, so certificate checking and color validation have been disabled. To re-enable these features, you need to update the app.": "Your app version is outdated, so certificate checking and color validation have been disabled. To re-enable these features, you need to update the app.",

"Update available": "Update available",
"A new version of the app is available. Would you like to update now?": "A new version of the app is available. Would you like to update now?",
"New version: {} (Last update: {})": "New version: {} (Last update: {})",
"Visit our website if you want to update manually": "Visit our website if you want to update manually",
"Not now": "Not now",
"Download update": "Download update",
"Downloading...": "Downloading...",
"An error occurred while downloading the update. Please try again later.": "An error occurred while downloading the update. Please try again later."
}
3 changes: 3 additions & 0 deletions lib/consts/configuration.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Configuration {
static const bool enable_android_updater = true;
}
16 changes: 16 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import 'dart:io';

import 'package:country_codes/country_codes.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_statusbarcolor_ns/flutter_statusbarcolor_ns.dart';
import 'package:flutter_vector_icons/flutter_vector_icons.dart';
import 'package:greenpass_app/consts/configuration.dart';
import 'package:greenpass_app/elements/platform_alert_dialog.dart';
import 'package:greenpass_app/services/detect_country.dart';
import 'package:greenpass_app/consts/colors.dart';
Expand All @@ -15,16 +19,19 @@ import 'package:greenpass_app/services/my_certs/my_certs.dart';
import 'package:greenpass_app/services/outdated_check.dart';
import 'package:greenpass_app/services/pub_certs/pub_certs.dart';
import 'package:greenpass_app/services/settings.dart';
import 'package:greenpass_app/services/update_check/update_check.dart';
import 'package:greenpass_app/views/country_selection_page.dart';
import 'package:greenpass_app/views/settings_page.dart';
import 'package:greenpass_app/views/my_passes_page.dart';
import 'package:greenpass_app/views/scan_others_pass.dart';
import 'package:greenpass_app/views/update_notification.dart';
import 'package:hive_flutter/hive_flutter.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
await OutdatedCheck.initAppStart();
if (Platform.isAndroid && kReleaseMode && Configuration.enable_android_updater) await UpdateCheck.initAppStart();
await CountryCodes.init();
await PubCerts.initAppStart();
await RegulationsProvider.initAppStart();
Expand Down Expand Up @@ -110,6 +117,14 @@ class _HomePageState extends State<MyHomePage> with SingleTickerProviderStateMix
)
);
}

if (UpdateCheck.updateAvailable) {
Future.delayed(Duration.zero, () =>
Navigator.push(context, MaterialPageRoute(
builder: (context) => UpdateNotification()
))
);
}
}

@override
Expand All @@ -120,6 +135,7 @@ class _HomePageState extends State<MyHomePage> with SingleTickerProviderStateMix
];

FlutterStatusbarcolor.setStatusBarColor(Colors.transparent);
FlutterStatusbarcolor.setStatusBarWhiteForeground(_currentPageIdx != 0);
FlutterStatusbarcolor.setNavigationBarWhiteForeground(Theme.of(context).brightness == Brightness.dark);
FlutterStatusbarcolor.setNavigationBarColor(Theme.of(context).scaffoldBackgroundColor);

Expand Down
19 changes: 19 additions & 0 deletions lib/services/update_check/android_update_check_result.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class AndroidUpdateCheckResult {
String installedVersion;
String newestVersion;
DateTime updatedAt;
String downloadUrl;
String sha256Checksum;
Map<String, dynamic>? changelog;

AndroidUpdateCheckResult({
required this.installedVersion,
required this.newestVersion,
required this.updatedAt,
required this.downloadUrl,
required this.sha256Checksum,
this.changelog,
});

get updateAvailable => this.installedVersion != this.newestVersion;
}
35 changes: 35 additions & 0 deletions lib/services/update_check/update_check.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'dart:convert';
import 'package:greenpass_app/services/update_check/android_update_check_result.dart';
import 'package:http/http.dart';
import 'package:package_info/package_info.dart';

class UpdateCheck {
static const String _currentVersionUrl = 'https://raw.githubusercontent.com/GreenPassApp/shared-data/main/current-app-version.json';

static AndroidUpdateCheckResult? androidUpdateCheckResult;
static bool updateAvailable = androidUpdateCheckResult?.updateAvailable ?? false;

static Future<void> initAppStart() async {
try {
Response res = await get(Uri.parse(_currentVersionUrl));
Map<String, dynamic> parsedJson = jsonDecode(res.body);
Map<String, dynamic> androidVerInfo = parsedJson['android'];

androidUpdateCheckResult = AndroidUpdateCheckResult(
installedVersion: await _getVersionCode(),
newestVersion: androidVerInfo['version'],
updatedAt: DateTime.parse(androidVerInfo['updated_at']),
downloadUrl: androidVerInfo['download']['url'],
sha256Checksum: androidVerInfo['download']['sha256'],
changelog: androidVerInfo.containsKey('changelog') ? androidVerInfo['changelog'] : null,
);
} catch (_) {
return null;
}
}

static Future<String> _getVersionCode() async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
return packageInfo.version;
}
}
Loading

0 comments on commit b753434

Please sign in to comment.