#768 Support API v1.0

Try to find highest supported API version and fallback to API version 0.2
This commit is contained in:
Stefan Niedermann 2020-04-15 12:06:26 +02:00
parent e1c0774d3b
commit 25d9b35a10
8 changed files with 75 additions and 37 deletions

View file

@ -686,15 +686,16 @@ public class NotesListViewActivity extends LockedActivity implements ItemAdapter
}
listView.scrollToPosition(0);
} else if (requestCode == server_settings) {
// Recreate activity completely, because theme switchting makes problems when only invalidating the views.
// Recreate activity completely, because theme switching makes problems when only invalidating the views.
// @see https://github.com/stefan-niedermann/nextcloud-notes/issues/529
recreate();
} else {
try {
AccountImporter.onActivityResult(requestCode, resultCode, data, this, (SingleSignOnAccount account) -> {
Log.v(TAG, "Added account: " + "name:" + account.name + ", " + account.url + ", userId" + account.userId);
Log.i(TAG, "Added account: " + "name:" + account.name + ", " + account.url + ", userId" + account.userId);
try {
db.addAccount(account.url, account.userId, account.name);
CapabilitiesWorker.update(this);
} catch (SQLiteConstraintException e) {
if (db.getAccounts().size() > 1) { // TODO ideally only show snackbar when this is a not migrated account
Snackbar.make(coordinatorLayout, R.string.account_already_imported, Snackbar.LENGTH_LONG).show();

View file

@ -13,17 +13,15 @@ public class ApiVersion implements Comparable<ApiVersion> {
private String originalVersion = "?";
private int major = 0;
private int minor = 0;
private int patch = 0;
public ApiVersion(String originalVersion, int major, int minor, int patch) {
this(major, minor, patch);
public ApiVersion(String originalVersion, int major, int minor) {
this(major, minor);
this.originalVersion = originalVersion;
}
public ApiVersion(int major, int minor, int patch) {
public ApiVersion(int major, int minor) {
this.major = major;
this.minor = minor;
this.patch = patch;
}
public int getMajor() {
@ -34,10 +32,6 @@ public class ApiVersion implements Comparable<ApiVersion> {
return minor;
}
public int getPatch() {
return patch;
}
public boolean isGreaterOrEqualTo(ApiVersion v) {
return compareTo(v) >= 0;
}
@ -54,13 +48,10 @@ public class ApiVersion implements Comparable<ApiVersion> {
major = extractNumber(split[0]);
if (split.length > 1) {
minor = extractNumber(split[1]);
if (split.length > 2) {
micro = extractNumber(split[2]);
}
}
}
}
return new ApiVersion(versionString, major, minor, micro);
return new ApiVersion(versionString, major, minor);
}
private static int extractNumber(String containsNumbers) {
@ -87,10 +78,6 @@ public class ApiVersion implements Comparable<ApiVersion> {
return -1;
} else if (compare.getMinor() < getMinor()) {
return 1;
} else if (compare.getPatch() > getPatch()) {
return -1;
} else if (compare.getPatch() < getPatch()) {
return 1;
}
return 0;
}
@ -102,7 +89,6 @@ public class ApiVersion implements Comparable<ApiVersion> {
"originalVersion='" + originalVersion + '\'' +
", major=" + major +
", minor=" + minor +
", patch=" + patch +
'}';
}
}

View file

@ -3,6 +3,17 @@ package it.niedermann.owncloud.notes.model;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.json.JSONArray;
import org.json.JSONException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.NoSuchElementException;
import it.niedermann.owncloud.notes.persistence.NotesClient;
public class LocalAccount {
@ -12,7 +23,7 @@ public class LocalAccount {
private String url;
private String etag;
private long modified;
private String apiVersion;
private ApiVersion preferredApiVersion;
@ColorInt
private int color;
@ColorInt
@ -66,12 +77,36 @@ public class LocalAccount {
this.modified = modified;
}
public String getApiVersion() {
return apiVersion;
public ApiVersion getPreferredApiVersion() {
return preferredApiVersion;
}
public void setApiVersion(String apiVersion) {
this.apiVersion = apiVersion;
/**
* @param availableApiVersions <code>["0.2", "1.0", ...]</code>
*/
public void setPreferredApiVersion(@Nullable String availableApiVersions) {
// TODO move this logic to NotesClient?
try {
if(availableApiVersions == null) {
this.preferredApiVersion = null;
return;
}
JSONArray versionsArray = new JSONArray(availableApiVersions);
Collection<ApiVersion> supportedApiVersions = new ArrayList<>(versionsArray.length());
for (int i = 0; i < versionsArray.length(); i++) {
ApiVersion parsedApiVersion = ApiVersion.of(versionsArray.getString(i));
for (ApiVersion temp : NotesClient.SUPPORTED_API_VERSIONS) {
if (temp.compareTo(parsedApiVersion) == 0) {
supportedApiVersions.add(parsedApiVersion);
break;
}
}
}
this.preferredApiVersion = Collections.max(supportedApiVersions);
} catch (JSONException | NoSuchElementException e) {
e.printStackTrace();
this.preferredApiVersion = null;
}
}
public int getColor() {
@ -100,7 +135,7 @@ public class LocalAccount {
", url='" + url + '\'' +
", etag='" + etag + '\'' +
", modified=" + modified +
", apiVersion='" + apiVersion + '\'' +
", apiVersion='" + preferredApiVersion + '\'' +
", color=" + color +
", textColor=" + textColor +
'}';

View file

@ -19,7 +19,6 @@ import java.util.Map;
import it.niedermann.owncloud.notes.model.Capabilities;
@SuppressWarnings("WeakerAccess")
@WorkerThread
public class CapabilitiesClient {

View file

@ -40,10 +40,10 @@ public class CapabilitiesWorker extends Worker {
@NonNull
@Override
public Result doWork() {
NotesDatabase db = NotesDatabase.getInstance(getApplicationContext());
final NotesDatabase db = NotesDatabase.getInstance(getApplicationContext());
for (LocalAccount account : db.getAccounts()) {
try {
SingleSignOnAccount ssoAccount = AccountImporter.getSingleSignOnAccount(getApplicationContext(), account.getAccountName());
final SingleSignOnAccount ssoAccount = AccountImporter.getSingleSignOnAccount(getApplicationContext(), account.getAccountName());
Log.i(TAG, "Refreshing capabilities for " + ssoAccount.name);
final Capabilities capabilities = CapabilitiesClient.getCapabilities(getApplicationContext(), ssoAccount);
db.updateBrand(account.getId(), capabilities);
@ -59,7 +59,7 @@ public class CapabilitiesWorker extends Worker {
public static void update(@NonNull Context context) {
deregister(context);
Log.i(TAG, "Registering worker running each 24 hours.");
Log.i(TAG, "Registering capabilities worker running each 24 hours.");
WorkManager.getInstance(context.getApplicationContext()).enqueueUniquePeriodicWork(WORKER_TAG, ExistingPeriodicWorkPolicy.REPLACE, work);
}

View file

@ -218,9 +218,10 @@ public class NoteServerSyncHelper {
Log.e(TAG, LocalAccount.class.getSimpleName() + " for ssoAccount \"" + ssoAccount.name + "\" is null. Cannot synchronize.", new IllegalStateException());
return;
}
// TODO should each LocalAccount have a NotesClient instance? This way we could save the Map<>
final NotesClient notesClient;
if (!notesClients.containsKey(ssoAccount.name)) {
notesClients.put(ssoAccount.name, NotesClient.newInstance(localAccount, context));
notesClients.put(ssoAccount.name, NotesClient.newInstance(localAccount.getPreferredApiVersion(), context));
}
notesClient = notesClients.get(ssoAccount.name);
if (notesClient == null) {

View file

@ -5,6 +5,7 @@ import android.content.pm.PackageInfo;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
import com.nextcloud.android.sso.aidl.NextcloudRequest;
@ -24,8 +25,8 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import it.niedermann.owncloud.notes.model.ApiVersion;
import it.niedermann.owncloud.notes.model.CloudNote;
import it.niedermann.owncloud.notes.model.LocalAccount;
import it.niedermann.owncloud.notes.util.ServerResponse.NoteResponse;
import it.niedermann.owncloud.notes.util.ServerResponse.NotesResponse;
@ -60,9 +61,24 @@ public abstract class NotesClient {
public static final String JSON_ETAG = "etag";
public static final String JSON_MODIFIED = "modified";
public static NotesClient newInstance(@NonNull LocalAccount localAccount,
public static final ApiVersion[] SUPPORTED_API_VERSIONS = new ApiVersion[]{
new ApiVersion(1, 0),
new ApiVersion(0, 2)
};
public static NotesClient newInstance(@Nullable ApiVersion preferredApiVersion,
@NonNull Context appContext) {
// TODO check localAccount API_VERSION and create another instance if necessary
if (preferredApiVersion == null) {
Log.i(TAG, "apiVersion is null, using " + NotesClient_0_2.class.getSimpleName());
return new NotesClient_0_2(appContext);
// } else if (preferredApiVersion.compareTo(SUPPORTED_API_VERSIONS[0]) == 0) {
// Log.i(TAG, "Using " + NotesClient_1_0.class.getSimpleName());
// return new NotesClient_1_0(appContext);
} else if (preferredApiVersion.compareTo(SUPPORTED_API_VERSIONS[1]) == 0) {
Log.i(TAG, "Using " + NotesClient_0_2.class.getSimpleName());
return new NotesClient_0_2(appContext);
}
Log.w(TAG, "Unsupported API version " + preferredApiVersion + " - try using " + NotesClient_0_2.class.getSimpleName());
return new NotesClient_0_2(appContext);
}

View file

@ -669,7 +669,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
account.setUserName(cursor.getString(3));
account.setETag(cursor.getString(4));
account.setModified(cursor.getLong(5));
account.setApiVersion(cursor.getString(6));
account.setPreferredApiVersion(cursor.getString(6));
account.setColor(Color.parseColor('#' + cursor.getString(7)));
account.setTextColor(Color.parseColor('#' + cursor.getString(8)));
}
@ -689,7 +689,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
account.setUserName(cursor.getString(3));
account.setETag(cursor.getString(4));
account.setModified(cursor.getLong(5));
account.setApiVersion(cursor.getString(6));
account.setPreferredApiVersion(cursor.getString(6));
account.setColor(Color.parseColor('#' + cursor.getString(7)));
account.setTextColor(Color.parseColor('#' + cursor.getString(8)));
accounts.add(account);
@ -714,7 +714,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
account.setUserName(cursor.getString(3));
account.setETag(cursor.getString(4));
account.setModified(cursor.getLong(5));
account.setApiVersion(cursor.getString(6));
account.setPreferredApiVersion(cursor.getString(6));
account.setColor(Color.parseColor('#' + cursor.getString(7)));
account.setTextColor(Color.parseColor('#' + cursor.getString(8)));
}