Android Open Source - Geotrackin Drop Box Helper






From Project

Back to project page Geotrackin.

License

The source code is released under:

GNU General Public License

If you think the Android project Geotrackin listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
*    This file is part of GPSLogger for Android.
*//  w  ww .j a  v a 2 s  .c om
*    GPSLogger for Android is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 2 of the License, or
*    (at your option) any later version.
*
*    GPSLogger for Android is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with GPSLogger for Android.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.geotrackin.gpslogger.senders.dropbox;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.android.AndroidAuthSession;
import com.dropbox.client2.session.AccessTokenPair;
import com.dropbox.client2.session.AppKeyPair;
import com.dropbox.client2.session.Session;
import com.dropbox.client2.session.TokenPair;
import com.geotrackin.gpslogger.common.AppSettings;
import com.geotrackin.gpslogger.common.IActionListener;
import com.geotrackin.gpslogger.senders.IFileSender;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;


public class DropBoxHelper implements IActionListener, IFileSender {

    private static final org.slf4j.Logger tracer = LoggerFactory.getLogger(DropBoxHelper.class.getSimpleName());
    final static public String ACCESS_KEY_NAME = "DROPBOX_ACCESS_KEY";
    final static public String ACCESS_SECRET_NAME = "DROPBOX_ACCESS_SECRET";
    final static private Session.AccessType ACCESS_TYPE = Session.AccessType.APP_FOLDER;
    Context ctx;
    DropboxAPI<AndroidAuthSession> dropboxApi;
    IActionListener callback;

    public DropBoxHelper(Context context, IActionListener listener) {
        ctx = context;
        AndroidAuthSession session = buildSession();
        dropboxApi = new DropboxAPI<AndroidAuthSession>(session);
        callback = listener;
    }

    /**
     * Whether the user has authorized GPSLogger with DropBox
     *
     * @return True/False
     */
    public boolean IsLinked() {
        return dropboxApi.getSession().isLinked();
    }

    public boolean FinishAuthorization() {
        AndroidAuthSession session = dropboxApi.getSession();
        if (!session.isLinked() && session.authenticationSuccessful()) {
            // Mandatory call to complete the auth
            session.finishAuthentication();

            // Store it locally in our app for later use
            TokenPair tokens = session.getAccessTokenPair();
            storeKeys(tokens.key, tokens.secret);
            return true;
        }

        return false;
    }


    /**
     * Shows keeping the access keys returned from Trusted Authenticator in a local
     * store, rather than storing user name & password, and re-authenticating each
     * time (which is not to be done, ever).
     *
     * @param key    The Access Key
     * @param secret The Access Secret
     */
    private void storeKeys(String key, String secret) {

        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(ctx);
        SharedPreferences.Editor edit = prefs.edit();
        edit.putString(ACCESS_KEY_NAME, key);
        edit.putString(ACCESS_SECRET_NAME, secret);
        edit.commit();
    }

    private void clearKeys() {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(ctx);
        SharedPreferences.Editor edit = prefs.edit();
        edit.remove(ACCESS_KEY_NAME);
        edit.remove(ACCESS_SECRET_NAME);
        edit.commit();
    }


    private AndroidAuthSession buildSession() {
        int dropboxAppKey = ctx.getResources().getIdentifier("dropbox_appkey", "string", ctx.getPackageName());
        int dropboxAppSecret = ctx.getResources().getIdentifier("dropbox_appsecret", "string", ctx.getPackageName());
        AppKeyPair appKeyPair = new AppKeyPair(ctx.getString(dropboxAppKey), ctx.getString(dropboxAppSecret));
        AndroidAuthSession session;

        String[] stored = getKeys();
        if (stored != null) {
            AccessTokenPair accessToken = new AccessTokenPair(stored[0], stored[1]);
            session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE, accessToken);
        } else {
            session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE);
        }

        return session;
    }

    /**
     * Shows keeping the access keys returned from Trusted Authenticator in a local
     * store, rather than storing user name & password, and re-authenticating each
     * time (which is not to be done, ever).
     *
     * @return Array of [access_key, access_secret], or null if none stored
     */
    private String[] getKeys() {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
        String key = prefs.getString(ACCESS_KEY_NAME, null);
        String secret = prefs.getString(ACCESS_SECRET_NAME, null);
        if (key != null && secret != null) {
            String[] ret = new String[2];
            ret[0] = key;
            ret[1] = secret;
            return ret;
        } else {
            return null;
        }
    }

    public void StartAuthentication(DropBoxAuthorizationActivity dropBoxAuthorizationActivity) {
        // Start the remote authentication
        dropboxApi.getSession().startAuthentication(dropBoxAuthorizationActivity);
    }

    public void UnLink() {
        // Remove credentials from the session
        dropboxApi.getSession().unlink();

        // Clear our stored keys
        clearKeys();
    }

    @Override
    public void UploadFile(List<File> files) {

        for (File f : files) {
            UploadFile(f.getName());
        }
    }

    public void UploadFile(String fileName) {
        Thread t = new Thread(new DropBoxUploadHandler(fileName, dropboxApi, this));
        t.start();
    }

    public void OnComplete() {
        callback.OnComplete();
    }

    public void OnFailure() {

    }

    @Override
    public boolean accept(File dir, String name) {
        return true;
    }

    public class DropBoxUploadHandler implements Runnable {
        DropboxAPI<AndroidAuthSession> api;
        String fileName;
        IActionListener helper;

        public DropBoxUploadHandler(String file, DropboxAPI<AndroidAuthSession> dbApi, IActionListener dbHelper) {
            fileName = file;
            api = dbApi;
            helper = dbHelper;
        }

        public void run() {
            try {
                File gpsDir = new File(AppSettings.getGpsLoggerFolder());
                File gpxFile = new File(gpsDir, fileName);

                FileInputStream fis = new FileInputStream(gpxFile);
                DropboxAPI.Entry upEntry = api.putFileOverwrite(gpxFile.getName(), fis, gpxFile.length(), null);
                tracer.info("DropBox uploaded file rev is: " + upEntry.rev);
                helper.OnComplete();
            } catch (Exception e) {
                tracer.error("DropBoxHelper.UploadFile", e);
            }
        }
    }

}




Java Source Code List

com.geotrackin.gpslogger.Faqtivity.java
com.geotrackin.gpslogger.GeneralLocationListener.java
com.geotrackin.gpslogger.GpsLoggingService.java
com.geotrackin.gpslogger.GpsMainActivity.java
com.geotrackin.gpslogger.IGpsLoggerServiceClient.java
com.geotrackin.gpslogger.NavigationDrawerFragment.java
com.geotrackin.gpslogger.StartupReceiver.java
com.geotrackin.gpslogger.common.AppSettings.java
com.geotrackin.gpslogger.common.GpsRollingFileAppender.java
com.geotrackin.gpslogger.common.IActionListener.java
com.geotrackin.gpslogger.common.IMessageBoxCallback.java
com.geotrackin.gpslogger.common.OpenGTSClient.java
com.geotrackin.gpslogger.common.RejectionHandler.java
com.geotrackin.gpslogger.common.Session.java
com.geotrackin.gpslogger.common.Utilities.java
com.geotrackin.gpslogger.common.FileDialog.FileDialog.java
com.geotrackin.gpslogger.common.FileDialog.SelectionMode.java
com.geotrackin.gpslogger.loggers.FileLoggerFactory.java
com.geotrackin.gpslogger.loggers.Gpx10FileLogger.java
com.geotrackin.gpslogger.loggers.IFileLogger.java
com.geotrackin.gpslogger.loggers.Kml22FileLogger.java
com.geotrackin.gpslogger.loggers.OpenGTSLogger.java
com.geotrackin.gpslogger.loggers.PlainTextFileLogger.java
com.geotrackin.gpslogger.loggers.customurl.CustomUrlLoggerActivity.java
com.geotrackin.gpslogger.loggers.customurl.HttpUrlLogger.java
com.geotrackin.gpslogger.loggers.nmea.NmeaFileLogger.java
com.geotrackin.gpslogger.senders.AlarmReceiver.java
com.geotrackin.gpslogger.senders.FileSenderFactory.java
com.geotrackin.gpslogger.senders.IFileSender.java
com.geotrackin.gpslogger.senders.ZipHelper.java
com.geotrackin.gpslogger.senders.dropbox.DropBoxAuthorizationActivity.java
com.geotrackin.gpslogger.senders.dropbox.DropBoxHelper.java
com.geotrackin.gpslogger.senders.email.AutoEmailActivity.java
com.geotrackin.gpslogger.senders.email.AutoEmailHelper.java
com.geotrackin.gpslogger.senders.email.Mail.java
com.geotrackin.gpslogger.senders.ftp.AutoFtpActivity.java
com.geotrackin.gpslogger.senders.ftp.FtpHelper.java
com.geotrackin.gpslogger.senders.ftp.Ftp.java
com.geotrackin.gpslogger.senders.gdocs.GDocsHelper.java
com.geotrackin.gpslogger.senders.gdocs.GDocsSettingsActivity.java
com.geotrackin.gpslogger.senders.opengts.GpxReader.java
com.geotrackin.gpslogger.senders.opengts.OpenGTSActivity.java
com.geotrackin.gpslogger.senders.opengts.OpenGTSHelper.java
com.geotrackin.gpslogger.senders.osm.OSMAuthorizationActivity.java
com.geotrackin.gpslogger.senders.osm.OSMHelper.java
com.geotrackin.gpslogger.settings.GeneralSettingsActivity.java
com.geotrackin.gpslogger.settings.LoggingSettingsActivity.java
com.geotrackin.gpslogger.settings.UploadSettingsActivity.java
com.geotrackin.gpslogger.shortcuts.ShortcutCreate.java
com.geotrackin.gpslogger.shortcuts.ShortcutStart.java
com.geotrackin.gpslogger.shortcuts.ShortcutStop.java
com.geotrackin.gpslogger.views.GenericViewFragment.java
com.geotrackin.gpslogger.views.GpsBigViewFragment.java
com.geotrackin.gpslogger.views.GpsDetailedViewFragment.java
com.geotrackin.gpslogger.views.GpsSimpleViewFragment.java
com.geotrackin.gpslogger.views.component.ToggleComponent.java