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