From 357b934aa50e51148d14ff238919868095a45afd Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期二, 24 八月 2021 10:15:17 +0800
Subject: [PATCH] 2021-08-24 1.呼叫页面增加震动提醒
---
HDLLinphoneSDKDemoNew/HDLLinphoneSDK/src/main/java/com/hdl/hdllinphonesdk/core/service/HDLLinphoneService.java | 212 ++++++++++++++++++++++++++++++++++++++--------------
1 files changed, 154 insertions(+), 58 deletions(-)
diff --git a/HDLLinphoneSDKDemoNew/HDLLinphoneSDK/src/main/java/com/hdl/hdllinphonesdk/core/service/HDLLinphoneService.java b/HDLLinphoneSDKDemoNew/HDLLinphoneSDK/src/main/java/com/hdl/hdllinphonesdk/core/service/HDLLinphoneService.java
index 9472865..04b9089 100644
--- a/HDLLinphoneSDKDemoNew/HDLLinphoneSDK/src/main/java/com/hdl/hdllinphonesdk/core/service/HDLLinphoneService.java
+++ b/HDLLinphoneSDKDemoNew/HDLLinphoneSDK/src/main/java/com/hdl/hdllinphonesdk/core/service/HDLLinphoneService.java
@@ -1,6 +1,9 @@
package com.hdl.hdllinphonesdk.core.service;
import android.app.AlarmManager;
+import android.app.Notification;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
@@ -11,14 +14,14 @@
import android.os.Handler;
import android.os.IBinder;
import android.os.SystemClock;
+import android.support.v4.app.NotificationCompat;
-import com.hdl.hdllinphonesdk.HDLLinphoneKit;
import com.hdl.hdllinphonesdk.R;
import com.hdl.hdllinphonesdk.core.callback.PhoneCallback;
import com.hdl.hdllinphonesdk.core.callback.RegistrationCallback;
import com.hdl.hdllinphonesdk.core.linphone.KeepAliveHandler;
-import com.hdl.hdllinphonesdk.core.linphone.LinphoneUtils;
+import com.hdl.hdllinphonesdk.utils.HDLLog;
import org.linphone.core.Call;
import org.linphone.core.Core;
@@ -51,7 +54,7 @@
private PendingIntent mKeepAlivePendingIntent;
private static PhoneCallback sPhoneCallback;//閫氳瘽鐘舵�佸洖璋�
private static RegistrationCallback sRegistrationCallback;//璐﹀彿娉ㄥ唽鐧诲綍鐘舵�佸洖璋�
- private String mRingSoundFile,mPauseSoundFile = null;
+ private String mRingSoundFile, mPauseSoundFile = null;
private Handler mHandler;
private Timer mTimer;
@@ -73,6 +76,9 @@
}
public static Core getCore() {
+ if(sInstance == null){
+ return null;
+ }
return sInstance.mCore;
}
@@ -82,26 +88,35 @@
return null;
}
+ /**
+ * 娣诲姞閫氳瘽鐘舵�佸洖璋�
+ * @param phoneCallback
+ */
public static void addPhoneCallback(PhoneCallback phoneCallback) {
sPhoneCallback = phoneCallback;
}
-
public static void removePhoneCallback() {
if (sPhoneCallback != null) {
sPhoneCallback = null;
}
}
+ /**
+ * 娣诲姞娉ㄥ唽鐘舵�佸洖璋�
+ * @param registrationCallback
+ */
public static void addRegistrationCallback(RegistrationCallback registrationCallback) {
sRegistrationCallback = registrationCallback;
}
-
public static void removeRegistrationCallback() {
if (sRegistrationCallback != null) {
sRegistrationCallback = null;
}
}
+ /**
+ * 绉婚櫎鎵�浠allback
+ */
public void removeAllCallback() {
removePhoneCallback();
removeRegistrationCallback();
@@ -110,16 +125,13 @@
@Override
public void onCreate() {
super.onCreate();
-
+ hdlStartForeground();
initLinphone();
-
Intent intent = new Intent(this, KeepAliveHandler.class);
mKeepAlivePendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
((AlarmManager) this.getSystemService(Context.ALARM_SERVICE)).setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 60000, 60000, mKeepAlivePendingIntent);
-
}
-
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
@@ -162,7 +174,7 @@
@Override
public void onDestroy() {
removeAllCallback();
-
+ logout();
mCore.removeListener(mCoreListener);
mTimer.cancel();
mCore.stop();
@@ -174,6 +186,7 @@
((AlarmManager) this.getSystemService(Context.ALARM_SERVICE)).cancel(mKeepAlivePendingIntent);
+ hdlStopForeground();
super.onDestroy();
}
@@ -189,21 +202,18 @@
* 鍒濆鍖朙inphone
*/
private void initLinphone() {
-
String basePath = getFilesDir().getAbsolutePath();
Factory.instance().setLogCollectionPath(basePath);
Factory.instance().enableLogCollection(LogCollectionState.Enabled);
Factory.instance().setDebugMode(false, getString(R.string.app_name));
// Dump some useful information about the device we're running on
- Log.i(START_LINPHONE_LOGS);
- dumpDeviceInformation();
- dumpInstalledLinphoneInformation();
+// HDLLog.I(START_LINPHONE_LOGS);
+// dumpDeviceInformation();
+// dumpInstalledLinphoneInformation();
mHandler = new Handler();
// This will be our main Core listener, it will change activities depending on events
initCoreListener();
-
copyAssetsFromPackage(basePath);
-
// Create the Core and add our listener
mCore = Factory.instance()
.createCore(basePath + "/.linphonerc", basePath + "/linphonerc", this);
@@ -212,11 +222,14 @@
configureCore();
}
+ /**
+ * 鍒濆鍖朇oreListener鐩戝惉
+ */
private void initCoreListener() {
mCoreListener = new CoreListenerStub() {
@Override
public void onCallStateChanged(Core core, Call linphoneCall, Call.State state, String message) {
- Log.e(START_LINPHONE_LOGS, "callState: " + state.toString());
+ HDLLog.e(START_LINPHONE_LOGS, "callState: " + state.toString());
currentCallState = state;
if (sPhoneCallback != null) {
if (state == Call.State.IncomingReceived) {
@@ -237,20 +250,28 @@
@Override
public void onRegistrationStateChanged(Core core, ProxyConfig proxyConfig, RegistrationState registrationState, String message) {
-
String state = registrationState.toString();
- Log.i(START_LINPHONE_LOGS, "registrationState: " + state);
- if (sRegistrationCallback != null) {
+ HDLLog.i(START_LINPHONE_LOGS, "registrationState: " + state);
- if (state.equals(RegistrationState.None.toString())) {
+ if (state.equals(RegistrationState.None.toString())) {
+ if (sRegistrationCallback != null){
sRegistrationCallback.registrationNone();
- } else if (state.equals(RegistrationState.Progress.toString())) {
+ }
+ } else if (state.equals(RegistrationState.Progress.toString())) {
+ if (sRegistrationCallback != null){
sRegistrationCallback.registrationProgress();
- } else if (state.equals(RegistrationState.Ok.toString())) {
+ }
+ } else if (state.equals(RegistrationState.Ok.toString())) {
+ if (sRegistrationCallback != null) {
sRegistrationCallback.registrationOk();
- } else if (state.equals(RegistrationState.Cleared.toString())) {
+ }
+ } else if (state.equals(RegistrationState.Cleared.toString())) {
+ removeProxyConfig(core, proxyConfig);
+ if (sRegistrationCallback != null) {
sRegistrationCallback.registrationCleared();
- } else if (state.equals(RegistrationState.Failed.toString())) {
+ }
+ } else if (state.equals(RegistrationState.Failed.toString())) {
+ if (sRegistrationCallback != null) {
sRegistrationCallback.registrationFailed();
}
}
@@ -258,6 +279,26 @@
};
}
+ /**
+ * 鏀跺埌娉ㄩ攢鎴愬姛鍚庣Щ闄ゅ綋鍓嶄唬鐞�
+ * @param core
+ * @param proxyConfig
+ */
+ void removeProxyConfig(Core core, ProxyConfig proxyConfig) {
+ try {
+ HDLLog.i(START_LINPHONE_LOGS, "registrationState: 娉ㄩ攢鎴愬姛绉婚櫎璐﹀彿锛�" + proxyConfig.getIdentityAddress().getUsername());
+ //鏀跺埌娉ㄩ攢鎴愬姛鍚庣Щ闄ゅ綋鍓嶄唬鐞�
+ if (core != null) {
+ core.removeProxyConfig(proxyConfig);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * mCore鍚姩榛樿鍙傛暟閰嶇疆
+ */
private void configureCore() {
// We will create a directory for user signed certificates if needed
String basePath = getFilesDir().getAbsolutePath();
@@ -265,50 +306,48 @@
File f = new File(userCerts);
if (!f.exists()) {
if (!f.mkdir()) {
- Log.e(userCerts + " can't be created.");
+ HDLLog.E(userCerts + " can't be created.");
}
}
- setUserAgent();
+ //1.璁剧疆瀛樺偍鐢ㄦ埛x509璇佷功鐨勭洰褰曠殑璺緞
mCore.setUserCertificatesPath(userCerts);
+ //2.璁剧疆UserAgent
+ setUserAgent();
// mCore.setNativeRingingEnabled(true);
-//
// mCore.setRemoteRingbackTone(mRingSoundFile);
// mCore.setTone(ToneID.CallWaiting, mRingSoundFile);
- mCore.setRing(mRingSoundFile);
+// mCore.setRing(mRingSoundFile);
// mCore.setPlayFile(mPauseSoundFile);
-
// mCore.enableVideoCapture(false);//绂佺敤鎵嬫満鎽勫儚澶磋棰戦噰闆�
-
mCore.setNetworkReachable(true);
- //鍥炲0娑堥櫎
-// boolean isEchoCancellation = (boolean) SPUtils.get(mServiceContext, "echo_cancellation", true);
- //鍥炲0娑堥櫎
+ //3.鍥炲0娑堥櫎
mCore.enableEchoCancellation(true);
mCore.enableEchoLimiter(true);
-
- //鑷�傚簲鐮佺巼鎺у埗
-// boolean isAdaptiveRateControl = (boolean) SPUtils.get(mServiceContext, "adaptive_rate_control", true);
+ //4.鑷�傚簲鐮佺巼鎺у埗
mCore.enableAdaptiveRateControl(true);
- //audio 鐮佺巼璁剧疆
+ //5.audio 鐮佺巼璁剧疆
mCore.getConfig().setInt("audio", "codec_bitrate_limit", 36);
-
+ //6.瑙嗛灏哄鍋忓ソ璁剧疆 榛樿qvga
VideoDefinition preferredVideoDefinition =
Factory.instance().createVideoDefinitionFromName("qvga");
mCore.setPreferredVideoDefinition(preferredVideoDefinition);
-
+ //7.璁剧疆甯﹀闄愬埗锛屽甫瀹藉崟浣嶄负kbits/s, 0琛ㄧず鏃犻檺
mCore.setUploadBandwidth(1536);
mCore.setDownloadBandwidth(1536);
-
+ //8.璁剧疆鍚敤H264缂栫爜
mCore.getConfig().setBool("app", "open_h264_download_enabled", true);
+ //9.璁剧疆瑙嗛鐨勯粯璁ょ瓥鐣�
VideoActivationPolicy vap = mCore.getVideoActivationPolicy();
vap.setAutomaticallyInitiate(true);
mCore.setVideoActivationPolicy(vap);
-
- // 璁剧疆缂栫爜鏍煎紡
+ //10.璁剧疆缂栫爜鏍煎紡
setCodecMime();
}
+ /**
+ * 璁剧疆缂栫爜鏍煎紡
+ */
private void setCodecMime() {
PayloadType[] ptList = mCore.getAudioPayloadTypes();
for (PayloadType pt : ptList) {
@@ -330,6 +369,10 @@
mCore.setVideoPayloadTypes(ptVideoList);
}
+ /**
+ * 璁剧疆鏄惁鍚敤閾冨0锛岀洰鍓嶅彂鐜拌缃棤鏁堝緟鍒嗘瀽鍘熷洜
+ * @param use
+ */
public void enableDeviceRingtone(boolean use) {
if (use) {
mCore.setRing(null);
@@ -338,6 +381,9 @@
}
}
+ /**
+ * 璁剧疆UserAgent
+ */
private void setUserAgent() {
try {
String versionName = this.getPackageManager().getPackageInfo(this.getPackageName(),
@@ -351,6 +397,9 @@
}
}
+ /**
+ * 鎵撳嵃鐩稿叧淇℃伅
+ */
private void dumpDeviceInformation() {
StringBuilder sb = new StringBuilder();
sb.append("DEVICE=").append(Build.DEVICE).append("\n");
@@ -362,9 +411,12 @@
sb.append(abi).append(", ");
}
sb.append("\n");
- Log.i(sb.toString());
+ HDLLog.I(sb.toString());
}
+ /**
+ * 鎵撳嵃鐗堟湰鍙蜂俊鎭�
+ */
private void dumpInstalledLinphoneInformation() {
PackageInfo info = null;
try {
@@ -374,11 +426,11 @@
}
if (info != null) {
- Log.i(
+ HDLLog.i(
"[Service] Linphone version is ",
info.versionName + " (" + info.versionCode + ")");
} else {
- Log.i("[Service] Linphone version is unknown");
+ HDLLog.I("[Service] Linphone version is unknown");
}
}
@@ -393,18 +445,10 @@
// The factory config is used to override any other setting, let's copy it each time
copyFromPackage(R.raw.linphonerc_factory, "linphonerc");
mRingSoundFile = basePath + "/oldphone_mono.wav";
- copyIfNotExist(R.raw.oldphone_mono, basePath + "/oldphone_mono.wav");
- copyIfNotExist(R.raw.ringback, basePath + "/ringback.wav");
- mPauseSoundFile = basePath + "/toy_mono.wav";
- copyIfNotExist( R.raw.toy_mono, basePath + "/toy_mono.wav");
-
-// LinphoneUtils.copyIfNotExist(mServiceContext, R.raw.ringback, mRingBackSoundFile);
-// LinphoneUtils.copyIfNotExist(mServiceContext, R.raw.toy_mono, mPauseSoundFile);
-// LinphoneUtils.copyIfNotExist(mServiceContext, R.raw.linphonerc_default, mLinphoneConfigFile);
-// LinphoneUtils.copyIfNotExist(mServiceContext, R.raw.linphonerc_factory, new File(mLinphoneFactoryConfigFile).getName());
-// LinphoneUtils.copyIfNotExist(mServiceContext, R.raw.lpconfig, mLPConfigXsd);
-// LinphoneUtils.copyIfNotExist(mServiceContext, R.raw.rootca, mLinphoneRootCaFile);
-
+ copyIfNotExist(R.raw.oldphone_mono, mRingSoundFile);
+// copyIfNotExist(R.raw.ringback, basePath + "/ringback.wav");
+// mPauseSoundFile = basePath + "/toy_mono.wav";
+// copyIfNotExist(R.raw.toy_mono, basePath + "/toy_mono.wav");
} catch (IOException ioe) {
Log.e(ioe);
@@ -412,6 +456,12 @@
}
+ /**
+ * copyIfNotExist
+ * @param ressourceId
+ * @param target
+ * @throws IOException
+ */
private void copyIfNotExist(int ressourceId, String target) throws IOException {
File lFileToCopy = new File(target);
if (!lFileToCopy.exists()) {
@@ -419,6 +469,12 @@
}
}
+ /**
+ * copyFromPackage
+ * @param ressourceId
+ * @param target
+ * @throws IOException
+ */
private void copyFromPackage(int ressourceId, String target) throws IOException {
FileOutputStream lOutputStream = openFileOutput(target, 0);
InputStream lInputStream = getResources().openRawResource(ressourceId);
@@ -438,4 +494,44 @@
public Call.State getCurrentCallState() {
return currentCallState;
}
+
+ /**
+ * 娉ㄩ攢鎵�鏈夎处鍙�
+ */
+ void logout() {
+ try {
+ if (mCore != null) {
+ ProxyConfig[] configs = mCore.getProxyConfigList();
+ for (ProxyConfig config : configs) {
+ if (config != null) {
+ config.edit();
+ config.enableRegister(false);
+ config.done();
+ }
+ }
+ HDLLog.E("娉ㄩ攢鎵�鏈夎处鍙�");
+// core.clearAllAuthInfo();
+// core.clearProxyConfig();
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ //閫氱煡ID
+ public static final int NOTIFICATION_ID = 2;
+ /**
+ * 瑙e喅android8.0浠ヤ笂鏃犳硶鍚姩鏈嶅姟鐨勯棶棰�
+ */
+ void hdlStartForeground() {
+
+ }
+ /**
+ * stopForeground
+ */
+ void hdlStopForeground() {
+// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+// stopForeground(true);
+// }
+ }
}
--
Gitblit v1.8.0