Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbWatershed committed Nov 24, 2023
2 parents 0af1300 + 524937e commit 99f86df
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 35 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ android {
minSdkVersion 23
targetSdkVersion 34
versionCode 130 // is updated automatically by BitRise; only used when building locally
versionName '1.18.12'
versionName '1.18.13'

def includeObjectBoxBrowser = System.getenv("INCLUDE_OBJECTBOX_BROWSER") ?: "false"
def includeLeakCanary = System.getenv("INCLUDE_LEAK_CANARY") ?: "false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import android.os.Message;
import android.provider.MediaStore;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.GestureDetector;
import android.view.MotionEvent;
Expand Down Expand Up @@ -391,6 +390,8 @@ public class CustomSubsamplingScaleImageView extends View {
private final int screenWidth;
private final int screenHeight;

private final float screenDpi;

private final CompositeDisposable loadDisposable = new CompositeDisposable();
// GPUImage instance to use to smoothen images; sharp mode will be used if not set
private GPUImage glEsRenderer;
Expand All @@ -401,6 +402,7 @@ public CustomSubsamplingScaleImageView(@NonNull Context context, @Nullable Attri
density = getResources().getDisplayMetrics().density;
screenWidth = context.getResources().getDisplayMetrics().widthPixels;
screenHeight = context.getResources().getDisplayMetrics().heightPixels;
screenDpi = Helper.getScreenDpi(context);

setMinimumDpi(160);
setDoubleTapZoomDpi(160);
Expand Down Expand Up @@ -454,7 +456,7 @@ public CustomSubsamplingScaleImageView(@NonNull Context context, @Nullable Attri
});
}

public CustomSubsamplingScaleImageView(Context context) {
public CustomSubsamplingScaleImageView(@NonNull Context context) {
this(context, null);
}

Expand Down Expand Up @@ -1698,9 +1700,7 @@ private void preDraw() {
*/
private int calculateInSampleSize(float scale) {
if (minimumTileDpi > 0) {
DisplayMetrics metrics = getResources().getDisplayMetrics();
float averageDpi = (metrics.xdpi + metrics.ydpi) / 2;
scale = (minimumTileDpi / averageDpi) * scale;
scale = (minimumTileDpi / screenDpi) * scale;
}

int reqWidth = (int) (sWidth() * scale);
Expand Down Expand Up @@ -2570,6 +2570,7 @@ private float minScale() {
private float limitedScale(float targetScale) {
targetScale = Math.max(minScale(), targetScale);
targetScale = Math.min(maxScale, targetScale);

return targetScale;
}

Expand Down Expand Up @@ -2732,9 +2733,7 @@ public final void setMinScale(float minScale) {
* @param dpi Source image pixel density at maximum zoom.
*/
public final void setMinimumDpi(int dpi) {
DisplayMetrics metrics = getResources().getDisplayMetrics();
float averageDpi = (metrics.xdpi + metrics.ydpi) / 2;
setMaxScale(averageDpi / dpi);
setMaxScale(screenDpi / dpi);
}

/**
Expand All @@ -2744,9 +2743,7 @@ public final void setMinimumDpi(int dpi) {
* @param dpi Source image pixel density at minimum zoom.
*/
public final void setMaximumDpi(int dpi) {
DisplayMetrics metrics = getResources().getDisplayMetrics();
float averageDpi = (metrics.xdpi + metrics.ydpi) / 2;
setMinScale(averageDpi / dpi);
setMinScale(screenDpi / dpi);
}


Expand Down Expand Up @@ -2778,9 +2775,7 @@ public final float getMinScale() {
* @param minimumTileDpi Tile loading threshold.
*/
public void setMinimumTileDpi(int minimumTileDpi) {
DisplayMetrics metrics = getResources().getDisplayMetrics();
float averageDpi = (metrics.xdpi + metrics.ydpi) / 2;
this.minimumTileDpi = (int) Math.min(averageDpi, minimumTileDpi);
this.minimumTileDpi = (int) Math.min(screenDpi, minimumTileDpi);
if (isReady()) {
reset(false);
invalidate();
Expand Down Expand Up @@ -3114,9 +3109,7 @@ public final void setDoubleTapZoomScale(float doubleTapZoomScale) {
* @param dpi New value for double tap gesture zoom scale.
*/
public final void setDoubleTapZoomDpi(int dpi) {
DisplayMetrics metrics = getResources().getDisplayMetrics();
float averageDpi = (metrics.xdpi + metrics.ydpi) / 2;
setDoubleTapZoomScale(averageDpi / dpi);
setDoubleTapZoomScale(screenDpi / dpi);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package me.devsaki.hentoid.customssiv.util;

import android.content.Context;
import android.os.Build;
import android.os.Looper;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import androidx.annotation.NonNull;

Expand Down Expand Up @@ -36,4 +40,23 @@ public static void copy(@NonNull InputStream in, @NonNull OutputStream out) thro
}
out.flush();
}

public static float getScreenDpi(@NonNull Context context) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
float averageDpi = (metrics.xdpi + metrics.ydpi) / 2;

WindowManager wMgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
float generalDpi;
if (Build.VERSION.SDK_INT >= 34) {
generalDpi = wMgr.getCurrentWindowMetrics().getDensity() * 160;
} else {
DisplayMetrics metrics3 = new DisplayMetrics();
wMgr.getDefaultDisplay().getRealMetrics(metrics3);
generalDpi = metrics3.densityDpi;
}

// Dimensions retrieved by metrics.xdpi/ydpi might be expressed as ppi (as per specs) and not dpi (as per naming)
// In that case, values are off scale => fallback to general dpi
return ((Math.abs(generalDpi - averageDpi) / averageDpi) > 1) ? generalDpi : averageDpi;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ class UnlockActivity : AppCompatActivity(), UnlockPinDialogFragment.Parent {
@Suppress("DEPRECATION")
private fun goToNextActivity() {
val parcelableExtra: Intent? = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU)
intent.getParcelableExtra<Parcelable>(EXTRA_INTENT) as Intent
intent.getParcelableExtra<Parcelable>(EXTRA_INTENT) as Intent?
else
intent.getParcelableExtra(EXTRA_INTENT, Intent::class.java)

val targetIntent: Intent
if (parcelableExtra != null) targetIntent = parcelableExtra else {
if (parcelableExtra != null) targetIntent = parcelableExtra
else {
val siteCode = intent.getIntExtra(EXTRA_SITE_CODE, Site.NONE.code)
if (siteCode == Site.NONE.code) {
finish()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;

import me.devsaki.hentoid.database.domains.Content;
Expand Down Expand Up @@ -66,25 +68,60 @@ public AnchiraWebClient(Site site, String[] galleryUrl, WebResultConsumer result
AnchiraWebClient(Site site, String[] galleryUrl, CustomWebActivity activity) {
super(site, galleryUrl, activity);
setJsStartupScripts("wysiwyg_parser.js");
addJsReplacement("$interface",AnchiraBackgroundWebView.Companion.getInterfaceName());
addJsReplacement("$fun",AnchiraBackgroundWebView.functionName);
addJsReplacement("$interface", AnchiraBackgroundWebView.Companion.getInterfaceName());
addJsReplacement("$fun", AnchiraBackgroundWebView.functionName);
}

@Override
public WebResourceResponse shouldInterceptRequest(@NonNull WebView view, @NonNull WebResourceRequest request) {
String url = request.getUrl().toString();

HttpHelper.UriParts uriParts = new HttpHelper.UriParts(url, true);
if (uriParts.getEntireFileName().startsWith("app.") && uriParts.getExtension().equals("js")) {
try (
Response response = HttpHelper.getOnlineResourceFast(
url,
HttpHelper.webkitRequestHeadersToOkHttpHeaders(request.getRequestHeaders(), url),
Site.ANCHIRA.useMobileAgent(), Site.ANCHIRA.useHentoidAgent(), Site.ANCHIRA.useWebviewAgent()
)) {
// Scram if the response is a redirection or an error
if (response.code() >= 300) return null;

// Scram if the response is empty
ResponseBody body = response.body();
if (null == body) throw new IOException("Empty body");

var jsFile = body.source().readString(StandardCharsets.UTF_8);

jsFile = jsFile.replace(".isTrusted", ".cancelable");

return HttpHelper.okHttpResponseToWebkitResponse(
response, new ByteArrayInputStream(
jsFile.getBytes(StandardCharsets.UTF_8)
)
);
} catch (IOException e) {
Timber.w(e);
}
}

if (url.contains(AnchiraGalleryMetadata.IMG_HOST)) {
String[] parts = url.split("/");
if (parts.length > 7) {
// TODO that's ugly; find a more suitable interface; e.g. onImagesReady
Content c = new Content();
c.setSite(Site.ANCHIRA);
c.setCoverImageUrl(url);
resConsumer.onContentReady(c, false);

// Kill CORS
if (request.getMethod().equalsIgnoreCase("options")) {
try {
Response response = HttpHelper.optOnlineResourceFast(
url,
HttpHelper.webkitRequestHeadersToOkHttpHeaders(request.getRequestHeaders(), url),
Site.ANCHIRA.useMobileAgent(), Site.ANCHIRA.useHentoidAgent(), Site.ANCHIRA.useWebviewAgent()
);
Site.ANCHIRA.useMobileAgent(), Site.ANCHIRA.useHentoidAgent(), Site.ANCHIRA.useWebviewAgent());

// Scram if the response is a redirection or an error
if (response.code() >= 300) return null;

Expand All @@ -93,23 +130,21 @@ public WebResourceResponse shouldInterceptRequest(@NonNull WebView view, @NonNul
if (null == body) throw new IOException("Empty body");

return HttpHelper.okHttpResponseToWebkitResponse(response, body.byteStream());
} catch (IOException e) {
} catch (Exception e) {
Timber.w(e);
}
}
// TODO that's ugly; find a more suitable interface; e.g. onImagesReady
Content c = new Content();
c.setSite(Site.ANCHIRA);
c.setCoverImageUrl(url);
resConsumer.onContentReady(c, false);
}
}

return super.shouldInterceptRequest(view, request);
}

@Override
protected WebResourceResponse parseResponse(@NonNull String urlStr, @Nullable Map<String, String> requestHeaders, boolean analyzeForDownload, boolean quickDownload) {
protected WebResourceResponse parseResponse(@NonNull String urlStr,
@Nullable Map<String, String> requestHeaders,
boolean analyzeForDownload,
boolean quickDownload) {
// Complete override of default behaviour because
// - There's no HTML to be parsed for ads
// - The interesting parts are loaded by JS, not now
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,7 @@ protected WebResourceResponse parseResponse(@NonNull String urlStr, @Nullable Ma
content2 -> resConsumer.onContentReady(content2, quickDownload),
throwable -> {
Timber.e(throwable, "Error parsing content.");
parserStream.close();
isHtmlLoaded.set(true);
resConsumer.onResultFailed();
})
Expand All @@ -696,8 +697,6 @@ protected WebResourceResponse parseResponse(@NonNull String urlStr, @Nullable Ma
return result;
} catch (IOException | IllegalStateException e) {
Timber.e(e);
} finally {
response.close();
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ class ImagePagerAdapter(val context: Context) :
Timber.d(
e, "Picture %d : Glide loading failed : %s", absoluteAdapterPosition, img!!.fileUri
)
noImgTxt?.visibility = View.VISIBLE
if (isImageView) noImgTxt?.visibility = View.VISIBLE
return false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1672,7 +1672,7 @@ public static void computeAndSaveCoverHash(@NonNull final Context context,
*/
public static boolean isDownloadable(@NonNull final Content content) {
List<ImageFile> images = content.getImageFiles();
if (null == images) return false;
if (null == images || images.isEmpty()) return false;

// Pick a random picture
ImageFile img = images.get(Helper.getRandomInt(images.size()));
Expand Down

0 comments on commit 99f86df

Please sign in to comment.