Skip to content

Commit 92e7361

Browse files
committed
fix(android): accounts shared preferences, garbage collector issue
1 parent c72ad2b commit 92e7361

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

android/src/main/java/com/carusto/ReactNativePjSip/PjSipService.java

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,18 @@
6161
import java.util.ArrayList;
6262
import java.util.LinkedList;
6363
import java.util.List;
64+
import java.util.ListIterator;
6465
import java.util.Map;
6566

67+
import com.google.gson.Gson;
68+
import com.google.gson.reflect.TypeToken;
69+
6670
public class PjSipService extends Service {
6771

6872
private static String TAG = "PjSipService";
6973

74+
private static String ACCOUNTS = "ACCOUNTS";
75+
7076
public static final String STARTED_FROM_SERVICE = "started_from_service";
7177

7278
private boolean mInitialized;
@@ -91,6 +97,8 @@ public class PjSipService extends Service {
9197

9298
private List<PjSipAccount> mAccounts = new ArrayList<>();
9399

100+
private List<AccountConfigurationDTO> mAccountsCfg = new ArrayList<>();
101+
94102
private List<PjSipCall> mCalls = new ArrayList<>();
95103

96104
// 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) {
324332
mInitialized = true;
325333
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
326334

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+
327344
job(new Runnable() {
328345
@Override
329346
public void run() {
@@ -413,7 +430,6 @@ public void run() {
413430

414431
// Remove account in PjSip
415432
account.delete();
416-
417433
}
418434

419435
public void evict(final PjSipCall call) {
@@ -596,13 +612,23 @@ private void handleAccountRegister(Intent intent) {
596612
}
597613

598614
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);
600631
}
601-
602-
account.register(renew);
603-
604-
// -----
605-
mEmitter.fireIntentHandled(intent);
606632
} catch (Exception e) {
607633
mEmitter.fireIntentHandled(intent, e);
608634
}
@@ -718,6 +744,12 @@ private PjSipAccount doAccountCreate(AccountConfigurationDTO configuration) thro
718744
mTrash.add(cfg);
719745
mTrash.add(cred);
720746

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+
721753
mAccounts.add(account);
722754

723755
return account;
@@ -739,6 +771,16 @@ private void handleAccountDelete(Intent intent) {
739771
throw new Exception("Account with \"" + accountId + "\" id not found");
740772
}
741773

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+
742784
evict(account);
743785

744786
// -----

android/src/main/java/com/carusto/ReactNativePjSip/dto/AccountConfigurationDTO.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
public class AccountConfigurationDTO {
99

10+
private Integer id;
11+
1012
public String name;
1113

1214
public String username;
@@ -34,6 +36,14 @@ public class AccountConfigurationDTO {
3436

3537
public boolean regOnAdd;
3638

39+
public Integer getId() {
40+
return id;
41+
}
42+
43+
public void setId(Integer id) {
44+
this.id = id;
45+
}
46+
3747
public String getName() {
3848
return name;
3949
}

0 commit comments

Comments
 (0)