Android Open Source - SpeechWriter Main Activity






From Project

Back to project page SpeechWriter.

License

The source code is released under:

MIT License

If you think the Android project SpeechWriter 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 edu.psu.rcy5017.speechwriter.activity;
/*from   w  ww.j  a  v a2  s. c om*/
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutionException;

import edu.psu.rcy5017.speechwriter.R;
import edu.psu.rcy5017.speechwriter.adapter.TabsPagerAdapter;
import edu.psu.rcy5017.speechwriter.constant.DefaultValues;
import edu.psu.rcy5017.speechwriter.controller.AudioCntl;
import edu.psu.rcy5017.speechwriter.datasource.NoteCardDataSource;
import edu.psu.rcy5017.speechwriter.datasource.SpeechRecordingDataSource;
import edu.psu.rcy5017.speechwriter.model.NoteCard;
import edu.psu.rcy5017.speechwriter.model.SpeechRecording;
import edu.psu.rcy5017.speechwriter.task.CreateSpeechRecordingTask;
import edu.psu.rcy5017.speechwriter.task.GetAllTask;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

/**
 * The activity that displays notes and speech time, while the speaker is giving a speech.
 * @author Ryan Yosua
 *
 */
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
    private static final String TAG = "MainActivity";
    
    private AudioCntl audioCntl = AudioCntl.INSTANCE;    
    private NoteCardDataSource noteCarddatasource;
    private long speechID;
    private ViewPager viewPager;    
    private TextView timerText;
    private SpeechRecordingDataSource speechRecordingdatasource;
    private int seconds;
      
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Log.d(TAG, "Main activity started");
        
        // Get speechId from intent.
        final Intent intent = this.getIntent();
        speechID = intent.getLongExtra("id", DefaultValues.DEFAULT_LONG_VALUE);
        
        noteCarddatasource = new NoteCardDataSource(this);
        speechRecordingdatasource = new SpeechRecordingDataSource(this);
        
        List<NoteCard> notecards = null;
        try {
            notecards = new GetAllTask<NoteCard>(noteCarddatasource, speechID).execute().get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        
        if (notecards != null) {
            viewPager = (ViewPager) findViewById(R.id.pager);
            final ActionBar actionBar = getActionBar();
            final TabsPagerAdapter tabAdapter = new TabsPagerAdapter(getSupportFragmentManager(), notecards);
     
            viewPager.setAdapter(tabAdapter);
            actionBar.setHomeButtonEnabled(false);
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        
             
            // Create a tab for each note card.
            for (NoteCard noteCard : notecards) {
                actionBar.addTab(actionBar.newTab().setText(noteCard.getTitle())
                        .setTabListener(this));
            }
            
            // On swiping the viewpager make respective tab selected.
            viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    // on changing the page
                    // make respected tab selected
                    actionBar.setSelectedNavigationItem(position);
                }
             
                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                    // Do nothing.
                }
             
                @Override
                public void onPageScrollStateChanged(int arg0) {
                    // Do nothing.
                }
            });
            
            // Set initial time to 0.
            timerText = (TextView) findViewById(R.id.timer_text);
            seconds = 0;
            
            // Start the speech recording.
            startRecording();
            
            // Start the timer.
            Timer speechTimer = new Timer();
            UpdateSpeechTimerTask updateTask = new UpdateSpeechTimerTask();
            speechTimer.schedule(updateTask, 0, 1000);
        }
    }
       
    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // Do nothing.
    }

     @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // Do nothing.
    }

    @Override
    protected void onPause() {
        audioCntl.stopRecording();
        super.onPause();
    }
    
    /** Converts seconds to a digital time - [minutes]:[seconds]. 
     * @param seconds time in seconds
     * @return the digital display
     */
    private String secondsToDigitalTime(int seconds) {
        String display = "";
                
        int remaingSeconds = 0; // The number of seconds left after converting seconds to minutes.
        
        // Convert minutes to seconds.
        if (seconds >= 60) {
            int minutes = seconds / 60;
            
            if (minutes >= 10) {
                display += minutes / 10;
                display += minutes % 10;
                display += ":";
            } else {
                display += 0;
                display += minutes;
                display += ":";
            }
            remaingSeconds = seconds % 60;
        } else {
            display += "00:";
            remaingSeconds = seconds;
        }
        
        // Display the remaining seconds.
        if (remaingSeconds >= 10) {
            display += remaingSeconds / 10;
            display += remaingSeconds % 10;
        } else {
            display += "0";
            display += remaingSeconds;
        }
        
        return display;
    }
    
    private void startRecording() {
        try {
            // Create the speech recording record in the database, name it with the date it was created.
            final SpeechRecording speechRecording = new CreateSpeechRecordingTask(speechRecordingdatasource, speechID).execute().get();
            
            // The order of the speech recording is not updated so it is added at the top of the list(newest first).
            // In the list view once one item has been dropped, they all get reordered, new recordings still go to the top.
            //new UpdateOrderTask<SpeechRecording>(speechRecordingdatasource, speechRecording, [no adapter]);
            
            // Start the voice recording.
            audioCntl.startRecording(speechRecording.getFile());
        } catch(InterruptedException ie) {
            ie.printStackTrace();
        } catch(ExecutionException ee) { 
            ee.printStackTrace();
        }
    }
    
    /**
     * A task that updates a speech timer every second on the main activity.
     * @author Ryan Yosua
     *
     */
    private final class UpdateSpeechTimerTask extends TimerTask {
        
        @Override
        public void run() {
            MainActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    seconds++;
                    timerText.setText(secondsToDigitalTime(seconds));
                }
            });
        }
    }

}




Java Source Code List

com.ericharlow.DragNDrop.DragListener.java
com.ericharlow.DragNDrop.DragNDropAdapter.java
com.ericharlow.DragNDrop.DragNDropListView.java
com.ericharlow.DragNDrop.DropListener.java
com.ericharlow.DragNDrop.RemoveListener.java
edu.psu.rcy5017.speechwriter.DatabaseHelper.java
edu.psu.rcy5017.speechwriter.activity.EditTextActivity.java
edu.psu.rcy5017.speechwriter.activity.MainActivity.java
edu.psu.rcy5017.speechwriter.activity.NoteCardListActivity.java
edu.psu.rcy5017.speechwriter.activity.NoteListActivity.java
edu.psu.rcy5017.speechwriter.activity.OptionsActivity.java
edu.psu.rcy5017.speechwriter.activity.SpeechListActivity.java
edu.psu.rcy5017.speechwriter.activity.SpeechRecordingListActivity.java
edu.psu.rcy5017.speechwriter.activity.SplashScreenActivity.java
edu.psu.rcy5017.speechwriter.adapter.TabsPagerAdapter.java
edu.psu.rcy5017.speechwriter.constant.DefaultValues.java
edu.psu.rcy5017.speechwriter.constant.MiscConstants.java
edu.psu.rcy5017.speechwriter.constant.RequestCodes.java
edu.psu.rcy5017.speechwriter.controller.AudioCntl.java
edu.psu.rcy5017.speechwriter.controller.OptionsCntl.java
edu.psu.rcy5017.speechwriter.datasource.DataSource.java
edu.psu.rcy5017.speechwriter.datasource.NoteCardDataSource.java
edu.psu.rcy5017.speechwriter.datasource.NoteDataSource.java
edu.psu.rcy5017.speechwriter.datasource.SpeechDataSource.java
edu.psu.rcy5017.speechwriter.datasource.SpeechRecordingDataSource.java
edu.psu.rcy5017.speechwriter.fragment.NoteCardFragement.java
edu.psu.rcy5017.speechwriter.listener.ChangeFontSizeListener.java
edu.psu.rcy5017.speechwriter.listener.DragListenerImpl.java
edu.psu.rcy5017.speechwriter.listener.DropListenerImpl.java
edu.psu.rcy5017.speechwriter.listener.DropReorderListener.java
edu.psu.rcy5017.speechwriter.listener.RemoveListenerImpl.java
edu.psu.rcy5017.speechwriter.model.NoteCard.java
edu.psu.rcy5017.speechwriter.model.Note.java
edu.psu.rcy5017.speechwriter.model.SpeechRecording.java
edu.psu.rcy5017.speechwriter.model.Speech.java
edu.psu.rcy5017.speechwriter.task.ChangeNoteTextTask.java
edu.psu.rcy5017.speechwriter.task.CreateNoteCardTask.java
edu.psu.rcy5017.speechwriter.task.CreateNoteTask.java
edu.psu.rcy5017.speechwriter.task.CreateSpeechRecordingTask.java
edu.psu.rcy5017.speechwriter.task.CreateSpeechTask.java
edu.psu.rcy5017.speechwriter.task.DeleteTask.java
edu.psu.rcy5017.speechwriter.task.GetAllTask.java
edu.psu.rcy5017.speechwriter.task.NoteCardTask.java
edu.psu.rcy5017.speechwriter.task.NoteTask.java
edu.psu.rcy5017.speechwriter.task.RenameNoteCardTask.java
edu.psu.rcy5017.speechwriter.task.RenameSpeechRecordingTask.java
edu.psu.rcy5017.speechwriter.task.RenameSpeechTask.java
edu.psu.rcy5017.speechwriter.task.SpeechRecordingTask.java
edu.psu.rcy5017.speechwriter.task.SpeechTask.java
edu.psu.rcy5017.speechwriter.task.UpdateOrderTask.java