@@ -50,7 +50,7 @@ public class RecordingService extends Service {
5050 private static final String CHANNEL_ID = "mg4_recording" ;
5151 private static final int NOTIF_ID = 42 ;
5252 private static final int TOTAL_CAMERAS = 4 ;
53- private static final long TEST_RECORDING_MS = 30_000L ;
53+ private static final long TEST_RECORDING_MS = 10_000L ;
5454
5555 private static volatile boolean sServiceRunning = false ;
5656
@@ -64,7 +64,8 @@ public static boolean isRunning() {
6464 public static void startIfNeeded (Context context ) {
6565 SharedPreferences prefs = context .getSharedPreferences (PREFS_NAME , MODE_PRIVATE );
6666 boolean enabled = prefs .getBoolean (KEY_ENABLED , false );
67- if (!enabled ) return ;
67+ if (!enabled )
68+ return ;
6869 Intent i = new Intent (context , RecordingService .class );
6970 i .setAction (ACTION_START );
7071 context .startForegroundService (i );
@@ -91,7 +92,8 @@ public void onCreate() {
9192
9293 @ Override
9394 public int onStartCommand (Intent intent , int flags , int startId ) {
94- if (intent == null ) return START_STICKY ;
95+ if (intent == null )
96+ return START_STICKY ;
9597 String action = intent .getAction ();
9698
9799 if (ACTION_STOP .equals (action )) {
@@ -134,7 +136,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
134136
135137 private void recordTestClip () {
136138 File baseDir = getRecordsBaseDir ();
137- //noinspection ResultOfMethodCallIgnored
139+ // noinspection ResultOfMethodCallIgnored
138140 baseDir .mkdirs ();
139141 if (!baseDir .exists () || !baseDir .canWrite ()) {
140142 publishStatus (STATUS_ERROR , 0 , TOTAL_CAMERAS , "storage not writable" );
@@ -144,7 +146,8 @@ private void recordTestClip() {
144146 return ;
145147 }
146148
147- boolean startedAnyCamera = recordClip (baseDir , TEST_RECORDING_MS , "test_" + makeTimestampBaseWithSeconds (System .currentTimeMillis ()), -1 );
149+ boolean startedAnyCamera = recordClip (baseDir , TEST_RECORDING_MS ,
150+ "test_" + makeTimestampBaseWithSeconds (System .currentTimeMillis ()), -1 );
148151 worker = null ;
149152 if (startedAnyCamera ) {
150153 publishStatus (STATUS_OFF , 0 , TOTAL_CAMERAS , "" );
@@ -168,7 +171,7 @@ private void recordLoop() {
168171 }
169172
170173 File baseDir = getRecordsBaseDir ();
171- //noinspection ResultOfMethodCallIgnored
174+ // noinspection ResultOfMethodCallIgnored
172175 baseDir .mkdirs ();
173176 if (!baseDir .exists () || !baseDir .canWrite ()) {
174177 publishStatus (STATUS_ERROR , 0 , TOTAL_CAMERAS , "storage not writable" );
@@ -179,20 +182,23 @@ private void recordLoop() {
179182
180183 long segmentMs = segmentMin * 60L * 1000L ;
181184
182- // Convert total duration into a segment count: segmentMin=3, totalMin=30 => keep 10 segments.
185+ // Convert total duration into a segment count: segmentMin=3, totalMin=30 =>
186+ // keep 10 segments.
183187 int keepSegments = Math .max (1 , totalMin / segmentMin );
184188 boolean endedWithFatalError = false ;
185189
186190 while (!stopRequested ) {
187- boolean startedAnyCamera = recordClip (baseDir , segmentMs , makeTimestampBase (System .currentTimeMillis ()), keepSegments );
191+ boolean startedAnyCamera = recordClip (baseDir , segmentMs , makeTimestampBase (System .currentTimeMillis ()),
192+ keepSegments );
188193 if (!startedAnyCamera ) {
189194 endedWithFatalError = true ;
190195 break ;
191196 }
192197
193198 // Check whether recording has been disabled in prefs.
194199 enabled = prefs .getBoolean (KEY_ENABLED , false );
195- if (!enabled ) break ;
200+ if (!enabled )
201+ break ;
196202 }
197203
198204 worker = null ;
@@ -206,12 +212,11 @@ private void recordLoop() {
206212 private boolean recordClip (File baseDir , long durationMs , String baseName , int keepSegments ) {
207213 // Output names for the 4 cameras.
208214 // F = v15 (front), R = v17 (rear), X = v16 (left), Y = v14 (right)
209- int [] slots = new int []{ 0 , 1 , 2 , 3 };
210- int [] videoIndices = new int []{ 15 , 17 , 16 , 14 };
211- char [] names = new char []{ 'F' , 'R' , 'X' , 'Y' };
215+ int [] slots = new int [] { 0 , 1 , 2 , 3 };
216+ int [] videoIndices = new int [] { 15 , 17 , 16 , 14 };
217+ char [] names = new char [] { 'F' , 'R' , 'X' , 'Y' };
212218 int recordingFps = DashcamSettingsController .getRecordingFps (
213- getSharedPreferences (PREFS_NAME , MODE_PRIVATE )
214- );
219+ getSharedPreferences (PREFS_NAME , MODE_PRIVATE ));
215220
216221 String [] outPaths = new String [4 ];
217222 for (int i = 0 ; i < 4 ; i ++) {
@@ -224,12 +229,12 @@ private boolean recordClip(File baseDir, long durationMs, String baseName, int k
224229 StringBuilder failedCameras = new StringBuilder ();
225230 for (int i = 0 ; i < 4 ; i ++) {
226231 boolean started = CameraProbe .startMp4Record (
227- slots [i ], videoIndices [i ], outPaths [i ], 720 , 240 , recordingFps , 2500000
228- );
232+ slots [i ], videoIndices [i ], outPaths [i ], 720 , 240 , recordingFps , 2500000 );
229233 if (started ) {
230234 activeCameras ++;
231235 } else {
232- if (failedCameras .length () > 0 ) failedCameras .append (", " );
236+ if (failedCameras .length () > 0 )
237+ failedCameras .append (", " );
233238 failedCameras .append (names [i ]).append (" /dev/video" ).append (videoIndices [i ]);
234239 }
235240 }
@@ -265,16 +270,19 @@ private boolean recordClip(File baseDir, long durationMs, String baseName, int k
265270
266271 private void cleanupOldSegments (File baseDir , int keepSegments ) {
267272 File [] files = baseDir .listFiles ();
268- if (files == null ) return ;
273+ if (files == null )
274+ return ;
269275
270276 // baseName => earliestModified
271277 Map <String , Long > groupTime = new HashMap <>();
272278 for (File f : files ) {
273279 String name = f .getName ();
274- if (!name .endsWith (".mp4" )) continue ;
280+ if (!name .endsWith (".mp4" ))
281+ continue ;
275282 // yymmddhhmm_X.mp4
276283 int underscore = name .indexOf ('_' );
277- if (underscore <= 0 ) continue ;
284+ if (underscore <= 0 )
285+ continue ;
278286 String base = name .substring (0 , underscore );
279287 long t = f .lastModified ();
280288 groupTime .merge (base , t , Math ::min );
@@ -283,15 +291,16 @@ private void cleanupOldSegments(File baseDir, int keepSegments) {
283291 List <Map .Entry <String , Long >> groups = new ArrayList <>(groupTime .entrySet ());
284292 groups .sort (Comparator .comparingLong (Map .Entry ::getValue ));
285293
286- if (groups .size () <= keepSegments ) return ;
294+ if (groups .size () <= keepSegments )
295+ return ;
287296 int deleteCount = groups .size () - keepSegments ;
288297
289- char [] suffixes = new char []{ 'F' , 'R' , 'X' , 'Y' };
298+ char [] suffixes = new char [] { 'F' , 'R' , 'X' , 'Y' };
290299 for (int i = 0 ; i < deleteCount ; i ++) {
291300 String base = groups .get (i ).getKey ();
292301 for (char s : suffixes ) {
293302 File f = new File (baseDir , base + "_" + s + ".mp4" );
294- //noinspection ResultOfMethodCallIgnored
303+ // noinspection ResultOfMethodCallIgnored
295304 f .delete ();
296305 }
297306 }
@@ -301,7 +310,7 @@ private File getRecordsBaseDir() {
301310 // Android 9: write directly into the Downloads folder.
302311 File downloads = Environment .getExternalStoragePublicDirectory (Environment .DIRECTORY_DOWNLOADS );
303312 File dir = new File (downloads , "mg4_cam_records" );
304- //noinspection ResultOfMethodCallIgnored
313+ // noinspection ResultOfMethodCallIgnored
305314 dir .mkdirs ();
306315 return dir ;
307316 }
@@ -345,13 +354,14 @@ private void publishCurrentStatus() {
345354 prefs .getString (KEY_STATUS , STATUS_OFF ),
346355 prefs .getInt (KEY_ACTIVE_CAMERAS , 0 ),
347356 prefs .getInt (KEY_TOTAL_CAMERAS , TOTAL_CAMERAS ),
348- prefs .getString (KEY_LAST_ERROR , "" )
349- );
357+ prefs .getString (KEY_LAST_ERROR , "" ));
350358 }
351359
352360 private void publishStatus (String status , int activeCameras , int totalCameras , String lastError ) {
353- if (status == null ) status = STATUS_OFF ;
354- if (lastError == null ) lastError = "" ;
361+ if (status == null )
362+ status = STATUS_OFF ;
363+ if (lastError == null )
364+ lastError = "" ;
355365 getSharedPreferences (PREFS_NAME , MODE_PRIVATE ).edit ()
356366 .putString (KEY_STATUS , status )
357367 .putInt (KEY_ACTIVE_CAMERAS , Math .max (0 , activeCameras ))
@@ -390,10 +400,10 @@ private void createNotificationChannel() {
390400 NotificationChannel ch = new NotificationChannel (
391401 CHANNEL_ID ,
392402 getString (R .string .notification_channel_recording ),
393- NotificationManager .IMPORTANCE_LOW
394- );
403+ NotificationManager .IMPORTANCE_LOW );
395404 NotificationManager nm = (NotificationManager ) getSystemService (Context .NOTIFICATION_SERVICE );
396- if (nm != null ) nm .createNotificationChannel (ch );
405+ if (nm != null )
406+ nm .createNotificationChannel (ch );
397407 }
398408
399409 @ Nullable
0 commit comments