Skip to content

Commit

Permalink
#63 Fixed screen cutout reading page
Browse files Browse the repository at this point in the history
  • Loading branch information
MewX committed Aug 28, 2020
1 parent 9398c85 commit 901159d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.DisplayCutout;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
Expand Down Expand Up @@ -49,6 +51,7 @@
import org.mewx.wenku8.reader.slider.base.OverlappedSlider;
import org.mewx.wenku8.reader.view.WenkuReaderPageView;
import org.mewx.wenku8.util.LightNetwork;
import org.mewx.wenku8.util.LightTool;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
Expand Down Expand Up @@ -134,6 +137,23 @@ public boolean onCreateOptionsMenu(Menu menu) {
return true;
}

@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();

// Update display cutout area.
if (Build.VERSION.SDK_INT >= 28) {
DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
if (cutout != null) {
LightTool.setDisplayCutout(
new Rect(cutout.getSafeInsetLeft(),
cutout.getSafeInsetTop(),
cutout.getSafeInsetRight(),
cutout.getSafeInsetBottom()));
}
}
}

private void hideNavigationBar() {
// This work only for android 4.4+
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Build;
import android.text.TextPaint;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.widget.Toast;

Expand Down Expand Up @@ -75,8 +77,9 @@ private class BitmapInfo {
static private WenkuReaderLoader mLoader;
static private WenkuReaderSettingV1 mSetting;
static private int pxLineDistance, pxParagraphDistance, pxParagraphEdgeDistance, pxPageEdgeDistance, pxWidgetHeight;
static private Point screenSize;
private Point textAreaSize;
static private Point screenSize; // Screen real size.
static private Pair<Point, Point> screenDrawArea; // The area we want to draw text/images in.
private Point textAreaSize; // TODO: remove this variable.
static private Typeface typeface;
static private TextPaint textPaint, widgetTextPaint;
static private int fontHeight, widgetFontHeihgt;
Expand Down Expand Up @@ -113,10 +116,10 @@ static public boolean switchDayMode() {
static public void setViewComponents(WenkuReaderLoader wrl, WenkuReaderSettingV1 wrs, boolean forceMode) {
mLoader = wrl;
mSetting = wrs;
pxLineDistance = LightTool.dip2px(MyApp.getContext(), mSetting.getLineDistance());
pxParagraphDistance = LightTool.dip2px(MyApp.getContext(), mSetting.getParagraphDistance());
pxParagraphEdgeDistance = LightTool.dip2px(MyApp.getContext(), mSetting.getParagraghEdgeDistance());
pxPageEdgeDistance = LightTool.dip2px(MyApp.getContext(), mSetting.getPageEdgeDistance());
pxLineDistance = LightTool.dip2px(MyApp.getContext(), mSetting.getLineDistance()); // 行间距
pxParagraphDistance = LightTool.dip2px(MyApp.getContext(), mSetting.getParagraphDistance()); // 段落间距
pxParagraphEdgeDistance = LightTool.dip2px(MyApp.getContext(), mSetting.getParagraghEdgeDistance()); // TODO: merge with page edge distance, with a config update
pxPageEdgeDistance = LightTool.dip2px(MyApp.getContext(), mSetting.getPageEdgeDistance()); // 页面边距
pxWidgetHeight = LightTool.dip2px(MyApp.getContext(), mSetting.widgetHeight);

// calc general var
Expand Down Expand Up @@ -185,6 +188,27 @@ static public void resetTextColor() {
widgetTextPaint.setColor(getInDayMode() ? mSetting.fontColorDark : mSetting.fontColorLight);
}

/**
* Calculate the actual area for drawing.
* @return the two points forming a rectangle that can actually hold content.
* The two points are: top left point and bottom right point.
*/
private Pair<Point, Point> getScreenLayout() {
int statusBarHeight = LightTool.getStatusBarHeightValue(MyApp.getContext());

// Add cutting positions.
Rect cutout = LightTool.getDisplayCutout();
// TODO: remove the top widget.
int top = pxPageEdgeDistance + pxParagraphEdgeDistance + pxWidgetHeight + Math.max(cutout.top, statusBarHeight);
int left = pxPageEdgeDistance + pxParagraphEdgeDistance + cutout.left;
int right = pxPageEdgeDistance + pxParagraphEdgeDistance + cutout.right;
int bottom = pxPageEdgeDistance + pxWidgetHeight + cutout.bottom;

Point topLeft = new Point(left, top);
Point bottomRight = new Point(screenSize.x - right, screenSize.y - bottom);
return new Pair<>(topLeft, bottomRight);
}

/**
* This function init the view class。
* Notice: (-1, -1), (-1, 0), (0, -1) means first page.
Expand All @@ -201,9 +225,15 @@ public WenkuReaderPageView(Context context, int lineIndex, int wordIndex, LOADIN
bitmapInfoList = new ArrayList<>();
mLoader.setCurrentIndex(lineIndex);

// get environmental vars, use actual layout size
textAreaSize = new Point(screenSize.x - 2 * (pxPageEdgeDistance + pxParagraphEdgeDistance),
screenSize.y - 2 * (pxPageEdgeDistance + pxWidgetHeight));
// first.x = left
// first.y = top
// second.x = screen.x - right
// second.y = screen.y - bottom
screenDrawArea = getScreenLayout();

// get environmental vars, use actual layout size: width x height
textAreaSize = new Point(screenDrawArea.second.x - screenDrawArea.first.x,
screenDrawArea.second.y - screenDrawArea.first.y);
if(Build.VERSION.SDK_INT < 19) textAreaSize.y = textAreaSize.y + pxWidgetHeight;

// save vars, calc all ints
Expand Down Expand Up @@ -530,13 +560,13 @@ protected void onDraw(Canvas canvas) {
}

// draw widgets
canvas.drawText(mLoader.getChapterName(), pxPageEdgeDistance, screenSize.y - pxPageEdgeDistance, widgetTextPaint);
canvas.drawText(mLoader.getChapterName(), screenDrawArea.first.x, screenDrawArea.second.y, widgetTextPaint);
String percentage = "( " + (lastLineIndex + 1) * 100 / mLoader.getElementCount() + "% )";
int tempWidth = (int) widgetTextPaint.measureText(percentage);
canvas.drawText(percentage, screenSize.x - pxPageEdgeDistance - tempWidth, screenSize.y - pxPageEdgeDistance, widgetTextPaint);
final int textWidth = (int) widgetTextPaint.measureText(percentage);
canvas.drawText(percentage, screenDrawArea.second.x - textWidth, screenDrawArea.second.y, widgetTextPaint);

// draw text on average in page and line
int heightSum = fontHeight + pxPageEdgeDistance + pxWidgetHeight;
int heightSum = screenDrawArea.first.y;
if(Build.VERSION.SDK_INT < 19) heightSum -= pxWidgetHeight;
for(int i = 0; i < lineInfoList.size(); i ++) {
final LineInfo li = lineInfoList.get(i);
Expand All @@ -551,7 +581,7 @@ protected void onDraw(Canvas canvas) {

Log.d("MewX", "draw: " + li.text);
if(li.type == WenkuReaderLoader.ElementType.TEXT) {
canvas.drawText( li.text, (float) (pxPageEdgeDistance + pxParagraphEdgeDistance), (float) heightSum, textPaint);
canvas.drawText( li.text, (float) screenDrawArea.first.x, (float) heightSum, textPaint);
heightSum += fontHeight;
}
else if(li.type == WenkuReaderLoader.ElementType.IMAGE_DEPENDENT){
Expand All @@ -566,11 +596,11 @@ else if(li.type == WenkuReaderLoader.ElementType.IMAGE_DEPENDENT){

if(foundIndex == -1) {
// not found, new load task
canvas.drawText("正在加载图片:" + li.text.substring(21), (float) (pxPageEdgeDistance + pxParagraphEdgeDistance), (float) heightSum, textPaint);
canvas.drawText("正在加载图片:" + li.text.substring(21), (float) screenDrawArea.first.x, (float) heightSum, textPaint);
BitmapInfo bitmapInfo = new BitmapInfo();
bitmapInfo.idxLineInfo = i;
bitmapInfo.x_beg = pxPageEdgeDistance + pxParagraphEdgeDistance;
bitmapInfo.y_beg = pxPageEdgeDistance + pxWidgetHeight;
bitmapInfo.x_beg = screenDrawArea.first.x;
bitmapInfo.y_beg = screenDrawArea.first.y;
if(Build.VERSION.SDK_INT < 19) bitmapInfo.y_beg -= pxWidgetHeight;
bitmapInfo.height = textAreaSize.y;
bitmapInfo.width = textAreaSize.x;
Expand All @@ -581,7 +611,7 @@ else if(li.type == WenkuReaderLoader.ElementType.IMAGE_DEPENDENT){
}
else {
if(bitmapInfoList.get(foundIndex).bm == null) {
canvas.drawText("正在加载图片:" + li.text.substring(21), (float) (pxPageEdgeDistance + pxParagraphEdgeDistance), (float) heightSum, textPaint);
canvas.drawText("正在加载图片:" + li.text.substring(21), (float) screenDrawArea.first.x, (float) heightSum, textPaint);
}
else {
int new_x = (screenSize.x - bitmapInfoList.get(foundIndex).x_beg * 2 - bitmapInfoList.get(foundIndex).width) / 2 + bitmapInfoList.get(foundIndex).x_beg;
Expand All @@ -591,11 +621,11 @@ else if(li.type == WenkuReaderLoader.ElementType.IMAGE_DEPENDENT){
}
}
else {
canvas.drawText("Unexpected array: " + li.text.substring(21), (float) (pxPageEdgeDistance + pxParagraphEdgeDistance), (float) heightSum, textPaint);
canvas.drawText("Unexpected array: " + li.text.substring(21), (float) screenDrawArea.first.x, (float) heightSum, textPaint);
}
}
else {
canvas.drawText("(!请先用旧引擎浏览)图片" + li.text.substring(21), (float) (pxPageEdgeDistance + pxParagraphEdgeDistance), (float) heightSum, textPaint);
canvas.drawText("(!请先用旧引擎浏览)图片" + li.text.substring(21), (float) screenDrawArea.first.x, (float) heightSum, textPaint);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Build;
import androidx.annotation.NonNull;
import android.view.Display;
Expand All @@ -15,6 +16,7 @@
* Util tools collects.
*/
public class LightTool {
private static Rect displayCutout = new Rect(0, 0, 0, 0);

/* Number related useful functions */
public static boolean isInteger(@NonNull String value) {
Expand Down Expand Up @@ -67,14 +69,23 @@ public static Point getAppUsableScreenSize(Context context) {
return size;
}

public static void setDisplayCutout(Rect rect) {
displayCutout = rect;
}

public static Rect getDisplayCutout() {
// Make a copy.
return new Rect(displayCutout);
}

public static Point getRealScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();

if (Build.VERSION.SDK_INT >= 17) {
display.getRealSize(size);
} else if (Build.VERSION.SDK_INT >= 14) {
} else {
try {
size.x = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
size.y = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
Expand Down

0 comments on commit 901159d

Please sign in to comment.