How Money Manager Ex Uses CloudRail
What is Money Manager Ex (MMEX)?
MMEX is a personal finance application that is available for various platforms such as Windows, Mac, Linux and Android. Its main purpose is to make you understand how and where you spend your money by tracking your income and expenditures throughout the month. The Android application allows you to track transactions and associate them with payees or categories in order to create meaningful reports. Furthermore, it provides you with a complete overview of your current financial situation by keeping track of your money. Be it invested in stocks or other financial instruments. Since the application is also available on desktop computers, you can synchronize your data with the cloud storage service of your choice. This is where CloudRail comes into play.
How does it use CloudRail?
MMEX uses CloudRail as its interface to several cloud storage providers, namely Dropbox, Google Drive, OneDrive and Box. The synchronization with the cloud is invoked in three different ways:
- Whenever data is updated locally, an automatic sync is triggered after a delay of 30 seconds
- A manual sync can be started by the user
- A scheduled sync is triggered on a regular basis to keep data up to date even when no local change occurs
One of the nice things about Money Manager Ex is that it is open source. Meaning that you can just simply take a look at it’s GitHub page to view the code. This is a great way to see how CloudRail is used in an App. We are going to, in the following paragraphs, take a closer look at this implementation.
For the purpose of syncing, MMEX uses a SyncService which extends the IntentService and is invoked whenever a sync action is triggered. An IntentService is a class that handles asynchronous requests whenever it receives an Intent. Requests can be sent by using startService(Intent) with the fitting Intent as a parameter. This is illustrated by the following code sample:
Every request is handled in its own worker thread. However, only one is processed at a time, meaning that using such an IntentService ensures that only one sync action is performed at a time and we don’t run into inconsistencies or race conditions. Every class extending the IntentService class has to implement onHandleIntent(Intent), so does the SyncService. The following happens there: Read the path to the local and to the remote file from the Intent, get a File resource for the local File, retrieve metadata for the remote file by invoking the SyncManager (will be discussed later) and decide based on the chosen action what will be done next. Simplified source code looks like this:
Let us quickly define what these actions are supposed to do. The download action replaces the local file with the remote file in order to get updates that happened in the meantime on a different device. The upload action basically does the opposite, it replaces the remote file with the local file. The sync action on the other hand compares the last updated date of both files in order to decide which one should be replaced. So the only two pieces missing are a download and an upload functionality for all the cloud storage services. Luckily, these are implemented in the SyncManager.
The SyncManager groups all functionalities that are relevant for syncing and is the only class that actually calls the CloudRail library. It is responsible for initializing the cloud services with their credentials, to persist the authentication information which is obtained after a user logged into one of the services and to keep track of which service is selected as the service to which the data is synced. Apart from that it also provides the upload and download method we need to finish our example:
I hope this gave you a rough overview on how CloudRail could fit into a real world use case. As you can see, the part of the code which communicates with the services can be replaced with only one line of code for uploading and one for downloading a file.
With this quick overview, you should hopefully have an idea of how you can use Cloudrail within your own project. With CloudRail, we don;t just have a library for Android. We also have iOS, Node.js and Java libraries. To get started with your own CloudRail integration, simply click on your platform. CloudRail is completely free to use, for both personal and commercial projects.