You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
7.2 KiB

2 years ago
import 'dart:async';
2 years ago
import 'dart:ui';
2 years ago
2 years ago
import 'package:call_log/call_log.dart';
import 'package:flustars/flustars.dart';
2 years ago
import 'package:flutter/cupertino.dart';
2 years ago
import 'package:flutter/material.dart';
2 years ago
import 'package:flutter_background_service/flutter_background_service.dart';
2 years ago
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:project_telephony/model/send_number_model.dart';
2 years ago
import 'package:shared_preferences/shared_preferences.dart';
2 years ago
import 'package:telephony/telephony.dart';
2 years ago
import '../../base/DBManager.dart';
import '../user/user_page.dart';
2 years ago
const nId = "my_foreground";
const notificationId = 888;
2 years ago
Future<void> initializeService() async {
2 years ago
final service = FlutterBackgroundService();
2 years ago
const AndroidNotificationChannel channel = AndroidNotificationChannel(
nId, // id
'我的前台服务', // title
description: '此通道用于重要通知。', // description
2 years ago
importance: Importance.low, // importance must be at low or higher level
);
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
2 years ago
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
2 years ago
?.createNotificationChannel(channel);
2 years ago
await service.configure(
iosConfiguration: IosConfiguration(
// auto start service
autoStart: true,
// this will be executed when app is in foreground in separated isolate
onForeground: onStart,
// you have to enable background fetch capability on xcode project
onBackground: onIosBackground,
),
2 years ago
androidConfiguration: AndroidConfiguration(
// this will be executed when app is in foreground or background in separated isolate
onStart: onStart,
2 years ago
2 years ago
// auto start service
autoStart: true,
isForegroundMode: true,
2 years ago
notificationChannelId: nId,
// this must match with notification channel you created above.
2 years ago
initialNotificationTitle: 'AWESOME SERVICE',
initialNotificationContent: 'Initializing',
foregroundServiceNotificationId: notificationId,
2 years ago
),
2 years ago
);
2 years ago
// service.startService();
2 years ago
}
2 years ago
bool onIosBackground(ServiceInstance service) {
2 years ago
WidgetsFlutterBinding.ensureInitialized();
2 years ago
print('FLUTTER BACKGROUND FETCH');
return true;
2 years ago
}
2 years ago
// @pragma('vm:entry-point')
2 years ago
void onStart(ServiceInstance service) async {
2 years ago
DartPluginRegistrant.ensureInitialized();
2 years ago
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
2 years ago
service.on("stopService").listen((event) {
service.stopSelf();
});
//定义
2 years ago
int flag = 0;
2 years ago
String? phoneNum = "";
String callState;
bool fff=false;
2 years ago
2 years ago
Timer.periodic(const Duration(seconds: 1), (timer) async {
2 years ago
//数据储存读取
2 years ago
final SharedPreferences prefs = await SharedPreferences.getInstance();
2 years ago
//获取来电状态
2 years ago
CallState state = await Telephony.instance.callState;
callState = state.name;
List<String> noNumberList = prefs.getStringList("specified") ?? [];
flutterLocalNotificationsPlugin.show(
notificationId,
'短信帮手',
2 years ago
'欢迎使用短信帮手(请勿关闭)',
// '$callState ${DateTime.now().second}',
const NotificationDetails(
android: AndroidNotificationDetails(
nId,
'我的前台服务',
icon: 'ic_bg_service_small',
ongoing: true,
),
),
);
2 years ago
2 years ago
//发送内容信息
2 years ago
String content = prefs.getString("refSms") ?? "现在有事,等会回电";
2 years ago
//发送信息的状态
2 years ago
int? numberSet = prefs.getInt("numIndex") ?? 1;
int? dayTimes =prefs.getInt("dayTimes")??0;
2 years ago
print("号码设置$numberSet 发送内容$content 指定不发送$noNumberList");
// print(flag);
2 years ago
if (callState == "IDLE") {
2 years ago
if (flag != 0) {
print(dayTimes);
2 years ago
final Iterable<CallLogEntry> entry = await CallLog.query();
2 years ago
phoneNum = entry.first.number;
var phone = SendNumber(
sendNumber: phoneNum,
createdAt: DateUtil.formatDate(DateTime.now()),
);
var s = await DBManager().find(phoneNum!);
if (s!.isEmpty) {
await DBManager().saveData(phone);
fff=true;
}else if(DateTime.parse(s.first.createdAt!)
.add(Duration(days: dayTimes))
.isBefore(DateTime.now())) {
await DBManager().update(phone);
fff=true;
}
if(fff){
switch (numberSet) {
case 0:
if (!noNumberList.contains(phoneNum)) {
print("所有都发+号码设置$numberSet+发送内容$content");
2 years ago
Telephony.backgroundInstance
.sendSms(to: phoneNum!, message: content);
flag = 0;
} else {
2 years ago
print("指定不发送");
flag = 0;
}
break;
case 1:
if (flag > 0) {
// print("来电拒接/未接");
// print("${phoneNum!}:${ref!}");
if (!noNumberList.contains(phoneNum)) {
print("来电拒接+号码设置$numberSet+发送内容$content");
2 years ago
Telephony.backgroundInstance
.sendSms(to: phoneNum!, message: content);
flag = 0;
} else {
print("指定不发送");
flag = 0;
2 years ago
}
// print("发送成功");
} else if (flag == -1) {
// print("来电接听");
2 years ago
// print("${phoneNum!}:$content");
if (!noNumberList.contains(phoneNum)) {
print("来电接听+号码设置$numberSet+发送内容$content");
2 years ago
Telephony.backgroundInstance
.sendSms(to: phoneNum!, message: content);
flag = 0;
} else {
2 years ago
print("指定不发送");
flag = 0;
}
}
break;
case 2:
if (flag == -2) {
if (entry.first.duration! > 0) {
// print("${phoneNum!}:$content");
if (!noNumberList.contains(phoneNum)) {
print("去电接听+号码设置$numberSet+发送内容$content");
Telephony.backgroundInstance
.sendSms(to: phoneNum!, message: content);
}
} else {
// print("${phoneNum!}:$content");
if (!noNumberList.contains(phoneNum)) {
print("去电挂断+号码设置$numberSet+发送内容$content");
Telephony.backgroundInstance
.sendSms(to: phoneNum!, message: content);
} else {
print("指定不发送");
flag = 0;
}
}
}
flag = 0;
break;
}
fff=false;
2 years ago
}
2 years ago
}
2 years ago
} else if (callState == "RINGING") {
flag = 1;
2 years ago
print("打印");
2 years ago
} else if (callState == "OFFHOOK") {
if (flag > 0) flag *= -1;
if (flag == 0) flag = -2;
2 years ago
print("打印");
2 years ago
}
2 years ago
});
}