Fixed issue with default linphonerc when app is installed for the first time

This commit is contained in:
Sylvain Berfini 2019-05-17 09:41:50 +02:00
parent f74f6e5911
commit 5aeebfeb72
4 changed files with 71 additions and 64 deletions

View file

@ -41,9 +41,6 @@ import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.CheckBox; import android.widget.CheckBox;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.Date; import java.util.Date;
import java.util.Timer; import java.util.Timer;
@ -81,11 +78,7 @@ import org.linphone.utils.PushNotificationUtils;
/** Handles Linphone's Core lifecycle */ /** Handles Linphone's Core lifecycle */
public class LinphoneManager implements SensorEventListener { public class LinphoneManager implements SensorEventListener {
private final String mConfigFile;
private final String mLPConfigXsd;
private final String mBasePath; private final String mBasePath;
private final String mLinphoneFactoryConfigFile;
private final String mLinphoneDynamicConfigFile, mDefaultDynamicConfigFile;
private final String mRingSoundFile; private final String mRingSoundFile;
private final String mCallLogDatabaseFile; private final String mCallLogDatabaseFile;
private final String mFriendsDatabaseFile; private final String mFriendsDatabaseFile;
@ -120,11 +113,6 @@ public class LinphoneManager implements SensorEventListener {
mExited = false; mExited = false;
mContext = c; mContext = c;
mBasePath = c.getFilesDir().getAbsolutePath(); mBasePath = c.getFilesDir().getAbsolutePath();
mLPConfigXsd = mBasePath + "/lpconfig.xsd";
mLinphoneFactoryConfigFile = mBasePath + "/linphonerc";
mConfigFile = mBasePath + "/.linphonerc";
mLinphoneDynamicConfigFile = mBasePath + "/linphone_assistant_create.rc";
mDefaultDynamicConfigFile = mBasePath + "/default_assistant_create.rc";
mCallLogDatabaseFile = mBasePath + "/linphone-log-history.db"; mCallLogDatabaseFile = mBasePath + "/linphone-log-history.db";
mFriendsDatabaseFile = mBasePath + "/linphone-friends.db"; mFriendsDatabaseFile = mBasePath + "/linphone-friends.db";
mRingSoundFile = mBasePath + "/share/sounds/linphone/rings/notes_of_the_optimistic.mkv"; mRingSoundFile = mBasePath + "/share/sounds/linphone/rings/notes_of_the_optimistic.mkv";
@ -448,11 +436,12 @@ public class LinphoneManager implements SensorEventListener {
public synchronized void startLibLinphone(boolean isPush) { public synchronized void startLibLinphone(boolean isPush) {
try { try {
copyAssetsFromPackage();
// traces alway start with traces enable to not missed first initialization
mCore = mCore =
Factory.instance() Factory.instance()
.createCore(mConfigFile, mLinphoneFactoryConfigFile, mContext); .createCore(
mPrefs.getLinphoneDefaultConfig(),
mPrefs.getLinphoneFactoryConfig(),
mContext);
mCore.addListener(mCoreListener); mCore.addListener(mCoreListener);
if (isPush) { if (isPush) {
Log.w( Log.w(
@ -664,38 +653,6 @@ public class LinphoneManager implements SensorEventListener {
dialog.show(); dialog.show();
} }
/* Assets stuff */
private void copyAssetsFromPackage() throws IOException {
copyIfNotExist(R.raw.linphonerc_default, mConfigFile);
copyFromPackage(R.raw.linphonerc_factory, new File(mLinphoneFactoryConfigFile).getName());
copyIfNotExist(R.raw.lpconfig, mLPConfigXsd);
copyFromPackage(
R.raw.default_assistant_create, new File(mDefaultDynamicConfigFile).getName());
copyFromPackage(
R.raw.linphone_assistant_create, new File(mLinphoneDynamicConfigFile).getName());
}
private void copyIfNotExist(int ressourceId, String target) throws IOException {
File lFileToCopy = new File(target);
if (!lFileToCopy.exists()) {
copyFromPackage(ressourceId, lFileToCopy.getName());
}
}
private void copyFromPackage(int ressourceId, String target) throws IOException {
FileOutputStream lOutputStream = mContext.openFileOutput(target, 0);
InputStream lInputStream = mContext.getResources().openRawResource(ressourceId);
int readByte;
byte[] buff = new byte[8048];
while ((readByte = lInputStream.read(buff)) != -1) {
lOutputStream.write(buff, 0, readByte);
}
lOutputStream.flush();
lOutputStream.close();
lInputStream.close();
}
/* Presence stuff */ /* Presence stuff */
private boolean isPresenceModelActivitySet() { private boolean isPresenceModelActivitySet() {
@ -871,18 +828,6 @@ public class LinphoneManager implements SensorEventListener {
} }
} }
public String getDefaultDynamicConfigFile() {
return mDefaultDynamicConfigFile;
}
public String getLinphoneDynamicConfigFile() {
return mLinphoneDynamicConfigFile;
}
public String getConfigFile() {
return mConfigFile;
}
public boolean getCallGsmON() { public boolean getCallGsmON() {
return mCallGsmON; return mCallGsmON;
} }

View file

@ -59,7 +59,7 @@ public abstract class AssistantActivity extends LinphoneGenericActivity
if (mAccountCreator == null) { if (mAccountCreator == null) {
String url = LinphonePreferences.instance().getXmlrpcUrl(); String url = LinphonePreferences.instance().getXmlrpcUrl();
Core core = LinphoneManager.getCore(); Core core = LinphoneManager.getCore();
core.loadConfigFromXml(LinphoneManager.getInstance().getDefaultDynamicConfigFile()); core.loadConfigFromXml(LinphonePreferences.instance().getDefaultDynamicConfigFile());
mAccountCreator = core.createAccountCreator(url); mAccountCreator = core.createAccountCreator(url);
} }
} }
@ -103,14 +103,14 @@ public abstract class AssistantActivity extends LinphoneGenericActivity
boolean useLinphoneDefaultValues = boolean useLinphoneDefaultValues =
getString(R.string.default_domain).equals(mAccountCreator.getDomain()); getString(R.string.default_domain).equals(mAccountCreator.getDomain());
if (useLinphoneDefaultValues) { if (useLinphoneDefaultValues) {
core.loadConfigFromXml(LinphoneManager.getInstance().getLinphoneDynamicConfigFile()); core.loadConfigFromXml(LinphonePreferences.instance().getLinphoneDynamicConfigFile());
} }
ProxyConfig proxyConfig = mAccountCreator.configure(); ProxyConfig proxyConfig = mAccountCreator.configure();
if (useLinphoneDefaultValues) { if (useLinphoneDefaultValues) {
// Restore default values // Restore default values
core.loadConfigFromXml(LinphoneManager.getInstance().getDefaultDynamicConfigFile()); core.loadConfigFromXml(LinphonePreferences.instance().getDefaultDynamicConfigFile());
} else { } else {
// If this isn't a sip.linphone.org account, disable push notifications and enable // If this isn't a sip.linphone.org account, disable push notifications and enable
// service notification, otherwise incoming calls won't work (most probably) // service notification, otherwise incoming calls won't work (most probably)

View file

@ -593,7 +593,7 @@ public class AccountSettingsFragment extends SettingsFragment {
// Create a proxy config if there is none // Create a proxy config if there is none
if (mProxyConfig == null) { if (mProxyConfig == null) {
// Ensure the default configuration is loaded first // Ensure the default configuration is loaded first
String defaultConfig = LinphoneManager.getInstance().getDefaultDynamicConfigFile(); String defaultConfig = LinphonePreferences.instance().getDefaultDynamicConfigFile();
core.loadConfigFromXml(defaultConfig); core.loadConfigFromXml(defaultConfig);
mProxyConfig = core.createProxyConfig(); mProxyConfig = core.createProxyConfig();
mAuthInfo = Factory.instance().createAuthInfo(null, null, null, null, null, null); mAuthInfo = Factory.instance().createAuthInfo(null, null, null, null, null, null);

View file

@ -25,6 +25,7 @@ import android.content.pm.PackageManager;
import androidx.appcompat.app.AppCompatDelegate; import androidx.appcompat.app.AppCompatDelegate;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@ -53,6 +54,12 @@ import org.linphone.utils.LinphoneUtils;
public class LinphonePreferences { public class LinphonePreferences {
private static final int LINPHONE_CORE_RANDOM_PORT = -1; private static final int LINPHONE_CORE_RANDOM_PORT = -1;
private static final String LINPHONE_DEFAULT_RC = "/.linphonerc";
private static final String LINPHONE_FACTORY_RC = "/linphonerc";
private static final String LINPHONE_LPCONFIG_XSD = "/lpconfig.xsd";
private static final String DEFAULT_ASSISTANT_RC = "/default_assistant_create.rc";
private static final String LINPHONE_ASSISTANT_RC = "/linphone_assistant_create.rc";
private static LinphonePreferences sInstance; private static LinphonePreferences sInstance;
private Context mContext; private Context mContext;
@ -77,6 +84,61 @@ public class LinphonePreferences {
public void setContext(Context c) { public void setContext(Context c) {
mContext = c; mContext = c;
mBasePath = mContext.getFilesDir().getAbsolutePath(); mBasePath = mContext.getFilesDir().getAbsolutePath();
try {
copyAssetsFromPackage();
} catch (IOException ioe) {
}
}
/* Assets stuff */
private void copyAssetsFromPackage() throws IOException {
copyIfNotExist(R.raw.linphonerc_default, getLinphoneDefaultConfig());
copyFromPackage(R.raw.linphonerc_factory, new File(getLinphoneFactoryConfig()).getName());
copyIfNotExist(R.raw.lpconfig, mBasePath + LINPHONE_LPCONFIG_XSD);
copyFromPackage(
R.raw.default_assistant_create,
new File(mBasePath + DEFAULT_ASSISTANT_RC).getName());
copyFromPackage(
R.raw.linphone_assistant_create,
new File(mBasePath + LINPHONE_ASSISTANT_RC).getName());
}
private void copyIfNotExist(int ressourceId, String target) throws IOException {
File lFileToCopy = new File(target);
if (!lFileToCopy.exists()) {
copyFromPackage(ressourceId, lFileToCopy.getName());
}
}
private void copyFromPackage(int ressourceId, String target) throws IOException {
FileOutputStream lOutputStream = mContext.openFileOutput(target, 0);
InputStream lInputStream = mContext.getResources().openRawResource(ressourceId);
int readByte;
byte[] buff = new byte[8048];
while ((readByte = lInputStream.read(buff)) != -1) {
lOutputStream.write(buff, 0, readByte);
}
lOutputStream.flush();
lOutputStream.close();
lInputStream.close();
}
public String getLinphoneDefaultConfig() {
return mBasePath + LINPHONE_DEFAULT_RC;
}
public String getLinphoneFactoryConfig() {
return mBasePath + LINPHONE_FACTORY_RC;
}
public String getDefaultDynamicConfigFile() {
return mBasePath + DEFAULT_ASSISTANT_RC;
}
public String getLinphoneDynamicConfigFile() {
return mBasePath + LINPHONE_ASSISTANT_RC;
} }
private String getString(int key) { private String getString(int key) {
@ -121,7 +183,7 @@ public class LinphonePreferences {
return Factory.instance().createConfigFromString(text.toString()); return Factory.instance().createConfigFromString(text.toString());
} }
} else { } else {
return Factory.instance().createConfig(LinphoneManager.getInstance().getConfigFile()); return Factory.instance().createConfig(getLinphoneDefaultConfig());
} }
return null; return null;
} }