优化 通话记录便签创建

pull/24/head
white-yj8109 1 month ago
parent a4be260753
commit 04a464f3be

@ -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);
}
}
}
}
}

@ -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);
}
}
}
Loading…
Cancel
Save