Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 82bbca0e authored by David Luhmer's avatar David Luhmer Committed by GitHub
Browse files

Merge pull request #40 from nextcloud/add-annotations

Add annotations
parents d3fb8a10 79df148a
Loading
Loading
Loading
Loading
+8 −54
Original line number Diff line number Diff line
@@ -101,6 +101,8 @@ AccountImporter.getSingleSignOnAccount(context, accountName);
    ```java
    public interface API {

        String mApiEndpoint = "/index.php/apps/news/api/v1-2/";

        @GET("user")
        Observable<UserInfo> user();

@@ -114,7 +116,7 @@ AccountImporter.getSingleSignOnAccount(context, accountName);
    }
    ```

    You might instantiate your `API` by using something like the following code: 
    You might instantiate your retrofit `API` by using something like this: 
    ```java
    public class ApiProvider {

@@ -126,58 +128,7 @@ AccountImporter.getSingleSignOnAccount(context, accountName);
    }
    ```

    4.1.2) Use Nextcloud Single Sign On:

    In order to use the nextcloud network stack, you'll need to implement the interface `API` shown above and use the nextcloud network stack instead of the retrofit one.

    ```java
    public class API_SSO implements API {

        private static final String mApiEndpoint = "/index.php/apps/news/api/v1-2/";
        private NextcloudAPI nextcloudAPI;

        public API_SSO(NextcloudAPI nextcloudAPI) {
            this.nextcloudAPI = nextcloudAPI;
        }

        @Override
        public Observable<UserInfo> user() {
            final Type type = UserInfo.class;
            NextcloudRequest request = new NextcloudRequest.Builder()
                    .setMethod("GET")
                    .setUrl(mApiEndpoint + "user")
                    .build();
            return nextcloudAPI.performRequestObservable(type, request);
        }

        @Override
        public Call<List<Feed>> createFeed(Map<String, Object> feedMap) {
            Type feedListType = new TypeToken<List<Feed>>() {}.getType();
            String body = new GsonBuilder().create().toJson(feedMap);
            NextcloudRequest request = new NextcloudRequest.Builder()
                    .setMethod("POST")
                    .setUrl(mApiEndpoint + "feeds")
                    .setRequestBody(body)
                    .build();
            return Retrofit2Helper.WrapInCall(nextcloudAPI, request, feedListType);
        }

        @Override
        public Completable deleteFeed(long feedId) {
            final NextcloudRequest request = new NextcloudRequest.Builder()
                    .setMethod("DELETE")
                    .setUrl(mApiEndpoint + "feeds/" + feedId)
                    .build();
            return ReactivexHelper.WrapInCompletable(nextcloudAPI, request);
        }


    }
    ```
    
    Note: If you need a different mapping between your json-structure and your java-structure you might want to create a custom type adapter using `new GsonBuilder().create().registerTypeAdapter(...)`. Take a look at [this](https://github.com/nextcloud/news-android/blob/783836390b4c27aba285bad1441b53154df16685/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/helper/GsonConfig.java) example for more information.

    4.1.3) Use of new API using the nextcloud app network stack
    4.1.2) Use of new API using the nextcloud app network stack

    ```java
    public class ApiProvider {
@@ -187,13 +138,16 @@ AccountImporter.getSingleSignOnAccount(context, accountName);
        public ApiProvider(NextcloudAPI.ApiConnectedListener callback) {
           SingleSignOnAccount ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context);
           NextcloudAPI nextcloudAPI = new NextcloudAPI(context, ssoAccount, new GsonBuilder().create(), callback);
           mApi = new API_SSO(nextcloudAPI);
           mApi = new NextcloudRetrofitApiBuilder(nextcloudAPI, API.mApiEndpoint).create(API.class);

       }
    }
    ```
    
    Enjoy! If you're already using retrofit, you don't need to modify your application logic. Just exchange the API and you're good to go!

    Note: If you need a different mapping between your json-structure and your java-structure you might want to create a custom type adapter using `new GsonBuilder().create().registerTypeAdapter(...)`. Take a look at [this](https://github.com/nextcloud/news-android/blob/783836390b4c27aba285bad1441b53154df16685/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/helper/GsonConfig.java) example for more information.

    4.2) **Without Retrofit**

    `NextcloudAPI` provides a method called `performNetworkRequest(NextcloudRequest request)` that allows you to handle the server response yourself.
+8 −3
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ buildscript {
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath 'com.android.tools.build:gradle:3.3.1'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
    }
}
@@ -50,6 +50,11 @@ dependencies {

    implementation 'commons-io:commons-io:2.6'

    implementation 'com.squareup.retrofit2:retrofit:2.3.0'
    implementation 'com.squareup.okhttp3:okhttp:3.8.0'
    implementation 'com.squareup.retrofit2:retrofit:2.5.0'
    implementation 'com.squareup.okhttp3:okhttp:3.12.0'

    // Required for local unit tests (JUnit 4 framework)
    testImplementation 'junit:junit:4.12'
    // required if you want to use Mockito for unit tests
    testImplementation 'org.mockito:mockito-core:2.23.4'
}
+2 −1
Original line number Diff line number Diff line
@@ -3,4 +3,5 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
distributionSha256Sum=36bf7ff499223d5139f005822130ccca784c91591b514677fd376eed966c907e
+4 −7
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ public class AccountImporter {
    public static final int CHOOSE_ACCOUNT_SSO = 4242;
    public static final int REQUEST_AUTH_TOKEN_SSO = 4243;

    public static boolean AccountsToImportAvailable(Context context) {
    public static boolean accountsToImportAvailable(Context context) {
        return findAccounts(context).size() > 0;
    }

@@ -96,7 +96,7 @@ public class AccountImporter {

        List<Account> accountsAvailable = new ArrayList<>();
        for (final Account account : accounts) {
            if (account.type.equals("nextcloud")) {
            if ("nextcloud".equals(account.type)) {
                accountsAvailable.add(account);
            }
        }
@@ -130,11 +130,8 @@ public class AccountImporter {
        if (mPrefs.contains(prefKey)) {
            try {
                return SingleSignOnAccount.fromString(mPrefs.getString(prefKey, null));
            } catch (ClassNotFoundException e) {
                Log.e(TAG, "This should never happen!");
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException | IOException e) {
                Log.e(TAG, "[getSingleSignOnAccount]", e);
            }
        }
        throw new NextcloudFilesAppAccountNotFoundException();
+52 −1
Original line number Diff line number Diff line
@@ -40,7 +40,10 @@ public class NextcloudRequest implements Serializable {

    private NextcloudRequest() { }

    public static class Builder {
    public static class Builder implements Serializable {

        private static final long serialVersionUID = 2121321432424242L; //assign a long value

        private NextcloudRequest ncr;

        public Builder() {
@@ -144,4 +147,52 @@ public class NextcloudRequest implements Serializable {
    public boolean isFollowRedirects() {
        return this.followRedirects;
    }


    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }

        if (!(obj instanceof NextcloudRequest)) {
            return false;
        }


        NextcloudRequest rq = (NextcloudRequest)obj;
        boolean equal;
        equal  = checkEqual("accountName", this.accountName, rq.accountName);
        equal &= checkEqual("header", this.header, rq.header);
        equal &= checkEqual("method", this.method, rq.method);
        equal &= checkEqual("packageName", this.packageName, rq.packageName);
        equal &= checkEqual("parameter", this.parameter, rq.parameter);
        equal &= checkEqual("requestBody", this.requestBody, rq.requestBody);
        equal &= checkEqual("token", this.token, rq.token);
        equal &= checkEqual("url", this.url, rq.url);
        equal &= checkEqual("followRedirects", this.followRedirects, rq.followRedirects);

        return equal;

        //return super.equals(obj);
    }

    private boolean checkEqual(String name, Object o1, Object o2) {
        if(o1 == null && o2 == null) {
            return true;
        }

        if(o1 != null) {
            boolean eq = o1.equals(o2);
            if(!eq) {
                System.err.println("[" + name + " ] Expected: " + o1 + " Was: " + o2);
            }
            return eq;
        } else {
            // o1 == null and o2 != null
            System.err.println("[" + name + " ] Expected: " + o1 + " Was: " + o2);
        }

        return false;
    }
}
Loading