[FM] "Can't play the track you requested" pops up after tapping "LISTEN"

Select "Start Recording" in options menu -> Wait for SD storage
to be nearly fully occupied -> "Insufficient storage" toast
message displays and "Save recording" dialog pops up ->Tap "Save"
to save the recording -> Tap "LISTEN" to playback the recording,
"Can't play the track you requested" pops up.

The fix is to also check storage free space when handling message
MSG_UPDATE_NOTIFICATION.

bug: 19007352
from: https://partner-android-review.googlesource.com/#/c/193407/

Change-Id: I63d7e22e699cb3ddb90e32a785d5c264258b6a06
Signed-off-by: Benson Huang <benson.huang@mediatek.com>
diff --git a/src/com/android/fmradio/FmRecordActivity.java b/src/com/android/fmradio/FmRecordActivity.java
index 4df42ec..950a8db 100644
--- a/src/com/android/fmradio/FmRecordActivity.java
+++ b/src/com/android/fmradio/FmRecordActivity.java
@@ -258,6 +258,7 @@
         mHandler.removeMessages(MSG_UPDATE_NOTIFICATION);
         if (mService != null) {
             mService.removeNotification();
+            mService.updatePlayingNotification();
         }
     }
 
@@ -357,24 +358,11 @@
             switch (msg.what) {
                 case FmListener.MSGID_REFRESH:
                     if (mService != null) {
-                        long recordTime = mService.getRecordTime();
-                        recordTime = recordTime / 1000L;
-                        mMintues.setText(addPaddingForString(recordTime / TIME_BASE));
-                        mSeconds.setText(addPaddingForString(recordTime % TIME_BASE));
-
-                        // Check storage free space
-                        String recordingSdcard = FmUtils.getDefaultStoragePath();
-                        if (!FmUtils.hasEnoughSpace(recordingSdcard)) {
-                            // Need to record more than 1s.
-                            // Avoid calling MediaRecorder.stop() before native record starts.
-                            if (recordTime > 1000) {
-                                // Insufficient storage
-                                mService.stopRecordingAsync();
-                                Toast.makeText(FmRecordActivity.this,
-                                        R.string.toast_sdcard_insufficient_space,
-                                        Toast.LENGTH_SHORT).show();
-                            }
-                        }
+                        long recordTimeInMillis = mService.getRecordTime();
+                        long recordTimeInSec = recordTimeInMillis / 1000L;
+                        mMintues.setText(addPaddingForString(recordTimeInSec / TIME_BASE));
+                        mSeconds.setText(addPaddingForString(recordTimeInSec % TIME_BASE));
+                        checkStorageSpaceAndStop();
                     }
                     mHandler.sendEmptyMessageDelayed(FmListener.MSGID_REFRESH, 1000);
                     break;
@@ -382,6 +370,7 @@
                 case MSG_UPDATE_NOTIFICATION:
                     if (mService != null) {
                         updateRecordingNotification(mService.getRecordTime());
+                        checkStorageSpaceAndStop();
                     }
                     mHandler.sendEmptyMessageDelayed(MSG_UPDATE_NOTIFICATION, 1000);
                     break;
@@ -415,6 +404,24 @@
         };
     };
 
+    private void checkStorageSpaceAndStop() {
+        long recordTimeInMillis = mService.getRecordTime();
+        long recordTimeInSec = recordTimeInMillis / 1000L;
+        // Check storage free space
+        String recordingSdcard = FmUtils.getDefaultStoragePath();
+        if (!FmUtils.hasEnoughSpace(recordingSdcard)) {
+            // Need to record more than 1s.
+            // Avoid calling MediaRecorder.stop() before native record starts.
+            if (recordTimeInSec >= 1) {
+                // Insufficient storage
+                mService.stopRecordingAsync();
+                Toast.makeText(FmRecordActivity.this,
+                        R.string.toast_sdcard_insufficient_space,
+                        Toast.LENGTH_SHORT).show();
+            }
+        }
+    }
+
     private void handleRecordError(int errorType) {
         Log.d(TAG, "handleRecordError, errorType = " + errorType);
         String showString = null;