/**
   * Displays some useful info about the account, to demonstrate that we've successfully logged in
   *
   * @param account
   */
  public void action(DropboxAPI.Account account) {

    synchronized (this) {
      if (account != null) {
        Intent intent = getIntent();
        int contact_id = intent.getIntExtra("action", -1);

        DBHelper mHelper = new DBHelper(this);
        mHelper.getReadableDatabase().close();
        File data = Environment.getDataDirectory();
        String currentDBPath =
            "/data/edu.stanford.mobisocial.dungbeetle/databases/" + DBHelper.DB_NAME;
        File currentDB = new File(data, currentDBPath);

        String newDBPath =
            "/data/edu.stanford.mobisocial.dungbeetle/databases/" + DBHelper.DB_NAME + ".new";
        File newDB = new File(data, newDBPath);

        if (contact_id == 0) {
          api.delete("dropbox", "/" + DBHelper.DB_NAME);
          api.putFile("dropbox", "/", currentDB);
          showToast("backed up");
        } else if (contact_id == 1) {
          try {
            downloadDropboxFile(DBHelper.DB_NAME, newDB);
            mHelper.importDatabase(newDBPath);
            showToast("restored");
          } catch (Exception e) {
            showToast("could not restore");
          }
        }
        finish();
      }
    }
  }
  private void getAccountInfo() {
    if (api.isAuthenticated()) {
      // If we're already authenticated, we don't need to get the login info

      dialog.show();
      LoginAsyncTask login = new LoginAsyncTask(this, null, null, getConfig(), dialog);
      login.execute();
    } else {

      String email = mLoginEmail.getText().toString();

      if (email.length() < 5 || email.indexOf("@") < 0 || email.indexOf(".") < 0) {
        showToast("Error, invalid e-mail");
        return;
      }

      String password = mLoginPassword.getText().toString();

      if (password.length() < 6) {
        showToast("Error, password too short");
        return;
      }

      // It's good to do Dropbox API (and any web API) calls in a separate thread,
      // so we don't get a force-close due to the UI thread stalling.

      dialog.show();
      LoginAsyncTask login = new LoginAsyncTask(this, email, password, getConfig(), dialog);
      login.execute();
    }
  }
 protected Config getConfig() {
   if (mConfig == null) {
     mConfig = api.getConfig(null, false);
     // TODO On a production app which you distribute, your consumer
     // key and secret should be obfuscated somehow.
     mConfig.consumerKey = CONSUMER_KEY;
     mConfig.consumerSecret = CONSUMER_SECRET;
     mConfig.server = "api.dropbox.com";
     mConfig.contentServer = "api-content.dropbox.com";
     mConfig.port = 80;
   }
   return mConfig;
 }
 /**
  * This handles authentication if the user's token & secret are stored locally, so we don't have
  * to store user-name & password and re-send every time.
  */
 protected boolean authenticate() {
   if (mConfig == null) {
     mConfig = getConfig();
   }
   String keys[] = getKeys();
   if (keys != null) {
     mConfig = api.authenticateToken(keys[0], keys[1], mConfig);
     if (mConfig != null) {
       return true;
     }
   }
   showToast("Failed user authentication for stored login tokens.");
   clearKeys();
   setLoggedIn(false);
   return false;
 }
  private boolean downloadDropboxFile(String dbPath, File localFile) throws IOException {

    BufferedInputStream br = null;
    BufferedOutputStream bw = null;

    try {
      if (!localFile.exists()) {
        localFile.createNewFile(); // otherwise dropbox client will fail silently
        // showToast("initial length: " + localFile.length());
      }

      FileDownload fd = api.getFileStream("dropbox", "/" + dbPath, null);
      br = new BufferedInputStream(fd.is);
      bw = new BufferedOutputStream(new FileOutputStream(localFile));

      byte[] buffer = new byte[4096];
      int read;
      while (true) {
        read = br.read(buffer);
        if (read <= 0) {
          break;
        }
        bw.write(buffer, 0, read);
      }
    } finally {
      // in finally block:
      if (bw != null) {
        bw.close();
      }
      if (br != null) {
        br.close();
      }
    }
    // showToast("length: " + localFile.length());
    return true;
  }