API
The API code is used to login to a Cloud Service Account. We create an instance of that class by calling the constructor RAPI(RService _rService):
RAPI rAPI = new RAPI(rService);
But, if we already have an instance of a service, it is better to call the getAPI method:
RAPI rAPI = rService.getAPI();
The preferred method is the second one, and this is the method that is used in the Demo code. In the above example we created an instance of the RAPI class. You should never create such an instance. This is a generic class, that has code that is used by the super-classes.
There are two ways to login to an account: Normal Login, and OAuth 2 Login. Based on the login type you need to create a different UI and provide different data to the RAPI instance.
Normal Login
Here the user is asked to enter his/her email and password. For that we create a UI with three EditText fields, and a “Login” button. We need to check that the value from those two fields is not an empty string. The code will not crash if they are an empty string, but, there is no need to make such a request.
Once the user taps on the Login button, we pass the value of those fields to the RAPI instance:
rAPI.email = <email>;
rApi.password = <password>;
rApi.name = <name>;
In the Demo code, we also give a value to the variable “name” of the RAPI instance. You do not have to do this, but, if your users can connect to more than one account that variable will help them to distinguish these accounts.
The Login button calls the method checkServices on that RAPI instance:
rAPI.checkServices();
Which Cloud Services use this type of Login?
- iDrive
- MediaFire
- SugarSync
OAuth2 Login
This type of Login requires fewer code to write, as the only thing that you need to do is to create a UI with a WebView instance. Then you create an instance of the RAPIWebViewClient class:
RAPIWebViewClient rAPIWebClient = new RAPIWebViewClient((ROauthService)service, rOauthAPI);
and you set the webClient of the WebView instance to be that instance.
webView.setWebViewClient(rAPIWebClient);
The ROAuthAPI class is a superclass of the RAPI class. The method getAPI() of the Service class will always return the correct instance.
Once again, do not forget to set the name variable of the ROAuthAPI instance.
Which Cloud Services use the OAuth2 Login?
- Box
- Dropbox
- Google Drive
- OneDrive
- ShareFile
- SkyDrive
Even though the Google Drive uses an OAuth2 Login to login to an account. The above code won’t work. Instead you have run the following code:
Rainbow.getInstance().loginToGoogle(service, getActivity());
The Rainbow.getInstance() method returns the singleton instance of the Rainbow class, and “service” is the instance of the GoogleDriveService class. We may say, that connecting to a Google Drive account is a lot easier than connecting to any other accounts. You only need to call that method and in your app Manifest add the following activity:
<activity android:name="com.ismartsolutions.rainbowlibrary.GoogleDriveActivity"
/>
Callbacks
RAPICallback
This callback is used by the RAPI class to inform us for the login status. It is named callback and we set it in the normal way:
rAPI.callback = new RAPICallback(){
@Override
public void APILoginWasSuccessful(RServiceItem sObj) {
}
@Override
public void APISomethingWentWrong(RServiceItem gObj, String errorMessage) {
}
};
or you can set it to this, and then set the class to implement the RAPICallback interface.
If the login is successful, the RServiceItem instance is the account object that you will use in your code to create a CloudManager instance. Also, a message with the name SAVE_SERVICE_ACCOUNTS will be broadcasted with the LocalBroadcastManager.
If there was an error, the errorMessage would provide some useful information about what went wrong.
The login will failed only in two cases: Wrong value of the clientID, clientSecret, and redirectURI or wrong credentials. In the first case, you need to double check their values. In the second case, you need to inform the user.
RAPIStatusCallback
This callback is named statusCallback and we set it as follows:
rAPI.statusCallback = new RAPIStatusCallback(){
@Override
public void onStatusChecked(boolean status) {
}
};
There is no need to use that callback.. It is used by the CloudManager instances to check and refresh the Access Tokens. Also, this callback should not be used during the Login process because it won’t give you any feedback on the error message.
Summary
The API class is perhaps the most important class in the Rainbow SDK. If you follow our Demo code, you only need to override the two methods of the callback, and leave the rest to the SDK. You may need to listen for the message with the name SAVE_SERVICE_ACCOUNTS. The Rainbow SDK is listening for it, and if you have given a filePath to the Rainbow instance, it will save the new account into the local filesystem.