Skip to content
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

Fix annotations #1028

Merged
merged 3 commits into from
Aug 12, 2020
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package fr.gaulupeau.apps.Poche.service;

import java.util.concurrent.Callable;

class CallableParameterizedAdapter<V> extends ParameterizedAdapter implements Callable<V> {

protected final ParameterizedCallable<V> parameterizedCallable;

public CallableParameterizedAdapter(ParameterizedCallable<V> parameterizedCallable) {
this.parameterizedCallable = parameterizedCallable;
}

@Override
public V call() throws Exception {
return parameterizedCallable.call(context);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.util.Log;

import java.util.List;
import java.util.concurrent.Future;

import fr.gaulupeau.apps.Poche.data.DbConnection;
import fr.gaulupeau.apps.Poche.data.DbUtils;
Expand All @@ -26,6 +27,7 @@
import static fr.gaulupeau.apps.Poche.service.ServiceHelper.enqueueServiceTask;
import static fr.gaulupeau.apps.Poche.service.ServiceHelper.enqueueSimpleServiceTask;
import static fr.gaulupeau.apps.Poche.service.ServiceHelper.startService;
import static fr.gaulupeau.apps.Poche.service.ServiceHelper.submitServiceCallableTask;

public class OperationsHelper {

Expand Down Expand Up @@ -73,8 +75,8 @@ public static void setArticleTags(Context context, Article article, List<Tag> ne
.setArticleTags(article, newTags), postCallCallback);
}

public static void addAnnotation(Context context, int articleId, Annotation annotation) {
enqueueServiceTask(context, ctx -> new OperationsWorker(ctx)
public static Future<Annotation> addAnnotation(Context context, int articleId, Annotation annotation) {
return submitServiceCallableTask(context, ctx -> new OperationsWorker(ctx)
.addAnnotation(articleId, annotation), null);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fr.gaulupeau.apps.Poche.service;

import android.content.Context;

interface Parameterized {
void setContext(Context context);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fr.gaulupeau.apps.Poche.service;

import android.content.Context;

abstract class ParameterizedAdapter implements Parameterized {

protected Context context;

@Override
public void setContext(Context context) {
this.context = context;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fr.gaulupeau.apps.Poche.service;

import android.content.Context;

public interface ParameterizedCallable<V> {
V call(Context context) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fr.gaulupeau.apps.Poche.service;

import android.content.Context;

public interface ParameterizedRunnable {
void run(Context context);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package fr.gaulupeau.apps.Poche.service;

import android.content.Context;

import java.util.Objects;

class ParameterizedRunnableAdapter implements ParameterizedRunnable {

protected Runnable runnable;
protected Parameterized parameterized;

public ParameterizedRunnableAdapter(Runnable runnable, Parameterized parameterized) {
this.runnable = Objects.requireNonNull(runnable);
this.parameterized = parameterized;
}

@Override
public void run(Context context) {
try {
if (parameterized != null) parameterized.setContext(context);
runnable.run();
} finally {
if (parameterized != null) parameterized.setContext(null);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package fr.gaulupeau.apps.Poche.service;

class RunnableParameterizedAdapter extends ParameterizedAdapter implements Runnable {

protected final ParameterizedRunnable parameterizedRunnable;

public RunnableParameterizedAdapter(ParameterizedRunnable parameterizedRunnable) {
this.parameterizedRunnable = parameterizedRunnable;
}

@Override
public void run() {
parameterizedRunnable.run(context);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

import androidx.core.util.Consumer;

import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

import fr.gaulupeau.apps.Poche.service.tasks.ActionRequestTask;
import fr.gaulupeau.apps.Poche.service.tasks.DownloadArticleAsFileTask;
import fr.gaulupeau.apps.Poche.service.tasks.FetchArticleImagesTask;
Expand Down Expand Up @@ -67,23 +70,69 @@ public static void enqueueSimpleServiceTask(Context context,
context.startService(TaskService.newSimpleTaskIntent(context, serviceClass, task));
}

public static void enqueueServiceTask(Context context, TaskService.Task task,
public static void enqueueServiceTask(Context context, ParameterizedRunnable task,
Runnable postCallCallback) {
enqueueServiceTask(context, MainService.class, task, postCallCallback);
}

public static <V> Future<V> submitServiceCallableTask(Context context,
ParameterizedCallable<V> task,
Runnable postCallCallback) {
return submit(context, MainService.class, task, postCallCallback);
}

public static Future<?> submitServiceTask(Context context, ParameterizedRunnable task,
Runnable postCallCallback) {
return submit(context, MainService.class, task, postCallCallback);
}

public static <V> Future<V> submit(Context context,
Class<? extends TaskService> serviceClass,
ParameterizedCallable<V> callable,
Runnable postCallCallback) {
CallableParameterizedAdapter<V> parameterizedCallable
= new CallableParameterizedAdapter<>(callable);
FutureTask<V> future = new FutureTask<>(parameterizedCallable);

enqueueServiceTask(context, serviceClass, future, parameterizedCallable, postCallCallback);

return future;
}

public static Future<?> submit(Context context,
Class<? extends TaskService> serviceClass,
ParameterizedRunnable runnable,
Runnable postCallCallback) {
RunnableParameterizedAdapter parameterizedRunnable
= new RunnableParameterizedAdapter(runnable);
FutureTask<?> future = new FutureTask<>(parameterizedRunnable, null);

enqueueServiceTask(context, serviceClass, future, parameterizedRunnable, postCallCallback);

return future;
}

private static void enqueueServiceTask(Context context,
Class<? extends TaskService> serviceClass,
Runnable runnable, Parameterized parameterized,
Runnable postCallCallback) {
enqueueServiceTask(context, serviceClass,
new ParameterizedRunnableAdapter(runnable, parameterized),
postCallCallback);
}

public static void enqueueServiceTask(Context context,
Class<? extends TaskService> serviceClass,
TaskService.Task task,
ParameterizedRunnable task,
Runnable postCallCallback) {
performBoundServiceCall(context, serviceClass, binder -> {
TaskService.TaskServiceBinder service = (TaskService.TaskServiceBinder) binder;
service.enqueueTask(task);
service.enqueue(task);
}, postCallCallback);
}

public static void performBoundServiceCall(Context context,
Class<? extends TaskService> serviceClass,
Class<?> serviceClass,
Consumer<IBinder> action,
Runnable postCallCallback) {
Log.d(TAG, "performBoundServiceCall() started");
Expand Down
18 changes: 7 additions & 11 deletions app/src/main/java/fr/gaulupeau/apps/Poche/service/TaskService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,9 @@ public class TaskService extends Service {

public static final String ACTION_SIMPLE_TASK = "action_simple_task";

public interface Task {
void run(Context context);
}

public class TaskServiceBinder extends Binder {
public void enqueueTask(Task task) {
TaskService.this.enqueueTask(task, true);
public void enqueue(ParameterizedRunnable parameterizedRunnable) {
TaskService.this.enqueueTask(parameterizedRunnable, true);
}
}

Expand All @@ -46,7 +42,7 @@ public void enqueueTask(Task task) {
private final Object startIdLock = new Object();
private volatile int lastStartId;

private final BlockingQueue<Task> taskQueue = new LinkedBlockingQueue<>();
private final BlockingQueue<ParameterizedRunnable> taskQueue = new LinkedBlockingQueue<>();

private volatile boolean running;

Expand Down Expand Up @@ -98,7 +94,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(tag, "onStartCommand()");

if (ACTION_SIMPLE_TASK.equals(intent.getAction())) {
Task task = taskFromSimpleTask(SimpleTask.fromIntent(intent));
ParameterizedRunnable task = taskFromSimpleTask(SimpleTask.fromIntent(intent));
if (task != null) {
enqueueTask(task, false);
}
Expand All @@ -111,7 +107,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}

private Task taskFromSimpleTask(SimpleTask simpleTask) {
private ParameterizedRunnable taskFromSimpleTask(SimpleTask simpleTask) {
if (simpleTask == null) {
Log.d(tag, "taskFromActionRequest() request is null");
return null;
Expand Down Expand Up @@ -144,7 +140,7 @@ private void run() {
Process.setThreadPriority(getThreadPriority());

while (running) {
Task task;
ParameterizedRunnable task;
try {
task = taskQueue.poll(WAIT_TIME, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Expand Down Expand Up @@ -183,7 +179,7 @@ private void readyToStop() {
}
}

private void enqueueTask(Task task, boolean ensureStarted) {
private void enqueueTask(ParameterizedRunnable task, boolean ensureStarted) {
Log.d(tag, "enqueueTask()");
Objects.requireNonNull(task, "task is null");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ private void setArticleTagsInternal(Article article, List<Tag> newTags) {
Log.d(TAG, "setArticleTagsInternal() finished");
}

public void addAnnotation(int articleId, Annotation annotation) {
public Annotation addAnnotation(int articleId, Annotation annotation) {
Log.d(TAG, String.format("addAnnotation(%d, %s) started", articleId, annotation));

Article article = getArticle(articleId);
Expand Down Expand Up @@ -441,6 +441,7 @@ public void addAnnotation(int articleId, Annotation annotation) {
}

Log.d(TAG, "addAnnotation() finished");
return annotation;
}

public void updateAnnotation(int articleId, Annotation annotation) {
Expand All @@ -454,9 +455,7 @@ public void updateAnnotation(int articleId, Annotation annotation) {

String newText = annotation.getText();

AnnotationDao annotationDao = getDaoSession().getAnnotationDao();
annotation = annotationDao.queryBuilder()
.where(AnnotationDao.Properties.Id.eq(annotationId)).unique();
annotation = getAnnotation(annotationId);

if (TextUtils.equals(annotation.getText(), newText)) {
Log.w(TAG, "updateAnnotation() annotation ID=" + annotationId
Expand All @@ -466,7 +465,7 @@ public void updateAnnotation(int articleId, Annotation annotation) {

annotation.setText(newText);

annotationDao.update(annotation);
getDaoSession().getAnnotationDao().update(annotation);
Log.d(TAG, "updateAnnotation() annotation object updated");

Article article = getArticle(articleId);
Expand All @@ -484,6 +483,8 @@ public void updateAnnotation(int articleId, Annotation annotation) {
public void deleteAnnotation(int articleId, Annotation annotation) {
Log.d(TAG, String.format("deleteAnnotation(%d, %s) started", articleId, annotation));

annotation = getAnnotation(annotation.getId());

Integer remoteId = annotation.getAnnotationId();

if (annotation.getId() != null) {
Expand Down Expand Up @@ -513,6 +514,11 @@ public void deleteAnnotation(int articleId, Annotation annotation) {
Log.d(TAG, "deleteAnnotation() finished");
}

private Annotation getAnnotation(long annotationId) {
return getDaoSession().getAnnotationDao().queryBuilder()
.where(AnnotationDao.Properties.Id.eq(annotationId)).unique();
}

private Article getArticle(int articleId) {
return getArticleDao().queryBuilder()
.where(ArticleDao.Properties.ArticleId.eq(articleId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.Date;
import java.util.EnumSet;
import java.util.List;
import java.util.concurrent.Future;

import fr.gaulupeau.apps.InThePoche.BuildConfig;
import fr.gaulupeau.apps.InThePoche.R;
Expand Down Expand Up @@ -798,10 +799,9 @@ public List<Annotation> getAnnotations() {
}

@Override
public Annotation createAnnotation(Annotation annotation) {
OperationsHelper.addAnnotation(ReadArticleActivity.this,
article.getArticleId(), annotation);
return annotation;
public Annotation createAnnotation(Annotation annotation) { // TODO: fix: waiting call
return waitForFuture(OperationsHelper.addAnnotation(
ReadArticleActivity.this, article.getArticleId(), annotation));
}

@Override
Expand All @@ -817,6 +817,15 @@ public Annotation deleteAnnotation(Annotation annotation) {
article.getArticleId(), annotation);
return annotation;
}

private <T> T waitForFuture(Future<T> future) {
try {
return future.get();
} catch (Exception e) {
Log.w("JsAnnotCtrl.Callback", "waitForFuture() exception", e);
}
return null;
}
});

webViewContent.addJavascriptInterface(annotationController, "hostAnnotationController");
Expand Down