Kyle Miller walks through the steps of integrating Dropbox in his home/SMB Android surveillance system. Get the source code for his Android app.
In the first installment in my series about creating a simple home or SMB video surveillance system using an Android device, I presented my ideas. In part two, I described how I developed a continuous video recorder and got it to work. For the final installment, I will integrate Dropbox in my Android surveillance app.
After I downloaded the Dropbox Android SDK, I read the documentation and got to work. I noticed I needed to create a Dropbox "app" on the Dropbox site. There are two types of access levels you can choose from when you create a Dropbox app:
- App Folder: Your app only has access to a folder created for your app.
- Full Access: Your app has access to a user's full Dropbox account. Dropbox states you will need to provide a good reason for needing this type of access before they will grant it.
I chose App Folder as my access level.
Once the app was created on the Dropbox site, I began working on the code. My initial idea was to create a Service that would run in the background and perform the uploads; once I looked over the DBRoulette sample that came with the SDK download, I saw Dropbox already had an UploadPicture class that extends AsyncTask and will upload any File you pass in to your Dropbox folder. I decided to re-use this existing code instead of writing my own.
The plan was, every time the MediaRecorder was reset, I would start an Upload task for the video that was just saved on the SD card. The only thing left to figure out was how to handle Dropbox authentication
The DBRoulette sample was helpful again -- I simply copied the code responsible for authentication from DBRouelette.java into my application. Authentication works as follows:
1. The buildSession() method creates an AndroidAuthSession object either by using previously stored token keys or by creating a new instance.
2. A new DropBoxAPI object is created using the AndroidAuthSession returned from the buildSession().
3. The DBRoulette example executes a method called checkAppKeySetup(), which is used to verify that you have set up the Manifest XML file correctly; this involves adding the following Activity to your Manifest (and replacing <YOUR_APP_KEY>):
<intent-filter><!-- Change this to be db- followed by your app key →<data android:scheme="db-<YOUR_APP_KEY>" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
4. The startAuthentication() method is called on the AndroidAuthSession object created earlier; this launches a Dropbox login screen for authentication (if this is the first time).
5. After logging in to your Dropbox account, you are taken to another screen where you have to "allow" the app being used access to your Dropbox account. On future authentication attempts, this will be the first screen the user sees instead of a Dropbox login screen (unless the user has logged out).
6. Once the application has been granted access, the onResume method of your Activity is called, and you can check the status of the authentication by calling the authenticationSuccessful() method of the AndroidAuthSession object. If authentication was successful, then to finish authentication you must:
1. Call finishAuthentication() on the AndroidAuthSession object.
2. Grab the TokenPairs by calling getAccessTokenPair() on the AndroidAuthSession object.
3. Store the auth keys for future use.
Tests and tweaks
The app was finally complete. In the first test, it ran for 12 hours and only ate up 110 MB on my SD card and Dropbox account. Six of those hours were during the night when everything was pitch black, and each file was around 150 kb, but I was still pleased with the results.
Since I only have 2.5 GB of storage on Dropbox, which would likely get used up in a few days, I plan to make two tweaks:
- I think it would be beneficial to have the application delete the videos from the SD card as they are uploaded to ensure the SD card will never run out of storage.
- I will likely switch from using Dropbox to using my hosted VPS for storage by uploading the videos over FTP. I have found a free java library that looks like it will work.
Get the source
I posted the full source code for this Android application on GitHub. I hope you'll be able to re-purpose my source code for your own home or SMB Android surveillance system.