-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from optimalstrategy/5-background-forwarding
Implement background sms forwarding
- Loading branch information
Showing
9 changed files
with
230 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import 'package:telephony/telephony.dart'; | ||
|
||
import 'forwarding.dart'; | ||
import 'manager.dart'; | ||
|
||
/// A wrapper for [ForwarderManager] that registers a background message handler. | ||
/// Resets the background forwarder every time a field is updated. | ||
class BackgroundForwarder { | ||
static ForwarderManager _backgroundManager; | ||
final ForwarderManager mgr = new ForwarderManager(); | ||
|
||
BackgroundForwarder(Telephony telephony) { | ||
telephony.listenIncomingSms( | ||
onNewMessage: (msg) async => await mgr.forward(msg), | ||
onBackgroundMessage: onBackgroundMessage | ||
); | ||
} | ||
|
||
static void onBackgroundMessage(SmsMessage msg) async { | ||
if (_backgroundManager == null) { | ||
_backgroundManager = new ForwarderManager(); | ||
await _backgroundManager.loadFromPrefs(); | ||
} | ||
await _backgroundManager.forward(msg); | ||
} | ||
|
||
HttpCallbackForwarder get httpCallbackForwarder => mgr.httpCallbackForwarder; | ||
|
||
TelegramBotForwarder get telegramBotForwarder => mgr.telegramBotForwarder; | ||
|
||
DeployedTelegramBotForwarder get deployedTelegramBotForwarder => | ||
mgr.deployedTelegramBotForwarder; | ||
|
||
set httpCallbackForwarder(HttpCallbackForwarder fwd) { | ||
mgr.httpCallbackForwarder = fwd; | ||
invalidateBackgroundManager(); | ||
} | ||
|
||
set telegramBotForwarder(TelegramBotForwarder fwd) { | ||
mgr.telegramBotForwarder = fwd; | ||
invalidateBackgroundManager(); | ||
} | ||
|
||
set deployedTelegramBotForwarder(DeployedTelegramBotForwarder fwd) { | ||
mgr.deployedTelegramBotForwarder = fwd; | ||
invalidateBackgroundManager(); | ||
} | ||
|
||
/// Loads the forwarders from a json. | ||
Future<Map> loadFromPrefs() async { | ||
var result = await mgr.loadFromPrefs(); | ||
return result; | ||
} | ||
|
||
/// Dumps the forwarders to shared preferences. | ||
void dumpToPrefs() async => mgr.dumpToPrefs(); | ||
|
||
/// Returns the mapping (forwarder name -> not null) | ||
Map reportReadiness() { | ||
return mgr.reportReadiness(); | ||
} | ||
|
||
/// Sets the background manager to `null`. | ||
static void invalidateBackgroundManager() { | ||
_backgroundManager = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.