61
61
import java .util .ArrayList ;
62
62
import java .util .LinkedList ;
63
63
import java .util .List ;
64
+ import java .util .ListIterator ;
64
65
import java .util .Map ;
65
66
67
+ import com .google .gson .Gson ;
68
+ import com .google .gson .reflect .TypeToken ;
69
+
66
70
public class PjSipService extends Service {
67
71
68
72
private static String TAG = "PjSipService" ;
69
73
74
+ private static String ACCOUNTS = "ACCOUNTS" ;
75
+
70
76
public static final String STARTED_FROM_SERVICE = "started_from_service" ;
71
77
72
78
private boolean mInitialized ;
@@ -91,6 +97,8 @@ public class PjSipService extends Service {
91
97
92
98
private List <PjSipAccount > mAccounts = new ArrayList <>();
93
99
100
+ private List <AccountConfigurationDTO > mAccountsCfg = new ArrayList <>();
101
+
94
102
private List <PjSipCall > mCalls = new ArrayList <>();
95
103
96
104
// In order to ensure that GC will not destroy objects that are used in PJSIP
@@ -324,6 +332,15 @@ public int onStartCommand(final Intent intent, int flags, int startId) {
324
332
mInitialized = true ;
325
333
mSharedPreferences = PreferenceManager .getDefaultSharedPreferences (getApplicationContext ());
326
334
335
+ String jsonAccountsCfg = mSharedPreferences .getString (ACCOUNTS , "" );
336
+ if (jsonAccountsCfg .isEmpty () || jsonAccountsCfg .equals ("[]" )) {
337
+ mAccountsCfg = new ArrayList <>();
338
+ } else {
339
+ Gson gson = new Gson ();
340
+ Type type = new TypeToken <ArrayList <AccountConfigurationDTO >>(){}.getType ();
341
+ mAccountsCfg = gson .fromJson (jsonAccountsCfg , type );
342
+ }
343
+
327
344
job (new Runnable () {
328
345
@ Override
329
346
public void run () {
@@ -413,7 +430,6 @@ public void run() {
413
430
414
431
// Remove account in PjSip
415
432
account .delete ();
416
-
417
433
}
418
434
419
435
public void evict (final PjSipCall call ) {
@@ -596,13 +612,23 @@ private void handleAccountRegister(Intent intent) {
596
612
}
597
613
598
614
if (account == null ) {
599
- throw new Exception ("Account with \" " + accountId + "\" id not found" );
615
+ AccountConfigurationDTO accCfg = null ;
616
+ for (AccountConfigurationDTO cfg : mAccountsCfg ) {
617
+ if (cfg .getId () == accountId ) {
618
+ accCfg = cfg ;
619
+ break ;
620
+ }
621
+ }
622
+ if (accCfg != null ) {
623
+ PjSipAccount acc = doAccountCreate (accCfg );
624
+ mEmitter .fireAccountCreated (intent , acc );
625
+ } else {
626
+ throw new Exception ("Register Account with \" " + accountId + "\" id not found" );
627
+ }
628
+ } else {
629
+ account .register (renew );
630
+ mEmitter .fireIntentHandled (intent );
600
631
}
601
-
602
- account .register (renew );
603
-
604
- // -----
605
- mEmitter .fireIntentHandled (intent );
606
632
} catch (Exception e ) {
607
633
mEmitter .fireIntentHandled (intent , e );
608
634
}
@@ -718,6 +744,12 @@ private PjSipAccount doAccountCreate(AccountConfigurationDTO configuration) thro
718
744
mTrash .add (cfg );
719
745
mTrash .add (cred );
720
746
747
+ configuration .setId (account .getId ());
748
+ mAccountsCfg .add (configuration );
749
+ Gson gson = new Gson ();
750
+ String jsonAccCfg = gson .toJson (mAccountsCfg );
751
+ mSharedPreferences .edit ().putString (ACCOUNTS , jsonAccCfg ).apply ();
752
+
721
753
mAccounts .add (account );
722
754
723
755
return account ;
@@ -739,6 +771,16 @@ private void handleAccountDelete(Intent intent) {
739
771
throw new Exception ("Account with \" " + accountId + "\" id not found" );
740
772
}
741
773
774
+ ListIterator <AccountConfigurationDTO > iter = mAccountsCfg .listIterator ();
775
+ while (iter .hasNext ()){
776
+ if (iter .next ().getId ().equals (account .getId ())){
777
+ iter .remove ();
778
+ }
779
+ }
780
+ Gson gson = new Gson ();
781
+ String jsonAccountsCfg = gson .toJson (mAccountsCfg );
782
+ mSharedPreferences .edit ().putString (ACCOUNTS , jsonAccountsCfg ).apply ();
783
+
742
784
evict (account );
743
785
744
786
// -----
0 commit comments