Skip to content

Commit

Permalink
Merge pull request #269 from codetoart/master
Browse files Browse the repository at this point in the history
Prepare v0.4.2
  • Loading branch information
Mahavir Jain authored Aug 31, 2018
2 parents 4c89e99 + 93e318f commit 2608657
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ repositories {
}
dependencies {
implementation 'com.folioreader:folioreader:0.4.1'
implementation 'com.folioreader:folioreader:0.4.2'
}
```

Expand Down
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ allprojects {
}

ext {
LIBRARY_VERSION = '0.4.1'
LIBRARY_VERSION = '0.4.2'
R2_STREAMER_VERSION = '0.1.8'

VERSION_NAME = "1.0"
Expand All @@ -39,6 +39,8 @@ ext {
ANDROID_COMPILE_SDK_VERSION = 28
ANDROID_TARGET_SDK_VERSION = 28
ANDROID_LIB_VERSION = '28.0.0-rc01'

CONSTRAINT_LAYOUT_VERSION = "1.1.2"
}

task clean(type: Delete) {
Expand Down
2 changes: 1 addition & 1 deletion folioreader/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation project(':webViewMarker')

implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation "com.android.support.constraint:constraint-layout:$CONSTRAINT_LAYOUT_VERSION"
implementation "com.android.support:appcompat-v7:$ANDROID_LIB_VERSION"
implementation "com.android.support:recyclerview-v7:$ANDROID_LIB_VERSION"
implementation "com.android.support:support-v4:$ANDROID_LIB_VERSION"
Expand Down
22 changes: 11 additions & 11 deletions folioreader/src/main/assets/css/Style.css
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ b, strong, th {font-weight: bolder !important;}
/* Forced font overrides */
code, kbd, pre, samp, tt {
font-family: monospace, monospace !important;
font-size: 1em;
font-size: 1rem;
}
button, input, select, textarea { display: inline-block !important; }
/*h1, h2, h3, h4, h5, h6 { font-weight: 400!important; }*/
Expand All @@ -100,8 +100,8 @@ hr {


/* Sub and Super */
big { font-size: 1.15em !important; }
small, sub, sup { font-size: .65em !important; }
big { font-size: 1.15rem !important; }
small, sub, sup { font-size: .65rem !important; }
sub { vertical-align: sub !important; }
sup {
font-family: monospace !important;
Expand Down Expand Up @@ -269,27 +269,27 @@ html.textSizeFour { font-size: 19px !important; }
html.textSizeFive { font-size: 21px !important; }

h1 {
font-size: 2em;
font-size: 2rem;
line-height: 1.2;
}
h2 {
font-size: 1.5em;
font-size: 1.5rem;
line-height: 1.2;
}
h3 {
font-size: 1.17em;
font-size: 1.17rem;
line-height: 1.2;
}
h4 {
font-size: 1em;
font-size: 1rem;
line-height: 1.2;
}
h5 {
font-size: 0.83em;
font-size: 0.83rem;
line-height: 1.2;
}
h6 {
font-size: 0.67em;
font-size: 0.67rem;
line-height: 1.2;
}
body {
Expand All @@ -298,12 +298,12 @@ body {
hyphens: auto !important;
}
p, span, div {
font-size: 1em;
font-size: 1rem;
line-height: 1.5 !important;
}
@media only screen and (min-device-width: 600px) {
div {
font-size: 1em;
font-size: 1rem;
line-height: 1.438em !important;
}
body {
Expand Down
60 changes: 53 additions & 7 deletions folioreader/src/main/java/com/folioreader/FolioReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,23 @@ public class FolioReader {
private boolean overrideConfig;
private OnHighlightListener onHighlightListener;
private ReadPositionListener readPositionListener;
private OnClosedListener onClosedListener;
private ReadPosition readPosition;
public static final String ACTION_SAVE_READ_POSITION = "com.folioreader.action.SAVE_READ_POSITION";
public static final String ACTION_CLOSE_FOLIOREADER = "com.folioreader.action.CLOSE_FOLIOREADER";
public static final String ACTION_FOLIOREADER_CLOSED = "com.folioreader.action.FOLIOREADER_CLOSED";
public static final String EXTRA_READ_POSITION = "com.folioreader.extra.READ_POSITION";

public interface OnClosedListener {
/**
* You may call {@link FolioReader#clear()} in this method, if you wont't require to open
* an epub again from the current activity.
* Or you may call {@link FolioReader#stop()} in this method, if you wont't require to open
* an epub again from your application.
*/
void onFolioReaderClosed();
}

private BroadcastReceiver highlightReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Expand All @@ -55,18 +68,26 @@ public void onReceive(Context context, Intent intent) {

ReadPosition readPosition =
intent.getParcelableExtra(FolioReader.EXTRA_READ_POSITION);
if (readPositionListener != null )
if (readPositionListener != null)
readPositionListener.saveReadPosition(readPosition);
}
};

private BroadcastReceiver closedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (onClosedListener != null)
onClosedListener.onFolioReaderClosed();
}
};

public static FolioReader get() {

if (singleton == null) {
synchronized (FolioReader.class) {
if (singleton == null) {
if (AppContext.get() == null) {
throw new IllegalArgumentException("-> context == null");
throw new IllegalStateException("-> context == null");
}
singleton = new FolioReader(AppContext.get());
}
Expand All @@ -81,10 +102,14 @@ private FolioReader() {
private FolioReader(Context context) {
this.context = context;
DbAdapter.initialize(context);
LocalBroadcastManager.getInstance(context).registerReceiver(highlightReceiver,

LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);
localBroadcastManager.registerReceiver(highlightReceiver,
new IntentFilter(HighlightImpl.BROADCAST_EVENT));
LocalBroadcastManager.getInstance(context).registerReceiver(readPositionReceiver,
localBroadcastManager.registerReceiver(readPositionReceiver,
new IntentFilter(ACTION_SAVE_READ_POSITION));
localBroadcastManager.registerReceiver(closedReceiver,
new IntentFilter(ACTION_FOLIOREADER_CLOSED));
}

public FolioReader openBook(String assetOrSdcardPath) {
Expand Down Expand Up @@ -160,7 +185,8 @@ private Intent getIntentFromUrl(String assetOrSdcardPath, int rawId) {

/**
* Pass your configuration and choose to override it every time or just for first execution.
* @param config custom configuration.
*
* @param config custom configuration.
* @param overrideConfig true will override the config, false will use either this
* config if it is null in application context or will fetch previously
* saved one while execution.
Expand All @@ -181,6 +207,11 @@ public FolioReader setReadPositionListener(ReadPositionListener readPositionList
return singleton;
}

public FolioReader setOnClosedListener(OnClosedListener onClosedListener) {
this.onClosedListener = onClosedListener;
return singleton;
}

public FolioReader setReadPosition(ReadPosition readPosition) {
this.readPosition = readPosition;
return singleton;
Expand All @@ -190,6 +221,18 @@ public void saveReceivedHighLights(List<HighLight> highlights, OnSaveHighlight o
new SaveReceivedHighlightTask(onSaveHighlight, highlights).execute();
}

/**
* Closes all the activities related to FolioReader.
* After closing all the activities of FolioReader, callback can be received in
* {@link OnClosedListener#onFolioReaderClosed()} if implemented.
* Developer is still bound to call {@link #clear()} or {@link #stop()}
* for clean up if required.
*/
public void close() {
Intent intent = new Intent(FolioReader.ACTION_CLOSE_FOLIOREADER);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}

/**
* Nullifies readPosition and listeners.
* This method ideally should be used in onDestroy() of Activity or Fragment.
Expand All @@ -202,6 +245,7 @@ public static synchronized void clear() {
singleton.readPosition = null;
singleton.onHighlightListener = null;
singleton.readPositionListener = null;
singleton.onClosedListener = null;
}
}

Expand All @@ -220,7 +264,9 @@ public static synchronized void stop() {
}

private void unregisterListeners() {
LocalBroadcastManager.getInstance(context).unregisterReceiver(highlightReceiver);
LocalBroadcastManager.getInstance(context).unregisterReceiver(readPositionReceiver);
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);
localBroadcastManager.unregisterReceiver(highlightReceiver);
localBroadcastManager.unregisterReceiver(readPositionReceiver);
localBroadcastManager.unregisterReceiver(closedReceiver);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import android.Manifest;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
Expand Down Expand Up @@ -137,6 +138,8 @@ public enum EpubSourceType {
private CharSequence searchQuery;
private SearchItem searchItem;
private float density;
private Boolean topActivity;
private int taskImportance;

private enum RequestCode {
CONTENT_HIGHLIGHT(77),
Expand All @@ -149,11 +152,89 @@ private enum RequestCode {
}
}

private BroadcastReceiver closeBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.v(LOG_TAG, "-> closeBroadcastReceiver -> onReceive -> " + intent.getAction());

String action = intent.getAction();
if (action != null && action.equals(FolioReader.ACTION_CLOSE_FOLIOREADER)) {

try {
ActivityManager activityManager = (ActivityManager)
context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> tasks =
activityManager.getRunningAppProcesses();
taskImportance = tasks.get(0).importance;
} catch (Exception e) {
Log.e(LOG_TAG, "-> ", e);
}

Intent closeIntent = new Intent(getApplicationContext(), FolioActivity.class);
closeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
closeIntent.setAction(FolioReader.ACTION_CLOSE_FOLIOREADER);
FolioActivity.this.startActivity(closeIntent);
}
}
};

@SuppressWarnings("PMD.CollapsibleIfStatements")
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
Log.v(LOG_TAG, "-> onNewIntent");

String action = getIntent().getAction();
if (action != null && action.equals(FolioReader.ACTION_CLOSE_FOLIOREADER)) {

if (topActivity == null || !topActivity) {
// FolioActivity was already left, so no need to broadcast ReadPosition again.
// Finish activity without going through onPause() and onStop()
finish();

// To determine if app in background or foreground
boolean appInBackground = false;
if (Build.VERSION.SDK_INT < 26) {
if (ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND == taskImportance)
appInBackground = true;
} else {
if (ActivityManager.RunningAppProcessInfo.IMPORTANCE_CACHED == taskImportance)
appInBackground = true;
}
if (appInBackground)
moveTaskToBack(true);
}
}
}

@Override
protected void onResume() {
super.onResume();
Log.v(LOG_TAG, "-> onResume");
topActivity = true;

String action = getIntent().getAction();
if (action != null && action.equals(FolioReader.ACTION_CLOSE_FOLIOREADER)) {
// FolioActivity is topActivity, so need to broadcast ReadPosition.
finish();
}
}

@Override
protected void onStop() {
super.onStop();
Log.v(LOG_TAG, "-> onStop");
topActivity = false;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler = new Handler();
density = getResources().getDisplayMetrics().density;
LocalBroadcastManager.getInstance(this).registerReceiver(closeBroadcastReceiver,
new IntentFilter(FolioReader.ACTION_CLOSE_FOLIOREADER));

// Fix for screen get turned off while reading
// TODO: -> Make this configurable
Expand Down Expand Up @@ -340,7 +421,7 @@ private void initBook(String mEpubFileName, int mEpubRawId, String mEpubFilePath
mEpubRawId, mEpubFileName);
addEpub(path);

String urlString = Constants.LOCALHOST + bookFileName + "/manifest";
String urlString = Constants.LOCALHOST + Uri.encode(bookFileName) + "/manifest";
new MainPresenter(this).parseManifest(urlString);

} catch (IOException e) {
Expand Down Expand Up @@ -543,6 +624,9 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RequestCode.SEARCH.value) {
Log.v(LOG_TAG, "-> onActivityResult -> " + RequestCode.SEARCH);

if (resultCode == RESULT_CANCELED)
return;

searchAdapterDataBundle = data.getBundleExtra(SearchAdapter.DATA_BUNDLE);
searchQuery = data.getCharSequenceExtra(SearchActivity.BUNDLE_SAVE_SEARCH_QUERY);

Expand Down Expand Up @@ -586,11 +670,16 @@ protected void onDestroy() {
if (outState != null)
outState.putParcelable(BUNDLE_READ_POSITION_CONFIG_CHANGE, lastReadPosition);

LocalBroadcastManager.getInstance(this).unregisterReceiver(searchReceiver);
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
localBroadcastManager.unregisterReceiver(searchReceiver);
localBroadcastManager.unregisterReceiver(closeBroadcastReceiver);

if (mEpubServer != null) {
mEpubServer.stop();
}

if (isFinishing())
localBroadcastManager.sendBroadcast(new Intent(FolioReader.ACTION_FOLIOREADER_CLOSED));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class SearchActivity : AppCompatActivity(), LoaderManager.LoaderCallbacks<Any?>,
}

enum class ResultCode(val value: Int) {
ITEM_SELECTED(1),
BACK_BUTTON_PRESSED(2)
ITEM_SELECTED(2),
BACK_BUTTON_PRESSED(3)
}

private lateinit var searchUri: Uri
Expand Down
Loading

0 comments on commit 2608657

Please sign in to comment.