Search in sources :

Example 11 with JsonStorage

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

the class NavigationDrawerActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mConnectivityHelper = new ConnectivityHelper((ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE), (TelephonyManager) getSystemService(TELEPHONY_SERVICE));
    mJsonStorage = new JsonStorage(this);
    mSystemUser = (SystemUser) mJsonStorage.get(SystemUser.class);
    if (mSystemUser != null && !TextUtils.isEmpty(getPassword())) {
        mApi = ServiceGenerator.createService(this, Api.class, getString(R.string.api_url), getEmail(), getPassword());
        // Preload availability.
        Call<VoipGridResponse<UserDestination>> call = mApi.getUserDestination();
        call.enqueue(this);
    }
}
Also used : VoipGridResponse(com.voipgrid.vialer.api.models.VoipGridResponse) ConnectivityHelper(com.voipgrid.vialer.util.ConnectivityHelper) TelephonyManager(android.telephony.TelephonyManager) ConnectivityManager(android.net.ConnectivityManager) Api(com.voipgrid.vialer.api.Api) JsonStorage(com.voipgrid.vialer.util.JsonStorage)

Example 12 with JsonStorage

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

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

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

the class ServiceGenerator method getHttpClient.

/**
 * Function to create the HttpClient to be used by retrofit for API calls.
 * @param context
 * @param username
 * @param password
 * @return
 */
private static OkHttpClient getHttpClient(final Context context, final String username, final String password) {
    httpClient.addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request original = chain.request();
            Request.Builder requestBuilder = original.newBuilder();
            if (username != null && password != null) {
                requestBuilder.header("Authorization", Credentials.basic(username, password));
            }
            requestBuilder.header("User-Agent", getUserAgentHeader(context));
            if (ConnectivityHelper.get(context).hasNetworkConnection()) {
                // read from cache for 1 minute
                int maxAge = 60;
                requestBuilder.header("Cache-Control", "public, max-age=" + maxAge);
            } else {
                // tolerate 4-weeks stale
                int maxStale = 60 * 60 * 24 * 28;
                requestBuilder.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale);
            }
            Request request = requestBuilder.build();
            Response response = chain.proceed(request);
            // Check if we get a 401 and are not in the onboarding.
            if (response.code() == 401 && !context.getClass().getSimpleName().equals(SetupActivity.class.getSimpleName())) {
                new RemoteLogger(ServiceGenerator.class).w("Logged out on 401 API response");
                // Clear logged in values.
                new JsonStorage(context).clear();
                new AccountHelper(context).clearCredentials();
                if (context instanceof Activity) {
                    // Start onboarding.
                    Intent intent = new Intent(context, SetupActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    context.startActivity(intent);
                    ((Activity) context).finish();
                }
            }
            return response;
        }
    });
    httpClient.cache(getCache(context));
    return httpClient.build();
}
Also used : AccountHelper(com.voipgrid.vialer.util.AccountHelper) GsonBuilder(com.google.gson.GsonBuilder) Request(okhttp3.Request) SetupActivity(com.voipgrid.vialer.onboarding.SetupActivity) Activity(android.app.Activity) Intent(android.content.Intent) IOException(java.io.IOException) Response(okhttp3.Response) SetupActivity(com.voipgrid.vialer.onboarding.SetupActivity) RemoteLogger(com.voipgrid.vialer.logging.RemoteLogger) JsonStorage(com.voipgrid.vialer.util.JsonStorage) Interceptor(okhttp3.Interceptor)

Aggregations

JsonStorage (com.voipgrid.vialer.util.JsonStorage)14 SystemUser (com.voipgrid.vialer.api.models.SystemUser)6 AnalyticsHelper (com.voipgrid.vialer.analytics.AnalyticsHelper)5 RemoteLogger (com.voipgrid.vialer.logging.RemoteLogger)5 AnalyticsApplication (com.voipgrid.vialer.analytics.AnalyticsApplication)4 AccountHelper (com.voipgrid.vialer.util.AccountHelper)4 Intent (android.content.Intent)3 Bundle (android.os.Bundle)3 Preferences (com.voipgrid.vialer.Preferences)3 Api (com.voipgrid.vialer.api.Api)3 SetupActivity (com.voipgrid.vialer.onboarding.SetupActivity)3 SharedPreferences (android.content.SharedPreferences)2 View (android.view.View)2 TextView (android.widget.TextView)2 Registration (com.voipgrid.vialer.api.Registration)2 PhoneAccount (com.voipgrid.vialer.api.models.PhoneAccount)2 ReachabilityReceiver (com.voipgrid.vialer.reachability.ReachabilityReceiver)2 ConnectivityHelper (com.voipgrid.vialer.util.ConnectivityHelper)2 PhoneAccountHelper (com.voipgrid.vialer.util.PhoneAccountHelper)2 ResponseBody (okhttp3.ResponseBody)2