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);
}
});
}
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;
}
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;
}
Aggregations