8000 Page Info Overlay by afarra · Pull Request #159 · quran/quran_android · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Page Info Overlay #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 1, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
<string name="prefs_landscape_orientation_summary_off">سيتم استخدام الوضع الرأسي</string>
<string name="prefs_night_mode_title">القراءة الليلية</string>
<string name="prefs_night_mode_summary">الخلفية باللون اﻷسود والخطوط باللون اﻷبيض</string>
<string name="prefs_overlay_page_info_title">عرض بيانات الصفحة (تجريبية)</string>
<string name="prefs_overlay_page_info_summary">عرض رقم الصفحة، إسم الصورة، ورقم الجزء أثناء القراءة</string>
<string name="prefs_display_marker_title">التنبيه</string>
<string name="prefs_display_marker_summary">عرض تنبيه عند الوصول إلى بداية الربع أو الجزء</string>
<string name="prefs_translation_text_title">خط التفسير والترجمة</string>
Expand Down Expand Up @@ -116,6 +118,7 @@
<string name="index_loading">جاري التحميل…</string>

<string name="canceling">جاري إلغاء اﻷمر&#8230;</string>
<string name="juz2_description">الجزء %1$d</string>
<string name="page_description">صفحة %1$d, جزء %2$d</string>
<string name="highlighting_database">جاري تحميل الملفات المطلوبة</string>
<string name="timing_database">جاري تحميل الملفات المطلوبة</string>
Expand Down
1 change: 1 addition & 0 deletions res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
<color name="transparent_actionbar_color">#AA000000</color>
<color name="translation_hdr_color">#ff649ab8</color>
<color name="translation_sura_header">#ff649ab8</color>
<color name="overlay_text_color">#ff888888</color>
</resources>
1 change: 1 addition & 0 deletions res/values/preferences_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<string name="prefs_use_arabic_names">useArabicNames</string>
<string name="prefs_new_background">useNewBackground</string>
<string name="prefs_night_mode">nightMode</string>
<string name="prefs_overlay_page_info">overlayPageInfo</string>
<string name="prefs_display_marker_popup">displayMarkerPopup</string>
<string name="prefs_lock_orientation">lockOrientation</string>
<string name="prefs_landscape_orientation">landscapeOrientation</string>
Expand Down
7 changes: 6 additions & 1 deletion res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@
<string name="prefs_landscape_orientation_title">Landscape orientation</string>
<string name="prefs_landscape_orientation_summary_on">Landscape orientation will be used</string>
<string name="prefs_landscape_orientation_summary_off">Portrait orientation will be used</string>
<string name="prefs_night_mode_title">Beta - Night mode</string>
<string name="prefs_night_mode_title">Night mode</string>
<string name="prefs_night_mode_summary">Dark background and light fonts will be used</string>
<string name="prefs_overlay_page_info_title">Show page info (beta)</string>
<string name="prefs_overlay_page_info_summary">Overlay page number, sura name, and juz\' number while reading</string>
<string name="prefs_display_marker_title">Display marker popups</string>
<string name="prefs_display_marker_summary">Display popup on reaching juz\', hizb, etc.</string>
<string name="prefs_translation_text_title">Translation text size</string>
Expand Down Expand Up @@ -161,6 +163,7 @@
<string name="index_loading">Loading…</string>

<string name="canceling">Canceling&#8230;</string>
<string name="juz2_description">Juz\' %1$d</string>
<string name="page_description">page %1$d, Juz\' %2$d</string>
<string name="highlighting_database">Required Files</string>
<string name="timing_database">Required Files</string>
Expand Down Expand Up @@ -219,4 +222,6 @@
<string name="ayah_copied_popup">Ayah Copied</string>
<string name="remove_bookmark">Remove Bookmark</string>
<string name="select_all">Select All</string>
<string name="edit_tag">Edit Tag</string>
<string name="new_tag">New Tag</string>
</resources>
8 changes: 8 additions & 0 deletions res/xml/quran_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@

<PreferenceCategory android:title="@string/prefs_category_reading" >

<CheckBoxPreference
android:defaultValue="true"
android:key="@string/prefs_overlay_page_info"
android:persistent="true"
android:summary="@string/prefs_overlay_page_info_summary"
android:title="@string/prefs_overlay_page_info_title" >
</CheckBoxPreference>

<CheckBoxPreference
android:defaultValue="true"
android:key="@string/prefs_display_marker_popup"
Expand Down
15 changes: 15 additions & 0 deletions src/com/quran/labs/androidquran/data/AyahInfoDatabaseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Rect;

import com.quran.labs.androidquran.common.QuranAyah;
import com.quran.labs.androidquran.util.QuranFileUtils;

Expand Down Expand Up @@ -44,6 +46,19 @@ public Cursor getVerseBounds(int sura, int ayah){
null, null, null, COL_POSITION);
}

public Rect getPageBounds(int page) {
if (!validDatabase()) return null;
String[] colNames = new String[] {
"MIN("+MIN_X+")", "MIN("+MIN_Y+")",
"MAX("+MAX_X+")", "MAX("+MAX_Y+")"};
Cursor c = database.query(GLYPHS_TABLE, colNames, COL_PAGE + "=" + page,
null, null, null, null);
if (!c.moveToFirst()) return null;
Rect r = new Rect(c.getInt(0), c.getInt(1), c.getInt(2), c.getInt(3));
c.close();
return r;
}

// TODO Improve efficiency -AF
public QuranAyah getVerseAtPoint(int page, float x, float y){
if (!validDatabase())
Expand Down
1 change: 1 addition & 0 deletions src/com/quran/labs/androidquran/data/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class Constants {
public static final String PREF_NIGHT_MODE = "nightMode";
public static final String PREF_DEFAULT_QARI = "defaultQari";
public static final String PREF_SHOULD_FETCH_PAGES ="shouldFetchPages";
public static final String PREF_OVERLAY_PAGE_INFO = "overlayPageInfo";
public static final String PREF_DISPLAY_MARKER_POPUP = "displayMarkerPopup";
public static final String PREF_AYAH_BEFORE_TRANSLATION = "ayahBeforeTranslation";
public static final String PREF_PREFER_STREAMING = "preferStreaming";
Expand Down
5 changes: 5 additions & 0 deletions src/com/quran/labs/androidquran/data/QuranInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public static String getPageSubtitle(Context context, int page){
return String.format(description, page, QuranInfo.getJuzFromPage(page));
}

public static String getJuzString(Context context, int page){
String description = context.getString(R.string.juz2_description);
return String.format(description, QuranInfo.getJuzFromPage(page));
}

public static String getSuraAyahString(Context context, int sura, int ayah){
String suraName = getSuraName(context, sura, false);
String format = context.getString(R.string.sura_ayah_notification_str);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ public boolean onTouch(View v, MotionEvent event) {
mImageView.setOnTouchListener(gestureListener);
mImageView.setClickable(true);
mImageView.setLongClickable(true);
if (prefs.getBoolean(Constants.PREF_OVERLAY_PAGE_INFO, true)) {
try {mImageView.setOverlayText(mPageNumber, true);}
catch (Exception e) {/*do nothing*/} // Temporary to avoid any unanticipated FC's
}
return view;
}

Expand Down
5 changes: 5 additions & 0 deletions src/com/quran/labs/androidquran/util/QuranSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public static boolean isNightMode(Context context){
Constants.PREF_NIGHT_MODE, false);
}

public static boolean shouldOverlayPageInfo(Context context){
return getBooleanPreference(context,
Constants.PREF_OVERLAY_PAGE_INFO, true);
}

public static boolean shouldDisplayMarkerPopup(Context context){
return getBooleanPreference(context,
Constants.PREF_DISPLAY_MARKER_POPUP, true);
Expand Down
120 changes: 120 additions & 0 deletions src/com/quran/labs/androidquran/widgets/HighlightingImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import android.database.Cursor;
import android.database.SQLException;
import android.graphics.*;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.drawable.Drawable;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
Expand All @@ -18,13 +20,22 @@
import com.quran.labs.androidquran.common.AyahBounds;
import com.quran.labs.androidquran.data.Constants;
import com.quran.labs.androidquran.data.AyahInfoDatabaseHandler;
import com.quran.labs.androidquran.data.QuranInfo;
import com.quran.labs.androidquran.util.QuranFileUtils;

public class HighlightingImageView extends ImageView {
// Max/Min font sizes for text overlay
private static final float MAX_FONT_SIZE = 28.0f;
private static final float MIN_FONT_SIZE = 16.0f;

private List<AyahBounds> currentlyHighlighting = null;
private boolean colorFilterOn = false;
private String highightedAyah = null;
private Bitmap mHighlightBitmap = null;

// Params for drawing text
private OverlayParams overlayParams = null;
private Rect pageBounds = null;

public HighlightingImageView(Context context){
super(context);
Expand Down Expand Up @@ -200,9 +211,118 @@ public PageScalingData(Drawable page) {
}
}

private class OverlayParams {
boolean init = false;
boolean showOverlay = false;
Paint paint = null;
float offsetX;
float topBaseline;
float bottomBaseline;
String suraText = null;
String juzText = null;
String pageText = null;
}

public void setOverlayText(int page, boolean show) {
// Calculate page bounding rect from ayainfo db
this.pageBounds = getPageBounds(page);
if (pageBounds == null) return;

overlayParams = new OverlayParams();
overlayParams.suraText = QuranInfo.getSuraNameFromPage(getContext(), page, true);
overlayParams.juzText = QuranInfo.getJuzString(getContext(), page);
overlayParams.pageText = String.format("%1$d", page);
overlayParams.showOverlay = show;
}

private Rect getPageBounds(int page){
Rect r = null;
try {
String filename = QuranFileUtils.getAyaPositionFileName();
if (filename == null) return null;
AyahInfoDatabaseHandler handler =
new AyahInfoDatabaseHandler(filename);
r = handler.getPageBounds(page);
handler.closeDatabase();
}
catch (SQLException se){/*do nothing*/}
return r;
}

private boolean initOverlayParams() {
if (overlayParams == null || pageBounds == null) return false;

// Overlay params previously initiated; skip
if (overlayParams.init) return true;

Drawable page = this.getDrawable();
if (page == null) return false;
PageScalingData scalingData = new PageScalingData(page);

overlayParams.paint = new Paint();
overlayParams.paint.setTextSize(MAX_FONT_SIZE);
overlayParams.paint.setColor(getResources().getColor(R.color.overlay_text_color));

// Use font metrics to calculate the maximum possible height of the text
FontMetrics fm = overlayParams.paint.getFontMetrics();
float textHeight = fm.bottom-fm.top;

// Text size scale based on the available 'header' and 'footer' space
// (i.e. gap between top/bottom of screen and actual start of the 'bitmap')
float scale = scalingData.offsetY/textHeight;

// If the height of the drawn text might be greater than the available gap...
// scale down the text size by the calculated scale
if (scale < 1.0) {
// If after scaling the text size will be less than the minimum size...
// get page bounds from db and find the empty area within the image and utilize that as well.
if (MAX_FONT_SIZE*scale < MIN_FONT_SIZE) {
float emptyYTop = scalingData.offsetY + pageBounds.top*scalingData.heightFactor;
float emptyYBottom = scalingData.offsetY
+ (scalingData.scaledPageHeight - pageBounds.bottom*scalingData.heightFactor);
float emptyY = Math.min(emptyYTop, emptyYBottom);
scale = Math.min(emptyY/textHeight, 1.0f);
}
// Set the scaled text size, and update the metrics
overlayParams.paint.setTextSize(MAX_FONT_SIZE*scale);
fm = overlayParams.paint.getFontMetrics();
textHeight = fm.bottom-fm.top;
}

// Calculate where the text's baseline should be (for top text and bottom text)
// (p.s. parts of the glyphs will be below the baseline such as a 'y' or 'ي')
overlayParams.topBaseline = -fm.top;
overlayParams.bottomBaseline = getHeight()-fm.bottom;

// Calculate the horizontal margins off the edge of screen
overlayParams.offsetX = scalingData.offsetX
+ (getWidth() - pageBounds.width()*scalingData.widthFactor)/2.0f;

overlayParams.init = true;
return true;
}

private void overlayText(Canvas canvas) {
if (overlayParams == null || !initOverlayParams()) return;

overlayParams.paint.setTextAlign(Align.LEFT);
canvas.drawText(overlayParams.suraText,
overlayParams.offsetX, overlayParams.topBaseline, overlayParams.paint);
overlayParams.paint.setTextAlign(Align.RIGHT);
canvas.drawText(overlayParams.juzText,
getWidth()-overlayParams.offsetX, overlayParams.topBaseline, overlayParams.paint);
overlayParams.paint.setTextAlign(Align.CENTER);
canvas.drawText(overlayParams.pageText,
getWidth()/2.0f, overlayParams.bottomBaseline, overlayParams.paint);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (overlayParams != null && overlayParams.showOverlay) {
try {overlayText(canvas);}
catch (Exception e) {/*do nothing*/} // Temporary to avoid any unanticipated FC's
}
if (this.currentlyHighlighting != null){
Drawable page = this.getDrawable();
if (page != null){
Expand Down
0