diff --git a/app/src/androidTest/java/ch/epfl/sdp/appart/FavoriteUITest.java b/app/src/androidTest/java/ch/epfl/sdp/appart/FavoriteUITest.java index 50e63d372..f035dabef 100644 --- a/app/src/androidTest/java/ch/epfl/sdp/appart/FavoriteUITest.java +++ b/app/src/androidTest/java/ch/epfl/sdp/appart/FavoriteUITest.java @@ -82,7 +82,7 @@ public void init() { // clear shared preferences to avoid auto-login mActivityTestRule.getScenario().onActivity(SharedPreferencesHelper::clearSavedUserForAutoLogin); } - + /** * taken from : * https://stackoverflow.com/questions/29378552/in-espresso-how-to-avoid-ambiguousviewmatcherexception-when-multiple-views-matc @@ -170,10 +170,7 @@ public void favoriteUITest() { ViewInteraction appCompatImageView = onView(withIndex(withId(R.id.image_CardLayout_imageView), 0)); appCompatImageView.perform(forceClick()); - ViewInteraction actionMenuItemView = onView( - allOf(withId(R.id.action_add_favorite), withContentDescription("Add to Favorites"), - isDisplayed())); - actionMenuItemView.perform(click()); + onView(withId(R.id.action_add_favorite)).perform(click()); ViewInteraction overflowMenuButton3 = onView( allOf(withContentDescription("More options"), @@ -307,5 +304,5 @@ public void release() { mActivityTestRule.getScenario().onActivity(SharedPreferencesHelper::clearSavedUserForAutoLogin); login.signOut(); } - + } diff --git a/app/src/androidTest/java/ch/epfl/sdp/appart/SimpleUserProfileActivityTest.java b/app/src/androidTest/java/ch/epfl/sdp/appart/SimpleUserProfileActivityTest.java index 3c50030c2..4098a9bbe 100644 --- a/app/src/androidTest/java/ch/epfl/sdp/appart/SimpleUserProfileActivityTest.java +++ b/app/src/androidTest/java/ch/epfl/sdp/appart/SimpleUserProfileActivityTest.java @@ -5,6 +5,8 @@ import android.view.ViewGroup; import android.view.ViewParent; +import android.widget.Button; +import androidx.appcompat.app.AlertDialog; import androidx.test.espresso.UiController; import androidx.test.espresso.ViewAction; import org.hamcrest.Description; @@ -35,9 +37,11 @@ import dagger.hilt.android.testing.UninstallModules; import static androidx.test.espresso.Espresso.onView; +import static androidx.test.espresso.Espresso.pressBack; import static androidx.test.espresso.action.ViewActions.click; import static androidx.test.espresso.action.ViewActions.scrollTo; import static androidx.test.espresso.assertion.ViewAssertions.matches; +import static androidx.test.espresso.matcher.RootMatchers.isDialog; import static androidx.test.espresso.matcher.ViewMatchers.isClickable; import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription; @@ -68,6 +72,7 @@ public void init() { hiltRule.inject(); } + public static ViewAction forceClick() { return new ViewAction() { @Override public Matcher getConstraints() { @@ -295,6 +300,24 @@ public void simpleUserProfileActivityTest() { withParent(withParent(IsInstanceOf.instanceOf(android.widget.LinearLayout.class))), isDisplayed())); imageView2.check(matches(isDisplayed())); + + ViewInteraction appCompatButton2 = onView( + allOf(withId(R.id.contact_SimpleUserProfile_button), withText("Contact"))); + appCompatButton2.perform(click()); + + + onView(withText("How did you prefer contact the announcer ?")) + .inRoot(isDialog()) + .check(matches(isDisplayed())); + + onView(withText("Contact via Email")) + .inRoot(isDialog()) + .check(matches(isDisplayed())); + + onView(withText("Contact via phone number")) + .inRoot(isDialog()) + .check(matches(isDisplayed())); + } private static Matcher childAtPosition( diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4473dfa0a..09bd625f8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -55,5 +55,7 @@ + + \ No newline at end of file diff --git a/app/src/main/java/ch/epfl/sdp/appart/SimpleUserProfileActivity.java b/app/src/main/java/ch/epfl/sdp/appart/SimpleUserProfileActivity.java index ecd8e66fb..0378b8894 100644 --- a/app/src/main/java/ch/epfl/sdp/appart/SimpleUserProfileActivity.java +++ b/app/src/main/java/ch/epfl/sdp/appart/SimpleUserProfileActivity.java @@ -1,29 +1,37 @@ package ch.epfl.sdp.appart; +import static android.widget.Toast.makeText; + +import android.Manifest; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.net.Uri; +import android.util.Log; +import android.widget.Toast; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; -import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; +import ch.epfl.sdp.appart.utils.PermissionRequest; import javax.inject.Inject; import androidx.lifecycle.ViewModelProvider; -import java.util.concurrent.CompletableFuture; - import ch.epfl.sdp.appart.database.DatabaseService; -import ch.epfl.sdp.appart.database.local.LocalDatabase; import ch.epfl.sdp.appart.database.local.LocalDatabaseService; import ch.epfl.sdp.appart.glide.visitor.GlideImageViewLoader; import ch.epfl.sdp.appart.user.User; import ch.epfl.sdp.appart.user.UserViewModel; import ch.epfl.sdp.appart.utils.ActivityCommunicationLayout; -import ch.epfl.sdp.appart.utils.DatabaseSync; import dagger.hilt.android.AndroidEntryPoint; @AndroidEntryPoint @@ -49,6 +57,8 @@ public class SimpleUserProfileActivity extends AppCompatActivity { private TextView emailTextView; private ImageView imageView; + private final static int PHONE_CALL_PERMISSION_CODE = 123; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -80,11 +90,61 @@ protected void onCreate(Bundle savedInstanceState) { } /** - * closes activity when back button pressed on UI + * Contact announcer. */ - public void contactAdUser(View view) { - // TODO: send message to user + public void openEmailOrPhone(View view){ + if(!advertiserUser.getPhoneNumber().isEmpty()) { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle("How did you prefer contact the announcer ?"); + builder.setPositiveButton("Contact via Email", (dialog, which) -> onEmail()); + builder.setNeutralButton("Contact via phone number", (dialog, which) -> onCall()); + AlertDialog dialog = builder.create(); + dialog.show(); + } else { + onEmail(); + } + } + + private void onEmail() { + Intent intent = new Intent(Intent.ACTION_SENDTO); + intent.setData(Uri.parse("mailto:")); + intent.putExtra(Intent.EXTRA_EMAIL, new String[]{advertiserUser.getUserEmail()}); + intent.putExtra(Intent.EXTRA_SUBJECT, "Rent apartment"); + if (intent.resolveActivity(getPackageManager()) != null) { + startActivity(intent); + } else { + makeText(this, "Error open email, try again",Toast.LENGTH_SHORT).show(); + + } + } + private void onCall() { + int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE); + if (permissionCheck != PackageManager.PERMISSION_GRANTED) { + ActivityCompat.requestPermissions( + this, + new String[]{Manifest.permission.CALL_PHONE}, + PHONE_CALL_PERMISSION_CODE); + } else { + startActivity(new Intent(Intent.ACTION_CALL).setData(Uri.parse("tel:"+advertiserUser.getPhoneNumber()))); + } + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + switch (requestCode) { + case PHONE_CALL_PERMISSION_CODE: + if(PermissionRequest.checkPermission(grantResults)){ + onCall(); + }else{ + makeText(this, "Permission Not Granted",Toast.LENGTH_SHORT).show(); + } + break; + default: + break; + } } + /** * @param user sets the value of the current user to the session user object diff --git a/app/src/main/java/ch/epfl/sdp/appart/utils/PermissionRequest.java b/app/src/main/java/ch/epfl/sdp/appart/utils/PermissionRequest.java index da2d86c49..e2faecc1c 100644 --- a/app/src/main/java/ch/epfl/sdp/appart/utils/PermissionRequest.java +++ b/app/src/main/java/ch/epfl/sdp/appart/utils/PermissionRequest.java @@ -112,4 +112,9 @@ public static void askForStoragePermission(Activity activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}); } + + public static boolean checkPermission(int[] grantResults){ + return ((grantResults.length > 0) && (grantResults[0] + == PackageManager.PERMISSION_GRANTED)); + } } diff --git a/app/src/main/res/layout/activity_filter.xml b/app/src/main/res/layout/activity_filter.xml index c37b17e85..d671e18b6 100644 --- a/app/src/main/res/layout/activity_filter.xml +++ b/app/src/main/res/layout/activity_filter.xml @@ -39,7 +39,7 @@ android:layout_height="wrap_content" android:layout_marginTop="32dp" android:text="@string/filterPrice" - android:textSize="16dp" + android:textSize="16sp" app:layout_constraintEnd_toStartOf="@+id/guideline30" app:layout_constraintStart_toStartOf="@+id/guideline29" app:layout_constraintTop_toBottomOf="@+id/title_Filter_TextView" /> @@ -117,7 +117,7 @@ android:layout_height="wrap_content" android:layout_marginTop="32dp" android:text="@string/filterLocation" - android:textSize="16dp" + android:textSize="16sp" app:layout_constraintEnd_toStartOf="@+id/guideline30" app:layout_constraintStart_toStartOf="@+id/guideline29" app:layout_constraintTop_toBottomOf="@+id/price_Filter_linearLayout" /> diff --git a/app/src/main/res/layout/activity_simple_user_profile.xml b/app/src/main/res/layout/activity_simple_user_profile.xml index 6978d6c28..e7e0a7bfb 100644 --- a/app/src/main/res/layout/activity_simple_user_profile.xml +++ b/app/src/main/res/layout/activity_simple_user_profile.xml @@ -300,6 +300,7 @@ android:drawableEnd="@drawable/ic_call" android:textColor="@color/primaryStart" android:textSize="16sp" + android:onClick="openEmailOrPhone" android:textAllCaps="false" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" />