Search in sources :

Example 86 with Contact

use of com.applozic.mobicommons.people.contact.Contact in project Applozic-Android-SDK by AppLozic.

the class RegisterUserClientService method createAccount.

/**
 * This is an internal method. Use {@link Applozic#connectUser(Context, User, AlLoginHandler)}.
 *
 * <p>Registers(or logs in) a {@link User} to the Applozic servers. It also initializes the SDK for that user.</p>
 *
 * @param user the user object to register/authenticate
 * @return the {@link RegistrationResponse}, {@link RegistrationResponse#isRegistrationSuccess()} will be true in case of a successful login/register. otherwise {@link RegistrationResponse#getMessage()} will have the error message
 * @throws Exception in case of empty or invalid user-id (see {@link User#isValidUserId()}, and connection errors
 */
@NonNull
public RegistrationResponse createAccount(@NonNull User user) throws Exception {
    if (user.getDeviceType() == null) {
        user.setDeviceType(Short.valueOf("1"));
    }
    user.setPrefContactAPI(Short.valueOf("2"));
    user.setTimezone(TimeZone.getDefault().getID());
    user.setEnableEncryption(user.isEnableEncryption());
    if (!TextUtils.isEmpty(user.getAlBaseUrl())) {
        ALSpecificSettings.getInstance(context).setAlBaseUrl(user.getAlBaseUrl());
    }
    if (!TextUtils.isEmpty(user.getKmBaseUrl())) {
        ALSpecificSettings.getInstance(context).setKmBaseUrl(user.getKmBaseUrl());
    }
    if (TextUtils.isEmpty(user.getUserId())) {
        throw new ApplozicException("userId cannot be empty");
    }
    if (!user.isValidUserId()) {
        throw new ApplozicException("Invalid userId. Spacing and set of special characters ^!$%&*:(), are not accepted. \nOnly english language characters are accepted");
    }
    MobiComUserPreference mobiComUserPreference = MobiComUserPreference.getInstance(context);
    Gson gson = new Gson();
    user.setAppVersionCode(MOBICOMKIT_VERSION_CODE);
    user.setApplicationId(getApplicationKey(context));
    user.setRegistrationId(mobiComUserPreference.getDeviceRegistrationId());
    if (getAppModuleName(context) != null) {
        user.setAppModuleName(getAppModuleName(context));
    }
    Utils.printLog(context, TAG, "Net status" + Utils.isInternetAvailable(context.getApplicationContext()));
    if (!Utils.isInternetAvailable(context.getApplicationContext())) {
        throw new ConnectException("No Internet Connection");
    }
    HttpRequestUtils.isRefreshTokenInProgress = true;
    Utils.printLog(context, TAG, "Registration json " + gson.toJson(user));
    String response = httpRequestUtils.postJsonToServer(getCreateAccountUrl(), gson.toJson(user));
    Utils.printLog(context, TAG, "Registration response is: " + response);
    if (TextUtils.isEmpty(response) || response.contains("<html")) {
        throw new Exception("503 Service Unavailable");
    }
    final RegistrationResponse registrationResponse = gson.fromJson(response, RegistrationResponse.class);
    if (registrationResponse.isRegistrationSuccess()) {
        Utils.printLog(context, "Registration response ", "is " + registrationResponse);
        if (registrationResponse.getNotificationResponse() != null) {
            Utils.printLog(context, "Registration response ", "" + registrationResponse.getNotificationResponse());
        }
        JWT.parseToken(context, registrationResponse.getAuthToken());
        mobiComUserPreference.setEncryptionKey(registrationResponse.getEncryptionKey());
        mobiComUserPreference.enableEncryption(user.isEnableEncryption());
        mobiComUserPreference.setCountryCode(user.getCountryCode());
        mobiComUserPreference.setUserId(user.getUserId());
        mobiComUserPreference.setContactNumber(user.getContactNumber());
        mobiComUserPreference.setEmailVerified(user.isEmailVerified());
        mobiComUserPreference.setDisplayName(user.getDisplayName());
        mobiComUserPreference.setMqttBrokerUrl(registrationResponse.getBrokerUrl());
        mobiComUserPreference.setDeviceKeyString(registrationResponse.getDeviceKey());
        mobiComUserPreference.setEmailIdValue(user.getEmail());
        mobiComUserPreference.setImageLink(user.getImageLink());
        mobiComUserPreference.setSuUserKeyString(registrationResponse.getUserKey());
        mobiComUserPreference.setLastSyncTimeForMetadataUpdate(String.valueOf(registrationResponse.getCurrentTimeStamp()));
        mobiComUserPreference.setLastSyncTime(String.valueOf(registrationResponse.getCurrentTimeStamp()));
        mobiComUserPreference.setLastSeenAtSyncTime(String.valueOf(registrationResponse.getCurrentTimeStamp()));
        mobiComUserPreference.setChannelSyncTime(String.valueOf(registrationResponse.getCurrentTimeStamp()));
        mobiComUserPreference.setUserBlockSyncTime("10000");
        mobiComUserPreference.setUserDeactivated(registrationResponse.isDeactivate());
        if (registrationResponse.getNotificationAfter() != null) {
            ALSpecificSettings.getInstance(context).setNotificationAfterTime(registrationResponse.getNotificationAfter());
        }
        ApplozicClient.getInstance(context).skipDeletedGroups(user.isSkipDeletedGroups()).hideActionMessages(user.isHideActionMessages());
        if (!TextUtils.isEmpty(registrationResponse.getUserEncryptionKey())) {
            mobiComUserPreference.setUserEncryptionKey(registrationResponse.getUserEncryptionKey());
        }
        mobiComUserPreference.setPassword(user.getPassword());
        mobiComUserPreference.setPricingPackage(registrationResponse.getPricingPackage());
        mobiComUserPreference.setAuthenticationType(String.valueOf(user.getAuthenticationTypeId()));
        if (registrationResponse.getRoleType() != null) {
            mobiComUserPreference.setUserRoleType(registrationResponse.getRoleType());
        }
        if (user.getUserTypeId() != null) {
            mobiComUserPreference.setUserTypeId(String.valueOf(user.getUserTypeId()));
        }
        if (!TextUtils.isEmpty(user.getNotificationSoundFilePath())) {
            Applozic.getInstance(context).setCustomNotificationSound(user.getNotificationSoundFilePath());
        }
        Contact contact = new Contact();
        contact.setUserId(user.getUserId());
        contact.setFullName(registrationResponse.getDisplayName());
        contact.setImageURL(registrationResponse.getImageLink());
        contact.setContactNumber(registrationResponse.getContactNumber());
        contact.setMetadata(registrationResponse.getMetadata());
        if (user.getUserTypeId() != null) {
            contact.setUserTypeId(user.getUserTypeId());
        }
        contact.setRoleType(user.getRoleType());
        contact.setStatus(registrationResponse.getStatusMessage());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Applozic.Store.setNotificationChannelVersion(context, NotificationChannels.NOTIFICATION_CHANNEL_VERSION - 1);
            new NotificationChannels(context).prepareNotificationChannels();
        }
        ApplozicClient.getInstance(context).setChatDisabled(contact.isChatForUserDisabled());
        new AppContactService(context).upsert(contact);
        ConversationWorker.enqueueWorkSync(context);
        ConversationWorker.enqueueWorkMutedUserListSync(context);
        ApplozicMqttWorker.enqueueWorkConnectPublish(context);
    }
    return registrationResponse;
}
Also used : NotificationChannels(com.applozic.mobicomkit.api.notification.NotificationChannels) AppContactService(com.applozic.mobicomkit.contact.AppContactService) MobiComUserPreference(com.applozic.mobicomkit.api.account.user.MobiComUserPreference) Gson(com.google.gson.Gson) ApplozicException(com.applozic.mobicomkit.exception.ApplozicException) ConnectException(java.net.ConnectException) ApplozicException(com.applozic.mobicomkit.exception.ApplozicException) ConnectException(java.net.ConnectException) Contact(com.applozic.mobicommons.people.contact.Contact) NonNull(androidx.annotation.NonNull)

Example 87 with Contact

use of com.applozic.mobicommons.people.contact.Contact in project Applozic-Android-SDK by AppLozic.

the class RegisterUserClientService method checkLoggedInAndCreateAccount.

/**
 * Internal method. Adds a logged in check to {@link #createAccount(User)}.
 */
@NonNull
public RegistrationResponse checkLoggedInAndCreateAccount(@NonNull User user) throws Exception {
    if (MobiComUserPreference.getInstance(context).isLoggedIn()) {
        RegistrationResponse registrationResponse = new RegistrationResponse();
        registrationResponse.setMessage("User already Logged in.");
        Contact contact = new ContactDatabase(context).getContactById(MobiComUserPreference.getInstance(context).getUserId());
        if (contact != null) {
            registrationResponse.setUserId(contact.getUserId());
            registrationResponse.setContactNumber(contact.getContactNumber());
            registrationResponse.setRoleType(contact.getRoleType());
            registrationResponse.setImageLink(contact.getImageURL());
            registrationResponse.setDisplayName(contact.getDisplayName());
            registrationResponse.setStatusMessage(contact.getStatus());
        }
        return registrationResponse;
    } else {
        return createAccount(user);
    }
}
Also used : ContactDatabase(com.applozic.mobicomkit.contact.database.ContactDatabase) Contact(com.applozic.mobicommons.people.contact.Contact) NonNull(androidx.annotation.NonNull)

Example 88 with Contact

use of com.applozic.mobicommons.people.contact.Contact in project Applozic-Android-SDK by AppLozic.

the class ApplozicClient method disableChatForUser.

public ApplozicClient disableChatForUser(final boolean disable, final AlCallback callback) {
    Map<String, String> userMetadata;
    Contact contact = new AppContactService(context).getContactById(MobiComUserPreference.getInstance(context).getUserId());
    if (contact != null && contact.getMetadata() != null) {
        userMetadata = contact.getMetadata();
        userMetadata.putAll(contact.getMetadata());
    } else {
        userMetadata = new HashMap<>();
    }
    userMetadata.put(Contact.DISABLE_CHAT_WITH_USER, String.valueOf(disable));
    User user = new User();
    user.setMetadata(userMetadata);
    UserService.getInstance(context).updateUser(user, new AlCallback() {

        @Override
        public void onSuccess(Object response) {
            sharedPreferences.edit().putBoolean(Contact.DISABLE_CHAT_WITH_USER, disable).commit();
            if (callback != null) {
                callback.onSuccess(response);
            }
        }

        @Override
        public void onError(Object error) {
            if (callback != null) {
                callback.onError(error);
            }
        }
    });
    return this;
}
Also used : User(com.applozic.mobicomkit.api.account.user.User) AppContactService(com.applozic.mobicomkit.contact.AppContactService) JSONObject(org.json.JSONObject) AlCallback(com.applozic.mobicomkit.listners.AlCallback) Contact(com.applozic.mobicommons.people.contact.Contact)

Example 89 with Contact

use of com.applozic.mobicommons.people.contact.Contact in project Applozic-Android-SDK by AppLozic.

the class UserServiceTest method getUserListBySearch.

@Test
public void getUserListBySearch() throws Exception {
    List<UserDetail> expectedUserDetails = Arrays.asList((UserDetail[]) GsonUtils.getObjectFromJson(userDetailsApiResponse, UserDetail[].class));
    ApiResponse expectedApiResponse = new ApiResponse();
    expectedApiResponse.setStatus("success");
    expectedApiResponse.setResponse(expectedUserDetails);
    Mockito.when(userClientService.getUsersBySearchString(ArgumentMatchers.anyString())).thenReturn(expectedApiResponse);
    List<Contact> expectedContactList = new ArrayList<>();
    for (UserDetail userDetail : expectedUserDetails) {
        // *A
        // has been tested, NOTE: this will also call baseContactService.upsert()
        expectedContactList.add(userService.getContactFromUserDetail(userDetail));
    }
    // *B
    List<Contact> actualContactList = userService.getUserListBySearch("Test Search");
    Truth.assertThat(actualContactList.size()).isEqualTo(expectedContactList.size());
    Truth.assertThat(actualContactList.get(0).toString()).isEqualTo(expectedContactList.get(0).toString());
    Truth.assertThat(actualContactList.get(1).toString()).isEqualTo(expectedContactList.get(1).toString());
    // 2 for *A, 2 for *B
    Mockito.verify(appContactService, Mockito.times(4)).upsert(ArgumentMatchers.any());
}
Also used : UserDetail(com.applozic.mobicomkit.api.account.user.UserDetail) ArrayList(java.util.ArrayList) MockedConstants.userDetailsApiResponse(com.applozic.mobicomkit.MockedConstants.userDetailsApiResponse) RegisteredUsersApiResponse(com.applozic.mobicomkit.feed.RegisteredUsersApiResponse) ApiResponse(com.applozic.mobicomkit.feed.ApiResponse) SyncBlockUserApiResponse(com.applozic.mobicomkit.feed.SyncBlockUserApiResponse) Contact(com.applozic.mobicommons.people.contact.Contact) Test(org.junit.Test)

Example 90 with Contact

use of com.applozic.mobicommons.people.contact.Contact in project Applozic-Android-SDK by AppLozic.

the class UserServiceTest method processUsers.

@Test
public void processUsers() {
    UserDetail userDetail = new UserDetail();
    userDetail.setUserId(testUserId);
    userDetail.setImageLink("Image Link");
    userService.processUser(userDetail, Contact.ContactType.APPLOZIC);
    Mockito.doAnswer(invocation -> {
        Contact contactToUpsert = invocation.getArgument(0);
        Truth.assertThat(contactToUpsert).isNotNull();
        Truth.assertThat(contactToUpsert.getUserId()).isEqualTo(testUserId);
        Truth.assertThat(contactToUpsert.getDisplayName()).isNull();
        Truth.assertThat(contactToUpsert.getImageURL()).isEqualTo(userDetail.getImageLink());
        return null;
    }).when(appContactService).upsert(ArgumentMatchers.any(Contact.class));
    Mockito.verify(appContactService, Mockito.times(1)).upsert(ArgumentMatchers.any(Contact.class));
}
Also used : UserDetail(com.applozic.mobicomkit.api.account.user.UserDetail) Contact(com.applozic.mobicommons.people.contact.Contact) Test(org.junit.Test)

Aggregations

Contact (com.applozic.mobicommons.people.contact.Contact)107 Channel (com.applozic.mobicommons.people.channel.Channel)26 Message (com.applozic.mobicomkit.api.conversation.Message)18 Intent (android.content.Intent)17 AppContactService (com.applozic.mobicomkit.contact.AppContactService)17 ArrayList (java.util.ArrayList)15 ApplozicException (com.applozic.mobicomkit.exception.ApplozicException)14 UserDetail (com.applozic.mobicomkit.api.account.user.UserDetail)12 Test (org.junit.Test)12 Bitmap (android.graphics.Bitmap)11 MobiComUserPreference (com.applozic.mobicomkit.api.account.user.MobiComUserPreference)10 Context (android.content.Context)9 SpannableString (android.text.SpannableString)8 NonNull (androidx.annotation.NonNull)8 FileMeta (com.applozic.mobicomkit.api.attachment.FileMeta)8 ApiResponse (com.applozic.mobicomkit.feed.ApiResponse)8 SyncBlockUserApiResponse (com.applozic.mobicomkit.feed.SyncBlockUserApiResponse)8 FileClientService (com.applozic.mobicomkit.api.attachment.FileClientService)7 ContactDatabase (com.applozic.mobicomkit.contact.database.ContactDatabase)7 RegisteredUsersApiResponse (com.applozic.mobicomkit.feed.RegisteredUsersApiResponse)7