Search in sources :

Example 11 with AccountHelper

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

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