diff --git a/lib/providers/user_provider.dart b/lib/providers/user_provider.dart index 1dde2a9..8f1caae 100644 --- a/lib/providers/user_provider.dart +++ b/lib/providers/user_provider.dart @@ -5,6 +5,7 @@ import 'package:project_telephony/utils/user_tool.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../constants/api.dart'; +import '../model/exclude_phone_model.dart'; import '../model/login_info_model.dart'; import '../model/network/api_client.dart'; import '../model/user_info_model.dart'; @@ -52,8 +53,9 @@ class UserProvider extends ChangeNotifier { _isLogin = true; //每次打开app更新用户信息 await updateUserInfo(); - updateConSms(); - updateRefSms(); + updateConSms(); + updateRefSms(); + getExclude(); } Future logout() async { @@ -79,11 +81,13 @@ class UserProvider extends ChangeNotifier { var base = await apiClient.request(API.app.find, data: {'status': 1}); if (base.code == 0) { await prefs.remove('conSms'); - await prefs.setString('conSms', (UserTool.userProvider.userInfo.tag)==""?"${base - .data['content']}":"【${UserTool.userProvider.userInfo.tag}】${base - .data['content']}"); + await prefs.setString( + 'conSms', + (UserTool.userProvider.userInfo.tag) == "" + ? "${base.data['content']}" + : "【${UserTool.userProvider.userInfo.tag}】${base.data['content']}"); service.invoke("stopService"); - if(prefs.getBool('kg')!){ + if (prefs.getBool('kg')!) { Future.delayed(const Duration(seconds: 1), () async { service.startService(); }); @@ -91,7 +95,6 @@ class UserProvider extends ChangeNotifier { } else { CloudToast.show(base.msg); } - notifyListeners(); } @@ -101,11 +104,32 @@ class UserProvider extends ChangeNotifier { var base = await apiClient.request(API.app.find, data: {'status': 2}); if (base.code == 0) { await prefs.remove('refSms'); - await prefs.setString('refSms', (UserTool.userProvider.userInfo.tag)==""?"${base - .data['content']}":"【${UserTool.userProvider.userInfo.tag}】${base - .data['content']}" ); + await prefs.setString( + 'refSms', + (UserTool.userProvider.userInfo.tag) == "" + ? "${base.data['content']}" + : "【${UserTool.userProvider.userInfo.tag}】${base.data['content']}"); + service.invoke("stopService"); + if (prefs.getBool('kg')!) { + Future.delayed(const Duration(seconds: 1), () async { + service.startService(); + }); + } + } else { + CloudToast.show(base.msg); + } + notifyListeners(); + } + + Future getExclude() async { + final prefs = await SharedPreferences.getInstance(); + final service = FlutterBackgroundService(); + var base = await apiClient.request(API.exclude.find); + if (base.code == 0) { + await prefs.remove('exclude'); + await prefs.setStringList('exclude', base.data); service.invoke("stopService"); - if(prefs.getBool('kg')!){ + if (prefs.getBool('kg')!) { Future.delayed(const Duration(seconds: 1), () async { service.startService(); }); diff --git a/lib/ui/home/call.dart b/lib/ui/home/call.dart index b19c050..6c3bba7 100644 --- a/lib/ui/home/call.dart +++ b/lib/ui/home/call.dart @@ -1,14 +1,8 @@ import 'dart:async'; import 'dart:ui'; - import 'package:call_log/call_log.dart'; - import 'package:flutter/cupertino.dart'; import 'package:flutter_background_service/flutter_background_service.dart'; -import 'package:flutter_background_service_android/flutter_background_service_android.dart'; -import 'package:flutter_background_service_android/flutter_background_service_android.dart'; -import 'package:project_telephony/utils/headers.dart'; - import 'package:shared_preferences/shared_preferences.dart'; import 'package:telephony/telephony.dart'; @@ -63,32 +57,44 @@ void onStart(ServiceInstance service) async { final SharedPreferences prefs = await SharedPreferences.getInstance(); CallState state = await Telephony.instance.callState; callState = state.name; - print(callState+"$flag"); + print(callState + "$flag"); String? ref = prefs.getString('refSms'); String? con = prefs.getString('conSms'); - // print(con); if (callState == "IDLE") { + //闲置 if (flag != 0) { final Iterable entry = await CallLog.query(); phoneNum = entry.first.number; callRecords = entry.first.duration; print(phoneNum); if (flag > 0) { - print("2"); + print("来电拒接/未接"); Telephony.backgroundInstance.sendSms(to: phoneNum!, message: ref!); print("发送成功"); - } else { - print("3"); + } else if(flag==-1){ + print("来电接听"); + Telephony.backgroundInstance.sendSms(to: phoneNum!, message: con!); + }else { + print(entry.first.callType); + print(entry.first.duration); + if(entry.first.duration!>0){ + print("去电接听"); + }else{ + print("去电未接"); + } Telephony.backgroundInstance.sendSms(to: phoneNum!, message: con!); print("发送成功"); } flag = 0; } } else if (callState == "RINGING") { - flag++; + //响铃 + flag=1; print('通话'); } else if (callState == "OFFHOOK") { + //通话 if (flag > 0) flag *= -1; + if(flag==0)flag=-2; print('不通话'); } });