diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/main/MainActivity.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/main/MainActivity.java index 480a941..0c02325 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/main/MainActivity.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/main/MainActivity.java @@ -284,7 +284,7 @@ public class MainActivity extends BaseActivity { } @Override - public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == LOCATION_PERMISSIONS_REQUEST_CODE_FOR_DISTANCE_FILTER) { MainTask mainTask = new MainTask(); mainTask.execute(); diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/base/BaseActivity.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/base/BaseActivity.java index ac7158f..8c4e1b1 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/base/BaseActivity.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/base/BaseActivity.java @@ -1,13 +1,16 @@ package gr.auth.databases.flavours.base; import android.Manifest; +import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; +import android.text.InputType; import android.view.View; +import android.widget.EditText; import com.mikepenz.fontawesome_typeface_library.FontAwesome; import com.mikepenz.google_material_typeface_library.GoogleMaterial; @@ -22,6 +25,7 @@ import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem; import com.mikepenz.materialdrawer.model.interfaces.IProfile; import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import androidx.core.content.ContextCompat; @@ -40,6 +44,7 @@ import static gr.auth.databases.flavours.activities.profile.ProfileActivity.BUND import static gr.auth.databases.flavours.activities.profile.ProfileActivity.BUNDLE_USER_NAME; public abstract class BaseActivity extends AppCompatActivity { + public static final String PREF_BASE_SERVER_IP = "PREF_BASE_SERVER_IP"; // Client & Cookies protected static OkHttpClient client; @@ -52,6 +57,7 @@ public abstract class BaseActivity extends AppCompatActivity { protected Toolbar toolbar; protected Drawer drawer; + private boolean isBaseIPSetDialogShown = false; private boolean isMainActivity; @Override @@ -78,6 +84,11 @@ public abstract class BaseActivity extends AppCompatActivity { protected void onResume() { super.onResume(); updateDrawer(); + String baseServerIP = sharedPreferences.getString(PREF_BASE_SERVER_IP, null); + if ((baseServerIP == null || baseServerIP.isEmpty()) && !isBaseIPSetDialogShown) { + isBaseIPSetDialogShown = true; + showBaseIPSetDialog(); + } } @Override @@ -328,4 +339,27 @@ public abstract class BaseActivity extends AppCompatActivity { updateDrawer(); } } + + private void showBaseIPSetDialog() { + AlertDialog.Builder builder = new AlertDialog.Builder(getThemedContext()); + + final EditText serverIPInput = new EditText(this); + serverIPInput.setInputType(InputType.TYPE_CLASS_PHONE); + + builder.setTitle("Base server IP address") + .setView(serverIPInput) + .setMessage("Please provide the base IP of the server (xxx.xxx.xxx.xxx) to be used in the requests.") + .setCancelable(false) + .setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.putString(PREF_BASE_SERVER_IP, serverIPInput.getText().toString()).apply(); + SessionManager.setBaseServerUrl(serverIPInput.getText().toString()); + isBaseIPSetDialogShown = false; + } + }); + AlertDialog alertDialog = builder.create(); + alertDialog.show(); + } } diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/base/BaseApplication.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/base/BaseApplication.java index f22fe74..47c81b6 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/base/BaseApplication.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/base/BaseApplication.java @@ -13,12 +13,15 @@ import java.io.IOException; import java.util.concurrent.TimeUnit; import androidx.annotation.NonNull; +import androidx.preference.PreferenceManager; import gr.auth.databases.flavours.session.SessionManager; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; +import static gr.auth.databases.flavours.base.BaseActivity.PREF_BASE_SERVER_IP; + public class BaseApplication extends Application { public static final String CSRF_TOKEN = "CSRF_TOKEN"; @@ -62,7 +65,6 @@ public class BaseApplication extends Application { final Request authorized = original.newBuilder() .addHeader("X-CSRFToken", csrfToken) .build(); - return chain.proceed(authorized); } @@ -70,7 +72,10 @@ public class BaseApplication extends Application { client = builder.build(); - sessionManager = new SessionManager(client, cookieJar, sharedPrefs); + + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext()); + String baseServerIP = sharedPreferences.getString(PREF_BASE_SERVER_IP, null); + sessionManager = new SessionManager(client, cookieJar, sharedPrefs, baseServerIP); DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics(); dpWidth = displayMetrics.widthPixels / displayMetrics.density; diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/session/SessionManager.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/session/SessionManager.java index e23b7e1..260d457 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/session/SessionManager.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/session/SessionManager.java @@ -48,39 +48,38 @@ public class SessionManager { } //Generic constants - private static final String baseServerUrl = "http://83.212.109.171:8181/"; - private static final String loginUrl = baseServerUrl + "api/rest-auth/login/"; - private static final String signupUrl = baseServerUrl + "api/rest-auth/registration/"; - private static final String logoutUrl = baseServerUrl + "api/rest-auth/logout/"; - private static final String setUserBirthday = baseServerUrl + "api/setUserBirthday/"; - public static final String restaurantsUrl = baseServerUrl + "api/restaurant/"; - public static final String restaurantsUserViewUrl = baseServerUrl + "api/restaurantUserView/"; - public static final String getUserDietsUrl = baseServerUrl + "api/userDiets/"; - public static final String rateDrinkUrl = baseServerUrl + "api/userratesdrink/"; - public static final String rateFoodUrl = baseServerUrl + "api/userratesfood/"; - public static final String rateRestaurantUrl = baseServerUrl + "api/userratesrestaurant/"; - public static final String foodsUserViewUrl = baseServerUrl + "api/foodUserView/"; - public static final String drinksUserViewUrl = baseServerUrl + "api/drinkUserView/"; - public static final String profileUserViewUrl = baseServerUrl + "api/profileUserView/"; - public static final String addFoodUrl = baseServerUrl + "api/addFood/"; - public static final String addDrinkUrl = baseServerUrl + "api/addDrink/"; - public static final String dietsUserViewUrl = baseServerUrl + "api/dietUserView/"; - public static final String ingredientsUserViewUrl = baseServerUrl + "api/ingredientUserView/"; - public static final String prohibitIngredientUrl = baseServerUrl + "api/prohibitIngredient/"; - public static final String followDietUrl = baseServerUrl + "api/followDiet/"; - public static final String addIngredientUrl = baseServerUrl + "api/ingredient/"; - public static final String addDietUrl = baseServerUrl + "api/addDiet/"; - public static final String addIngredientToFoodUrl = baseServerUrl + "api/foodhasingredient/"; - public static final String addIngredientToDrinkUrl = baseServerUrl + "api/drinkhasingredient/"; - public static final String acceptRestaurantUrl = baseServerUrl + "api/acceptRestaurant/"; - public static final String acceptFoodUrl = baseServerUrl + "api/acceptFood/"; - public static final String acceptDrinkUrl = baseServerUrl + "api/acceptDrink/"; - public static final String acceptDietUrl = baseServerUrl + "api/acceptDiet/"; + private static String baseServerUrl = "http://0.0.0.0:8181/"; + public static String restaurantsUrl = baseServerUrl + "api/restaurant/"; + public static String restaurantsUserViewUrl = baseServerUrl + "api/restaurantUserView/"; + public static String getUserDietsUrl = baseServerUrl + "api/userDiets/"; + public static String rateDrinkUrl = baseServerUrl + "api/userratesdrink/"; + public static String rateFoodUrl = baseServerUrl + "api/userratesfood/"; + public static String rateRestaurantUrl = baseServerUrl + "api/userratesrestaurant/"; + public static String foodsUserViewUrl = baseServerUrl + "api/foodUserView/"; + public static String drinksUserViewUrl = baseServerUrl + "api/drinkUserView/"; + public static String profileUserViewUrl = baseServerUrl + "api/profileUserView/"; + public static String addFoodUrl = baseServerUrl + "api/addFood/"; + public static String addDrinkUrl = baseServerUrl + "api/addDrink/"; + public static String dietsUserViewUrl = baseServerUrl + "api/dietUserView/"; + public static String ingredientsUserViewUrl = baseServerUrl + "api/ingredientUserView/"; + public static String prohibitIngredientUrl = baseServerUrl + "api/prohibitIngredient/"; + public static String followDietUrl = baseServerUrl + "api/followDiet/"; + public static String addIngredientUrl = baseServerUrl + "api/ingredient/"; + public static String addDietUrl = baseServerUrl + "api/addDiet/"; + public static String addIngredientToFoodUrl = baseServerUrl + "api/foodhasingredient/"; + public static String addIngredientToDrinkUrl = baseServerUrl + "api/drinkhasingredient/"; + public static String acceptRestaurantUrl = baseServerUrl + "api/acceptRestaurant/"; + public static String acceptFoodUrl = baseServerUrl + "api/acceptFood/"; + public static String acceptDrinkUrl = baseServerUrl + "api/acceptDrink/"; + public static String acceptDietUrl = baseServerUrl + "api/acceptDiet/"; + private static String loginUrl = baseServerUrl + "api/rest-auth/login/"; + private static String signupUrl = baseServerUrl + "api/rest-auth/registration/"; + private static String logoutUrl = baseServerUrl + "api/rest-auth/logout/"; + private static String setUserBirthday = baseServerUrl + "api/setUserBirthday/"; // Client & Cookies private final OkHttpClient client; private final PersistentCookieJar cookieJar; - //private final SharedPrefsCookiePersistor cookiePersistor; //Shared Preferences & its keys private final SharedPreferences sharedPrefs; @@ -91,56 +90,50 @@ public class SessionManager { private static final String LOGIN_SCREEN_AS_DEFAULT = "LoginScreenAsDefault"; //Constructor - public SessionManager(OkHttpClient client, PersistentCookieJar cookieJar, SharedPreferences sharedPrefs) { + public SessionManager(OkHttpClient client, PersistentCookieJar cookieJar, SharedPreferences sharedPrefs, String baseServerIP) { this.client = client; this.cookieJar = cookieJar; this.sharedPrefs = sharedPrefs; - } - public int login(String... strings) { - //Builds the login request for each case - Request request; - clearSessionData(); - - String username = strings[0]; - String password = strings[1]; - - RequestBody formBody = new FormBody.Builder() - .add("username", username) - .add("password", password) - .build(); - request = new Request.Builder() - .url(loginUrl) - .post(formBody) - .build(); - - try { - //Makes request & handles response - Response response = client.newCall(request).execute(); - - setPersistentCookieSession(loginUrl); //Store cookies - - ResponseBody responseBody = response.body(); - assert responseBody != null; - JSONObject jsonResponse = new JSONObject(responseBody.string()); - JSONObject jsonUser = jsonResponse.getJSONObject("user"); - int extractedUserId = jsonUser.getInt("id"); - int extractedRole = jsonUser.getInt("role"); - - //Edit SharedPreferences, save session's data - SharedPreferences.Editor editor = sharedPrefs.edit(); - setLoginScreenAsDefault(false); - editor.putBoolean(LOGGED_IN, true); - editor.putString(USERNAME, username); - editor.putInt(USER_ID, extractedUserId); - editor.putInt(USER_TYPE, extractedRole); - editor.apply(); - return 0; - } catch (Exception e) { - return 2; + if (baseServerIP != null && !baseServerIP.isEmpty()) { + setBaseServerUrl(baseServerIP); } } + public static void setBaseServerUrl(String baseServerIP) { + // Sets base url + SessionManager.baseServerUrl = "http://" + baseServerIP + ":8181/"; + + // Sets all other api point urls + loginUrl = baseServerUrl + "api/rest-auth/login/"; + signupUrl = baseServerUrl + "api/rest-auth/registration/"; + logoutUrl = baseServerUrl + "api/rest-auth/logout/"; + setUserBirthday = baseServerUrl + "api/setUserBirthday/"; + restaurantsUrl = baseServerUrl + "api/restaurant/"; + restaurantsUserViewUrl = baseServerUrl + "api/restaurantUserView/"; + getUserDietsUrl = baseServerUrl + "api/userDiets/"; + rateDrinkUrl = baseServerUrl + "api/userratesdrink/"; + rateFoodUrl = baseServerUrl + "api/userratesfood/"; + rateRestaurantUrl = baseServerUrl + "api/userratesrestaurant/"; + foodsUserViewUrl = baseServerUrl + "api/foodUserView/"; + drinksUserViewUrl = baseServerUrl + "api/drinkUserView/"; + profileUserViewUrl = baseServerUrl + "api/profileUserView/"; + addFoodUrl = baseServerUrl + "api/addFood/"; + addDrinkUrl = baseServerUrl + "api/addDrink/"; + dietsUserViewUrl = baseServerUrl + "api/dietUserView/"; + ingredientsUserViewUrl = baseServerUrl + "api/ingredientUserView/"; + prohibitIngredientUrl = baseServerUrl + "api/prohibitIngredient/"; + followDietUrl = baseServerUrl + "api/followDiet/"; + addIngredientUrl = baseServerUrl + "api/ingredient/"; + addDietUrl = baseServerUrl + "api/addDiet/"; + addIngredientToFoodUrl = baseServerUrl + "api/foodhasingredient/"; + addIngredientToDrinkUrl = baseServerUrl + "api/drinkhasingredient/"; + acceptRestaurantUrl = baseServerUrl + "api/acceptRestaurant/"; + acceptFoodUrl = baseServerUrl + "api/acceptFood/"; + acceptDrinkUrl = baseServerUrl + "api/acceptDrink/"; + acceptDietUrl = baseServerUrl + "api/acceptDiet/"; + } + public int signup(String... strings) { //Builds the signup request Request request; @@ -206,18 +199,6 @@ public class SessionManager { } } - public void validateSession() { - if (isLoggedIn()) { - int loginResult = login(); - if (loginResult != 1) - return; - } else if (isLoginScreenDefault()) - return; - - setLoginScreenAsDefault(true); - clearSessionData(); - } - public int logout() { RequestBody requestBody = RequestBody.create(null, new byte[]{}); @@ -252,6 +233,51 @@ public class SessionManager { return UserType.fromInteger(sharedPrefs.getInt(USER_TYPE, -1)); } + public int login(String... strings) { + //Builds the login request for each case + Request request; + clearSessionData(); + + String username = strings[0]; + String password = strings[1]; + + RequestBody formBody = new FormBody.Builder() + .add("username", username) + .add("password", password) + .build(); + request = new Request.Builder() + .url(loginUrl) + .post(formBody) + .build(); + + try { + //Makes request & handles response + Response response = client.newCall(request).execute(); + + setPersistentCookieSession(loginUrl); //Store cookies + + ResponseBody responseBody = response.body(); + assert responseBody != null; + JSONObject jsonResponse = new JSONObject(responseBody.string()); + JSONObject jsonUser = jsonResponse.getJSONObject("user"); + int extractedUserId = jsonUser.getInt("id"); + int extractedRole = jsonUser.getInt("role"); + + //Edit SharedPreferences, save session's data + SharedPreferences.Editor editor = sharedPrefs.edit(); + setLoginScreenAsDefault(false); + editor.putBoolean(LOGGED_IN, true); + editor.putString(USERNAME, username); + editor.putInt(USER_ID, extractedUserId); + editor.putInt(USER_TYPE, extractedRole); + editor.apply(); + return 0; + } catch (Exception e) { + e.printStackTrace(); + return 2; + } + } + public boolean isLoggedIn() { return sharedPrefs.getBoolean(LOGGED_IN, false); } @@ -264,9 +290,21 @@ public class SessionManager { HttpUrl httpUrl = HttpUrl.parse(forUrl); assert httpUrl != null; List cookieList = cookieJar.loadForRequest(httpUrl); + String csrfToken = ""; + int maxLength = 0; SharedPreferences.Editor editor = sharedPrefs.edit(); - editor.putString(CSRF_TOKEN, cookieList.get(0).value()); + + for (int cookieIndex = 0; cookieIndex < cookieList.size(); ++cookieIndex) { + Cookie cookie = cookieList.get(cookieIndex); + if (!cookie.value().contains("Successfully signed in") && cookie.value().length() > maxLength) { + // The longest one is the CSRF token + csrfToken = cookie.value(); + maxLength = csrfToken.length(); + } + } + + editor.putString(CSRF_TOKEN, csrfToken); editor.apply(); } diff --git a/release.apk b/release.apk deleted file mode 100644 index 2cee9c9..0000000 Binary files a/release.apk and /dev/null differ