Android Open Source - PomodoroTimer Dropbox Link






From Project

Back to project page PomodoroTimer.

License

The source code is released under:

GNU General Public License

If you think the Android project PomodoroTimer 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

package com.example.pomodorotimer;
// w w w. j a v  a2s  .c om
import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import com.dropbox.sync.android.DbxAccountManager;
import com.dropbox.sync.android.DbxFile;
import com.dropbox.sync.android.DbxFileSystem;
import com.dropbox.sync.android.DbxPath;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * The Class DropboxLink. Contains all operations realted to Dropbox.
 */
public class DropboxLink extends Activity implements OnClickListener
{
  
  /** The Constant appKey. */
  private static final String appKey = "arggjbm1ulqdohf";
    
    /** The Constant appSecret. */
    private static final String appSecret = "e4ppvr584228tzr";

    /** The Constant REQUEST_LINK_TO_DBX. */
    private static final int REQUEST_LINK_TO_DBX = 0;
    
    /** The log's tag. */
    private static final String TAG = "Dropbox";

    /** The link button. Used for link/unlink account. */
    private Button mLinkButton;
    
    /** The sync button. Used for synchronization with Dropbox. */
    private Button mSyncButton;
    
    /** The account manager. Used for operations on Dropbox account. */
    private DbxAccountManager mDbxAcctMgr;
    
    /** The database. Used for reading data from database. */
    private PomodoroTimerDBHelper db;
    
  /* (non-Javadoc)
   * @see android.app.Activity#onCreate(android.os.Bundle)
   */
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dropbox_link);
        mLinkButton = (Button) findViewById(R.id.link_button);
        mSyncButton = (Button) findViewById(R.id.sync_button);

        mDbxAcctMgr = DbxAccountManager.getInstance(getApplicationContext(), appKey, appSecret);
        
        mLinkButton.setOnClickListener(this);
        mSyncButton.setOnClickListener(this);
        
        db = new PomodoroTimerDBHelper(getApplicationContext());
    }

  /* (non-Javadoc)
   * @see android.app.Activity#onResume()
   */
  @Override
  protected void onResume() {
    super.onResume();
    if (mDbxAcctMgr.hasLinkedAccount()) {
        showLinkedView();
    } else {
      showUnlinkedView();
    }
  }

    /**
     * Shows linked view: 
     * - user can unlink account,
     * - user can sync data with Dropbox.
     */
    private void showLinkedView() {
        mLinkButton.setText(R.string.unlink_button);
        mSyncButton.setVisibility(View.VISIBLE);
    }

    /**
     * Shows unlinked view:
     * - user can link account.
     */
    private void showUnlinkedView() {
      mLinkButton.setText(R.string.link_button);
      mSyncButton.setVisibility(View.GONE);
    }

    /**
     * On click unlinks from Dropbox.
     */
    private void onClickUnlinkToDropbox() {
        mDbxAcctMgr.unlink();
    }

    /**
     * On click links to Dropbox.
     */
    private void onClickLinkToDropbox() {
        mDbxAcctMgr.startLink((Activity)this, REQUEST_LINK_TO_DBX);
    }
    
    /* (non-Javadoc)
     * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_LINK_TO_DBX) {
            if (resultCode == Activity.RESULT_OK) {
                showLinkedView();
            } else {
                showUnlinkedView();
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

  /* (non-Javadoc)
   * @see android.view.View.OnClickListener#onClick(android.view.View)
   */
    /**
   * Implementation of onClick() method.
   * Supports all activity buttons:
   * linkButton:
   * - if there's no link with Dropbox, allows linking,
   * - if there's link with Dropbox, allows unlinking,
   * syncButton:
   * - starts method generating CSV files and saving them on Dropbox account.
   */
  @Override
  public void onClick(View v)
  {
    switch(v.getId())
    {
        case R.id.link_button:
          if (mDbxAcctMgr.hasLinkedAccount()) {
            onClickUnlinkToDropbox();
            showUnlinkedView();
          } else {
          onClickLinkToDropbox();
          }
          
        break;
        case R.id.sync_button:
      try
      {
        exportTheDB();
        Log.i(TAG, "Syncing finished!");
      } catch (IOException e)
      {
        Log.e(TAG, "Cannot export database to Dropbox");
        e.printStackTrace();
      }
      
          break;
    }
    
  }
  
  /**
   * Export the database's tables to CSV files and saves them on linked Dropbox account.
   *
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private void exportTheDB() throws IOException
  {
      Calendar cal = Calendar.getInstance();
      String TimeStampDB = String.format(Locale.getDefault(),"%04d-%02d-%02d_%02d:%02d:%02d_", 
          cal.get(Calendar.YEAR),cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH),
          cal.get(Calendar.HOUR_OF_DAY),cal.get(Calendar.MINUTE),cal.get(Calendar.SECOND)); 
                  
      
      DbxPath pomodorosTablePath = new DbxPath(DbxPath.ROOT, TimeStampDB + "POMODOROSTABLE" + ".csv");
      DbxPath tagsTablePath = new DbxPath(DbxPath.ROOT, TimeStampDB + "TAGSTABLE" + ".csv");
      DbxPath pomodorostagTablePath = new DbxPath(DbxPath.ROOT, TimeStampDB + "POMODOROSTAGTABLE" + ".csv");
      
      DbxFileSystem dbxFs = DbxFileSystem.forAccount(mDbxAcctMgr.getLinkedAccount());
      
      if (!dbxFs.exists(pomodorosTablePath)) {
            DbxFile csvFile = dbxFs.create(pomodorosTablePath);
            try {
              PomodoroClass pomodoro;
              List<PomodoroClass>  list = db.getAllPomodoros();
              String data = "id,startdate,stopdate,starthour,stophour,numinrow\n";
              csvFile.writeString(data);
              
              for(int i = 0; i < list.size(); i++)
              {
                pomodoro = list.get(i);
                data = pomodoro.getID() + "," + 
                    pomodoro.getStartDate() + "," +  pomodoro.getStopDate() + "," +  
                    pomodoro.getStartHour() + "," +  pomodoro.getStopHour() + "," +  
                    pomodoro.getNumInRow() + "\n";

                csvFile.appendString(data);
              }
            } finally {
                csvFile.close();
                Log.i(TAG, "Table with pomodoros exported.");
            }
        }
      
      if (!dbxFs.exists(tagsTablePath)) {
            DbxFile csvFile = dbxFs.create(tagsTablePath);
            try {
              TagClass tag;
              List<TagClass>  list = db.getAllTags();
              String data = "id,tagname,lastused\n";
              csvFile.writeString(data);
              
              for(int i = 0; i < list.size(); i++)
              {
                tag = list.get(i);
                data = tag.getID() + "," + 
                    tag.getTagName() + "," + 
                    tag.getLastUsed() + "\n" ;
 
                csvFile.appendString(data);
              }
            } finally {
                csvFile.close();
                Log.i(TAG, "Table with tags exported.");
            }
        }
      
      if (!dbxFs.exists(pomodorostagTablePath)) 
      {
      DbxFile csvFile = dbxFs.create(pomodorostagTablePath);
            try {
              PomodorosTagClass pomodorostag;
              List<PomodorosTagClass> list = db.getAllPomodorosTags();
              String data = "id,pomodoroid,tagid\n";
              csvFile.writeString(data);
              
              for(int i = 0; i < list.size(); i++)
              {
                pomodorostag = list.get(i);
                data = pomodorostag.getID() + "," + 
                    pomodorostag.getPomodoroID() + "," + 
                    pomodorostag.getTagID() + "\n" ;
 
                csvFile.appendString(data);
              }              
            } finally {
                csvFile.close();
                Log.i(TAG, "Table with pomodoros' tags exported.");
            }
        }
  }
}




Java Source Code List

com.example.pomodorotimer.About.java
com.example.pomodorotimer.DropboxLink.java
com.example.pomodorotimer.MainActivity.java
com.example.pomodorotimer.PomodoroClass.java
com.example.pomodorotimer.PomodoroContentProvider.java
com.example.pomodorotimer.PomodoroDetails.java
com.example.pomodorotimer.PomodoroEdit.java
com.example.pomodorotimer.PomodoroList.java
com.example.pomodorotimer.PomodoroTimerDBContract.java
com.example.pomodorotimer.PomodoroTimerDBHelper.java
com.example.pomodorotimer.PomodorosTagClass.java
com.example.pomodorotimer.SettingsActivity.java
com.example.pomodorotimer.TagClass.java
com.example.pomodorotimer.TagDetails.java
com.example.pomodorotimer.TagEdit.java
com.example.pomodorotimer.TagList.java