From c7f797851aadaeab49a83ce285b6c3fff2971517 Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期五, 20 八月 2021 11:03:43 +0800
Subject: [PATCH] 2021-08-20 1.优化更新

---
 HDLLinphoneSDKDemoNew/HDLLinphoneSDK/src/main/java/com/hdl/hdllinphonesdk/core/service/HDLLinphoneService.java |  183 +++++++++++++++++++++++++++++++++++----------
 1 files changed, 141 insertions(+), 42 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..8772ac9 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,19 +306,18 @@
         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.");
             }
         }
+        //璁剧疆UserAgent
         setUserAgent();
 
         mCore.setUserCertificatesPath(userCerts);
 //        mCore.setNativeRingingEnabled(true);
-//
 //        mCore.setRemoteRingbackTone(mRingSoundFile);
 //        mCore.setTone(ToneID.CallWaiting, mRingSoundFile);
         mCore.setRing(mRingSoundFile);
 //        mCore.setPlayFile(mPauseSoundFile);
-
 //        mCore.enableVideoCapture(false);//绂佺敤鎵嬫満鎽勫儚澶磋棰戦噰闆�
 
         mCore.setNetworkReachable(true);
@@ -288,7 +328,6 @@
         mCore.enableEchoLimiter(true);
 
         //鑷�傚簲鐮佺巼鎺у埗
-//        boolean isAdaptiveRateControl = (boolean) SPUtils.get(mServiceContext, "adaptive_rate_control", true);
         mCore.enableAdaptiveRateControl(true);
         //audio 鐮佺巼璁剧疆
         mCore.getConfig().setInt("audio", "codec_bitrate_limit", 36);
@@ -309,6 +348,9 @@
         setCodecMime();
     }
 
+    /**
+     * 璁剧疆缂栫爜鏍煎紡
+     */
     private void setCodecMime() {
         PayloadType[] ptList = mCore.getAudioPayloadTypes();
         for (PayloadType pt : ptList) {
@@ -330,6 +372,10 @@
         mCore.setVideoPayloadTypes(ptVideoList);
     }
 
+    /**
+     * 璁剧疆鏄惁鍚敤閾冨0锛岀洰鍓嶅彂鐜拌缃棤鏁堝緟鍒嗘瀽鍘熷洜
+     * @param use
+     */
     public void enableDeviceRingtone(boolean use) {
         if (use) {
             mCore.setRing(null);
@@ -338,6 +384,9 @@
         }
     }
 
+    /**
+     * 璁剧疆UserAgent
+     */
     private void setUserAgent() {
         try {
             String versionName = this.getPackageManager().getPackageInfo(this.getPackageName(),
@@ -351,6 +400,9 @@
         }
     }
 
+    /**
+     * 鎵撳嵃鐩稿叧淇℃伅
+     */
     private void dumpDeviceInformation() {
         StringBuilder sb = new StringBuilder();
         sb.append("DEVICE=").append(Build.DEVICE).append("\n");
@@ -362,9 +414,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 +429,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 +448,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.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.toy_mono, basePath + "/toy_mono.wav");
 
         } catch (IOException ioe) {
             Log.e(ioe);
@@ -412,6 +459,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 +472,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 +497,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