Fix contacts list on airplane mode or no connectivity

This commit is contained in:
Erwan Croze 2018-11-13 10:06:53 +01:00
parent f7f6789816
commit 8ac378d5a0
6 changed files with 68 additions and 6 deletions

View file

@ -117,6 +117,7 @@ import org.linphone.ui.LinphoneMediaScanner;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@ -568,12 +569,25 @@ public class LinphoneManager implements CoreListener, SensorEventListener, Accou
}
public synchronized final void destroyCore() {
String linphoneRcBack = mServiceContext.getFilesDir().getAbsolutePath() + "/linphonerc.back";
String linphoneRcPath = mServiceContext.getFilesDir().getAbsolutePath() + "/.linphonerc";
sExited = true;
ContactsManagerDestroy();
BluetoothManagerDestroy();
try {
mTimer.cancel();
destroyLinphoneCore();
try {
InputStream backup = new FileInputStream(linphoneRcBack);
if (backup != null) {
File rcfile = new File(linphoneRcPath);
if (rcfile.exists()) {
LinphoneUtils.copyToFile(backup, rcfile);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
catch (RuntimeException e) {
Log.e(e);
@ -1120,6 +1134,27 @@ public class LinphoneManager implements CoreListener, SensorEventListener, Accou
} catch (CoreException e) {
Log.e(e);
}
} else if (state == GlobalState.Configuring && lc.getProvisioningUri() != null && !lc.getProvisioningUri().isEmpty()) {
//Obiane spec
String linphoneRcPath = mServiceContext.getFilesDir().getAbsolutePath() + "/.linphonerc";
String linphoneRcBack = mServiceContext.getFilesDir().getAbsolutePath() + "/linphonerc.back";
FriendList savedList = lc.getDefaultFriendList();
if (savedList != null && savedList.getFriends() != null && savedList.getFriends().length > 0) {
try {
InputStream rc = new FileInputStream(linphoneRcPath);
File backup = new File(linphoneRcBack);
Config cfg = lc.getConfig();
LinphoneUtils.copyToFile(rc, backup);
for (int i = 0 ; i < savedList.getFriends().length ; i++) {
cfg.cleanSection("friend_" + i);
}
cfg.sync();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
@ -1628,14 +1663,41 @@ public class LinphoneManager implements CoreListener, SensorEventListener, Accou
Log.d("Remote provisioning status = " + state.toString() + " (" + message + ")");
LinphonePreferences prefs = LinphonePreferences.instance();
final String linphoneRcBack = mServiceContext.getFilesDir().getAbsolutePath() + "/linphonerc.back";
if (state == ConfiguringState.Successful) {
if (lc.getFriendsLists() != null) lc.removeFriendList(lc.getFriendsLists()[0]);
if (prefs.isProvisioningLoginViewEnabled()) {
ProxyConfig proxyConfig = lc.createProxyConfig();
Address addr = proxyConfig.getIdentityAddress();
wizardLoginViewDomain = (addr != null) ? addr.getDomain() : "";
}
prefs.setPushNotificationEnabled(prefs.isPushNotificationEnabled());
File backup = new File(linphoneRcBack);
if (backup.exists()) backup.delete();
} else if (state == ConfiguringState.Failed) {
final CoreListener listener = this;
Handler mainHandler = new Handler(mServiceContext.getMainLooper());
Runnable thread = new Runnable() {
@Override
public void run() {
//Obiane spec
String linphoneRcPath = mServiceContext.getFilesDir().getAbsolutePath() + "/.linphonerc";
String remoteProvisioning = mLc.getProvisioningUri();
mLc.removeListener(listener);
destroyCore();
Config cfg = Factory.instance().createConfig(linphoneRcPath);
cfg.setString("misc", "config-uri", "");
cfg.sync();
startLibLinphone(mServiceContext);
sExited = false;
mLc.setProvisioningUri(remoteProvisioning);
File backup = new File(linphoneRcBack);
if (backup.exists()) {
backup.delete();
}
if (ContactsManager.getInstance() != null) ContactsManager.getInstance().fetchContactsAsync();
}
};
mainHandler.post(thread);
}
}

View file

@ -668,7 +668,7 @@ public final class LinphoneUtils {
* Copy data from a source stream to destFile.
* Return true if succeed, return false if failed.
*/
private static boolean copyToFile(InputStream inputStream, File destFile) {
public static boolean copyToFile(InputStream inputStream, File destFile) {
if (inputStream == null || destFile == null) return false;
try {
OutputStream out = new FileOutputStream(destFile);

View file

@ -254,7 +254,7 @@ public class RemoteProvisioningLoginActivity extends Activity implements OnClick
public void onVideoRenderingSurfaceDestroyed(AndroidVideoWindowImpl vw) {}
public void onVideoPreviewSurfaceReady(AndroidVideoWindowImpl vw, SurfaceView surface) {
LinphoneManager.getLc().setNativePreviewWindowId(androidVideoWindowImpl);
if (LinphoneManager.getLc() != null) LinphoneManager.getLc().setNativePreviewWindowId(androidVideoWindowImpl);
}
public void onVideoPreviewSurfaceDestroyed(AndroidVideoWindowImpl vw) {}

View file

@ -103,7 +103,7 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//TODO get contact
//LinphoneManager.getInstance().restartCore();
if (LinphoneManager.getLc() != null) {
LinphoneManager.getLc().refreshRegisters();
}

View file

@ -232,8 +232,8 @@ public class ContactsManager extends ContentObserver {
}
public synchronized LinphoneContact findContactFromAddress(Address address) {
if (address == null) return null;
Core lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
if (address == null || lc == null) return null;
Friend lf = lc.findFriend(address);
if (lf != null) {
LinphoneContact contact = (LinphoneContact)lf.getUserData();

View file

@ -37,7 +37,7 @@ public class NetworkManager extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Boolean lNoConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,false);
if (LinphoneManager.isInstanciated()) {
if (LinphoneManager.isInstanciated() && LinphoneManager.getLc() != null) {
LinphoneManager.getInstance().connectivityChanged(cm, lNoConnectivity);
}
}