From f021dbf6a6b57e8ae1d674a6a0f0b25a6c66f91b Mon Sep 17 00:00:00 2001 From: Godwin Asuquo Date: Fri, 21 Jan 2022 14:02:00 +0100 Subject: [PATCH 1/3] add stay awake swift support --- ios/CakeWallet/wakeLock.swift | 20 ++++ ios/Runner.xcodeproj/project.pbxproj | 4 + ios/Runner/AppDelegate.swift | 6 ++ lib/di.dart | 98 +++++++++---------- lib/entities/wake_lock.dart | 21 ++++ .../on_wallet_sync_status_change.dart | 10 ++ 6 files changed, 107 insertions(+), 52 deletions(-) create mode 100644 ios/CakeWallet/wakeLock.swift create mode 100644 lib/entities/wake_lock.dart diff --git a/ios/CakeWallet/wakeLock.swift b/ios/CakeWallet/wakeLock.swift new file mode 100644 index 000000000..efd65fdb8 --- /dev/null +++ b/ios/CakeWallet/wakeLock.swift @@ -0,0 +1,20 @@ +// +// wakeLock.swift +// Runner +// +// Created by Godwin Asuquo on 1/21/22. +// + +import Foundation +import UIKit + +func enableWakeScreen() -> String { + UIApplication.shared.isIdleTimerDisabled = true + + return "screen wake turned ON" +} + +func disableWakeScreen() -> String{ + UIApplication.shared.isIdleTimerDisabled = false + return "screen wake turned OFF" +} diff --git a/ios/Runner.xcodeproj/project.pbxproj b/ios/Runner.xcodeproj/project.pbxproj index f66cd7099..cadcd78bb 100644 --- a/ios/Runner.xcodeproj/project.pbxproj +++ b/ios/Runner.xcodeproj/project.pbxproj @@ -12,6 +12,7 @@ 1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; }; 20ED0868E1BD7E12278C0CB3 /* Pods_Runner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B26E3F56D69167FBB1DC160A /* Pods_Runner.framework */; }; 3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; }; + 5AFFEBFD279AD49C00F906A4 /* wakeLock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5AFFEBFC279AD49C00F906A4 /* wakeLock.swift */; }; 74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74858FAE1ED2DC5600515810 /* AppDelegate.swift */; }; 97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; }; 97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; }; @@ -28,6 +29,7 @@ 20F67A1B2C2FCB2A3BB048C1 /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = ""; }; 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = ""; }; 501EA9286675DC8636978EA4 /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = ""; }; + 5AFFEBFC279AD49C00F906A4 /* wakeLock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = wakeLock.swift; sourceTree = ""; }; 61CAA8652B54F23356F7592A /* Pods-Runner.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.profile.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig"; sourceTree = ""; }; 74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Runner-Bridging-Header.h"; sourceTree = ""; }; 74858FAE1ED2DC5600515810 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; @@ -68,6 +70,7 @@ children = ( 0C44A7192518EF8000B570ED /* decrypt.swift */, 0C9D68C8264854B60011B691 /* secRandom.swift */, + 5AFFEBFC279AD49C00F906A4 /* wakeLock.swift */, ); path = CakeWallet; sourceTree = ""; @@ -277,6 +280,7 @@ files = ( 74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */, 1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */, + 5AFFEBFD279AD49C00F906A4 /* wakeLock.swift in Sources */, 0C9D68C9264854B60011B691 /* secRandom.swift in Sources */, 0C44A71A2518EF8000B570ED /* decrypt.swift in Sources */, ); diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift index ebf578d09..c4d460e08 100644 --- a/ios/Runner/AppDelegate.swift +++ b/ios/Runner/AppDelegate.swift @@ -96,6 +96,12 @@ import UnstoppableDomainsResolution result(address) } + case "enableWakeScreen": + result(enableWakeScreen()) + + case "disableWakeScreen": + result(disableWakeScreen()) + default: result(FlutterMethodNotImplemented) } diff --git a/lib/di.dart b/lib/di.dart index 2a1430796..b117a2a96 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -1,3 +1,4 @@ +import 'package:cake_wallet/entities/wake_lock.dart'; import 'package:cake_wallet/monero/monero.dart'; import 'package:cake_wallet/bitcoin/bitcoin.dart'; import 'package:cw_core/unspent_coins_info.dart'; @@ -192,8 +193,8 @@ Future setup( getIt.registerSingleton( ExchangeTemplateStore(templateSource: _exchangeTemplates)); getIt.registerSingleton(YatStore( - appStore: getIt.get(), - secureStorage: getIt.get()) + appStore: getIt.get(), + secureStorage: getIt.get()) ..init()); final secretStore = @@ -237,11 +238,9 @@ Future setup( type: type, language: language); }); - getIt.registerFactory( - () => WalletAddressListViewModel( - appStore: getIt.get(), - yatStore: getIt.get() - )); + getIt.registerFactory(() => + WalletAddressListViewModel( + appStore: getIt.get(), yatStore: getIt.get())); getIt.registerFactory(() => BalanceViewModel( appStore: getIt.get(), @@ -323,8 +322,7 @@ Future setup( getIt.get().wallet, getIt.get().settingsStore, getIt.get(), - getIt.get() - )); + getIt.get())); getIt.registerFactory(() => SendViewModel( getIt.get().wallet, @@ -336,9 +334,8 @@ Future setup( getIt.registerFactory( () => SendPage(sendViewModel: getIt.get())); - getIt.registerFactory( - () => SendTemplatePage( - sendTemplateViewModel: getIt.get())); + getIt.registerFactory(() => SendTemplatePage( + sendTemplateViewModel: getIt.get())); getIt.registerFactory(() => WalletListViewModel( _walletInfoSource, @@ -471,9 +468,11 @@ Future setup( case WalletType.monero: return monero.createMoneroWalletService(_walletInfoSource); case WalletType.bitcoin: - return bitcoin.createBitcoinWalletService(_walletInfoSource, _unspentCoinsInfoSource); + return bitcoin.createBitcoinWalletService( + _walletInfoSource, _unspentCoinsInfoSource); case WalletType.litecoin: - return bitcoin.createLitecoinWalletService(_walletInfoSource, _unspentCoinsInfoSource); + return bitcoin.createLitecoinWalletService( + _walletInfoSource, _unspentCoinsInfoSource); default: return null; } @@ -507,13 +506,13 @@ Future setup( getIt .registerFactoryParam( (TransactionInfo transactionInfo, _) { - final wallet = getIt.get().wallet; - return TransactionDetailsViewModel( - transactionInfo: transactionInfo, - transactionDescriptionBox: _transactionDescriptionBox, - wallet: wallet, - settingsStore: getIt.get()); - }); + final wallet = getIt.get().wallet; + return TransactionDetailsViewModel( + transactionInfo: transactionInfo, + transactionDescriptionBox: _transactionDescriptionBox, + wallet: wallet, + settingsStore: getIt.get()); + }); getIt.registerFactoryParam( (TransactionInfo transactionInfo, _) => TransactionDetailsPage( @@ -574,23 +573,21 @@ Future setup( return PreOrderPage(buyViewModel: getIt.get()); }); - getIt.registerFactoryParam( - (List args, _) { - final url = args.first as String; - final buyViewModel = args[1] as BuyViewModel; + getIt.registerFactoryParam((List args, _) { + final url = args.first as String; + final buyViewModel = args[1] as BuyViewModel; - return BuyWebViewPage(buyViewModel: buyViewModel, - ordersStore: getIt.get(), url: url); - }); + return BuyWebViewPage( + buyViewModel: buyViewModel, + ordersStore: getIt.get(), + url: url); + }); - getIt.registerFactoryParam( - (order, _) { - final wallet = getIt.get().wallet; + getIt.registerFactoryParam((order, _) { + final wallet = getIt.get().wallet; - return OrderDetailsViewModel( - wallet: wallet, - orderForDetails: order); - }); + return OrderDetailsViewModel(wallet: wallet, orderForDetails: order); + }); getIt.registerFactoryParam((Order order, _) => OrderDetailsPage(getIt.get(param1: order))); @@ -603,31 +600,28 @@ Future setup( final wallet = getIt.get().wallet; return UnspentCoinsListViewModel( - wallet: wallet, - unspentCoinsInfo: _unspentCoinsInfoSource); + wallet: wallet, unspentCoinsInfo: _unspentCoinsInfoSource); }); getIt.registerFactory(() => UnspentCoinsListPage( - unspentCoinsListViewModel: getIt.get() - )); + unspentCoinsListViewModel: getIt.get())); - getIt.registerFactoryParam((item, model) => - UnspentCoinsDetailsViewModel( - unspentCoinsItem: item, - unspentCoinsListViewModel: model)); + getIt.registerFactoryParam( + (item, model) => UnspentCoinsDetailsViewModel( + unspentCoinsItem: item, unspentCoinsListViewModel: model)); getIt.registerFactoryParam( - (List args, _) { - final item = args.first as UnspentCoinsItem; - final unspentCoinsListViewModel = args[1] as UnspentCoinsListViewModel; + (List args, _) { + final item = args.first as UnspentCoinsItem; + final unspentCoinsListViewModel = args[1] as UnspentCoinsListViewModel; - return UnspentCoinsDetailsPage( - unspentCoinsDetailsViewModel: - getIt.get( - param1: item, - param2: unspentCoinsListViewModel)); + return UnspentCoinsDetailsPage( + unspentCoinsDetailsViewModel: getIt.get( + param1: item, param2: unspentCoinsListViewModel)); }); + getIt.registerFactory(() => WakeLock()); + _isSetupFinished = true; } diff --git a/lib/entities/wake_lock.dart b/lib/entities/wake_lock.dart new file mode 100644 index 000000000..784f59c85 --- /dev/null +++ b/lib/entities/wake_lock.dart @@ -0,0 +1,21 @@ +import 'package:flutter/services.dart'; + +class WakeLock { + static const _utils = const MethodChannel('com.cake_wallet/native_utils'); + + Future enableWake() async { + try { + await _utils.invokeMethod('enableWakeScreen'); + } on PlatformException catch (_) { + print('Failed enabling screen wakelock'); + } + } + + Future disableWake() async { + try { + await _utils.invokeMethod('disableWakeScreen'); + } on PlatformException catch (_) { + print('Failed enabling screen wakelock'); + } + } +} diff --git a/lib/reactions/on_wallet_sync_status_change.dart b/lib/reactions/on_wallet_sync_status_change.dart index 8f14d7927..f2c2d07cc 100644 --- a/lib/reactions/on_wallet_sync_status_change.dart +++ b/lib/reactions/on_wallet_sync_status_change.dart @@ -1,9 +1,12 @@ +import 'package:cake_wallet/di.dart'; +import 'package:cake_wallet/entities/wake_lock.dart'; import 'package:mobx/mobx.dart'; import 'package:cw_core/transaction_history.dart'; import 'package:cw_core/wallet_base.dart'; import 'package:cw_core/balance.dart'; import 'package:cw_core/transaction_info.dart'; import 'package:cw_core/sync_status.dart'; +import 'package:flutter/services.dart'; ReactionDisposer _onWalletSyncStatusChangeReaction; @@ -11,11 +14,18 @@ void startWalletSyncStatusChangeReaction( WalletBase, TransactionInfo> wallet) { + final WakeLock _wakeLock = getIt.get(); _onWalletSyncStatusChangeReaction?.reaction?.dispose(); _onWalletSyncStatusChangeReaction = reaction((_) => wallet.syncStatus, (SyncStatus status) async { if (status is ConnectedSyncStatus) { await wallet.startSync(); } + if (status is SyncingSyncStatus) { + await _wakeLock.enableWake(); + } + if (status is SyncedSyncStatus || status is FailedSyncStatus) { + await _wakeLock.disableWake(); + } }); } From e84f122956ee1f5c66a5f31897ace9aab478d558 Mon Sep 17 00:00:00 2001 From: Godwin Asuquo Date: Fri, 21 Jan 2022 14:32:39 +0100 Subject: [PATCH 2/3] add wakelock for android --- .../java/com/cakewallet/cake_wallet/MainActivity.java | 9 +++++++++ .../app/src/main/java/com/monero/app/MainActivity.java | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/android/app/src/main/java/com/cakewallet/cake_wallet/MainActivity.java b/android/app/src/main/java/com/cakewallet/cake_wallet/MainActivity.java index 3afdc111e..d0453f571 100644 --- a/android/app/src/main/java/com/cakewallet/cake_wallet/MainActivity.java +++ b/android/app/src/main/java/com/cakewallet/cake_wallet/MainActivity.java @@ -13,6 +13,7 @@ import android.os.AsyncTask; import android.os.Build; import android.os.Handler; import android.os.Looper; +import android.view.WindowManager; import com.unstoppabledomains.resolution.DomainResolution; import com.unstoppabledomains.resolution.Resolution; @@ -39,6 +40,14 @@ public class MainActivity extends FlutterFragmentActivity { try { switch (call.method) { + case "enableWakeScreen": + getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + handler.post(() -> result.success("screen wake turned ON")); + break; + case "disableWakeScreen": + getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + handler.post(() -> result.success("screen wake turned OFF")); + break; case "sec_random": int count = call.argument("count"); SecureRandom random = new SecureRandom(); diff --git a/android/app/src/main/java/com/monero/app/MainActivity.java b/android/app/src/main/java/com/monero/app/MainActivity.java index 8a46333db..0ee3d4dd5 100644 --- a/android/app/src/main/java/com/monero/app/MainActivity.java +++ b/android/app/src/main/java/com/monero/app/MainActivity.java @@ -13,6 +13,7 @@ import android.os.AsyncTask; import android.os.Build; import android.os.Handler; import android.os.Looper; +import android.view.WindowManager; import com.unstoppabledomains.resolution.DomainResolution; import com.unstoppabledomains.resolution.Resolution; @@ -39,6 +40,14 @@ public class MainActivity extends FlutterFragmentActivity { try { switch (call.method) { + case "enableWakeScreen": + getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + handler.post(() -> result.success("screen wake turned ON")); + break; + case "disableWakeScreen": + getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + handler.post(() -> result.success("screen wake turned ON")); + break; case "sec_random": int count = call.argument("count"); SecureRandom random = new SecureRandom(); From c70ced70dd763ec2f79930ea57c5b15832ab1514 Mon Sep 17 00:00:00 2001 From: Godwin Asuquo Date: Mon, 24 Jan 2022 16:21:52 +0100 Subject: [PATCH 3/3] fix issues from PR --- .../java/com/cakewallet/cake_wallet/MainActivity.java | 4 ++-- .../app/src/main/java/com/monero/app/MainActivity.java | 4 ++-- ios/CakeWallet/wakeLock.swift | 8 ++++---- lib/reactions/on_wallet_sync_status_change.dart | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/android/app/src/main/java/com/cakewallet/cake_wallet/MainActivity.java b/android/app/src/main/java/com/cakewallet/cake_wallet/MainActivity.java index d0453f571..141c26944 100644 --- a/android/app/src/main/java/com/cakewallet/cake_wallet/MainActivity.java +++ b/android/app/src/main/java/com/cakewallet/cake_wallet/MainActivity.java @@ -42,11 +42,11 @@ public class MainActivity extends FlutterFragmentActivity { switch (call.method) { case "enableWakeScreen": getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - handler.post(() -> result.success("screen wake turned ON")); + handler.post(() -> result.success(true)); break; case "disableWakeScreen": getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - handler.post(() -> result.success("screen wake turned OFF")); + handler.post(() -> result.success(true)); break; case "sec_random": int count = call.argument("count"); diff --git a/android/app/src/main/java/com/monero/app/MainActivity.java b/android/app/src/main/java/com/monero/app/MainActivity.java index 0ee3d4dd5..385932b38 100644 --- a/android/app/src/main/java/com/monero/app/MainActivity.java +++ b/android/app/src/main/java/com/monero/app/MainActivity.java @@ -42,11 +42,11 @@ public class MainActivity extends FlutterFragmentActivity { switch (call.method) { case "enableWakeScreen": getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - handler.post(() -> result.success("screen wake turned ON")); + handler.post(() -> result.success(true)); break; case "disableWakeScreen": getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - handler.post(() -> result.success("screen wake turned ON")); + handler.post(() -> result.success(true)); break; case "sec_random": int count = call.argument("count"); diff --git a/ios/CakeWallet/wakeLock.swift b/ios/CakeWallet/wakeLock.swift index efd65fdb8..35f23eafa 100644 --- a/ios/CakeWallet/wakeLock.swift +++ b/ios/CakeWallet/wakeLock.swift @@ -8,13 +8,13 @@ import Foundation import UIKit -func enableWakeScreen() -> String { +func enableWakeScreen() -> Bool{ UIApplication.shared.isIdleTimerDisabled = true - return "screen wake turned ON" + return true } -func disableWakeScreen() -> String{ +func disableWakeScreen() -> Bool{ UIApplication.shared.isIdleTimerDisabled = false - return "screen wake turned OFF" + return true } diff --git a/lib/reactions/on_wallet_sync_status_change.dart b/lib/reactions/on_wallet_sync_status_change.dart index f2c2d07cc..bb245898c 100644 --- a/lib/reactions/on_wallet_sync_status_change.dart +++ b/lib/reactions/on_wallet_sync_status_change.dart @@ -14,7 +14,7 @@ void startWalletSyncStatusChangeReaction( WalletBase, TransactionInfo> wallet) { - final WakeLock _wakeLock = getIt.get(); + final _wakeLock = getIt.get(); _onWalletSyncStatusChangeReaction?.reaction?.dispose(); _onWalletSyncStatusChangeReaction = reaction((_) => wallet.syncStatus, (SyncStatus status) async {