From 83a4978e5fc461d6307445f98d6fe9c23698ede7 Mon Sep 17 00:00:00 2001 From: s0llvan <178677095+s0llvan@users.noreply.github.com> Date: Sun, 5 Jan 2025 17:53:15 +0100 Subject: [PATCH] feature: add debug log --- app/src/main/java/com/fadcam/Log.java | 166 ++++++++++++++++++ .../com/fadcam/services/RecordingService.java | 4 +- .../main/java/com/fadcam/ui/HomeFragment.java | 4 +- .../java/com/fadcam/ui/SettingsFragment.java | 5 +- 4 files changed, 176 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/com/fadcam/Log.java diff --git a/app/src/main/java/com/fadcam/Log.java b/app/src/main/java/com/fadcam/Log.java new file mode 100644 index 00000000..5c8db0c3 --- /dev/null +++ b/app/src/main/java/com/fadcam/Log.java @@ -0,0 +1,166 @@ +package com.fadcam; + +import android.content.ContentResolver; +import android.content.ContentUris; +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.net.Uri; +import android.os.Build; +import android.os.Environment; +import android.provider.MediaStore; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +public class Log { + private static File logFile; + + private static final String TAG = "Log"; + + private static Context context; + + private static Uri fileUri; + + public static void init(Context context) + { + Log.context = context; + + createHtmlFile(context, "debug.html"); + } + + private static String getCurrentTimeStamp() { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); + return simpleDateFormat.format(new Date()); + } + + public static void d(String tag, String message) { + String logMessage = "" + getCurrentTimeStamp() + " INFO: [" + tag + "]" + message + ""; + appendHtmlToFile(logMessage); + } + + public static void w(String tag, String message) { + String logMessage = "" + getCurrentTimeStamp() + " WARNING: [" + tag + "]" + message + ""; + appendHtmlToFile(logMessage); + } + + public static void e(String tag, Object... objects) { + StringBuilder message = new StringBuilder(); + for(Object object: objects) + { + if(object instanceof String) + { + message.append(object); + } + else if(object instanceof Exception) + { + StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter); + ((Exception) object).printStackTrace(printWriter); + String stackTrace = stringWriter.toString(); + message.append(stackTrace); + } + } + + String logMessage = "" + getCurrentTimeStamp() + " ERROR: [" + tag + "]" + message + ""; + appendHtmlToFile(logMessage); + } + + public static Uri createHtmlFile(Context context, String fileName) { + try { + Uri existingFileUri = checkIfFileExists(context, fileName); + + if (existingFileUri != null) { + return existingFileUri; + } + + ContentValues contentValues = new ContentValues(); + contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName); + contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "text/html"); + contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS + "/FadCam"); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + fileUri = context.getContentResolver().insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues); + } else { + fileUri = Uri.parse("file://" + context.getExternalFilesDir(null).getPath() + "/Download/" + fileName); + } + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } + + public static void appendHtmlToFile(String htmlContent) { + OutputStream outputStream = null; + + try { + outputStream = context.getContentResolver().openOutputStream(fileUri, "wa"); + if (outputStream != null) { + outputStream.write(htmlContent.getBytes()); + outputStream.write(("
").getBytes()); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (outputStream != null) { + try { + outputStream.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + } + + private static Uri checkIfFileExists(Context context, String fileName) { + Uri uri = null; + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + uri = MediaStore.Downloads.EXTERNAL_CONTENT_URI; + } else { + String downloadsPath = context.getExternalFilesDir(null).getPath() + "/Download"; + File downloadsDir = new File(downloadsPath); + + if (!downloadsDir.exists()) { + boolean dirCreated = downloadsDir.mkdirs(); + if (!dirCreated) { + return null; + } + } + + File file = new File(downloadsDir, fileName); + uri = Uri.fromFile(file); + } + + String[] projection = {MediaStore.MediaColumns._ID}; + String selection = MediaStore.MediaColumns.DISPLAY_NAME + "=?"; + String[] selectionArgs = new String[]{fileName}; + + Cursor cursor = null; + try { + ContentResolver resolver = context.getContentResolver(); + cursor = resolver.query(uri, projection, selection, selectionArgs, null); + + if (cursor != null && cursor.moveToFirst()) { + long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID)); + return ContentUris.withAppendedId(uri, id); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (cursor != null) { + cursor.close(); + } + } + + return null; + } +} diff --git a/app/src/main/java/com/fadcam/services/RecordingService.java b/app/src/main/java/com/fadcam/services/RecordingService.java index bf7222e4..23a57692 100644 --- a/app/src/main/java/com/fadcam/services/RecordingService.java +++ b/app/src/main/java/com/fadcam/services/RecordingService.java @@ -23,7 +23,6 @@ import android.os.IBinder; import android.os.SystemClock; import android.preference.PreferenceManager; -import android.util.Log; import android.util.Range; import android.view.Surface; import android.widget.Toast; @@ -38,6 +37,7 @@ import com.arthenica.ffmpegkit.ReturnCode; import com.fadcam.CameraType; import com.fadcam.Constants; +import com.fadcam.Log; import com.fadcam.MainActivity; import com.fadcam.R; import com.fadcam.RecordingState; @@ -105,6 +105,8 @@ public boolean isPaused() { public void onCreate() { super.onCreate(); + Log.init(getApplicationContext()); + locationHelper = new LocationHelper(getApplicationContext()); createNotificationChannel(); diff --git a/app/src/main/java/com/fadcam/ui/HomeFragment.java b/app/src/main/java/com/fadcam/ui/HomeFragment.java index 43d9a415..f82c4af9 100644 --- a/app/src/main/java/com/fadcam/ui/HomeFragment.java +++ b/app/src/main/java/com/fadcam/ui/HomeFragment.java @@ -37,7 +37,6 @@ import android.text.Html; import android.text.Spanned; import android.text.format.Formatter; -import android.util.Log; import android.view.LayoutInflater; import android.view.Surface; import android.view.TextureView; @@ -59,6 +58,7 @@ import com.fadcam.CameraType; import com.fadcam.Constants; +import com.fadcam.Log; import com.fadcam.R; import com.fadcam.services.RecordingService; import com.fadcam.RecordingState; @@ -356,6 +356,8 @@ private void resetTimers() { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + Log.init(requireContext()); + Log.d(TAG, "HomeFragment created."); // Request essential permissions on every launch diff --git a/app/src/main/java/com/fadcam/ui/SettingsFragment.java b/app/src/main/java/com/fadcam/ui/SettingsFragment.java index 7f1ad9b2..458136cc 100644 --- a/app/src/main/java/com/fadcam/ui/SettingsFragment.java +++ b/app/src/main/java/com/fadcam/ui/SettingsFragment.java @@ -15,7 +15,6 @@ import android.os.Bundle; import android.os.VibrationEffect; import android.os.Vibrator; -import android.util.Log; import android.util.Size; import android.view.LayoutInflater; import android.view.View; @@ -32,6 +31,7 @@ import com.fadcam.CameraType; import com.fadcam.Constants; +import com.fadcam.Log; import com.fadcam.MainActivity; import com.fadcam.R; import com.fadcam.SharedPreferencesManager; @@ -101,6 +101,7 @@ private void updateButtonAppearance(MaterialButton button, boolean isSelected) { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + Log.init(requireContext()); locationHelper = new LocationHelper(requireContext()); sharedPreferencesManager = SharedPreferencesManager.getInstance(requireContext()); initializeCamcorderProfiles(); @@ -861,6 +862,8 @@ private List getCamcorderProfiles(CameraType cameraType) { CamcorderProfile profile = CamcorderProfile.get(cameraId, quality); if(profile != null) { profiles.add(profile); + + Log.d("SettingsFragment", cameraType + ": " + profile.videoFrameWidth + "x" + profile.videoFrameHeight + " (FPS: " + profile.videoFrameRate + ")"); } } }