Skip to content

Commit

Permalink
Async task for search results
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashpex committed Jan 12, 2022
1 parent 3a3f761 commit e9de0b0
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 49 deletions.
8 changes: 5 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ dependencies {
implementation 'androidx.preference:preference:1.1.1'
implementation 'org.tensorflow:tensorflow-lite-support:0.1.0'
implementation 'org.tensorflow:tensorflow-lite-metadata:0.1.0'
implementation 'com.google.firebase:firebase-ml-vision:24.0.3'
implementation 'com.google.firebase:firebase-core:15.0.2'
implementation 'com.google.firebase:firebase-ml-vision:24.0.1'
//implementation 'com.google.firebase:firebase-bom:29.0.3'
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-ml-vision-image-label-model:20.0.1'
implementation 'com.google.android.gms:play-services-vision:17.0.2'
implementation 'com.google.firebase:firebase-bom:29.0.3'
//implementation 'com.google.firebase:firebase-analytics'
implementation 'com.android.volley:volley:1.2.1'

testImplementation 'junit:junit:4.+'
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<uses-permission android:name="android.permission.STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<!-- permission for internet -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INTERNET"
android:required="true"/>
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
Expand All @@ -27,7 +28,12 @@
android:configChanges="locale"

android:theme="@style/Theme.TestGallery">

<meta-data
android:name="com.google.mlkit.vision.DEPENDENCIES"
android:value="ica" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
android:value="label" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import com.example.testgallery.adapters.SearchRVAdapter;
import com.example.testgallery.adapters.SlideImageAdapter;
import com.example.testgallery.fragments.mainFragments.BottomSheetFragment;
import com.example.testgallery.fragments.mainFragments.PhotoFragment;
import com.example.testgallery.models.Album;
import com.example.testgallery.models.Image;
import com.example.testgallery.models.SearchRV;
Expand Down Expand Up @@ -108,7 +109,7 @@ public class PictureActivity extends AppCompatActivity implements PictureInterfa
private String title, link, displayedLink, snippet;
private RecyclerView resultsRV;
private SearchRVAdapter searchRVAdapter;
private ArrayList<SearchRV> searchRVArrayList;
//private ArrayList<SearchRV> searchRVArrayList;
private BottomSheetDialog bottomSheetDialog;
private RecyclerView ryc_album;
public static Set<String> imageListFavor = DataLocalManager.getListSet();
Expand Down Expand Up @@ -359,17 +360,58 @@ public void onClick(DialogInterface dialog, int which) {
intent.putExtra("mimeType", "image/*");
startActivity(Intent.createChooser(intent, "Set as:"));
case R.id.searchImage:
actionSearchImage();
searchImage();
}

return true;
}
});
}

private void actionSearchImage() {
searchRVArrayList = new ArrayList<>();
//searchRVAdapter = new SearchRVAdapter(searchRVArrayList,PictureActivity.this);
private void searchImage(){
SearchAsyncTask searchAsyncTask = new SearchAsyncTask();
searchAsyncTask.execute();
}

public class SearchAsyncTask extends AsyncTask<Void, Integer, Void> {

private ProgressDialog mProgressDialog ;
ArrayList<SearchRV> searchRVArrayList = new ArrayList<>();

@Override
protected Void doInBackground(Void... voids) {
//searchRVAdapter = new SearchRVAdapter(searchRVArrayList,PictureActivity.this);
getResults(searchRVArrayList);
return null;
}

@Override
protected void onPostExecute(Void unused) {
super.onPostExecute(unused);
BottomSheetFragment bottomSheetFragment = new BottomSheetFragment(searchRVArrayList, new IClickListener() {
@Override
public void clickItem(SearchRV searchRV) {
Toast.makeText(PictureActivity.this,"test",Toast.LENGTH_SHORT).show();
}
});
bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
mProgressDialog.cancel();
}

@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(PictureActivity.this);
mProgressDialog.setMessage("Loading, please wait...");
mProgressDialog.show();
searchRVArrayList.add(new SearchRV("Test 1","https://ashpex.eu.org","https://ashpex.eu.org","test test test"));
searchRVArrayList.add(new SearchRV("Test 2","https://ashpex.eu.org","https://ashpex.eu.org","test test test"));
}

}


private void getResults(ArrayList<SearchRV> searchRVArrayList){
Uri imageUri = Uri.parse("file://" + thumb);
try {
ParcelFileDescriptor parcelFileDescriptor =
Expand All @@ -381,49 +423,13 @@ private void actionSearchImage() {
} catch (IOException e) {
e.printStackTrace();
}

FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(imageBitmap);
FirebaseVisionImageLabeler labeler = FirebaseVision.getInstance().getOnDeviceImageLabeler();
labeler.processImage(image).addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionImageLabel>>() {
@Override
public void onSuccess(List<FirebaseVisionImageLabel> firebaseVisionImageLabels) {
String searchQuery = firebaseVisionImageLabels.get(0).getText();
getSearchResults(searchQuery,searchRVArrayList);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(PictureActivity.this, "Failed to detect image...", Toast.LENGTH_SHORT).show();
}
});

searchRVArrayList.add(new SearchRV("Test 1","https://ashpex.eu.org","https://ashpex.eu.org","test test test"));
searchRVArrayList.add(new SearchRV("Test 2","https://ashpex.eu.org","https://ashpex.eu.org","test test test"));
searchRVArrayList.add(new SearchRV("Test 3","https://ashpex.eu.org","https://ashpex.eu.org","test test test"));
searchRVArrayList.add(new SearchRV("Test 3","https://ashpex.eu.org","https://ashpex.eu.org","test test test"));


//resultsRV.setLayoutManager(new LinearLayoutManager(PictureActivity.this,LinearLayoutManager.HORIZONTAL,false));
//resultsRV.setAdapter(searchRVAdapter);
BottomSheetFragment bottomSheetFragment = new BottomSheetFragment(searchRVArrayList, new IClickListener() {
@Override
public void clickItem(SearchRV searchRV) {
Toast.makeText(PictureActivity.this,"test",Toast.LENGTH_SHORT).show();
}
});
bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
}

private void getResults(){
Drawable mDrawable = Drawable.createFromPath(imgPath);
Bitmap mBitmap = ((BitmapDrawable) mDrawable).getBitmap();
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(mBitmap);
FirebaseVisionImageLabeler labeler = FirebaseVision.getInstance().getOnDeviceImageLabeler();
labeler.processImage(image).addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionImageLabel>>() {
@Override
public void onSuccess(List<FirebaseVisionImageLabel> firebaseVisionImageLabels) {
String searchQuery = firebaseVisionImageLabels.get(0).getText();
//getSearchResults(searchQuery);
getSearchResults(searchQuery, searchRVArrayList);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ public void onClick(View v) {
@Override
public int getItemCount() {
// returning the size of array list.
return searchRVModals.size();
if( searchRVModals != null){
return searchRVModals.size();
}
else return 0;
}

public class ViewHolder extends RecyclerView.ViewHolder {
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ buildscript {

allprojects {
repositories {
jcenter()
//jcenter()
maven { url "https://www.jitpack.io" }
mavenCentral()
google() // Google's Maven repository
flatDir {
dirs 'libs'
}
jcenter()
}
}

Expand Down

0 comments on commit e9de0b0

Please sign in to comment.