From 04a464f3befce773b3887aa90c07687f1ef98265 Mon Sep 17 00:00:00 2001 From: white-yj8109 <19310195525@163.com> Date: Sat, 17 Jan 2026 19:39:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20=E9=80=9A=E8=AF=9D?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E4=BE=BF=E7=AD=BE=E5=88=9B=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ui/CallRecordReceiver.java | 102 ++++++++++++++++++++++++++++++++ src/ui/CallRecordService.java | 105 +++++++++++++++++++++++++++++++++ 2 files changed, 207 insertions(+) create mode 100644 src/ui/CallRecordReceiver.java create mode 100644 src/ui/CallRecordService.java diff --git a/src/ui/CallRecordReceiver.java b/src/ui/CallRecordReceiver.java new file mode 100644 index 0000000..8683e9d --- /dev/null +++ b/src/ui/CallRecordReceiver.java @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.micode.notes.ui; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.telephony.TelephonyManager; +import android.util.Log; + +import net.micode.notes.data.Notes; + +/** + * 通话记录广播接收器,用于监听系统通话状态变化 + */ +public class CallRecordReceiver extends BroadcastReceiver { + private static final String TAG = "CallRecordReceiver"; + private static String mLastPhoneNumber;// 记录最后一个电话号码 + private static long mCallStartTime;// 记录通话开始时间 + private static boolean mIsOutgoingCall;// 是否为拨出电话 + + @Override + public void onReceive(Context context, Intent intent) { + if (intent.getAction() != null) { + Log.d(TAG, "Received broadcast: " + intent.getAction()); + + if (intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) { + String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); + String phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); + + Log.d(TAG, "Phone state changed: " + state + ", number: " + phoneNumber); + + if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) { + // 电话响铃,记录来电号码 + mLastPhoneNumber = phoneNumber; + mCallStartTime = System.currentTimeMillis(); + mIsOutgoingCall = false; + Log.d(TAG, "Incoming call ringing: " + phoneNumber); + } else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state)) { + // 电话接通,记录开始时间 + if (mCallStartTime == 0) { + mCallStartTime = System.currentTimeMillis(); + } + if (phoneNumber != null) { + mLastPhoneNumber = phoneNumber; + } + Log.d(TAG, "Call answered"); + } else if (TelephonyManager.EXTRA_STATE_IDLE.equals(state)) { + // 电话挂断,创建通话记录便签 + if (mLastPhoneNumber != null && mCallStartTime > 0) { + long callEndTime = System.currentTimeMillis(); + long callDuration = callEndTime - mCallStartTime; + + // 创建通话记录便签 + Log.d(TAG, "Call ended, creating call note for: " + mLastPhoneNumber); + Intent serviceIntent = new Intent(context, CallRecordService.class); + serviceIntent.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER, mLastPhoneNumber); + serviceIntent.putExtra("call_date", callEndTime); + serviceIntent.putExtra("call_duration", callDuration); + + // 启动服务创建通话记录便签 + try { + context.startService(serviceIntent); + Log.d(TAG, "Successfully started CallRecordService"); + } catch (Exception e) { + Log.e(TAG, "Error starting CallRecordService", e); + } + } + + // 重置状态 + mLastPhoneNumber = null; + mCallStartTime = 0; + mIsOutgoingCall = false; + Log.d(TAG, "Call state reset to idle"); + } + } else if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) { + // 拨出电话 + String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); + if (phoneNumber != null) { + mLastPhoneNumber = phoneNumber; + mCallStartTime = System.currentTimeMillis(); + mIsOutgoingCall = true; + Log.d(TAG, "Outgoing call started: " + phoneNumber); + } + } + } + } +} diff --git a/src/ui/CallRecordService.java b/src/ui/CallRecordService.java new file mode 100644 index 0000000..15451ef --- /dev/null +++ b/src/ui/CallRecordService.java @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.micode.notes.ui; + +import android.app.IntentService; +import android.content.Intent; +import android.util.Log; + +import net.micode.notes.data.Notes; +import net.micode.notes.model.WorkingNote; +import net.micode.notes.tool.ResourceParser; + +/** + * 通话记录服务,用于创建通话记录便签 + */ +public class CallRecordService extends IntentService { + private static final String TAG = "CallRecordService"; + + public CallRecordService() { + super("CallRecordService"); + } + + @Override + protected void onHandleIntent(Intent intent) { + if (intent != null) { + String phoneNumber = intent.getStringExtra(android.telephony.TelephonyManager.EXTRA_INCOMING_NUMBER); + long callDate = intent.getLongExtra("call_date", System.currentTimeMillis()); + long callDuration = intent.getLongExtra("call_duration", 0); + + if (phoneNumber != null) { + Log.d(TAG, "Creating call note for number: " + phoneNumber + ", date: " + callDate); + createCallNote(phoneNumber, callDate, callDuration); + } + } + } + + /** + * 创建通话记录便签并跳转到编辑视图 + * @param phoneNumber 电话号码 + * @param callDate 通话时间 + * @param callDuration 通话时长 + */ + private void createCallNote(String phoneNumber, long callDate, long callDuration) { + try { + Log.d(TAG, "Starting createCallNote for number: " + phoneNumber); + + // 创建空便签 + WorkingNote note = WorkingNote.createEmptyNote(this, Notes.ID_CALL_RECORD_FOLDER, + 0, Notes.TYPE_WIDGET_INVALIDE, ResourceParser.BLUE); + + // 转换为通话便签 + note.convertToCallNote(phoneNumber, callDate); + + // 设置空格作为内容,确保能保存 + note.setWorkingText(" "); + + // 保存便签获取ID + long noteId = -1; + if (note.saveNote()) { + noteId = note.getNoteId(); + Log.d(TAG, "Call note created successfully for: " + phoneNumber + ", noteId: " + noteId); + } else { + Log.e(TAG, "Failed to create call note for: " + phoneNumber); + return; + } + + // 跳转到刚刚创建的通话记录便签的编辑视图 + if (noteId > 0) { + Log.d(TAG, "Attempting to start NoteEditActivity for noteId: " + noteId); + Intent intent = new Intent(this, NoteEditActivity.class); + // 使用 ACTION_VIEW 以便 NoteEditActivity 正确处理并打开已有便签 + intent.setAction(Intent.ACTION_VIEW); + // 使用正确的键名传递便签ID + intent.putExtra(Intent.EXTRA_UID, noteId); + intent.putExtra(Notes.INTENT_EXTRA_CALL_DATE, callDate); + intent.putExtra("phone_number", phoneNumber); + // 从服务启动 Activity,需要 NEW_TASK 标志;避免清除整个任务栈 + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + try { + startActivity(intent); + Log.d(TAG, "Successfully started NoteEditActivity for call note: " + noteId); + } catch (Exception e) { + Log.e(TAG, "Error starting NoteEditActivity", e); + } + } + } catch (Exception e) { + Log.e(TAG, "Error creating call note", e); + } + } + +} -- 2.34.1