diff --git a/.gitignore b/.gitignore index fd45b12..09b993d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,7 @@ *.iml .gradle /local.properties -/.idea/caches/build_file_checksums.ser -/.idea/libraries -/.idea/modules.xml -/.idea/workspace.xml +/.idea .DS_Store /build /captures diff --git a/app/build.gradle b/app/build.gradle index a32792b..caa745d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,13 +20,23 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } + buildToolsVersion '28.0.3' + dexOptions { + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } } dependencies { - implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" + implementation fileTree(include: ['*.jar'], dir: 'libs') + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' + implementation 'com.android.volley:volley:1.1.1' + implementation 'org.jsoup:jsoup:1.7.2' + implementation group: 'commons-io', name: 'commons-io', version: '2.4' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c09b7f7..f9c6b08 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,7 @@ + imageData = new AtomicReference<>(); + try { + imageData.set(IOUtils.toString(Objects.requireNonNull(getContentResolver().openInputStream(imageUri)))); + } catch (FileNotFoundException e) { + String err = "Couldn't open the shared image as an input stream."; + Toast.makeText(this, err, Toast.LENGTH_LONG).show(); + Log.e("GET_IMAGE_FILE", err); + this.finish(); + return; + } catch (IOException e) { + String err = "Couldn't read input stream of image."; + Toast.makeText(this, err, Toast.LENGTH_LONG).show(); + Log.e("GET_IMAGE_FILE", err); + this.finish(); + return; + } Intent searchIntent = new Intent(); searchIntent.setAction(Intent.ACTION_VIEW); //TODO: Implement browser intent with POST method + RequestQueue requestQueue = Volley.newRequestQueue(this); + final AtomicReference browserRedirect = new AtomicReference<>(); + StringRequest getGoogleRedirectUrl = new StringRequest( + Request.Method.POST, + GOOGLE_REVERSE_IMAGE_SEARCH_URL, + response -> { + Document doc = Jsoup.parse(response); + Element content = doc.getElementById("content"); + Elements redirects = content.getElementsByTag("meta"); + for (Element redirect : redirects) { + String redirectUrl = redirect.attr("url"); + if (redirectUrl.contains("/search?tbs=sbi:")) { + browserRedirect.set(redirectUrl); + break; + } + } + if (browserRedirect.get().isEmpty()) { + Toast.makeText(this, "Failed to get redirect URL", Toast.LENGTH_LONG).show(); + } else { + //TODO: Launch browser intent with the redirect + Toast.makeText(this, "Found redirect URL for reverse search.", Toast.LENGTH_SHORT).show(); + Log.i("REVERSE_SEARCH_URL", browserRedirect.get()); + } + }, + error -> Toast.makeText(this, "POST call to reverse search failed: "+error.getMessage(), Toast.LENGTH_LONG).show() + ) { + protected Map getParams() { + Map params = new HashMap<>(); + params.put("sch", "sch"); + params.put("encoded_image", imageData.get()); + return params; + } + public Map getHeaders() { + Map headers = new HashMap<>(); + headers.put("User-Agent", FAKE_USER_AGENT); + return headers; + } + }; + requestQueue.add(getGoogleRedirectUrl); Toast.makeText(this, "Handling google reverse image search.", Toast.LENGTH_LONG).show(); this.finish(); }