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

[Not for merge] Android 4 compatibility #1107

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {

defaultConfig {
applicationId "fr.gaulupeau.apps.InThePoche"
minSdkVersion 21
minSdkVersion 14
targetSdkVersion 30
versionCode 228
versionName "2.4.2"
Expand Down Expand Up @@ -66,12 +66,12 @@ dependencies {
implementation 'org.greenrobot:eventbus:3.2.0'
implementation 'org.greenrobot:greendao:3.3.0'
annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.2.0'
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
implementation 'com.squareup.okhttp3:okhttp-urlconnection:4.9.0'
implementation 'com.squareup.okhttp3:okhttp:3.12.12'
implementation 'com.squareup.okhttp3:okhttp-urlconnection:3.12.12'
implementation 'org.conscrypt:conscrypt-android:2.5.1'
implementation 'com.facebook.stetho:stetho:1.5.1'
implementation 'com.facebook.stetho:stetho-okhttp3:1.5.1'
implementation 'com.mikepenz:aboutlibraries:7.1.0'
implementation 'com.mikepenz:aboutlibraries:6.2.1'
implementation 'com.github.di72nn.wallabag-api-wrapper:api-wrapper:v2.0.0-beta.6'
implementation 'org.slf4j:slf4j-android:1.7.30'
}
3 changes: 3 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
-keepclassmembers class fr.gaulupeau.apps.Poche.ui.JsActionController {
public *;
}
-keepclassmembers class fr.gaulupeau.apps.Poche.ui.JsAnnotationController {
public *;
}
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@
android:name="android.appwidget.provider"
android:resource="@xml/icon_unread_info" />
</receiver>
<receiver
android:name="fr.gaulupeau.apps.Poche.network.ConnectivityChangeReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"
tools:ignore="BatteryLife" />
</intent-filter>
</receiver>
<receiver
android:name="fr.gaulupeau.apps.Poche.service.BootReceiver"
android:enabled="false">
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/assets/annotations-android-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ document.addEventListener("DOMContentLoaded", function() {
});

const authorization = {
permits() { return true; },
permits: function() { return true; },
};
app.registry.registerUtility(authorization, 'authorizationPolicy');

Expand Down Expand Up @@ -46,7 +46,7 @@ document.addEventListener("DOMContentLoaded", function() {

app.include(myStorage);

app.start().then(() => {
app.start().then(function() {
app.annotations.load({});
});
});
Expand Down
15 changes: 13 additions & 2 deletions app/src/main/java/fr/gaulupeau/apps/Poche/data/DbConnection.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package fr.gaulupeau.apps.Poche.data;

import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.util.Log;

import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.query.QueryBuilder;

import fr.gaulupeau.apps.InThePoche.BuildConfig;
Expand All @@ -25,8 +28,16 @@ public static DaoSession getSession() {

Log.d(TAG, "creating new db session");
WallabagDbOpenHelper dbHelper = new WallabagDbOpenHelper(App.getInstance(), dbPath, null);
dbHelper.setWriteAheadLoggingEnabled(true);
DaoMaster daoMaster = new DaoMaster(dbHelper.getWritableDb());
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
dbHelper.setWriteAheadLoggingEnabled(true);
}
Database db = dbHelper.getWritableDb();
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
if(!((SQLiteDatabase)db.getRawDatabase()).enableWriteAheadLogging()) {
Log.w(TAG, "write ahead logging was not enabled");
}
}
DaoMaster daoMaster = new DaoMaster(db);
Holder.session = daoMaster.newSession();
} else {
Log.d(TAG, "using existing db session");
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/fr/gaulupeau/apps/Poche/data/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import fr.gaulupeau.apps.InThePoche.R;
import fr.gaulupeau.apps.Poche.App;
import fr.gaulupeau.apps.Poche.network.ConnectivityChangeReceiver;
import fr.gaulupeau.apps.Poche.service.WallabagJobService;
import fr.gaulupeau.apps.Poche.ui.HttpSchemeHandlerActivity;
import fr.gaulupeau.apps.Poche.ui.Sortable;
Expand Down Expand Up @@ -51,7 +52,11 @@ public static boolean checkFirstRunInit(Context context) {
}

public static void enableConnectivityChangeReceiver(Context context, boolean enable) {
WallabagJobService.enable(context, enable);
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
enableComponent(context, ConnectivityChangeReceiver.class, enable);
} else {
WallabagJobService.enable(context, enable);
}
}

// TODO: reuse in setHandleHttpScheme
Expand Down
21 changes: 18 additions & 3 deletions app/src/main/java/fr/gaulupeau/apps/Poche/data/dao/FtsDao.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package fr.gaulupeau.apps.Poche.data.dao;

import android.os.Build;

import org.greenrobot.greendao.database.Database;

import fr.gaulupeau.apps.Poche.App;
Expand Down Expand Up @@ -29,31 +31,44 @@ public class FtsDao {
"article_content_after_delete_tr"
};

public static boolean isFtsSupported() {
// https://www.sqlite.org/changes.html#version_3_7_9 is required for the 'content' option
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN; // https://stackoverflow.com/a/4377116
}

public static String getQueryString() {
return "select " + COLUMN_ID + " from " + TABLE_NAME + " where " + TABLE_NAME + " match ";
}

public static void createAll(Database db, boolean ifNotExists) {
if (!isFtsSupported()) return;

createViewForFts(db, ifNotExists);
createTable(db, ifNotExists);
createTriggers(db, ifNotExists);
}

public static void dropAll(Database db, boolean ifExists) {
if (!isFtsSupported()) return;

dropTriggers(db, ifExists);
dropTable(db, ifExists);
dropViewForFts(db, ifExists);
}

public static void deleteAllArticles(Database db) {
if (!isFtsSupported()) return;

dropTable(db, true);
createTable(db, true);
}

private static void createTable(Database db, boolean ifNotExists) {
String options = ", content=\"" + VIEW_FOR_FTS_NAME + "\""
+ ", tokenize="
+ (App.getSettings().isFtsIcuTokenizerEnabled() ? "icu" : "unicode61");
String options = ", content=\"" + VIEW_FOR_FTS_NAME + "\"";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
options += ", tokenize="
+ (App.getSettings().isFtsIcuTokenizerEnabled() ? "icu" : "unicode61");
}

db.execSQL("create virtual table " + getIfNotExistsConstraint(ifNotExists) +
TABLE_NAME + " using fts4(" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
package fr.gaulupeau.apps.Poche.events;

public class ConnectivityChangedEvent {}
public class ConnectivityChangedEvent {

private boolean noConnectivity;

public ConnectivityChangedEvent() {}

public ConnectivityChangedEvent(boolean noConnectivity) {
this.noConnectivity = noConnectivity;
}

public boolean isNoConnectivity() {
return noConnectivity;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class EventProcessor {
private Handler mainHandler;
private NotificationManager notificationManager;

private boolean delayedNetworkChangedTask;

private NotificationCompat.Builder syncQueueNotificationBuilder;
private NotificationCompat.Builder updateArticlesNotificationBuilder;
private NotificationCompat.Builder sweepDeletedArticlesNotificationBuilder;
Expand Down Expand Up @@ -118,11 +120,12 @@ public void onAlarmReceivedEvent(AlarmReceivedEvent event) {
public void onConnectivityChangedEvent(ConnectivityChangedEvent event) {
Log.d(TAG, "onConnectivityChangedEvent() started");

Settings settings = getSettings();
if (settings.isOfflineQueuePending() && settings.isConfigurationOk()) {
Log.d(TAG, "networkChanged() requesting SyncQueue operation");
OperationsHelper.syncQueue(getContext(), true);
if(event.isNoConnectivity()) {
Log.d(TAG, "onConnectivityChangedEvent() isNoConnectivity is true; ignoring event");
return;
}

networkChanged(false);
}

@Subscribe
Expand Down Expand Up @@ -559,6 +562,33 @@ public void onActionResultEvent(ActionResultEvent event) {
}
}

private void networkChanged(boolean delayed) {
if(!delayed && delayedNetworkChangedTask) return;

if(!WallabagConnection.isNetworkAvailable()) return;

Settings settings = getSettings();

if(settings.isOfflineQueuePending() && settings.isConfigurationOk()) {
if(delayed) {
Log.d(TAG, "networkChanged() requesting SyncQueue operation");

OperationsHelper.syncQueue(getContext(), true);

delayedNetworkChangedTask = false;
} else {
getMainHandler().postDelayed(new Runnable() {
@Override
public void run() {
networkChanged(true);
}
}, 3000);

delayedNetworkChangedTask = true;
}
}
}

private void enableConnectivityChangeReceiver(boolean enable) {
if(getSettings().isAutoSyncQueueEnabled()) {
Log.d(TAG, "enableConnectivityChangeReceiver() enable connectivity change receiver: " + enable);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fr.gaulupeau.apps.Poche.network;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.util.Log;

import fr.gaulupeau.apps.Poche.events.ConnectivityChangedEvent;
import fr.gaulupeau.apps.Poche.events.EventHelper;

public class ConnectivityChangeReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if(ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
Log.d("ConnectivityChangeRcvr", "Connectivity changed");

EventHelper.postEvent(new ConnectivityChangedEvent(
intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private void readyToStop() {

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

Log.v(tag, "enqueueTask() enqueueing task");
taskQueue.add(task);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
import android.app.job.JobService;
import android.content.ComponentName;
import android.content.Context;
import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;

import fr.gaulupeau.apps.Poche.events.ConnectivityChangedEvent;
import fr.gaulupeau.apps.Poche.events.EventHelper;

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class WallabagJobService extends JobService {

private static final int CONNECTIVITY_CHANGE_JOB_ID = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ActionRequestTask extends SimpleTask {
protected ActionRequest actionRequest;

public ActionRequestTask(ActionRequest actionRequest) {
Objects.requireNonNull(actionRequest, "actionRequest is null");
Objects.requireNonNull(actionRequest);
this.actionRequest = actionRequest;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public T createFromParcel(Parcel in) {
T instance;
try {
instance = clazz.newInstance();
} catch (IllegalAccessException | InstantiationException e) {
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Uh oh");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.util.Log;
import android.util.Pair;

import androidx.core.util.ObjectsCompat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -13,7 +15,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import fr.gaulupeau.apps.Poche.data.DbUtils;
Expand Down Expand Up @@ -357,15 +358,15 @@ private void processArticle(ArticlesChangedEvent event, boolean full,
article.setUpdateDate(apiArticle.updatedAt);
articleChanges.add(ChangeType.UPDATED_DATE_CHANGED);
}
if (!Objects.equals(article.getPublishedAt(), apiArticle.publishedAt)) {
if (!ObjectsCompat.equals(article.getPublishedAt(), apiArticle.publishedAt)) {
article.setPublishedAt(apiArticle.publishedAt);
articleChanges.add(ChangeType.PUBLISHED_AT_CHANGED);
}
if (!Objects.equals(article.getStarredAt(), apiArticle.starredAt)) {
if (!ObjectsCompat.equals(article.getStarredAt(), apiArticle.starredAt)) {
article.setStarredAt(apiArticle.starredAt);
articleChanges.add(ChangeType.STARRED_AT_CHANGED);
}
if (!Objects.equals(article.getIsPublic(), apiArticle.isPublic)) {
if (!ObjectsCompat.equals(article.getIsPublic(), apiArticle.isPublic)) {
article.setIsPublic(apiArticle.isPublic);
articleChanges.add(ChangeType.IS_PUBLIC_CHANGED);
}
Expand Down Expand Up @@ -552,11 +553,11 @@ private boolean processAnnotations(wallabag.apiwrapper.models.Article apiArticle
annotation.setQuote(apiAnnotation.quote);
annotationChanged = true;
}
if (!Objects.equals(annotation.getCreatedAt(), apiAnnotation.createdAt)) {
if (!ObjectsCompat.equals(annotation.getCreatedAt(), apiAnnotation.createdAt)) {
annotation.setCreatedAt(apiAnnotation.createdAt);
annotationChanged = true;
}
if (!Objects.equals(annotation.getUpdatedAt(), apiAnnotation.updatedAt)) {
if (!ObjectsCompat.equals(annotation.getUpdatedAt(), apiAnnotation.updatedAt)) {
annotation.setUpdatedAt(apiAnnotation.updatedAt);
annotationChanged = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import android.util.Log;
import android.util.Pair;

import androidx.core.util.ObjectsCompat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import fr.gaulupeau.apps.Poche.data.DbUtils;
import fr.gaulupeau.apps.Poche.data.QueueHelper;
Expand Down Expand Up @@ -307,7 +308,7 @@ private void addLink(AddLinkItem item) throws IncorrectConfigurationException,
}

private void updateGivenUrl(Article article, String givenUrl) {
if (!Objects.equals(article.getGivenUrl(), givenUrl)) {
if (!ObjectsCompat.equals(article.getGivenUrl(), givenUrl)) {
article.setGivenUrl(givenUrl);
article.update();
}
Expand Down
Loading