|
| 1 | +package org.cientopolis.samplers.authentication; |
| 2 | + |
| 3 | + |
| 4 | +import android.content.Context; |
| 5 | +import android.content.SharedPreferences; |
| 6 | +import com.google.gson.Gson; |
| 7 | +import org.cientopolis.samplers.bus.BusProvider; |
| 8 | +import org.cientopolis.samplers.bus.LoginEvent; |
| 9 | + |
| 10 | +/** |
| 11 | + * Created by Xavier on 04/04/2018. |
| 12 | + */ |
| 13 | + |
| 14 | +public class AuthenticationManager { |
| 15 | + |
| 16 | + private static final String PREFERENCES_NAME = "org.cientopolis.samplers.PREFERENCES_NAME"; |
| 17 | + private static final String PREFERENCES_KEY_USER = "org.cientopolis.samplers.PREFERENCES_KEY_USER"; |
| 18 | + private static final String PREFERENCES_KEY_USER_CLASS = "org.cientopolis.samplers.PREFERENCES_KEY_USER_CLASS"; |
| 19 | + |
| 20 | + private static boolean authenticationEnabled = false; |
| 21 | + private static boolean authenticationOptional = true; |
| 22 | + private static Class loginFragmentClass = StandardLoginFragment.class; |
| 23 | + |
| 24 | + public static <T extends LoginFragment> Class<T> getLoginFragmentClass() { |
| 25 | + return loginFragmentClass; |
| 26 | + } |
| 27 | + |
| 28 | + public static <T extends LoginFragment> void setLoginFragmentClass(Class<T> type) { |
| 29 | + AuthenticationManager.loginFragmentClass = type; |
| 30 | + } |
| 31 | + |
| 32 | + public static User getUser(Context context) { |
| 33 | + |
| 34 | + return retrieveUser(context); |
| 35 | + } |
| 36 | + |
| 37 | + public static void login(User user, Context context) { |
| 38 | + |
| 39 | + saveUser(user, context); |
| 40 | + |
| 41 | + BusProvider.getInstance().post(new LoginEvent(user)); |
| 42 | + } |
| 43 | + |
| 44 | + public static void logout(Context context) { |
| 45 | + |
| 46 | + removeUser(context); |
| 47 | + |
| 48 | + BusProvider.getInstance().post(new LoginEvent(null)); |
| 49 | + } |
| 50 | + |
| 51 | + private static void saveUser(User user, Context context) { |
| 52 | + |
| 53 | + SharedPreferences sharedPref = context.getSharedPreferences (PREFERENCES_NAME,Context.MODE_PRIVATE); |
| 54 | + SharedPreferences.Editor editor = sharedPref.edit(); |
| 55 | + |
| 56 | + Gson gson = new Gson(); |
| 57 | + String jsonUser = gson.toJson(user); |
| 58 | + |
| 59 | + editor.putString(PREFERENCES_KEY_USER,jsonUser); |
| 60 | + editor.putString(PREFERENCES_KEY_USER_CLASS,user.getClass().getCanonicalName()); |
| 61 | + editor.commit(); |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + private static void removeUser(Context context) { |
| 66 | + SharedPreferences sharedPref = context.getSharedPreferences (PREFERENCES_NAME,Context.MODE_PRIVATE); |
| 67 | + SharedPreferences.Editor editor = sharedPref.edit(); |
| 68 | + |
| 69 | + editor.remove(PREFERENCES_KEY_USER); |
| 70 | + editor.remove(PREFERENCES_KEY_USER_CLASS); |
| 71 | + editor.commit(); |
| 72 | + } |
| 73 | + |
| 74 | + private static User retrieveUser(Context context) { |
| 75 | + SharedPreferences sharedPref = context.getSharedPreferences (PREFERENCES_NAME,Context.MODE_PRIVATE); |
| 76 | + |
| 77 | + Gson gson = new Gson(); |
| 78 | + String jsonUser = sharedPref.getString(PREFERENCES_KEY_USER, ""); |
| 79 | + String className = sharedPref.getString(PREFERENCES_KEY_USER_CLASS, ""); |
| 80 | + User obj = null; |
| 81 | + |
| 82 | + if (!jsonUser.equals("")) { |
| 83 | + |
| 84 | + try { |
| 85 | + obj = (User) gson.fromJson(jsonUser, Class.forName(className)); |
| 86 | + } catch (ClassNotFoundException e) { |
| 87 | + e.printStackTrace(); |
| 88 | + throw new RuntimeException(""); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return obj; |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + public static boolean isAuthenticationEnabled() { |
| 97 | + return authenticationEnabled; |
| 98 | + } |
| 99 | + |
| 100 | + public static void setAuthenticationEnabled(boolean authenticationEnabled) { |
| 101 | + AuthenticationManager.authenticationEnabled = authenticationEnabled; |
| 102 | + } |
| 103 | + |
| 104 | + public static boolean isAuthenticationOptional() { |
| 105 | + return authenticationOptional; |
| 106 | + } |
| 107 | + |
| 108 | + public static void setAuthenticationOptional(boolean authenticationOptional) { |
| 109 | + AuthenticationManager.authenticationOptional = authenticationOptional; |
| 110 | + } |
| 111 | +} |
0 commit comments