-
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 #11 from optimalstrategy/update-checking-and-start…
…up-on-boot Update checking, startup on boot, test messages
- Loading branch information
Showing
10 changed files
with
275 additions
and
6 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
34 changes: 34 additions & 0 deletions
34
...src/main/java/team/whatever/sms_forwarder_app/StartSmsForwarderServiceAtBootReceiver.java
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,34 @@ | ||
package team.whatever.sms_forwarder_app; | ||
|
||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.SharedPreferences; | ||
import android.os.Handler; | ||
|
||
|
||
import static android.content.Context.MODE_PRIVATE; | ||
|
||
class StartSmsForwarderServiceAtBootReceiver extends BroadcastReceiver { | ||
private static final String TAG = StartSmsForwarderServiceAtBootReceiver.class.getSimpleName(); | ||
|
||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
Handler h = new Handler(); | ||
h.post(new Runnable() { | ||
@Override | ||
public void run() { | ||
final SharedPreferences prefs = context.getSharedPreferences( | ||
"FlutterSharedPreferences", | ||
MODE_PRIVATE | ||
); | ||
final boolean launchOnStartup = prefs.getBoolean("flutter.launch_on_startup", true); | ||
if (launchOnStartup) { | ||
Intent intent_ = new Intent(context, MainActivity.class); | ||
intent_.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | ||
context.startActivity(intent_); | ||
} | ||
} | ||
}); | ||
} | ||
} |
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,156 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
import 'package:sms_forwarder/background_forwarder.dart'; | ||
import 'package:telephony/telephony.dart'; | ||
|
||
class SettingStrings { | ||
static final String launchOnStartup = "launch_on_startup"; | ||
} | ||
|
||
/// A screen with the App's settings. | ||
class AppSettingsScreen extends StatefulWidget { | ||
const AppSettingsScreen(this.fwd, {Key key}) : super(key: key); | ||
|
||
final BackgroundForwarder fwd; | ||
|
||
@override | ||
_AppSettingsScreenState createState() => _AppSettingsScreenState(this.fwd); | ||
} | ||
|
||
class _AppSettingsScreenState extends State<AppSettingsScreen> { | ||
_AppSettingsScreenState(this.fwd); | ||
|
||
final BackgroundForwarder fwd; | ||
|
||
OutlineInputBorder _testMessageBorder; | ||
TextEditingController _testMessageController; | ||
|
||
bool _launchOnStartup = true; | ||
Map<String, bool> _forwardingResults; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
|
||
_testMessageController = new TextEditingController(text: "Test message"); | ||
_testMessageController.addListener(_onTextChanged); | ||
setState(_onTextChanged); | ||
|
||
_forwardingResults = { | ||
"HttpCallbackForwarder": null, | ||
"TelegramBotForwarder": null, | ||
"DeployedTelegramBotForwarder": null, | ||
}; | ||
|
||
fwd.loadFromPrefs(); | ||
SharedPreferences.getInstance().then((prefs) { | ||
setState(() { | ||
_launchOnStartup = | ||
prefs.getBool(SettingStrings.launchOnStartup) ?? true; | ||
}); | ||
}); | ||
} | ||
|
||
void _onTextChanged() { | ||
setState(() { | ||
_testMessageBorder = OutlineInputBorder( | ||
borderSide: BorderSide( | ||
color: _testMessageController.text.length > 0 | ||
? Colors.green | ||
: Colors.red)); | ||
}); | ||
} | ||
|
||
void _updatePreferences() async { | ||
final instance = await SharedPreferences.getInstance(); | ||
instance.setBool(SettingStrings.launchOnStartup, _launchOnStartup); | ||
} | ||
|
||
void _testForwarders() async { | ||
// ignore: invalid_use_of_visible_for_testing_member | ||
final results = await fwd.mgr.forward(SmsMessage.fromMap({ | ||
"address": "SmsForwarder", | ||
"body": _testMessageController.text, | ||
"date": DateTime.now().millisecondsSinceEpoch.toString(), | ||
}, [ | ||
SmsColumn.ADDRESS, | ||
SmsColumn.BODY, | ||
SmsColumn.DATE, | ||
])); | ||
setState(() { | ||
_forwardingResults = results; | ||
}); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final results = _forwardingResults.keys.toList(growable: false); | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: Text("App Settings"), | ||
), | ||
body: Center( | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||
children: <Widget>[ | ||
Row(children: <Widget>[ | ||
Text( | ||
"Launch on Startup: ", | ||
style: TextStyle(fontSize: 20), | ||
), | ||
Switch( | ||
activeColor: Colors.green, | ||
value: _launchOnStartup, | ||
onChanged: (value) { | ||
setState(() => _launchOnStartup = value); | ||
_updatePreferences(); | ||
}, | ||
) | ||
], mainAxisAlignment: MainAxisAlignment.spaceAround), | ||
Column(children: <Widget>[ | ||
Container( | ||
child: TextField( | ||
controller: _testMessageController, | ||
decoration: InputDecoration( | ||
border: _testMessageBorder, | ||
enabledBorder: _testMessageBorder, | ||
disabledBorder: _testMessageBorder, | ||
hintText: "Enter a test message"), | ||
), | ||
width: 350, | ||
), | ||
ElevatedButton( | ||
child: Text('Send Test Message'), | ||
onPressed: _testForwarders, | ||
), | ||
ListView.builder( | ||
padding: const EdgeInsets.all(8), | ||
shrinkWrap: true, | ||
itemCount: results.length, | ||
itemBuilder: (context, index) { | ||
final forwarder = results[index]; | ||
final result = _forwardingResults[forwarder]; | ||
Color color; | ||
if (result == null) { | ||
color = Colors.grey; | ||
} else if (result) { | ||
color = Colors.green; | ||
} else { | ||
color = Colors.red[200]; | ||
} | ||
return Container( | ||
height: 50, | ||
child: Center(child: Text(forwarder)), | ||
decoration: | ||
BoxDecoration(border: Border.all(), color: color), | ||
margin: EdgeInsets.symmetric(vertical: 1), | ||
); | ||
}, | ||
) | ||
]) | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} |
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,25 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:http/http.dart' as http; | ||
|
||
const String APP_VERSION = "v1.5.0"; | ||
const String GITHUB_URL = "https://github.com/optimalstrategy/" | ||
"sms_forwarder_app/releases/latest"; | ||
const String GITHUB_API_URL = "https://api.github.com/repos/" | ||
+ "optimalstrategy/sms_forwarder_app/releases/latest"; | ||
|
||
Future<bool> isUpdateAvailable() async { | ||
try { | ||
var r = await http.get(GITHUB_API_URL); | ||
if (r.statusCode != 200) return false; | ||
var json = jsonDecode(r.body); | ||
return json["tag_name"] != APP_VERSION; | ||
} on HttpException { | ||
return false; | ||
} on JsonUnsupportedObjectError { | ||
return false; | ||
} on StateError { | ||
return false; | ||
} | ||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.