Search in sources :

Example 6 with AccountHelper

use of com.voipgrid.vialer.util.AccountHelper in project vialer-android by VoIPGRID.

the class NavigationDrawerActivity method performLogout.

/**
 * Perform logout; Remove the stored SystemUser and PhoneAccount and show the login view
 */
private void performLogout() {
    if (mConnectivityHelper.hasNetworkConnection()) {
        MiddlewareHelper.unregister(this);
        // Delete our account information.
        mJsonStorage.clear();
        new AccountHelper(this).clearCredentials();
        // Mark ourselves as unregistered.
        PreferenceManager.getDefaultSharedPreferences(this).edit().putInt(REGISTRATION_STATUS, STATUS_UNREGISTERED).apply();
        // Start a new session.
        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    } else {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setTitle(getText(R.string.cannot_logout_error_title));
        alertDialogBuilder.setMessage(getText(R.string.cannot_logout_error_text)).setCancelable(false).setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
            }
        });
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }
}
Also used : AlertDialog(android.app.AlertDialog) AccountHelper(com.voipgrid.vialer.util.AccountHelper) DialogInterface(android.content.DialogInterface) Intent(android.content.Intent)

Example 7 with AccountHelper

use of com.voipgrid.vialer.util.AccountHelper in project vialer-android by VoIPGRID.

the class WebActivity method onOptionsItemSelected.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeAllCookie();
    if (getIntent().getStringExtra(PAGE).equals(getString(R.string.web_password_change))) {
        AccountHelper accountHelper = new AccountHelper(this);
        accountHelper.clearCredentials();
    }
    finish();
    return true;
}
Also used : AccountHelper(com.voipgrid.vialer.util.AccountHelper) CookieManager(android.webkit.CookieManager)

Example 8 with AccountHelper

use of com.voipgrid.vialer.util.AccountHelper in project vialer-android by VoIPGRID.

the class WebActivityHelper method startWebActivity.

/**
 * Start a new WebActivity to display the page.
 * @param title
 * @param page
 */
public void startWebActivity(String title, String page, String gaTitle) {
    AccountHelper accountHelper = new AccountHelper(mContext);
    Intent intent = new Intent(mContext, WebActivity.class);
    intent.putExtra(WebActivity.PAGE, page);
    intent.putExtra(WebActivity.TITLE, title);
    intent.putExtra(WebActivity.USERNAME, accountHelper.getEmail());
    intent.putExtra(WebActivity.PASSWORD, accountHelper.getPassword());
    if (gaTitle != null) {
        intent.putExtra(WebActivity.GA_TITLE, gaTitle);
    }
    mContext.startActivity(intent);
}
Also used : AccountHelper(com.voipgrid.vialer.util.AccountHelper) Intent(android.content.Intent)

Example 9 with AccountHelper

use of com.voipgrid.vialer.util.AccountHelper 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 10 with AccountHelper

use of com.voipgrid.vialer.util.AccountHelper 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)

Aggregations

AccountHelper (com.voipgrid.vialer.util.AccountHelper)11 JsonStorage (com.voipgrid.vialer.util.JsonStorage)4 Intent (android.content.Intent)3 PhoneAccount (com.voipgrid.vialer.api.models.PhoneAccount)3 SystemUser (com.voipgrid.vialer.api.models.SystemUser)3 PhoneAccountHelper (com.voipgrid.vialer.util.PhoneAccountHelper)3 SharedPreferences (android.content.SharedPreferences)2 Preferences (com.voipgrid.vialer.Preferences)2 Api (com.voipgrid.vialer.api.Api)2 Registration (com.voipgrid.vialer.api.Registration)2 RemoteLogger (com.voipgrid.vialer.logging.RemoteLogger)2 IOException (java.io.IOException)2 ResponseBody (okhttp3.ResponseBody)2 Activity (android.app.Activity)1 AlertDialog (android.app.AlertDialog)1 FragmentManager (android.app.FragmentManager)1 DialogInterface (android.content.DialogInterface)1 CookieManager (android.webkit.CookieManager)1 Tracker (com.google.android.gms.analytics.Tracker)1 GsonBuilder (com.google.gson.GsonBuilder)1