Search in sources :

Example 6 with PhoneAccount

use of com.voipgrid.vialer.api.models.PhoneAccount 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 7 with PhoneAccount

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

the class MiddlewareHelper method unregister.

/**
 * Function to synchronously unregister at the middleware if a phone account and
 * token are present.
 * @param context
 */
public static void unregister(final Context context) {
    final RemoteLogger remoteLogger = new RemoteLogger(MiddlewareHelper.class).enableConsoleLogging();
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    String token = preferences.getString(CURRENT_TOKEN, "");
    // Check if we have a phone account and a push token.
    if (new Preferences(context).hasPhoneAccount() && !token.equals("")) {
        JsonStorage jsonStorage = new JsonStorage(context);
        AccountHelper accountHelper = new AccountHelper(context);
        Registration api = ServiceGenerator.createService(context, Registration.class, getBaseApiUrl(context), accountHelper.getEmail(), accountHelper.getPassword());
        String sipUserId = ((PhoneAccount) jsonStorage.get(PhoneAccount.class)).getAccountId();
        String appName = context.getPackageName();
        Call<ResponseBody> call = api.unregister(token, sipUserId, appName);
        call.enqueue(new Callback<ResponseBody>() {

            @Override
            public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    remoteLogger.d("unregister successful");
                    setRegistrationStatus(context, STATUS_UNREGISTERED);
                } else {
                    remoteLogger.d("unregister failed");
                    setRegistrationStatus(context, STATUS_FAILED);
                }
            }

            @Override
            public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
                remoteLogger.d("unregister failed");
                setRegistrationStatus(context, STATUS_FAILED);
            }
        });
    } else {
        remoteLogger.d("No token or phone account so unregister");
        setRegistrationStatus(context, STATUS_FAILED);
    }
}
Also used : AccountHelper(com.voipgrid.vialer.util.AccountHelper) SharedPreferences(android.content.SharedPreferences) ResponseBody(okhttp3.ResponseBody) PhoneAccount(com.voipgrid.vialer.api.models.PhoneAccount) Registration(com.voipgrid.vialer.api.Registration) RemoteLogger(com.voipgrid.vialer.logging.RemoteLogger) SharedPreferences(android.content.SharedPreferences) Preferences(com.voipgrid.vialer.Preferences) JsonStorage(com.voipgrid.vialer.util.JsonStorage)

Example 8 with PhoneAccount

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

the class SipService method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    mHandler = new Handler();
    mToneGenerator = new ToneGenerator(AudioManager.STREAM_VOICE_CALL, SipConstants.RINGING_VOLUME);
    mSipBroadcaster = new SipBroadcaster(this);
    mPreferences = new Preferences(this);
    mRemoteLogger = new RemoteLogger(SipService.class).enableConsoleLogging();
    mNativeCallManager = new NativeCallManager((TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE));
    mRemoteLogger.d("onCreate");
    IntentFilter filter = new IntentFilter();
    filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
    registerReceiver(phoneStateReceiver, filter);
    // Create runnable to check if the SipService is still in use.
    mCheckServiceHandler = new Handler();
    mCheckServiceRunnable = new Runnable() {

        @Override
        public void run() {
            // Check if the service is being used after 10 seconds and shutdown the service
            // if required.
            checkServiceBeingUsed();
            mCheckServiceHandler.postDelayed(this, mCheckServiceUsedTimer);
        }
    };
    mCheckServiceHandler.postDelayed(mCheckServiceRunnable, mCheckServiceUsedTimer);
    PhoneAccount phoneAccount = new JsonStorage<PhoneAccount>(this).get(PhoneAccount.class);
    if (phoneAccount != null) {
        // Try to load PJSIP library.
        mSipConfig = new SipConfig(this, phoneAccount);
        try {
            mSipConfig.initLibrary();
        } catch (SipConfig.LibraryInitFailedException e) {
            stopSelf();
        }
    } else {
        // User has no sip account so destroy the service.
        mRemoteLogger.w("No sip account when trying to create service");
        stopSelf();
    }
}
Also used : IntentFilter(android.content.IntentFilter) Handler(android.os.Handler) NativeCallManager(com.voipgrid.vialer.call.NativeCallManager) ToneGenerator(com.voipgrid.vialer.dialer.ToneGenerator) PhoneAccount(com.voipgrid.vialer.api.models.PhoneAccount) TelephonyManager(android.telephony.TelephonyManager) RemoteLogger(com.voipgrid.vialer.logging.RemoteLogger) Preferences(com.voipgrid.vialer.Preferences)

Example 9 with PhoneAccount

use of com.voipgrid.vialer.api.models.PhoneAccount 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 10 with PhoneAccount

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

the class PhoneAccountHelper method savePhoneAccountAndRegister.

public void savePhoneAccountAndRegister(PhoneAccount phoneAccount) {
    // Check if we have something to save and register.
    if (phoneAccount == null) {
        return;
    }
    // Get the phone account currently saved in the local storage.
    PhoneAccount existingPhoneAccount = ((PhoneAccount) mJsonStorage.get(PhoneAccount.class));
    // Save the linked phone account in the local storage.
    mJsonStorage.save(phoneAccount);
    // Check if the user can use sip and if the phone account changed or unregistered.
    if (mPreferences.canUseSip()) {
        boolean register = false;
        if (!phoneAccount.equals(existingPhoneAccount)) {
            // New registration because phone account changed.
            MiddlewareHelper.setRegistrationStatus(mContext, STATUS_UPDATE_NEEDED);
            register = true;
        } else if (MiddlewareHelper.needsRegistration(mContext)) {
            // New registration because we need a registration.
            register = true;
        }
        if (register) {
            startMiddlewareRegistrationService();
        }
    }
}
Also used : PhoneAccount(com.voipgrid.vialer.api.models.PhoneAccount)

Aggregations

PhoneAccount (com.voipgrid.vialer.api.models.PhoneAccount)10 SystemUser (com.voipgrid.vialer.api.models.SystemUser)5 Preferences (com.voipgrid.vialer.Preferences)3 AccountHelper (com.voipgrid.vialer.util.AccountHelper)3 SharedPreferences (android.content.SharedPreferences)2 Registration (com.voipgrid.vialer.api.Registration)2 RemoteLogger (com.voipgrid.vialer.logging.RemoteLogger)2 JsonStorage (com.voipgrid.vialer.util.JsonStorage)2 IOException (java.io.IOException)2 ResponseBody (okhttp3.ResponseBody)2 FragmentManager (android.app.FragmentManager)1 Intent (android.content.Intent)1 IntentFilter (android.content.IntentFilter)1 Handler (android.os.Handler)1 TelephonyManager (android.telephony.TelephonyManager)1 Tracker (com.google.android.gms.analytics.Tracker)1 WebActivityHelper (com.voipgrid.vialer.WebActivityHelper)1 AnalyticsApplication (com.voipgrid.vialer.analytics.AnalyticsApplication)1 AnalyticsHelper (com.voipgrid.vialer.analytics.AnalyticsHelper)1 Destination (com.voipgrid.vialer.api.models.Destination)1