diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 30b5cf0..dcf0c60 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -68,7 +68,7 @@ dependencies { debugImplementation(libs.ui.tooling) debugImplementation(libs.ui.test.manifest) - // Room Database - TAMBAHKAN INI SEMUA + // Room Database val roomVersion = "2.6.1" implementation("androidx.room:room-runtime:$roomVersion") implementation("androidx.room:room-ktx:$roomVersion") @@ -91,4 +91,7 @@ dependencies { // Gson implementation("com.google.code.gson:gson:2.10.1") + + // PDF Support + implementation("com.tom-roush:pdfbox-android:2.0.27.0") } \ No newline at end of file diff --git a/app/src/main/java/com/example/notebook/MainActivity.kt b/app/src/main/java/com/example/notebook/MainActivity.kt index d12da38..a8f648f 100644 --- a/app/src/main/java/com/example/notebook/MainActivity.kt +++ b/app/src/main/java/com/example/notebook/MainActivity.kt @@ -40,6 +40,10 @@ class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + + // Initialize PDFBox + com.example.notebook.utils.PdfHelper.initialize(this) + setContent { NotebookTheme { Surface( diff --git a/app/src/main/java/com/example/notebook/api/GeminiApiService.kt b/app/src/main/java/com/example/notebook/api/GeminiApiService.kt index 1e57b01..824688a 100644 --- a/app/src/main/java/com/example/notebook/api/GeminiApiService.kt +++ b/app/src/main/java/com/example/notebook/api/GeminiApiService.kt @@ -15,7 +15,7 @@ import java.util.concurrent.TimeUnit */ interface GeminiApiService { - @POST("v1beta/models/gemini-1.5-flash:generateContent") + @POST("v1beta/models/gemini-2.0-flash:generateContent") suspend fun generateContent( @Query("key") apiKey: String, @Body request: GeminiRequest diff --git a/app/src/main/java/com/example/notebook/utils/FileHelper.kt b/app/src/main/java/com/example/notebook/utils/FileHelper.kt index 1c97023..413b657 100644 --- a/app/src/main/java/com/example/notebook/utils/FileHelper.kt +++ b/app/src/main/java/com/example/notebook/utils/FileHelper.kt @@ -76,11 +76,27 @@ object FileHelper { } /** - * Baca text dari file + * Baca text dari file (support Text, Markdown, dan PDF) */ fun readTextFromFile(filePath: String): String? { return try { - File(filePath).readText() + val file = File(filePath) + val extension = file.extension.lowercase() + + when (extension) { + "pdf" -> { + // Extract text dari PDF + PdfHelper.extractTextFromPdf(filePath) + } + "txt", "md", "markdown" -> { + // Baca text biasa + file.readText() + } + else -> { + println("⚠️ Format file tidak didukung untuk ekstraksi teks: $extension") + null + } + } } catch (e: Exception) { e.printStackTrace() null diff --git a/app/src/main/java/com/example/notebook/utils/PdfHelper.kt b/app/src/main/java/com/example/notebook/utils/PdfHelper.kt new file mode 100644 index 0000000..c2fc1bf --- /dev/null +++ b/app/src/main/java/com/example/notebook/utils/PdfHelper.kt @@ -0,0 +1,93 @@ +package com.example.notebook.utils + +import android.content.Context +import android.net.Uri +import com.tom_roush.pdfbox.android.PDFBoxResourceLoader +import com.tom_roush.pdfbox.pdmodel.PDDocument +import com.tom_roush.pdfbox.text.PDFTextStripper +import java.io.File + +/** + * Helper untuk extract text dari PDF + */ +object PdfHelper { + + /** + * Initialize PDFBox (panggil sekali saat app start) + */ + fun initialize(context: Context) { + PDFBoxResourceLoader.init(context) + } + + /** + * Extract text dari PDF file + * Return: Text content atau null jika error + */ + fun extractTextFromPdf(filePath: String): String? { + return try { + val file = File(filePath) + val document = PDDocument.load(file) + + val stripper = PDFTextStripper() + val text = stripper.getText(document) + + document.close() + + // Cleanup whitespace + text.trim() + } catch (e: Exception) { + println("❌ Error extract PDF: ${e.message}") + e.printStackTrace() + null + } + } + + /** + * Extract text dari PDF URI (sebelum disimpan) + */ + fun extractTextFromPdfUri(context: Context, uri: Uri): String? { + return try { + val inputStream = context.contentResolver.openInputStream(uri) + val document = PDDocument.load(inputStream) + + val stripper = PDFTextStripper() + val text = stripper.getText(document) + + document.close() + inputStream?.close() + + text.trim() + } catch (e: Exception) { + println("❌ Error extract PDF from URI: ${e.message}") + e.printStackTrace() + null + } + } + + /** + * Get info PDF (jumlah halaman, dll) + */ + fun getPdfInfo(filePath: String): PdfInfo? { + return try { + val file = File(filePath) + val document = PDDocument.load(file) + + val info = PdfInfo( + pageCount = document.numberOfPages, + title = document.documentInformation.title ?: "Unknown", + author = document.documentInformation.author ?: "Unknown" + ) + + document.close() + info + } catch (e: Exception) { + null + } + } +} + +data class PdfInfo( + val pageCount: Int, + val title: String, + val author: String +) \ No newline at end of file