From 4b4d145e75a858339af75f556e8b3a6bbacf5831 Mon Sep 17 00:00:00 2001 From: Ayaz Ahmad Date: Tue, 30 Jul 2013 16:43:36 +0530 Subject: FM: Added recording option on RF Stats activity - Added recording button on action bar to start/stop recording from RF Stats activity Change-Id: I81b614d3731be47ff794c67622adb08fe81bff43 --- fmapp/res/menu/menu_rf_stats.xml | 40 +++ fmapp/src/com/codeaurora/fmradio/FMRadio.java | 118 ++------- .../src/com/codeaurora/fmradio/FMRadioService.java | 17 ++ fmapp/src/com/codeaurora/fmradio/FMStats.java | 274 +++++++++++++++++++-- .../com/codeaurora/fmradio/IFMRadioService.aidl | 1 + 5 files changed, 343 insertions(+), 107 deletions(-) create mode 100644 fmapp/res/menu/menu_rf_stats.xml (limited to 'fmapp') diff --git a/fmapp/res/menu/menu_rf_stats.xml b/fmapp/res/menu/menu_rf_stats.xml new file mode 100644 index 0000000..505e49b --- /dev/null +++ b/fmapp/res/menu/menu_rf_stats.xml @@ -0,0 +1,40 @@ + + + + + + + + + + diff --git a/fmapp/src/com/codeaurora/fmradio/FMRadio.java b/fmapp/src/com/codeaurora/fmradio/FMRadio.java index 408cf04..f9b9e15 100644 --- a/fmapp/src/com/codeaurora/fmradio/FMRadio.java +++ b/fmapp/src/com/codeaurora/fmradio/FMRadio.java @@ -393,7 +393,7 @@ public class FMRadio extends Activity } } } - if(isRecordTimerActive() ) { + if(isRecording()) { try { if ( null != mRecordUpdateHandlerThread) { mRecordUpdateHandlerThread.interrupt(); @@ -401,17 +401,6 @@ public class FMRadio extends Activity } catch (NullPointerException e) { e.printStackTrace(); } - long rtimeNow = ((SystemClock.elapsedRealtime())); - if (rtimeNow < mRecordDuration) - { - try { - if (null != mService) { - mService.delayedStop((mRecordDuration - rtimeNow), FMRadioService.STOP_RECORD); - } - } catch (Exception e) { - e.printStackTrace(); - } - } } super.onStop(); } @@ -442,7 +431,7 @@ public class FMRadio extends Activity } initiateSleepThread(); } - if(isRecordTimerActive()) { + if(isRecording()) { Log.d(LOGTAG,"isRecordTimerActive is true"); try { if (null != mService) { @@ -452,7 +441,7 @@ public class FMRadio extends Activity e.printStackTrace(); } if(isRecording()) { - initiateRecordThread(); + initiateRecordThread(); } } @@ -760,66 +749,23 @@ public class FMRadio extends Activity private static final int RECORDTIMER_EXPIRED = 0x1003; private static final int RECORDTIMER_UPDATE = 0x1004; - private boolean hasRecordTimerExpired() { - boolean expired = true; - - /* If record duration is 'until stopped' then return false and exit */ - if ( mRecordUntilStopped ) - return false; - - if (isRecordTimerActive()) - { - long timeNow = ((SystemClock.elapsedRealtime())); - //Log.d(LOGTAG, "hasSleepTimerExpired - " + mSleepAtPhoneTime + " now: "+ timeNow); - if (mRecording == false || timeNow < mRecordDuration) - { - expired = false; - } - } - return expired; - } - - private boolean isRecordTimerActive() { - boolean active = false; - if (mRecordDuration > 0) - { - active = true; - } - return active; - } - private void updateExpiredRecordTime() { int vis = View.VISIBLE; - if ( mRecordUntilStopped || isRecordTimerActive()) + if(isRecording()) { long timeNow = ((SystemClock.elapsedRealtime())); - if (mRecordUntilStopped || mRecordDuration >= timeNow) - { - long seconds = (timeNow - mRecordStartTime) / 1000; - String Msg = makeTimeString(seconds); - mRecordingMsgTV.setText(Msg); - } else - { - /* Clean up timer */ - mRecordDuration = 0; - } + long seconds = (timeNow - getRecordingStartTime()) / 1000; + String Msg = makeTimeString(seconds); + mRecordingMsgTV.setText(Msg); + mRecordingMsgTV.setVisibility(vis); } - mRecordingMsgTV.setVisibility(vis); } /* Recorder Thread processing */ private Runnable doRecordProcessing = new Runnable() { public void run() { - boolean recordTimerExpired; - if( mRecordUntilStopped ) - { - recordTimerExpired = false; - } - else - { - recordTimerExpired = hasRecordTimerExpired(); - } - while (recordTimerExpired == false && (!Thread.currentThread().isInterrupted()) ) + while (isRecording() && + (!Thread.currentThread().isInterrupted())) { try { @@ -827,38 +773,34 @@ public class FMRadio extends Activity Message statusUpdate = new Message(); statusUpdate.what = RECORDTIMER_UPDATE; mUIUpdateHandlerHandler.sendMessage(statusUpdate); - recordTimerExpired = hasRecordTimerExpired(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } - if( true == recordTimerExpired) { - Message finished = new Message(); - finished.what = RECORDTIMER_EXPIRED; - mUIUpdateHandlerHandler.sendMessage(finished); + if(!isRecording()) { + Message finished = new Message(); + finished.what = RECORDTIMER_EXPIRED; + mUIUpdateHandlerHandler.sendMessage(finished); } } } }; - private static long mRecordDuration = 0; - private static long mRecordStartTime = 0; - private static boolean mRecordUntilStopped =false; private Thread mRecordUpdateHandlerThread = null; - private void initiateRecordDurationTimer(long mins ) { + private long getRecordingStartTime() { - if( mins == FmSharedPreferences.RECORD_DUR_INDEX_3_VAL ) - { - mRecordUntilStopped = true; - mRecordStartTime = SystemClock.elapsedRealtime(); - } - else - { - mRecordUntilStopped = false; - mRecordDuration = ( mRecordStartTime = SystemClock.elapsedRealtime()) + (mins * 60 * 1000); + if(mService == null) + return 0; + + try { + return mService.getRecordingStartTime(); + }catch(RemoteException e) { + return 0; } + } + private void initiateRecordDurationTimer(long mins ) { Log.d(LOGTAG, "Stop Recording in mins : " + mins); initiateRecordThread(); } @@ -2152,6 +2094,7 @@ public class FMRadio extends Activity int durationInMins = FmSharedPreferences.getRecordDuration(); Log.e(LOGTAG, " Fected duration:" + durationInMins ); initiateRecordDurationTimer( durationInMins ); + invalidateOptionsMenu(); } private void stopRecording() { mRecording = false; @@ -2171,7 +2114,7 @@ public class FMRadio extends Activity e.printStackTrace(); } } - mRecordDuration = 0; + invalidateOptionsMenu(); } private boolean isRecording() { @@ -2813,7 +2756,6 @@ public class FMRadio extends Activity } case RECORDTIMER_EXPIRED: { Log.d(LOGTAG, "mUIUpdateHandlerHandler - RECORDTIMER_EXPIRED"); - mRecordDuration = 0; //Clear the Recorder text mRecordingMsgTV.setText(""); if (mRecording != false) @@ -3464,14 +3406,6 @@ public class FMRadio extends Activity if(isRecording()) { initiateRecordThread(); } - else if((mRecordDuration > 0) && - (mRecordUpdateHandlerThread != null)) { - mRecordUpdateHandlerThread.interrupt(); - if(mRecordingMsgTV != null) { - mRecordingMsgTV.setVisibility(View.INVISIBLE); - } - mRecordDuration = 0; - } return; } else { diff --git a/fmapp/src/com/codeaurora/fmradio/FMRadioService.java b/fmapp/src/com/codeaurora/fmradio/FMRadioService.java index e15117d..b3a2b42 100644 --- a/fmapp/src/com/codeaurora/fmradio/FMRadioService.java +++ b/fmapp/src/com/codeaurora/fmradio/FMRadioService.java @@ -297,6 +297,7 @@ public class FMRadioService extends Service if (state == RECORD_START) { Log.d(LOGTAG, "FM Recording started"); mFmRecordingOn = true; + mSampleStart = SystemClock.elapsedRealtime(); try { if ((mServiceInUse) && (mCallbacks != null) ) { Log.d(LOGTAG, "start recording thread"); @@ -315,6 +316,7 @@ public class FMRadioService extends Service } catch (RemoteException e) { e.printStackTrace(); } + mSampleStart = 0; } } } @@ -691,6 +693,14 @@ public class FMRadioService extends Service private void sendRecordIntent(int action) { Intent intent = new Intent(ACTION_FM_RECORDING); intent.putExtra("state", action); + if(action == RECORD_START) { + int mRecordDuration = -1; + if(FmSharedPreferences.getRecordDuration() != + FmSharedPreferences.RECORD_DUR_INDEX_3_VAL) { + mRecordDuration = (FmSharedPreferences.getRecordDuration() * 60 * 1000); + } + intent.putExtra("record_duration", mRecordDuration); + } Log.d(LOGTAG, "Sending Recording intent for = " +action); getApplicationContext().sendBroadcast(intent); } @@ -1470,6 +1480,10 @@ private Runnable mSpeakerDisableTask = new Runnable() { { return (mService.get().setRxRepeatCount(count)); } + public long getRecordingStartTime() + { + return (mService.get().getRecordingStartTime()); + } } private final IBinder mBinder = new ServiceStub(this); @@ -2930,6 +2944,9 @@ private Runnable mSpeakerDisableTask = new Runnable() { return false; } + public long getRecordingStartTime() { + return mSampleStart; + } //handling the sleep and record stop when FM App not in focus private void delayedStop(long duration, int nType) { int whatId = (nType == STOP_SERVICE) ? STOPSERVICE_ONSLEEP: STOPRECORD_ONTIMEOUT; diff --git a/fmapp/src/com/codeaurora/fmradio/FMStats.java b/fmapp/src/com/codeaurora/fmradio/FMStats.java index a68ccc4..6d6c516 100644 --- a/fmapp/src/com/codeaurora/fmradio/FMStats.java +++ b/fmapp/src/com/codeaurora/fmradio/FMStats.java @@ -65,6 +65,9 @@ import android.app.AlarmManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.IntentFilter; +import android.view.Menu; +import android.view.MenuItem; +import android.view.MenuInflater; import android.os.SystemClock; import java.io.FileNotFoundException; @@ -75,6 +78,8 @@ import java.util.HashMap; import android.os.SystemProperties; import java.io.BufferedReader; import java.io.FileReader; +import java.util.Formatter; +import java.util.Locale; public class FMStats extends Activity { @@ -177,6 +182,8 @@ public class FMStats extends Activity { private Thread mMultiUpdateThread = null; private static final int STATUS_UPDATE = 1; private static final int STATUS_DONE = 2; + private static final int RECORDTIMER_UPDATE = 3; + private static final int RECORDTIMER_EXPIRED = 4; private static final int STOP_ROW_ID = 200; private static final int NEW_ROW_ID = 300; private int mStopIds = STOP_ROW_ID; @@ -230,6 +237,15 @@ public class FMStats extends Activity { private int curSweepMthd = 0; + private Thread mRecordUpdateHandlerThread = null; + boolean mRecording = false; + + + private static StringBuilder sFormatBuilder = new StringBuilder(); + private static Formatter sFormatter = new Formatter(sFormatBuilder, Locale + .getDefault()); + private static final Object[] sTimeArgs = new Object[5]; + private final String FREQ_LIST_FILE_NAME = "/freq_list_comma_separated.txt"; private static final String BAND_SWEEP_START_DELAY_TIMEOUT = "com.codeaurora.fmradio.SWEEP_START_DELAY_EXP"; private static final String BAND_SWEEP_DWELL_DELAY_TIMEOUT = "com.codeaurora.fmradio.SWEEP_DWELL_DELAY_EXP"; @@ -306,14 +322,14 @@ public class FMStats extends Activity { Log.d(LOGTAG, "onCreate: Start Service completed successfully"); } - /*Initialize the column header with - constant values*/ - mColumnHeader.setFreq("Freq"); - mColumnHeader.setRSSI("RMSSI"); - mColumnHeader.setIoC("IoC"); + /*Initialize the column header with + constant values*/ + mColumnHeader.setFreq("Freq"); + mColumnHeader.setRSSI("RMSSI"); + mColumnHeader.setIoC("IoC"); mColumnHeader.setSINR("SINR"); - mColumnHeader.setMpxDcc("Offset"); - mColumnHeader.setIntDet("IntDet"); + mColumnHeader.setMpxDcc("Offset"); + mColumnHeader.setIntDet("IntDet"); bandSweepSettingButton = (TextView)findViewById(R.id.BandSweepSetting); if(bandSweepSettingButton != null) { @@ -328,6 +344,30 @@ public class FMStats extends Activity { registerBandSweepDwellExprdListener(); } + @Override + public void onStart() { + super.onStart(); + if(isRecording()) { + Log.d(LOGTAG, "onStart"); + initiateRecordThread(); + } + } + + @Override + public void onStop() { + super.onStop(); + if(isRecording()) { + try { + if(null != mRecordUpdateHandlerThread) { + mRecordUpdateHandlerThread.interrupt(); + } + }catch (NullPointerException e) { + e.printStackTrace(); + } + } + } + + @Override public void onDestroy() { stopCurTest(); @@ -336,8 +376,8 @@ public class FMStats extends Activity { unRegisterBroadcastReceiver(mBandSweepDwellExprdListener); if(null != mFileCursor ) { - try { - mFileCursor.close(); + try { + mFileCursor.close(); }catch (IOException e) { e.printStackTrace(); } @@ -364,10 +404,10 @@ public class FMStats extends Activity { }else { mTestRunning = false; /*Stop the thread by interrupting it*/ - if(mMultiUpdateThread != null) { - mMultiUpdateThread.interrupt(); - mMultiUpdateThread = null; - } + if(mMultiUpdateThread != null) { + mMultiUpdateThread.interrupt(); + mMultiUpdateThread = null; + } if(SEARCH_TEST == mTestSelected) { try { @@ -1887,6 +1927,17 @@ public class FMStats extends Activity { case STATUS_DONE: SetButtonState(true); break; + case RECORDTIMER_EXPIRED: + Log.d(LOGTAG, "mUIUpdateHandlerHandler - RECORDTIMER_EXPIRED"); + if(!isRecording()) { + Log.d(LOGTAG, "Stop Recording"); + stopRecording(); + } + break; + case RECORDTIMER_UPDATE: + Log.d(LOGTAG, "mUIUpdateHandlerHandler - RECORDTIMER_UPDATE"); + updateExpiredRecordTime(); + break; } } }; @@ -1965,6 +2016,9 @@ public class FMStats extends Activity { { e.printStackTrace(); } + if(isRecording()) { + initiateRecordThread(); + } return; } else { @@ -1982,17 +2036,19 @@ public class FMStats extends Activity { public void onEnabled() { Log.d(LOGTAG, "mServiceCallbacks.onEnabled :"); + invalidateOptionsMenu(); } public void onDisabled() { Log.d(LOGTAG, "mServiceCallbacks.onDisabled :"); - stopCurTest(); + stopAllOperations(); } public void onRadioReset() { Log.d(LOGTAG, "mServiceCallbacks.onRadioReset :"); + stopAllOperations(); } public void onTuneStatusChanged() @@ -2057,6 +2113,7 @@ public class FMStats extends Activity { public void onRecordingStopped() { Log.d(LOGTAG, "mServiceCallbacks.onDisabled :"); + stopRecording(); } public void onFinishActivity() { @@ -2065,6 +2122,10 @@ public class FMStats extends Activity { public void onRecordingStarted() { Log.d(LOGTAG, "mServiceCallbacks.onRecordingStarted:"); + int durationInMins = FmSharedPreferences.getRecordDuration(); + Log.e(LOGTAG, " Fected duration: " + durationInMins); + initiateRecordDurationTimer(durationInMins); + invalidateOptionsMenu(); } }; /* Radio Vars */ @@ -2307,4 +2368,187 @@ public class FMStats extends Activity { mNextFreqInterface = null; } } - } + + public boolean isRecording() { + if(mService == null) + return false; + try { + return mService.isFmRecordingOn(); + }catch(RemoteException e) { + return false; + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.menu_rf_stats, menu); + return true; + } + + @Override + public boolean onPrepareOptionsMenu(Menu menu) { + super.onPrepareOptionsMenu(menu); + MenuItem item; + + item = menu.findItem(R.id.menu_recording); + if(item != null && !isRecording()) { + item.setTitle(R.string.menu_record_start); + item.setEnabled(isFmOn()); + }else if(item != null) { + item.setTitle(R.string.menu_record_stop); + setRecordDurationDisplay(menu, R.id.menu_record_duration); + } + return true; + } + + private void setRecordDurationDisplay(Menu menu, int id) { + MenuItem item; + long timeNow; + long seconds; + + if(menu == null) + return; + item = menu.findItem(id); + if(item != null) { + timeNow = SystemClock.elapsedRealtime(); + seconds = (timeNow - getRecordingStartTime()) / 1000; + item.setTitle(makeTimeString(seconds)); + } + } + + private void startRecording() { + if(isFmOn()) { + try { + mService.startRecording(); + }catch(RemoteException e) { + e.printStackTrace(); + } + } + } + + private void stopRecording() { + if(null != mRecordUpdateHandlerThread) { + mRecordUpdateHandlerThread.interrupt(); + } + if(mService != null) { + try { + mService.stopRecording(); + }catch (RemoteException e) { + e.printStackTrace(); + } + } + invalidateOptionsMenu(); + } + + private long getRecordingStartTime() { + if(mService == null) + return 0; + try { + return mService.getRecordingStartTime(); + }catch(RemoteException e) { + return 0; + } + } + + private void initiateRecordDurationTimer(long mins ) { + Log.d(LOGTAG, "Stop Recording in mins : " + mins); + initiateRecordThread(); + } + + private void initiateRecordThread() { + if(mRecordUpdateHandlerThread == null) { + mRecordUpdateHandlerThread = new Thread(null, doRecordProcessing, + "RecordUpdateThread"); + } + /* Launch the dummy thread to simulate the transfer progress */ + Log.d(LOGTAG, "Thread State: " + mRecordUpdateHandlerThread.getState()); + if(mRecordUpdateHandlerThread.getState() == Thread.State.TERMINATED) { + mRecordUpdateHandlerThread = new Thread(null, doRecordProcessing, + "RecordUpdateThread"); + } + /* If the thread state is "new" then the thread has not yet started */ + if(mRecordUpdateHandlerThread.getState() == Thread.State.NEW) { + mRecordUpdateHandlerThread.start(); + } + } + + /* Recorder Thread processing */ + private Runnable doRecordProcessing = new Runnable() { + public void run() { + while(isRecording() && + (!Thread.currentThread().isInterrupted())) { + try { + Thread.sleep(500); + Message statusUpdate = new Message(); + statusUpdate.what = RECORDTIMER_UPDATE; + mUIUpdateHandlerHandler.sendMessage(statusUpdate); + }catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + if(!isRecording()) { + Message finished = new Message(); + finished.what = RECORDTIMER_EXPIRED; + mUIUpdateHandlerHandler.sendMessage(finished); + } + } + } + }; + + private void updateExpiredRecordTime() { + int vis = View.VISIBLE; + if(isRecording()) { + invalidateOptionsMenu(); + } + } + + private String makeTimeString(long secs) { + String durationformat = getString(R.string.durationformat); + + /** Provide multiple arguments so the format can be changed easily by + * modifying the xml. + **/ + sFormatBuilder.setLength(0); + + final Object[] timeArgs = sTimeArgs; + timeArgs[0] = secs / 3600; + timeArgs[1] = secs / 60; + timeArgs[2] = (secs / 60) % 60; + timeArgs[3] = secs; + timeArgs[4] = secs % 60; + + return sFormatter.format(durationformat, timeArgs).toString(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch(item.getItemId()) + { + case R.id.menu_recording: + if(isRecording()) { + stopRecording(); + }else { + startRecording(); + } + break; + } + return true; + } + + private boolean isFmOn() { + boolean status = false; + + if(mService != null) { + try { + status = mService.isFmOn(); + }catch(RemoteException e) { + } + } + return status; + } + + private void stopAllOperations() { + stopCurTest(); + stopRecording(); + } +} diff --git a/fmapp/src/com/codeaurora/fmradio/IFMRadioService.aidl b/fmapp/src/com/codeaurora/fmradio/IFMRadioService.aidl index d80c320..317a176 100644 --- a/fmapp/src/com/codeaurora/fmradio/IFMRadioService.aidl +++ b/fmapp/src/com/codeaurora/fmradio/IFMRadioService.aidl @@ -98,5 +98,6 @@ interface IFMRadioService int getGoodChRmssiTh(); int getAfJmpRmssiSamplesCnt(); boolean setRxRepeatCount(int count); + long getRecordingStartTime(); } -- cgit v1.2.3