You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Tobias Kaminsky 5a32f97779
Bump gradle from 3.4.2 to 3.5.0 (#93)
пре 16 часа
.tx Add transifex config пре 1 година
doc update readme пре 1 година
gradle/wrapper Bump gradle from 3.4.2 to 3.5.0 пре 16 часа
scripts Bump gradle from 3.4.2 to 3.5.0 пре 16 часа
src [tx-robot] updated from transifex пре 1 недеља
.drone.yml initial drone пре 4 недеља
.gitignore Change build script пре 1 година
LICENSE Create LICENSE пре 1 година
NextcloudSingleSignOn.png Update documentation / add flow diagram пре 1 година
NextcloudSingleSignOn.xml Update documentation / add flow diagram пре 1 година
README.md Update ReadMe to include Uri.encode() in the examples пре 3 недеља
build.gradle Bump gradle from 3.4.2 to 3.5.0 пре 16 часа
detekt.yml initial drone пре 4 недеља
gradlew Change build script пре 1 година
gradlew.bat Change build script пре 1 година
proguard-rules.pro initial upload пре 5 година
spotbugs-filter.xml initial drone пре 4 недеља

README.md

Nextcloud Single Sign On

Codacy Badge

This library allows you to use accounts as well as the network stack provided by the nextcloud files app. Therefore you as a developer don’t need to worry about asking the user for credentials as well as you don’t need to worry about self-signed ssl certificates, two factor authentication, save credential storage etc.

Please note that the user needs to install the nextcloud files app in order to use those features. While this might seem like a “no-go” for some developers, we still think that using this library is worth consideration as it makes the account handling much faster and safer.

How to use this library

1) Add this library to your project

repositories {
    ...
    maven { url "https://jitpack.io" }

}

dependencies {
	implementation "com.github.nextcloud:Android-SingleSignOn:0.4.1"
}

2) To choose an account, include the following code in your login dialog:

From an Activity

private void openAccountChooser() {
        try {
            AccountImporter.pickNewAccount(this);
        } 
        catch (NextcloudFilesAppNotInstalledException e) {
            UiExceptionManager.showDialogForException(this, e);
        } catch (AndroidGetAccountsPermissionNotGranted e) {
            UiExceptionManager.showDialogForException(this, e);
        }
}

From a Fragment

private void openAccountChooser() {
        try {
            AccountImporter.pickNewAccount(currentFragment);
        } catch (NextcloudFilesAppNotInstalledException e) {
            UiExceptionManager.showDialogForException(this, e);
        } catch (AndroidGetAccountsPermissionNotGranted e) {
            UiExceptionManager.showDialogForException(this, e);
        }
}

3) To handle the result of the Account Chooser, include the following:

From an Activity

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
            AccountImporter.onActivityResult(requestCode, resultCode, data, this, new AccountImporter.IAccountAccessGranted() {

                    NextcloudAPI.ApiConnectedListener callback = new NextcloudAPI.ApiConnectedListener() {
                        @Override
                        public void onConnected() {
                            // ignore this one..
                        }

                        @Override
                        public void onError(Exception ex) {
                            // TODO handle errors
                        }
                    };

                    @Override
                    public void accountAccessGranted(SingleSignOnAccount account) {

                        Context l_context = getApplicationContext();

                        // As this library supports multiple accounts we created some helper methods if you only want to use one.
                        // The following line stores the selected account as the "default" account which can be queried by using
                        // the SingleAccountHelper.getCurrentSingleSignOnAccount(context) method
                        SingleAccountHelper.setCurrentAccount(l_context, account.name);

                        // Get the "default" account
                        SingleSignOnAccount ssoAccount = null;
                        try
                        {
                            ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(l_context);
                        } catch (NextcloudFilesAppAccountNotFoundException e)
                        {
                            UiExceptionManager.showDialogForException(l_context, e);
                        } catch (NoCurrentAccountSelectedException e)
                        {
                            UiExceptionManager.showDialogForException(l_context, e);
                        }
                        
                        NextcloudAPI nextcloudAPI = new NextcloudAPI(l_context, ssoAccount, new GsonBuilder().create(), callback);

                        // TODO ... (see code in section 4 and below)
                    }
                });
}

From a Fragment

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    AccountImporter.onActivityResult(requestCode, resultCode, data, LoginDialogFragment.this, new AccountImporter.IAccountAccessGranted() {
    
        NextcloudAPI.ApiConnectedListener callback = new NextcloudAPI.ApiConnectedListener() {
        @Override
        public void onConnected() { 
            // ignore this one..
        }

        @Override
        public void onError(Exception ex) { 
            // TODO handle errors
        }
    };
    
        @Override
        public void accountAccessGranted(SingleSignOnAccount account) {
            // As this library supports multiple accounts we created some helper methods if you only want to use one.
            // The following line stores the selected account as the "default" account which can be queried by using 
            // the SingleAccountHelper.getCurrentSingleSignOnAccount(context) method
            SingleAccountHelper.setCurrentAccount(getActivity(), account.name);
            
            // Get the "default" account
            SingleSignOnAccount ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context);
            NextcloudAPI nextcloudAPI = new NextcloudAPI(context, ssoAccount, new GsonBuilder().create(), callback);

            // TODO ... (see code in section 4 and below)
        }
    });
}

From both an Activity and Fragment

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    AccountImporter.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}

// Complete example: https://github.com/nextcloud/news-android/blob/master/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/LoginDialogFragment.java

4) How to get account information?

// If you stored the "default" account using setCurrentAccount(...) you can get the account by using the following line:
SingleSignOnAccount ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context);

// Otherwise (for multi-account support): (you'll have to keep track of the account names yourself. Note: this has to be the name of SingleSignOnAccount.name)
AccountImporter.getSingleSignOnAccount(context, accountName);

// ssoAccount.name // Name of the account used in the android account manager
// ssoAccount.username
// ssoAccount.token
// ssoAccount.url

5) How to make a network request?

You’ll notice that there is an callback parameter in the constructor of the NextcloudAPI.

    public NextcloudAPI(Context context, SingleSignOnAccount account, Gson gson, ApiConnectedListener callback) {

You can use this callback to subscribe to errors that might occur during the initialization of the API. You can start making requests to the API as soon as you instantiated the NextcloudAPI object. For a minimal example to get started (without retrofit) take a look at section 5.2. The callback method onConnected will be called once the connection to the files app is established. You can start making calls to the api before that callback is fired as the library will queue your calls until the connection is established.

5.1) Using Retrofit

5.1.1) Before using this single sign on library, your interface for your retrofit API might look like this:

    public interface API {

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

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

        @POST("feeds")
        Call<List<Feed>> createFeed(@Body Map<String, Object> feedMap);

        @DELETE("feeds/{feedId}")
        Completable deleteFeed(@Path("feedId") long feedId);

        …
    }

You might instantiate your retrofit API by using something like this:

    public class ApiProvider {

        private API mApi;

        public ApiProvider() {
            mApi = retrofit.create(API.class);
        }
    }

5.1.2) Use of new API using the nextcloud app network stack

    public class ApiProvider {

        private API mApi;

        public ApiProvider(NextcloudAPI.ApiConnectedListener callback) {
           SingleSignOnAccount ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context);
           NextcloudAPI nextcloudAPI = new NextcloudAPI(context, ssoAccount, new GsonBuilder().create(), callback);
           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 example for more information.

5.2) Without Retrofit

NextcloudAPI provides a method called performNetworkRequest(NextcloudRequest request) that allows you to handle the server response yourself.

    public class MyActivity extends AppCompatActivity {
    
        private NextcloudAPI mNextcloudAPI;

        @Override
        protected void onStart() {
            super.onStart();
            try {
                SingleSignOnAccount ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(this);
                mNextcloudAPI = new NextcloudAPI(this, ssoAccount, new GsonBuilder().create(), apiCallback);

                // Start download of file in background thread (otherwise you'll get a NetworkOnMainThreadException)
                new Thread() {
                    @Override
                    public void run() {
                        downloadFile();
                    }
                }.start();
            } catch (NextcloudFilesAppAccountNotFoundException e) {
                // TODO handle errors
            } catch (NoCurrentAccountSelectedException e) {
                // TODO handle errors
            }
        }

        @Override
        protected void onStop() {
            super.onStop();
            // Close Service Connection to Nextcloud Files App and
            // disconnect API from Context (prevent Memory Leak)
            mNextcloudAPI.stop();
        }
        
        private NextcloudAPI.ApiConnectedListener apiCallback = new NextcloudAPI.ApiConnectedListener() {
            @Override
            public void onConnected() {
                // ignore this one..
            }

            @Override
            public void onError(Exception ex) {
                // TODO handle error in your app
            }
        };

        private void downloadFile() {
            NextcloudRequest nextcloudRequest = new NextcloudRequest.Builder()
                    .setMethod("GET")
                    .setUrl(Uri.encode("/remote.php/webdav/sample movie.mp4","/"))
                    .build();

            try {
                InputStream inputStream = mNextcloudAPI.performNetworkRequest(nextcloudRequest);
                while(inputStream.available() > 0) {
                    inputStream.read();
                    // TODO do something useful with the data here..
                    // like writing it to a file..?
                }
                inputStream.close();
            } catch (Exception e) {
                // TODO handle errors
            }
        }
    }

6) WebDAV

The following WebDAV Methods are supported: PROPFIND / MKCOL

The following examples shows how to use the PROPFIND method. With a depth of 0.

List<String>depth = new ArrayList<>();
depth.add("0");
header.put("Depth", depth);

NextcloudRequest nextcloudRequest = new NextcloudRequest.Builder()
        .setMethod("PROPFIND")
        .setHeader(header)
        .setUrl(Uri.encode("/remote.php/webdav/"+remotePath,"/"))
        .build();

Additional Info:

In case that you require some sso features that were introduced in a specific nextcloud files app version, you can run a simple version check using the following helper method:

int MIN_NEXTCLOUD_FILES_APP_VERSION_CODE = 30030052;

if (VersionCheckHelper.verifyMinVersion(context, MIN_NEXTCLOUD_FILES_APP_VERSION_CODE)) {
   // Version requirement is satisfied! 
}

Nextcloud Conference 2018 Talk (5min)

Nextcloud Single Sign On for Android David Luhmer

Video

Examples

Flow Diagram

Note that the “Make network request” section in the diagram only shows the workflow if you use the “retrofit” api.

Translations

We manage translations via Transifex. So just request joining the translation team for Android on the site and start translating. All translations will then be automatically pushed to this repository, there is no need for any pull request for translations.