Skip to content

Commit 2a12f27

Browse files
committed
Initial commit
0 parents  commit 2a12f27

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1114
-0
lines changed

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle.kts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
plugins {
2+
alias(libs.plugins.androidApplication)
3+
alias(libs.plugins.jetbrainsKotlinAndroid)
4+
}
5+
6+
android {
7+
namespace = "com.kaushalvasava.apps.documentscanner"
8+
compileSdk = 34
9+
10+
defaultConfig {
11+
applicationId = "com.kaushalvasava.apps.documentscanner"
12+
minSdk = 24
13+
targetSdk = 34
14+
versionCode = 1
15+
versionName = "1.0"
16+
17+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
18+
vectorDrawables {
19+
useSupportLibrary = true
20+
}
21+
}
22+
23+
buildTypes {
24+
release {
25+
isMinifyEnabled = false
26+
proguardFiles(
27+
getDefaultProguardFile("proguard-android-optimize.txt"),
28+
"proguard-rules.pro"
29+
)
30+
}
31+
}
32+
compileOptions {
33+
sourceCompatibility = JavaVersion.VERSION_1_8
34+
targetCompatibility = JavaVersion.VERSION_1_8
35+
}
36+
kotlinOptions {
37+
jvmTarget = "1.8"
38+
}
39+
buildFeatures {
40+
compose = true
41+
}
42+
composeOptions {
43+
kotlinCompilerExtensionVersion = "1.5.1"
44+
}
45+
packaging {
46+
resources {
47+
excludes += "/META-INF/{AL2.0,LGPL2.1}"
48+
}
49+
}
50+
}
51+
52+
dependencies {
53+
54+
implementation(libs.androidx.core.ktx)
55+
implementation(libs.androidx.lifecycle.runtime.ktx)
56+
implementation(libs.androidx.activity.compose)
57+
implementation(platform(libs.androidx.compose.bom))
58+
implementation(libs.androidx.ui)
59+
implementation(libs.androidx.ui.graphics)
60+
implementation(libs.androidx.ui.tooling.preview)
61+
implementation(libs.androidx.material3)
62+
testImplementation(libs.junit)
63+
androidTestImplementation(libs.androidx.junit)
64+
androidTestImplementation(libs.androidx.espresso.core)
65+
androidTestImplementation(platform(libs.androidx.compose.bom))
66+
androidTestImplementation(libs.androidx.ui.test.junit4)
67+
debugImplementation(libs.androidx.ui.tooling)
68+
debugImplementation(libs.androidx.ui.test.manifest)
69+
70+
implementation(libs.play.services.mlkit.document.scanner)
71+
}

app/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.kaushalvasava.apps.documentscanner
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.kaushalvasava.app.documentscanner", appContext.packageName)
23+
}
24+
}

app/src/main/AndroidManifest.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:dataExtractionRules="@xml/data_extraction_rules"
8+
android:fullBackupContent="@xml/backup_rules"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:roundIcon="@mipmap/ic_launcher_round"
12+
android:supportsRtl="true"
13+
android:theme="@style/Theme.DocumentScanner"
14+
tools:targetApi="31">
15+
<activity
16+
android:name=".MainActivity"
17+
android:exported="true"
18+
android:theme="@style/Theme.DocumentScanner">
19+
<intent-filter>
20+
<action android:name="android.intent.action.MAIN" />
21+
22+
<category android:name="android.intent.category.LAUNCHER" />
23+
</intent-filter>
24+
</activity>
25+
26+
<provider
27+
android:name="androidx.core.content.FileProvider"
28+
android:authorities="${applicationId}.provider"
29+
android:exported="false"
30+
android:grantUriPermissions="true">
31+
<meta-data
32+
android:name="android.support.FILE_PROVIDER_PATHS"
33+
android:resource="@xml/filepaths"/>
34+
</provider>
35+
</application>
36+
37+
</manifest>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.kaushalvasava.apps.documentscanner
2+
3+
import android.os.Bundle
4+
import androidx.activity.ComponentActivity
5+
import androidx.activity.compose.setContent
6+
import androidx.compose.foundation.layout.fillMaxSize
7+
import androidx.compose.material3.MaterialTheme
8+
import androidx.compose.material3.Surface
9+
import androidx.compose.ui.Modifier
10+
import com.kaushalvasava.apps.documentscanner.ui.screen.HomeScreen
11+
import com.kaushalvasava.apps.documentscanner.ui.theme.DocumentScannerTheme
12+
13+
class MainActivity : ComponentActivity() {
14+
override fun onCreate(savedInstanceState: Bundle?) {
15+
super.onCreate(savedInstanceState)
16+
setContent {
17+
DocumentScannerTheme {
18+
// A surface container using the 'background' color from the theme
19+
Surface(
20+
modifier = Modifier.fillMaxSize(),
21+
color = MaterialTheme.colorScheme.background
22+
) {
23+
HomeScreen()
24+
}
25+
}
26+
}
27+
}
28+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package com.kaushalvasava.apps.documentscanner.ui.screen
2+
3+
import android.app.Activity
4+
import android.content.Intent
5+
import android.util.Log
6+
import androidx.activity.compose.rememberLauncherForActivityResult
7+
import androidx.activity.result.IntentSenderRequest
8+
import androidx.activity.result.contract.ActivityResultContracts
9+
import androidx.compose.foundation.Image
10+
import androidx.compose.foundation.layout.Arrangement
11+
import androidx.compose.foundation.layout.PaddingValues
12+
import androidx.compose.foundation.layout.Row
13+
import androidx.compose.foundation.layout.fillMaxHeight
14+
import androidx.compose.foundation.layout.fillMaxSize
15+
import androidx.compose.foundation.layout.fillMaxWidth
16+
import androidx.compose.foundation.layout.height
17+
import androidx.compose.foundation.layout.padding
18+
import androidx.compose.foundation.lazy.LazyColumn
19+
import androidx.compose.foundation.lazy.items
20+
import androidx.compose.material3.Card
21+
import androidx.compose.material3.ExperimentalMaterial3Api
22+
import androidx.compose.material3.ExtendedFloatingActionButton
23+
import androidx.compose.material3.Icon
24+
import androidx.compose.material3.Scaffold
25+
import androidx.compose.material3.Surface
26+
import androidx.compose.material3.Text
27+
import androidx.compose.material3.TopAppBar
28+
import androidx.compose.material3.TopAppBarDefaults
29+
import androidx.compose.runtime.Composable
30+
import androidx.compose.runtime.mutableStateListOf
31+
import androidx.compose.runtime.remember
32+
import androidx.compose.ui.Alignment
33+
import androidx.compose.ui.Modifier
34+
import androidx.compose.ui.platform.LocalContext
35+
import androidx.compose.ui.res.painterResource
36+
import androidx.compose.ui.res.stringResource
37+
import androidx.compose.ui.unit.dp
38+
import androidx.core.content.FileProvider
39+
import androidx.core.net.toFile
40+
import com.google.mlkit.vision.documentscanner.GmsDocumentScannerOptions
41+
import com.google.mlkit.vision.documentscanner.GmsDocumentScanning
42+
import com.google.mlkit.vision.documentscanner.GmsDocumentScanningResult
43+
import com.kaushalvasava.apps.documentscanner.MainActivity
44+
import com.kaushalvasava.apps.documentscanner.R
45+
46+
typealias Pdf = GmsDocumentScanningResult.Pdf
47+
48+
49+
@OptIn(ExperimentalMaterial3Api::class)
50+
@Composable
51+
fun HomeScreen() {
52+
53+
val activity = LocalContext.current as MainActivity
54+
val options = remember { getOptions() }
55+
val docs = remember {
56+
mutableStateListOf<Pdf>()
57+
}
58+
val scannerLauncher = rememberLauncherForActivityResult(
59+
contract =
60+
ActivityResultContracts.StartIntentSenderForResult()
61+
) { result ->
62+
if (result.resultCode == Activity.RESULT_OK) {
63+
val scanningResult =
64+
GmsDocumentScanningResult.fromActivityResultIntent(result.data)
65+
scanningResult?.pdf?.let { pdf ->
66+
docs.add(pdf)
67+
}
68+
}
69+
}
70+
val scanner = remember {
71+
GmsDocumentScanning.getClient(options)
72+
}
73+
Scaffold(
74+
topBar = {
75+
TopAppBar(
76+
title = { Text(stringResource(R.string.app_name)) },
77+
colors = TopAppBarDefaults.mediumTopAppBarColors()
78+
)
79+
},
80+
floatingActionButton = {
81+
ExtendedFloatingActionButton(
82+
onClick = {
83+
scanner.getStartScanIntent(activity)
84+
.addOnSuccessListener { intentSender ->
85+
scannerLauncher.launch(
86+
IntentSenderRequest.Builder(intentSender).build()
87+
)
88+
}
89+
.addOnFailureListener {
90+
Log.d("TAG", "HomeScreen: ${it.message}")
91+
}
92+
},
93+
text = {
94+
Text(text = stringResource(R.string.scan))
95+
},
96+
icon = {
97+
Icon(
98+
painterResource(id = R.drawable.ic_camera),
99+
contentDescription = null,
100+
)
101+
}
102+
)
103+
},
104+
content = {
105+
Surface(modifier = Modifier.padding(it)) {
106+
LazyColumn(
107+
modifier = Modifier.fillMaxSize(),
108+
contentPadding = PaddingValues(10.dp)
109+
) {
110+
items(items = docs) {
111+
Card(modifier = Modifier
112+
.fillMaxWidth()
113+
.height(80.dp)
114+
.padding(10.dp),
115+
onClick = {
116+
val pdfUri = it.uri
117+
val pdfFileUri = FileProvider.getUriForFile(
118+
activity,
119+
activity.packageName + ".provider",
120+
pdfUri.toFile()
121+
)
122+
val browserIntent = Intent(Intent.ACTION_VIEW, pdfFileUri)
123+
browserIntent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
124+
activity.startActivity(browserIntent)
125+
}) {
126+
Row(
127+
modifier = Modifier
128+
.fillMaxSize()
129+
.padding(10.dp),
130+
horizontalArrangement = Arrangement.spacedBy(10.dp),
131+
verticalAlignment = Alignment.CenterVertically
132+
) {
133+
Image(
134+
modifier = Modifier.fillMaxHeight(),
135+
painter = painterResource(id = R.drawable.ic_pdf),
136+
contentDescription = null,
137+
)
138+
Text(text = it.uri.lastPathSegment ?: "")
139+
}
140+
}
141+
}
142+
}
143+
}
144+
}
145+
)
146+
}
147+
148+
149+
fun getOptions(): GmsDocumentScannerOptions {
150+
return GmsDocumentScannerOptions.Builder()
151+
.setGalleryImportAllowed(true)
152+
.setResultFormats(GmsDocumentScannerOptions.RESULT_FORMAT_PDF)
153+
.setScannerMode(GmsDocumentScannerOptions.SCANNER_MODE_FULL)
154+
.build()
155+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.kaushalvasava.apps.documentscanner.ui.theme
2+
3+
import androidx.compose.ui.graphics.Color
4+
5+
val Purple80 = Color(0xFFD0BCFF)
6+
val PurpleGrey80 = Color(0xFFCCC2DC)
7+
val Pink80 = Color(0xFFEFB8C8)
8+
9+
val Purple40 = Color(0xFF6650a4)
10+
val PurpleGrey40 = Color(0xFF625b71)
11+
val Pink40 = Color(0xFF7D5260)

0 commit comments

Comments
 (0)