Skip to content

Commit

Permalink
Added an option to share image from URL
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrewTSW committed Nov 2, 2018
1 parent 3c0de58 commit 354193a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 66 deletions.
38 changes: 0 additions & 38 deletions .idea/misc.xml

This file was deleted.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "eu.dasancti.reversee"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
versionCode 2
versionName "ALPHA v0.3"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
</application>
</manifest>
77 changes: 53 additions & 24 deletions app/src/main/java/eu/dasancti/reversee/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
public class MainActivity extends AppCompatActivity {

private static final String GOOGLE_REVERSE_IMAGE_SEARCH_URL = "https://www.google.com/searchbyimage/upload";
private static final String GOOGLE_REVERSE_IMAGE_SEARCH_URL_BY_URL = "https://www.google.com/searchbyimage?&image_url=";
private static final String FAKE_USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11";
private static final String API_SCH_KEY = "sch";
private static final String API_SCH_VALUE = "sch";
Expand All @@ -49,38 +50,67 @@ protected void onCreate(Bundle savedInstanceState) {
progressStatus = findViewById(R.id.progressStatus);

Intent intent = getIntent();
if (intent == null) {
Toast.makeText(MainActivity.this, "No intent sent to application, closing.", Toast.LENGTH_SHORT).show();
this.finish();
return;
}
String action = intent.getAction();
if (action == null) {
Toast.makeText(MainActivity.this, "No action sent to application, closing.", Toast.LENGTH_SHORT).show();
this.finish();
return;
}
if (isPermissionGranted()) {
if (action.equals(Intent.ACTION_SEND)) {
progressStatus.setText(getString(R.string.progress_status_recieved_intent));
try {
handleImageSearch(intent.getParcelableExtra(Intent.EXTRA_STREAM));
} catch (FileNotFoundException e) {
String err = "Failed to handle Share image intent:"+e.getMessage();
Log.e("INTENT_HANDLE",err);
Toast.makeText(getApplicationContext(), err, Toast.LENGTH_SHORT).show();
if (action.equals(Intent.ACTION_SEND)) {
if (intent.hasExtra(Intent.EXTRA_TEXT)) {
String intentExtraText = intent.getParcelableExtra(Intent.EXTRA_TEXT).toString();
progressStatus.setText(getString(R.string.progress_status_parsing_link));
if (intentExtraText.endsWith(".jpg") ||
intentExtraText.endsWith(".png") ||
intentExtraText.endsWith(".gif") ||
intentExtraText.endsWith(".webp")) {
progressStatus.setText(getString(R.string.progress_status_opening_from_url));
Toast.makeText(MainActivity.this, getString(R.string.progress_status_opening_from_url), Toast.LENGTH_SHORT).show();
Intent searchIntent = new Intent();
searchIntent.setAction(Intent.ACTION_VIEW);
searchIntent.setData(Uri.parse(GOOGLE_REVERSE_IMAGE_SEARCH_URL_BY_URL.concat(intentExtraText)));
startActivity(searchIntent);
MainActivity.this.finish();
} else {
progressStatus.setText(getString(R.string.progress_status_unsupported_format));
Toast.makeText(MainActivity.this, getString(R.string.progress_status_unsupported_format), Toast.LENGTH_SHORT).show();
this.finish();
}
} else if (intent.hasExtra(Intent.EXTRA_STREAM)) {
if (isPermissionGranted()) {
progressStatus.setText(getString(R.string.progress_status_recieved_intent));
try {
handleImageSearch(intent.getParcelableExtra(Intent.EXTRA_STREAM));
} catch (FileNotFoundException e) {
String err = "Failed to handle Share image intent:" + e.getMessage();
Log.e("INTENT_HANDLE", err);
Toast.makeText(getApplicationContext(), err, Toast.LENGTH_SHORT).show();
}
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Read storage permission required.");
builder.setMessage("This application requires READ_EXTERNAL_STORAGE permission in order to function properly.");
builder.setPositiveButton("Grant permission", (dialog, which) -> {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_PERMISSION_EXTERNAL_STORAGE_STATE);
if (action.equals(Intent.ACTION_SEND)) {
requestPermissionsFallbackUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
}
});
builder.setNegativeButton("Deny permission", ((dialog, which) -> {
Toast.makeText(MainActivity.this, "Permission not granted, closing.", Toast.LENGTH_SHORT).show();
this.finish();
}));
builder.create().show();
}
}
} else {
android.support.v7.app.AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Read storage permission required.");
builder.setMessage("This application requires READ_EXTERNAL_STORAGE permission in order to function properly.");
builder.setPositiveButton("Grant permission", (dialog, which) -> {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_PERMISSION_EXTERNAL_STORAGE_STATE);
if (action.equals(Intent.ACTION_SEND)) {
requestPermissionsFallbackUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
}
});
builder.setNegativeButton("Deny permission", ((dialog, which) -> {
Toast.makeText(MainActivity.this, "Permission not granted, closing.", Toast.LENGTH_SHORT).show();
this.finish();
}));
builder.create().show();
Toast.makeText(MainActivity.this, "Unhandled action sent to application, closing.", Toast.LENGTH_SHORT).show();
this.finish();
}
}

Expand Down Expand Up @@ -156,7 +186,6 @@ public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Thr
}
}
});
// TODO: Display spinner until requestHandle.isFinished() is true
}

private boolean isPermissionGranted() {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="We are processing your queue."
android:text="We are processing your querry."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<resources>
<string name="app_name">Reversee</string>
<string name="progress_status_recieved_intent">Recieved intent data (image share)</string>
<string name="progress_status_recieved_intent">Received intent data (image share)</string>
<string name="progress_status_handling_image">Handling google reverse image search.</string>
<string name="progress_status_response_success">Google request successful.</string>
<string name="progress_status_redirect_found">Found redirect URL, opening browser intent.</string>
<string name="progress_status_parsing_link">Parsing URL from share.</string>
<string name="progress_status_unsupported_format">Unsupported file format from link.</string>
<string name="progress_status_opening_from_url">Opening browser with shared URL</string>
</resources>

0 comments on commit 354193a

Please sign in to comment.