From 56a01f1be2498369520a2ffa042394ed06848f85 Mon Sep 17 00:00:00 2001 From: giomfo Date: Thu, 15 Sep 2016 09:44:22 +0200 Subject: [PATCH] Local contacts: Consider the local contacts when the access is authorized even if the server sync option is disabled. --- Vector/AppDelegate.m | 15 +++++++-- .../ContactPickerViewController.m | 31 +++++++++++++++++++ .../RoomParticipantsViewController.m | 30 +++++++++++++----- .../ViewController/SettingsViewController.m | 16 ++++++++++ .../ViewController/StartChatViewController.m | 16 +++++++++- 5 files changed, 96 insertions(+), 12 deletions(-) diff --git a/Vector/AppDelegate.m b/Vector/AppDelegate.m index a9a61f6ac..c61e99525 100644 --- a/Vector/AppDelegate.m +++ b/Vector/AppDelegate.m @@ -41,6 +41,9 @@ #import "CallViewController.h" +// Uncomment the following line to use local contacts to discover matrix users. +//#define MX_USE_CONTACTS_SERVER_SYNC + //#define MX_CALL_STACK_OPENWEBRTC #ifdef MX_CALL_STACK_OPENWEBRTC #import @@ -313,6 +316,9 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN // Configure Google Analytics here if the option is enabled [self startGoogleAnalytics]; + // Configure local contacts management + [MXKContactManager sharedManager].enableFullMatrixIdSyncOnLocalContactsDidLoad = NO; + // Add matrix observers, and initialize matrix sessions if the app is not launched in background. [self initMatrixSessions]; @@ -469,9 +475,12 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN [account resume]; } - // refresh the contacts list - [MXKContactManager sharedManager].enableFullMatrixIdSyncOnLocalContactsDidLoad = NO; - [[MXKContactManager sharedManager] loadLocalContacts]; + // Check if the application is allowed to access the local contacts + if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) + { + // Refresh the local contacts list by reloading it + [[MXKContactManager sharedManager] loadLocalContacts]; + } _isAppForeground = YES; } diff --git a/Vector/ViewController/ContactPickerViewController.m b/Vector/ViewController/ContactPickerViewController.m index 65867c310..c298b8054 100644 --- a/Vector/ViewController/ContactPickerViewController.m +++ b/Vector/ViewController/ContactPickerViewController.m @@ -108,6 +108,37 @@ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshContactsList) name:kMXKContactManagerDidUpdateMatrixContactsNotification object:nil]; [self refreshContactsList]; + + // Handle here local contacts +#ifdef MX_USE_CONTACTS_SERVER_SYNC + if (![MXKAppSettings standardAppSettings].syncLocalContacts) + { + // If not requested yet, ask user permission to sync their local contacts + if (![MXKAppSettings standardAppSettings].syncLocalContacts && ![MXKAppSettings standardAppSettings].syncLocalContactsPermissionRequested) + { + [MXKAppSettings standardAppSettings].syncLocalContactsPermissionRequested = YES; + + [MXKContactManager requestUserConfirmationForLocalContactsSyncInViewController:self completionHandler:^(BOOL granted) { + if (granted) + { + // Allow local contacts sync in order to add address book emails in search result + [MXKAppSettings standardAppSettings].syncLocalContacts = YES; + } + }]; + } + } +#else + // If not requested yet, ask user permission to access their local contacts + if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) + { + // Try to load the local contacts list + dispatch_async(dispatch_get_main_queue(), ^{ + + [[MXKContactManager sharedManager] loadLocalContacts]; + + }); + } +#endif } - (void)viewWillDisappear:(BOOL)animated diff --git a/Vector/ViewController/RoomParticipantsViewController.m b/Vector/ViewController/RoomParticipantsViewController.m index c38da87bd..5596e2e05 100644 --- a/Vector/ViewController/RoomParticipantsViewController.m +++ b/Vector/ViewController/RoomParticipantsViewController.m @@ -1684,20 +1684,34 @@ { self.isAddParticipantSearchBarEditing = YES; searchBar.showsCancelButton = YES; - + + // Handle here local contacts +#ifdef MX_USE_CONTACTS_SERVER_SYNC // If not requested yet, ask user permission to sync their local contacts if (![MXKAppSettings standardAppSettings].syncLocalContacts && ![MXKAppSettings standardAppSettings].syncLocalContactsPermissionRequested) { [MXKAppSettings standardAppSettings].syncLocalContactsPermissionRequested = YES; - + [MXKContactManager requestUserConfirmationForLocalContactsSyncInViewController:self completionHandler:^(BOOL granted) { - if (granted) - { - // Allow local contacts sync in order to add address book emails in search result - [MXKAppSettings standardAppSettings].syncLocalContacts = YES; - } - }]; + if (granted) + { + // Allow local contacts sync in order to add address book emails in search result + [MXKAppSettings standardAppSettings].syncLocalContacts = YES; + } + }]; } +#else + // If not requested yet, ask user permission to access their local contacts + if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) + { + // Try to load the local contacts list + dispatch_async(dispatch_get_main_queue(), ^{ + + [[MXKContactManager sharedManager] loadLocalContacts]; + + }); + } +#endif return YES; } diff --git a/Vector/ViewController/SettingsViewController.m b/Vector/ViewController/SettingsViewController.m index 6924d2d10..2702498e3 100644 --- a/Vector/ViewController/SettingsViewController.m +++ b/Vector/ViewController/SettingsViewController.m @@ -27,6 +27,8 @@ #import #import +#ifdef MX_USE_CONTACTS_SERVER_SYNC + #define SETTINGS_SECTION_SIGN_OUT_INDEX 0 #define SETTINGS_SECTION_USER_SETTINGS_INDEX 1 #define SETTINGS_SECTION_NOTIFICATIONS_SETTINGS_INDEX 2 @@ -37,6 +39,20 @@ #define SETTINGS_SECTION_LABS_INDEX 7 #define SETTINGS_SECTION_COUNT 7 // Not 8 because the LABS section is currently hidden +#else + +#define SETTINGS_SECTION_SIGN_OUT_INDEX 0 +#define SETTINGS_SECTION_USER_SETTINGS_INDEX 1 +#define SETTINGS_SECTION_NOTIFICATIONS_SETTINGS_INDEX 2 +#define SETTINGS_SECTION_IGNORED_USERS_INDEX 3 +#define SETTINGS_SECTION_ADVANCED_INDEX 4 +#define SETTINGS_SECTION_OTHER_INDEX 5 +#define SETTINGS_SECTION_CONTACTS_INDEX 6 +#define SETTINGS_SECTION_LABS_INDEX 7 +#define SETTINGS_SECTION_COUNT 6 // Not 8 because the CONTACTS and LABS section is currently hidden + +#endif + #define NOTIFICATION_SETTINGS_ENABLE_PUSH_INDEX 0 #define NOTIFICATION_SETTINGS_GLOBAL_SETTINGS_INDEX 1 //#define NOTIFICATION_SETTINGS_CONTAINING_MY_USER_NAME_INDEX 1 diff --git a/Vector/ViewController/StartChatViewController.m b/Vector/ViewController/StartChatViewController.m index d9ae0e467..3b4ba4cb4 100644 --- a/Vector/ViewController/StartChatViewController.m +++ b/Vector/ViewController/StartChatViewController.m @@ -801,13 +801,15 @@ self.isAddParticipantSearchBarEditing = YES; searchBar.showsCancelButton = NO; + // Handle here local contacts +#ifdef MX_USE_CONTACTS_SERVER_SYNC if (![MXKAppSettings standardAppSettings].syncLocalContacts) { // If not requested yet, ask user permission to sync their local contacts if (![MXKAppSettings standardAppSettings].syncLocalContacts && ![MXKAppSettings standardAppSettings].syncLocalContactsPermissionRequested) { [MXKAppSettings standardAppSettings].syncLocalContactsPermissionRequested = YES; - + [MXKContactManager requestUserConfirmationForLocalContactsSyncInViewController:self completionHandler:^(BOOL granted) { if (granted) { @@ -817,6 +819,18 @@ }]; } } +#else + // If not requested yet, ask user permission to access their local contacts + if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) + { + // Try to load the local contacts list + dispatch_async(dispatch_get_main_queue(), ^{ + + [[MXKContactManager sharedManager] loadLocalContacts]; + + }); + } +#endif return YES; }