Search in sources :

Example 11 with SystemUser

use of com.voipgrid.vialer.api.models.SystemUser in project vialer-android by VoIPGRID.

the class MiddlewareHelper method register.

public static void register(final Context context, String token) {
    Preferences sipPreferences = new Preferences(context);
    Tracker analyticsTracker = ((AnalyticsApplication) context.getApplicationContext()).getDefaultTracker();
    final AnalyticsHelper analyticsHelper = new AnalyticsHelper(analyticsTracker);
    if (!sipPreferences.canUseSip()) {
        return;
    }
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    final SharedPreferences.Editor editor = preferences.edit();
    editor.putLong(LAST_REGISTRATION, System.currentTimeMillis());
    JsonStorage jsonStorage = new JsonStorage(context);
    AccountHelper accountHelper = new AccountHelper(context);
    if (!jsonStorage.has(PhoneAccount.class)) {
        return;
    }
    Registration api = ServiceGenerator.createService(context, Registration.class, getBaseApiUrl(context), accountHelper.getEmail(), accountHelper.getPassword());
    String sipUserId = ((PhoneAccount) jsonStorage.get(PhoneAccount.class)).getAccountId();
    String fullName = ((SystemUser) jsonStorage.get(SystemUser.class)).getFullName();
    String appName = context.getPackageName();
    Call<ResponseBody> call = api.register(fullName, token, sipUserId, Build.VERSION.CODENAME, Build.VERSION.RELEASE, appName);
    editor.putString(CURRENT_TOKEN, token);
    call.enqueue(new Callback<ResponseBody>() {

        @Override
        public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
            if (response.isSuccessful()) {
                setRegistrationStatus(context, STATUS_REGISTERED);
            } else {
                setRegistrationStatus(context, STATUS_FAILED);
                analyticsHelper.sendException(context.getString(R.string.analytics_event_description_registration_failed));
            }
            editor.apply();
        }

        @Override
        public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
            t.printStackTrace();
            setRegistrationStatus(context, STATUS_FAILED);
        }
    });
}
Also used : Tracker(com.google.android.gms.analytics.Tracker) AccountHelper(com.voipgrid.vialer.util.AccountHelper) SharedPreferences(android.content.SharedPreferences) ResponseBody(okhttp3.ResponseBody) PhoneAccount(com.voipgrid.vialer.api.models.PhoneAccount) AnalyticsApplication(com.voipgrid.vialer.analytics.AnalyticsApplication) Registration(com.voipgrid.vialer.api.Registration) SystemUser(com.voipgrid.vialer.api.models.SystemUser) SharedPreferences(android.content.SharedPreferences) Preferences(com.voipgrid.vialer.Preferences) JsonStorage(com.voipgrid.vialer.util.JsonStorage) AnalyticsHelper(com.voipgrid.vialer.analytics.AnalyticsHelper)

Example 12 with SystemUser

use of com.voipgrid.vialer.api.models.SystemUser in project vialer-android by VoIPGRID.

the class PhoneAccountHelper method getLinkedPhoneAccount.

/**
 * Function to get the phone linked to the user. This also updates the systemuser.
 * @return PhoneAccount object or null.
 */
public PhoneAccount getLinkedPhoneAccount() {
    SystemUser systemUser = getAndUpdateSystemUser();
    PhoneAccount phoneAccount = (PhoneAccount) mJsonStorage.get(PhoneAccount.class);
    if (phoneAccount == null) {
        phoneAccount = null;
    }
    if (systemUser != null) {
        mPreferences.setSipPermission(true);
        String phoneAccountId = systemUser.getPhoneAccountId();
        // Get phone account from API if one is provided in the systemuser API.
        if (phoneAccountId != null) {
            // If no PhoneAccountId is returned, remove current PhoneAccount information from jsonstorage.
            new JsonStorage(mContext).remove(PhoneAccount.class);
            Call<PhoneAccount> phoneAccountCall = mApi.phoneAccount(phoneAccountId);
            try {
                Response<PhoneAccount> phoneAccountResponse = phoneAccountCall.execute();
                if (phoneAccountResponse.isSuccessful() && phoneAccountResponse.body() != null) {
                    phoneAccount = phoneAccountResponse.body();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return phoneAccount;
}
Also used : PhoneAccount(com.voipgrid.vialer.api.models.PhoneAccount) SystemUser(com.voipgrid.vialer.api.models.SystemUser) IOException(java.io.IOException)

Example 13 with SystemUser

use of com.voipgrid.vialer.api.models.SystemUser in project vialer-android by VoIPGRID.

the class PhoneAccountHelper method getAndUpdateSystemUser.

/**
 * Function to get the current systemuser from the api.
 * @return
 */
public SystemUser getAndUpdateSystemUser() {
    Call<SystemUser> call = mApi.systemUser();
    SystemUser systemUser = (SystemUser) mJsonStorage.get(SystemUser.class);
    if (systemUser == null) {
        systemUser = null;
    }
    try {
        Response<SystemUser> response = call.execute();
        if (response.isSuccessful() && response.body() != null) {
            systemUser = response.body();
            updateSystemUser(systemUser);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return systemUser;
}
Also used : SystemUser(com.voipgrid.vialer.api.models.SystemUser) IOException(java.io.IOException)

Aggregations

SystemUser (com.voipgrid.vialer.api.models.SystemUser)13 PhoneAccount (com.voipgrid.vialer.api.models.PhoneAccount)5 JsonStorage (com.voipgrid.vialer.util.JsonStorage)5 Bundle (android.os.Bundle)3 AccountHelper (com.voipgrid.vialer.util.AccountHelper)3 PhoneAccountHelper (com.voipgrid.vialer.util.PhoneAccountHelper)3 IOException (java.io.IOException)3 Preferences (com.voipgrid.vialer.Preferences)2 RemoteLogger (com.voipgrid.vialer.logging.RemoteLogger)2 Fragment (android.app.Fragment)1 FragmentManager (android.app.FragmentManager)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 FloatingActionButton (android.support.design.widget.FloatingActionButton)1 MotionEvent (android.view.MotionEvent)1 View (android.view.View)1 GoogleAnalytics (com.google.android.gms.analytics.GoogleAnalytics)1 Tracker (com.google.android.gms.analytics.Tracker)1 WebActivityHelper (com.voipgrid.vialer.WebActivityHelper)1 AnalyticsApplication (com.voipgrid.vialer.analytics.AnalyticsApplication)1