Fetch contacts when service starts instead of waiting for main activity to be started, otherwise won't have any contact after auto start on boot

This commit is contained in:
Sylvain Berfini 2019-05-15 12:36:57 +02:00
parent 011ac4aa9f
commit 7f59bc2f95
4 changed files with 19 additions and 17 deletions

View file

@ -191,15 +191,16 @@ public final class LinphoneService extends Service {
}
mNotificationManager = new NotificationsManager(this);
if (Version.sdkAboveOrEqual(Version.API26_O_80)
&& intent != null
&& intent.getBooleanExtra("ForceStartForeground", false)) {
// We need to call this asap
mNotificationManager.startForeground();
}
mLinphoneManager = new LinphoneManager(this);
sInstance = this; // sInstance is ready once linphone manager has been created
if (Version.sdkAboveOrEqual(Version.API26_O_80)
&& intent != null
&& intent.getBooleanExtra("ForceStartForeground", false)) {
// We need to call this asap after the Service can be accessed through it's singleton
mNotificationManager.startForeground();
}
mLinphoneManager.startLibLinphone(isPush);
LinphoneManager.getCore().addListener(mListener);
@ -212,6 +213,10 @@ public final class LinphoneService extends Service {
.registerContentObserver(
ContactsContract.Contacts.CONTENT_URI, true, mContactsManager);
}
if (mContactsManager.hasReadContactsAccess()) {
ContactsManager.getInstance().enableContactsAccess();
}
ContactsManager.getInstance().initializeContactManager();
Compatibility.createChatShortcuts(this);
mOrientationHelper.enable();

View file

@ -280,11 +280,6 @@ public abstract class MainActivity extends LinphoneGenericActivity
requestRequiredPermissions();
if (checkPermission(Manifest.permission.READ_CONTACTS)) {
ContactsManager.getInstance().enableContactsAccess();
}
ContactsManager.getInstance().initializeContactManager();
if (DeviceUtils.isAppUserRestricted(this)) {
Log.w(
"[Main Activity] Device has been restricted by user (Android 9+), push notifications won't work !");

View file

@ -99,6 +99,7 @@ public class ContactsManager extends ContentObserver implements FriendListListen
@Override
public void onChange(boolean selfChange, Uri uri) {
Log.i("[Contacts Manager] Content observer detected a changing in at least one contact");
fetchContactsAsync();
}
@ -119,7 +120,7 @@ public class ContactsManager extends ContentObserver implements FriendListListen
}
public void destroy() {
mContext.getContentResolver().unregisterContentObserver(ContactsManager.getInstance());
mContext.getContentResolver().unregisterContentObserver(this);
if (mLoadContactTask != null) {
mLoadContactTask.cancel(true);
@ -256,7 +257,7 @@ public class ContactsManager extends ContentObserver implements FriendListListen
&& hasWriteContactsAccess()
&& hasWriteSyncPermission()) {
if (LinphoneService.isReady()) {
ContactsManager.getInstance().initializeSyncAccount();
initializeSyncAccount();
mInitialized = true;
}
}
@ -462,7 +463,7 @@ public class ContactsManager extends ContentObserver implements FriendListListen
public void onPresenceReceived(FriendList list, Friend[] friends) {
boolean updated = false;
for (Friend lf : friends) {
boolean newContact = ContactsManager.getInstance().refreshSipContact(lf);
boolean newContact = refreshSipContact(lf);
if (newContact) {
updated = true;
}

View file

@ -32,14 +32,15 @@ public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_SHUTDOWN)) {
android.util.Log.d(
"LinphoneBootReceiver",
"Device is shutting down, destroying Core to unregister");
"Linphone",
"[Boot Receiver] Device is shutting down, destroying Core to unregister");
context.stopService(
new Intent(Intent.ACTION_MAIN).setClass(context, LinphoneService.class));
} else {
LinphonePreferences.instance().setContext(context);
boolean autostart = LinphonePreferences.instance().isAutoStartEnabled();
android.util.Log.i(
"LinphoneBootReceiver", "Device is starting, auto_start is " + autostart);
"Linphone", "[Boot Receiver] Device is starting, auto_start is " + autostart);
if (autostart && !LinphoneService.isReady()) {
Intent serviceIntent = new Intent(Intent.ACTION_MAIN);
serviceIntent.setClass(context, LinphoneService.class);