load contacts and start observer after giving permissions

This commit is contained in:
Daniel Gultsch 2018-09-06 15:55:54 +02:00
parent 816f5b6d22
commit d86f4ce3a6
2 changed files with 18 additions and 23 deletions

View file

@ -190,15 +190,6 @@ public class XmppConnectionService extends Service {
public DatabaseBackend databaseBackend; public DatabaseBackend databaseBackend;
private ReplacingSerialSingleThreadExecutor mContactMergerExecutor = new ReplacingSerialSingleThreadExecutor(true); private ReplacingSerialSingleThreadExecutor mContactMergerExecutor = new ReplacingSerialSingleThreadExecutor(true);
private long mLastActivity = 0; private long mLastActivity = 0;
private ContentObserver contactObserver = new ContentObserver(null) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
if (restoredFromDatabaseLatch.getCount() == 0) {
loadPhoneContacts();
}
}
};
private FileBackend fileBackend = new FileBackend(this); private FileBackend fileBackend = new FileBackend(this);
private MemorizingTrustManager mMemorizingTrustManager; private MemorizingTrustManager mMemorizingTrustManager;
private NotificationService mNotificationService = new NotificationService(this); private NotificationService mNotificationService = new NotificationService(this);
@ -984,8 +975,7 @@ public class XmppConnectionService extends Service {
restoreFromDatabase(); restoreFromDatabase();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
//TODO get this restarted if users gives permission startContactObserver();
getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contactObserver);
} }
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Log.d(Config.LOGTAG, "starting file observer"); Log.d(Config.LOGTAG, "starting file observer");
@ -1024,6 +1014,18 @@ public class XmppConnectionService extends Service {
} }
} }
public void startContactObserver() {
getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, new ContentObserver(null) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
if (restoredFromDatabaseLatch.getCount() == 0) {
loadPhoneContacts();
}
}
});
}
@Override @Override
public void onTrimMemory(int level) { public void onTrimMemory(int level) {
super.onTrimMemory(level); super.onTrimMemory(level);
@ -1538,7 +1540,7 @@ public class XmppConnectionService extends Service {
} }
Log.d(Config.LOGTAG, "finished merging phone contacts"); Log.d(Config.LOGTAG, "finished merging phone contacts");
mShortcutService.refresh(mInitialAddressbookSyncCompleted.compareAndSet(false, true)); mShortcutService.refresh(mInitialAddressbookSyncCompleted.compareAndSet(false, true));
updateAccountUi(); updateRosterUi();
} }
})); }));
} }

View file

@ -677,19 +677,11 @@ public class StartConversationActivity extends XmppActivity implements XmppConne
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.sync_with_contacts); builder.setTitle(R.string.sync_with_contacts);
builder.setMessage(R.string.sync_with_contacts_long); builder.setMessage(R.string.sync_with_contacts_long);
builder.setPositiveButton(R.string.next, (dialog, which) -> { builder.setPositiveButton(R.string.next, (dialog, which) -> requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_SYNC_CONTACTS));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { builder.setOnDismissListener(dialog -> requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_SYNC_CONTACTS));
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_SYNC_CONTACTS);
}
});
builder.setOnDismissListener(dialog -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_SYNC_CONTACTS);
}
});
builder.create().show(); builder.create().show();
} else { } else {
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, 0); requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_SYNC_CONTACTS);
} }
} }
} }
@ -703,6 +695,7 @@ public class StartConversationActivity extends XmppActivity implements XmppConne
ScanActivity.onRequestPermissionResult(this, requestCode, grantResults); ScanActivity.onRequestPermissionResult(this, requestCode, grantResults);
if (requestCode == REQUEST_SYNC_CONTACTS && xmppConnectionServiceBound) { if (requestCode == REQUEST_SYNC_CONTACTS && xmppConnectionServiceBound) {
xmppConnectionService.loadPhoneContacts(); xmppConnectionService.loadPhoneContacts();
xmppConnectionService.startContactObserver();
} }
} }
} }