Skip to content

Commit

Permalink
Version 1.0.0 release
Browse files Browse the repository at this point in the history
 - Added Open Source Licenses
 - Added MediaPlayer (PlaybackFragment) and an OnClickListener in RecyclerView to listen to recordings.
 - Added OnDataBasedChangedListener to check for new items added to the database.
 - Correctly update RecyclerView after saving a recording
 - Correctly display the item length and date-added in the RecyclerView
  • Loading branch information
dkim0419 committed Jan 4, 2015
1 parent 1ac6e16 commit 5d032ee
Show file tree
Hide file tree
Showing 20 changed files with 669 additions and 131 deletions.
86 changes: 45 additions & 41 deletions app/src/main/java/com/danielkim/soundrecorder/DBHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

import com.danielkim.soundrecorder.listeners.OnDatabaseChangedListener;

import java.util.Comparator;

/**
Expand All @@ -15,6 +17,10 @@
public class DBHelper extends SQLiteOpenHelper {
private Context mContext;

private static final String LOG_TAG = "DBHelper";

private static OnDatabaseChangedListener mOnDatabaseChangedListener;

public static final String DATABASE_NAME = "saved_recordings.db";
private static final int DATABASE_VERSION = 1;

Expand All @@ -27,12 +33,6 @@ public static abstract class DBHelperItem implements BaseColumns {
public static final String COLUMN_NAME_TIME_ADDED = "time_added";
}

public interface OnDatabaseChangedListener {
void onDatabaseEntryUpdated();
}

private OnDatabaseChangedListener mOnDatabaseChangedListener;

private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ",";
private static final String SQL_CREATE_ENTRIES =
Expand All @@ -46,18 +46,23 @@ public interface OnDatabaseChangedListener {
@SuppressWarnings("unused")
private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + DBHelperItem.TABLE_NAME;

public long addRecording(String recordingName, String filePath, long length) {
SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(DBHelperItem.COLUMN_NAME_RECORDING_NAME, recordingName);
cv.put(DBHelperItem.COLUMN_NAME_RECORDING_FILE_PATH, filePath);
cv.put(DBHelperItem.COLUMN_NAME_RECORDING_LENGTH, length);
cv.put(DBHelperItem.COLUMN_NAME_TIME_ADDED, System.currentTimeMillis());
long rowId = db.insert(DBHelperItem.TABLE_NAME, null, cv);
if (mOnDatabaseChangedListener != null)
mOnDatabaseChangedListener.onDatabaseEntryUpdated();
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}

return rowId;
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mContext = context;
}

public static void setOnDatabaseChangedListener(OnDatabaseChangedListener listener) {
mOnDatabaseChangedListener = listener;
}

public RecordingItem getItemAt(int position) {
Expand Down Expand Up @@ -87,8 +92,6 @@ public void removeItemWithId(int id) {
SQLiteDatabase db = getWritableDatabase();
String[] whereArgs = { String.valueOf(id) };
db.delete(DBHelperItem.TABLE_NAME, "_ID=?", whereArgs);
if (mOnDatabaseChangedListener != null)
mOnDatabaseChangedListener.onDatabaseEntryUpdated();
}

public int getCount() {
Expand All @@ -100,21 +103,6 @@ public int getCount() {
return count;
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mContext = context;
}

public Context getContext() {
return mContext;
}
Expand All @@ -127,18 +115,33 @@ public int compare(RecordingItem item1, RecordingItem item2) {
}
}

public long addRecording(String recordingName, String filePath, long length) {

SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(DBHelperItem.COLUMN_NAME_RECORDING_NAME, recordingName);
cv.put(DBHelperItem.COLUMN_NAME_RECORDING_FILE_PATH, filePath);
cv.put(DBHelperItem.COLUMN_NAME_RECORDING_LENGTH, length);
cv.put(DBHelperItem.COLUMN_NAME_TIME_ADDED, System.currentTimeMillis());
long rowId = db.insert(DBHelperItem.TABLE_NAME, null, cv);

if (mOnDatabaseChangedListener != null) {
mOnDatabaseChangedListener.onNewDatabaseEntryAdded();
}

return rowId;
}

public void renameItem(RecordingItem item, String recordingName) {
SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(DBHelperItem.COLUMN_NAME_RECORDING_NAME, recordingName);
db.update(DBHelperItem.TABLE_NAME, cv,
DBHelperItem._ID + "=" + item.getId(), null);
if (mOnDatabaseChangedListener != null)
mOnDatabaseChangedListener.onDatabaseEntryUpdated();
}

public void setOnDatabaseChangedListener(OnDatabaseChangedListener listener) {
mOnDatabaseChangedListener = listener;
if (mOnDatabaseChangedListener != null) {
mOnDatabaseChangedListener.onDatabaseEntryRenamed();
}
}

public long restoreRecording(RecordingItem item) {
Expand All @@ -150,8 +153,9 @@ public long restoreRecording(RecordingItem item) {
cv.put(DBHelperItem.COLUMN_NAME_TIME_ADDED, item.getTime());
cv.put(DBHelperItem._ID, item.getId());
long rowId = db.insert(DBHelperItem.TABLE_NAME, null, cv);
if (mOnDatabaseChangedListener != null)
mOnDatabaseChangedListener.onDatabaseEntryUpdated();
if (mOnDatabaseChangedListener != null) {
//mOnDatabaseChangedListener.onNewDatabaseEntryAdded();
}
return rowId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
* Created by Daniel on 12/30/2014.
*/
public class RecordingItem implements Parcelable {
private String mName;
private String mFilePath;
private int mId;
private int mLength; // length of recording
private String mName; // file name
private String mFilePath; //file path
private int mId; //id in database
private int mLength; // length of recording in seconds
private long mTime; // date/time of the recording

public RecordingItem()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
package com.danielkim.soundrecorder;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.FileObserver;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Toast;

import com.danielkim.soundrecorder.activities.MainActivity;
import com.danielkim.soundrecorder.adapters.FileViewerAdapter;
import com.danielkim.soundrecorder.fragments.FileViewerFragment;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
Expand All @@ -43,7 +33,8 @@ public class RecordingService extends Service {

private DBHelper mDatabase;

private int mElapsedSeconds = 0;
private long mStartingTimeMillis = 0;
private int mElapsedMillis = 0;
private static final SimpleDateFormat mTimerFormat = new SimpleDateFormat("mm:ss", Locale.getDefault());

private Timer mTimer = null;
Expand All @@ -63,24 +54,9 @@ public void onCreate() {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startRecording();
startTimer();
return START_STICKY;
}

private void startTimer() {
mTimer = new Timer();
mIncrementTimerTask = new TimerTask() {
@Override
public void run() {
mElapsedSeconds++;
// NotificationManager mgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mgr.notify(1, createNotification());
}
};

mTimer.scheduleAtFixedRate(mIncrementTimerTask, 1000, 1000);
}

@Override
public void onDestroy() {
if (mRecorder != null) {
Expand All @@ -107,6 +83,7 @@ public void startRecording() {
try {
mRecorder.prepare();
mRecorder.start();
mStartingTimeMillis = System.currentTimeMillis();

} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
Expand All @@ -115,30 +92,26 @@ public void startRecording() {

public void stopRecording() {
mRecorder.stop();
mElapsedMillis = (int) (System.currentTimeMillis() - mStartingTimeMillis);
mRecorder.release();
Toast.makeText(this, R.string.toast_recording_finish + " " + mFilePath, Toast.LENGTH_LONG).show();
Toast.makeText(this, getString(R.string.toast_recording_finish) + " " + mFilePath, Toast.LENGTH_LONG).show();

mRecorder = null;

try {
mDatabase.addRecording(mFileName, mFilePath, mElapsedSeconds);
FileViewerAdapter adapter = new FileViewerAdapter(getApplicationContext());
RecyclerView view = new RecyclerView(getApplicationContext());
view.swapAdapter(adapter, true);

//add the new file to the top of the list (position 0)
adapter.notifyItemInserted(0);
mDatabase.addRecording(mFileName, mFilePath, mElapsedMillis);

} catch (Exception e){
Log.e(LOG_TAG, "exception", e);
}
}

//TODO: add timer
private Notification createNotification() {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ic_mic_white_36dp)
.setContentTitle(getString(R.string.notification_recording))
.setContentText(mTimerFormat.format(1000 * mElapsedSeconds))
.setOngoing(true);

return mBuilder.build();
Expand Down
Loading

0 comments on commit 5d032ee

Please sign in to comment.