Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
hdkhang6803 committed Jan 24, 2024
2 parents fa9131d + 1674eae commit f807a68
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.annotation.NonNull;

Expand All @@ -15,12 +19,15 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.SocketTimeoutException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
Expand All @@ -37,8 +44,14 @@ public HttpImageTask(Context context, ImageQueryTaskListener imageQueryTaskListe
this.imageQueryTaskListener = imageQueryTaskListener;

Gson gson = new GsonBuilder().setLenient().create();
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS) // Connection timeout
.readTimeout(10, TimeUnit.SECONDS) // Read timeout
.writeTimeout(10, TimeUnit.SECONDS) // Write timeout
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://164.92.122.168:5000/")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
apiService = retrofit.create(ApiService.class);
Expand All @@ -49,7 +62,7 @@ public void uploadImage(String userId, File imageFile) {
// Create request body for userId
RequestBody userIdRequestBody = RequestBody.create(MediaType.parse("text/plain"), userId);

// Create request body for imageFile's base64 string
// Create request body for imageFile and MultipartBody for sending the whole file
RequestBody imageFileRequestBody = RequestBody.create(MediaType.parse("image/*"), imageFile);
MultipartBody.Part imageFilePart = MultipartBody.Part.createFormData("file", imageFile.getName(), imageFileRequestBody);

Expand All @@ -70,6 +83,7 @@ public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerRespo
notifyImageQueryResponseReceived(imageUriList);
}
} else {
changeButtonsVisibility();
Log.e("HTTP Image Query Server error", "Server Response Code: " + response.code());
}
} finally {
Expand All @@ -82,7 +96,26 @@ public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerRespo

@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
Log.e("HTTP Image failure", "Error: " + t.getMessage());
changeButtonsVisibility();
showTimeoutToast();
if (t instanceof SocketTimeoutException) {
showTimeoutToast();
} else {
Log.e("HTTP Image failure", "Error: " + t.getMessage());
}
}

private void changeButtonsVisibility(){
ImageButton sendButton = ((MainActivity) context).findViewById(R.id.sendButton);
ImageButton cameraButton = ((MainActivity) context).findViewById(R.id.cameraButton);
ProgressBar progressBarQuery = ((MainActivity) context).findViewById(R.id.progressBarQuery);
sendButton.setVisibility(View.VISIBLE);
cameraButton.setVisibility(View.VISIBLE);
progressBarQuery.setVisibility(View.GONE);
}

private void showTimeoutToast() {
Toast.makeText(context, "Request timed out. Please check your Internet connection.", Toast.LENGTH_LONG).show();
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.Toast;

import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
Expand All @@ -22,8 +29,15 @@ public class HttpTextTask {
public HttpTextTask(Context context, TextQueryTaskListener textQueryTaskListener) {
this.context = context;
this.textQueryTaskListener = textQueryTaskListener;

OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS) // Connection timeout
.readTimeout(10, TimeUnit.SECONDS) // Read timeout
.writeTimeout(10, TimeUnit.SECONDS) // Write timeout
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://164.92.122.168:5000/")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();

Expand All @@ -47,6 +61,7 @@ public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerRespo
notifyTextQueryResponseReceived(imageUriList);
}
} else {
changeButtonsVisibility();
Log.e("HTTP Text Query Server error", "Server Response Code: " + response.code());
}
} finally {
Expand All @@ -59,14 +74,31 @@ public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerRespo

@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
if (t instanceof HttpException) {
changeButtonsVisibility();
showTimeoutToast();
if (t instanceof SocketTimeoutException) {
showTimeoutToast();
} else if (t instanceof HttpException) {
HttpException httpException = (HttpException) t;
int statusCode = httpException.code();
Log.e("HTTP Failure", "Error Code: " + statusCode);
} else {
Log.e("HTTP Failure", "Error: " + t.getMessage());
}
}

private void changeButtonsVisibility(){
ImageButton sendButton = ((MainActivity) context).findViewById(R.id.sendButton);
ImageButton cameraButton = ((MainActivity) context).findViewById(R.id.cameraButton);
ProgressBar progressBarQuery = ((MainActivity) context).findViewById(R.id.progressBarQuery);
sendButton.setVisibility(View.VISIBLE);
cameraButton.setVisibility(View.VISIBLE);
progressBarQuery.setVisibility(View.GONE);
}

private void showTimeoutToast() {
Toast.makeText(context, "Request timed out. Please check your Internet connection.", Toast.LENGTH_LONG).show();
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,19 @@ public void showImageOptionsPopup(String imageUri, String userId) {
public void onClick(View v) {
try {
File imageFile = new File(imageUri);
ImageButton sendButton = ((MainActivity) context).findViewById(R.id.sendButton);
ImageButton cameraButton = ((MainActivity) context).findViewById(R.id.cameraButton);
ProgressBar progressBarQuery = ((MainActivity) context).findViewById(R.id.progressBarQuery);
sendButton.setVisibility(View.GONE);
cameraButton.setVisibility(View.GONE);
progressBarQuery.setVisibility(View.VISIBLE);

HttpImageTask httpImageTask = new HttpImageTask(context, new HttpImageTask.ImageQueryTaskListener() {
@Override
public void onImageQueryResponseReceived(List<String> imageUriList) {
ProgressBar progressBar = ((MainActivity) context).findViewById(R.id.progressBarQuery);
progressBar.setVisibility(View.GONE);
sendButton.setVisibility(View.VISIBLE);
cameraButton.setVisibility(View.VISIBLE);
progressBarQuery.setVisibility(View.GONE);
if (imageUriList == null || imageUriList.size() == 0){
setImageUriList(new ArrayList<>());
notifyDataSetChanged();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ public void onClick(View view) {
editText.clearFocus();
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
CameraUtil.dispatchTakePictureIntent(context);
sendButton.setVisibility(View.GONE);
cameraButton.setVisibility(View.GONE);
progressBarQuery.setVisibility(View.VISIBLE);
}
});
Expand All @@ -248,6 +250,8 @@ public void onClick(View view) {
editText.clearFocus();
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

sendButton.setVisibility(View.GONE);
cameraButton.setVisibility(View.GONE);
progressBarQuery.setVisibility(View.VISIBLE);

Toast.makeText(getApplicationContext(), "The query is sent, please wait.", Toast.LENGTH_SHORT).show();
Expand Down Expand Up @@ -284,7 +288,9 @@ public void run() {
@Override
public void onTextQueryResponseReceived(List<String> imageUriList) {

// ẩn progress bar
// ẩn progress bar và hiện lại các nút
sendButton.setVisibility(View.VISIBLE);
cameraButton.setVisibility(View.VISIBLE);
progressBarQuery.setVisibility(View.GONE);

// đoạn này dùng để test
Expand Down Expand Up @@ -313,7 +319,9 @@ public void onTextQueryResponseReceived(List<String> imageUriList) {
@Override
public void onImageQueryResponseReceived(List<String> imageUriList) {

// ẩn progress bar
// ẩn progress bar và hiện lại các nút
sendButton.setVisibility(View.VISIBLE);
cameraButton.setVisibility(View.VISIBLE);
progressBarQuery.setVisibility(View.GONE);

if (imageUriList == null || imageUriList.size() == 0){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="36dp"
android:height="36dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="@color/white">
<group
android:pivotX="12"
android:pivotY="12"
android:scaleX="-1">
<path
android:fillColor="@android:color/white"
android:pathData="M15.5,14h-0.79l-0.28,-0.27c1.2,-1.4 1.82,-3.31 1.48,-5.34 -0.47,-2.78 -2.79,-5 -5.59,-5.34 -4.23,-0.52 -7.79,3.04 -7.27,7.27 0.34,2.8 2.56,5.12 5.34,5.59 2.03,0.34 3.94,-0.28 5.34,-1.48l0.27,0.28v0.79l4.25,4.25c0.41,0.41 1.08,0.41 1.49,0 0.41,-0.41 0.41,-1.08 0,-1.49L15.5,14zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/>
</group>

</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="36dp"
android:height="36dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="@color/white">
<path
android:fillColor="@android:color/white"
android:pathData="M18,16.08c-0.76,0 -1.44,0.3 -1.96,0.77L8.91,12.7c0.05,-0.23 0.09,-0.46 0.09,-0.7s-0.04,-0.47 -0.09,-0.7l7.05,-4.11c0.54,0.5 1.25,0.81 2.04,0.81 1.66,0 3,-1.34 3,-3s-1.34,-3 -3,-3 -3,1.34 -3,3c0,0.24 0.04,0.47 0.09,0.7L8.04,9.81C7.5,9.31 6.79,9 6,9c-1.66,0 -3,1.34 -3,3s1.34,3 3,3c0.79,0 1.5,-0.31 2.04,-0.81l7.12,4.16c-0.05,0.21 -0.08,0.43 -0.08,0.65 0,1.61 1.31,2.92 2.92,2.92s2.92,-1.31 2.92,-2.92 -1.31,-2.92 -2.92,-2.92z"/>
</vector>
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:layout_height="match_parent"
android:paddingBottom="16dp"
>
<ImageView
android:id="@+id/fullImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"/>
android:paddingBottom="8dp"
android:theme="@style/Base.Theme.Mobilesemanticimagesearchfrontend">

<ImageButton
android:id="@+id/cancelButton"
Expand All @@ -19,52 +15,51 @@
android:src="@drawable/cancel_icon"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="10dp"
android:layout_marginEnd="5dp"
android:background="@android:color/transparent"
android:layout_marginTop="10dp" />

android:layout_marginTop="5dp"/>

<ImageView
android:id="@+id/fullImageView"
android:layout_width="match_parent"
android:layout_height="630dp"
android:layout_below="@id/cancelButton"
android:scaleType="fitCenter"/>

<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/fullImageView"
app:cardCornerRadius="8dp"
app:cardBackgroundColor="@color/design_default_color_primary_dark"
app:cardElevation="10dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="16dp"

app:cardBackgroundColor="@color/white"
app:cardCornerRadius="20dp"
app:cardUseCompatPadding="false"
>

android:layout_marginStart="15dp">
<ImageButton
android:id="@+id/deleteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@android:color/transparent"
android:src="@drawable/search_next" />
android:layout_margin="8dp"
android:src="@drawable/round_search"
android:background="@android:color/transparent"/>
</com.google.android.material.card.MaterialCardView>

<com.google.android.material.card.MaterialCardView
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="16dp"
app:cardCornerRadius="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/white"
app:cardUseCompatPadding="false"
>
<ImageButton
android:id="@+id/shareButton"
android:background="@android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/share"
android:layout_below="@id/fullImageView"
app:cardCornerRadius="8dp"
app:cardBackgroundColor="@color/design_default_color_primary_dark"
app:cardElevation="10dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_margin="5dp"
/>
android:layout_marginEnd="15dp">
<ImageButton
android:id="@+id/shareButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:src="@drawable/round_share"
android:background="@android:color/transparent"/>
</com.google.android.material.card.MaterialCardView>
</RelativeLayout>


</RelativeLayout>

0 comments on commit f807a68

Please sign in to comment.