Skip to content

Commit

Permalink
Download image on long click
Browse files Browse the repository at this point in the history
  • Loading branch information
woheller69 committed Oct 25, 2023
1 parent 3672bc4 commit d5a15cf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
33 changes: 30 additions & 3 deletions app/src/main/java/org/woheller69/gptassist/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,25 @@
*/
package org.woheller69.gptassist;

import static android.webkit.WebView.HitTestResult.IMAGE_TYPE;
import android.Manifest;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.ClientCertRequest;
import android.webkit.ConsoleMessage;
import android.webkit.CookieManager;
import android.webkit.URLUtil;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
Expand Down Expand Up @@ -77,6 +82,7 @@ protected void onCreate(Bundle savedInstanceState) {

//Create the WebView
chatWebView = findViewById(R.id.chatWebView);
registerForContextMenu(chatWebView);

//Set cookie options
chatCookieManager = CookieManager.getInstance();
Expand Down Expand Up @@ -239,8 +245,6 @@ private static void initURLs() {
allowedDomains.add("cdn.oaistatic.com");
allowedDomains.add("oaiusercontent.com");



}

@Override
Expand All @@ -262,4 +266,27 @@ protected void onActivityResult(int requestCode, int resultCode, Intent intent)
}
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu, v, menuInfo);
WebView.HitTestResult result = chatWebView.getHitTestResult();
if (result.getExtra() != null) {
if (result.getType() == IMAGE_TYPE) {
String url = result.getExtra();
Uri source = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(source);
request.addRequestHeader("Cookie", CookieManager.getInstance().getCookie(url));
request.addRequestHeader("Accept", "text/html, application/xhtml+xml, *" + "/" + "*");
request.addRequestHeader("Accept-Language", "en-US,en;q=0.7,he;q=0.3");
request.addRequestHeader("Referer", url);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
String filename = URLUtil.guessFileName(url, null, null);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
Toast.makeText(this,getString(R.string.download)+"\n"+filename, Toast.LENGTH_SHORT).show();
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
assert dm != null;
dm.enqueue(request);
}
}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values-tr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
<string name="dialog_NO_button">Hayır</string>
<string name="dialog_Later_button">Belki sonra</string>
<string name="dialog_StarOnGitHub">Bu uygulamayı beğendiniz mi? Lütfen GitHub\'da bir yıldız verin veya geliştiriciye PayPal ile bir kahve ısmarlayın.</string>
<string name="download">İndirme başladı:</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
<string name="dialog_NO_button">No</string>
<string name="dialog_Later_button">Maybe later</string>
<string name="dialog_StarOnGitHub">Do you like this app? Please give a star on GitHub or buy the developer a coffee via PayPal.</string>
<string name="download">Download started:</string>
</resources>

0 comments on commit d5a15cf

Please sign in to comment.