Skip to content

Pull request to update for supporting LCP #79

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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
import android.app.AlertDialog;
import android.content.DialogInterface;

import android.os.Handler;
import android.os.Message;
/**
* @author chtian
*
Expand All @@ -71,6 +73,8 @@ public void done() {

private Context context;
private final String testPath = "epubtest";
private Handler mHandler = null;
private String mBookName;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -100,41 +104,85 @@ protected void onCreate(Bundle savedInstanceState) {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {

String bookName = list.get(arg2);
mBookName = list.get(arg2);

String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + testPath + "/" + bookName;
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + testPath + "/" + mBookName;

Toast.makeText(context, "Select " + bookName, Toast.LENGTH_SHORT).show();
Toast.makeText(context, "Select " + mBookName, Toast.LENGTH_SHORT).show();

m_SdkErrorHandler_Messages = new Stack<String>();

EPub3.setSdkErrorHandler(ContainerList.this);
Container container = EPub3.openBook(path);
EPub3.setSdkErrorHandler(null);

ContainerHolder.getInstance().put(container.getNativePtr(), container);

Intent intent = new Intent(getApplicationContext(), BookDataActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Constants.BOOK_NAME, bookName);
intent.putExtra(Constants.CONTAINER_ID, container.getNativePtr());

SdkErrorHandlerMessagesCompleted callback = new SdkErrorHandlerMessagesCompleted(intent) {
@Override
public void once() {
startActivity(m_intent);
}
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Container container = (Container)msg.obj;
if(container == null){
//error message show
SdkErrorHandlerMessagesCompleted callback = new SdkErrorHandlerMessagesCompleted(null) {
@Override
public void once() {
//do notthing
}
};
// async!
popSdkErrorHandlerMessage(context, callback);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that there is a similar PR for the proposed above code change (nil container):
https://github.com/readium/SDKLauncher-Android/pull/76/files#diff-c50ac569905f558500aec3f615f04357

else{
ContainerHolder.getInstance().put(container.getNativePtr(), container);

Intent intent = new Intent(getApplicationContext(), BookDataActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Constants.BOOK_NAME, mBookName);
intent.putExtra(Constants.CONTAINER_ID, container.getNativePtr());

SdkErrorHandlerMessagesCompleted callback = new SdkErrorHandlerMessagesCompleted(intent) {
@Override
public void once() {
startActivity(m_intent);
}
};

// async!
popSdkErrorHandlerMessage(context, callback);
}
}
};

// async!
popSdkErrorHandlerMessage(context, callback);
//Run on thread
OpenBookOnThread task = new OpenBookOnThread(path);
task.start();
}
});

// Loads the native lib and sets the path to use for cache
EPub3.setCachePath(getCacheDir().getAbsolutePath());
}

class OpenBookOnThread extends Thread{
String mPath;
OpenBookOnThread(final String path){
mPath = path;
}
@Override
public void run()
{
Container container = EPub3.openBook(mPath);
EPub3.setSdkErrorHandler(null);
Message msg = new Message();
msg.obj = container;
mHandler.sendMessage(msg);
}
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly to my question on iOS ( https://github.com/readium/SDKLauncher-iOS/pull/68/files#r49284709 ) I wonder if a new thread is necessary here? Instead, could the "client" user interface can be dispatched into its own execution context, when it needs it? Side note: popSdkErrorHandlerMessage in ContainerList.java handles async UI dialogs by queuing messages in interruptable callback chains...but I don't know if a similar approach would work with the LCP-related UI input dialogs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like iOS, It is necessary for the UI thread.
And we verified "popSdkErrorHandlerMessage" performs normally regardless of UI dailogs.
At container open in thread, messages are added to callback chain simply. But the task show a message(popSdkErrorHandlerMessage) is run on main thread. It is separated and it is not affect each other.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your feedback @fasooDev6
I am concerned about potential hard-to-find regression bugs when dispatching the entire execution of Container container = EPub3.openBook(mPath) in a new thread. Can we narrow the scope of the code that needs to be executed in a non-blocking way? (i.e. the LCP popup dialog that "waits" for user input) Can we use a better asynchronous design, rather than creating a new thread?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"OpenContainer" can not be excuted on the main thread in roder to apply for integrate the readium-DRM(LCP) from DRM Inside. Because it is block readium-DRM to receive user input when run UI dialog.
As with android, even when run using the opencontainer ios "dispatch_async (backgroundQueue, ^ {})" and create a new thread. If you do not want to create a new thread, it must first be readium-DRM is modified by non-block mode.

Also, we try to use the "asyncTask" android framework in place to implement the existing, but where the UI dialog to run the openContainer could not be applied because it must reside in separate project.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CC @clebeaupin who is in the process of implementing support for Content Module (and has developed the entire Android Studio / Gradle port, and LCP-JNI integration)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up: @clebeaupin implemented the asynchronous openBook() in the feature/lcp branch:
4badf01#diff-0ad69b820947c790fa940a8655809251R91


private void containerRegisterContentModules(Context context)
{
DrmInitialize drmInitialize = new DrmInitialize();
drmInitialize.initialize(context);
}

private Stack<String> m_SdkErrorHandler_Messages = null;

// async!
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// DrmInitialize.java
// SDKLauncher-OSX
//
// Created by Fasoo.com Development Team on 2015-12-12.
// ( M.N. Kim )
//
// Copyright (c) 2015 The Readium Foundation and contributors. All rights reserved.
//
// The Readium SDK is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>
//
package org.readium.sdk.android.launcher;

import android.content.Context;

public class DrmInitialize {
void initialize(Context context){
}
}