Skip to content

Commit fb518de

Browse files
committed
SQL example for Android Basics with Compose
1 parent 5b527c3 commit fb518de

Some content is hidden

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

47 files changed

+458
-231
lines changed

app/build.gradle

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021 The Android Open Source Project
2+
* Copyright (C) 2022 The Android Open Source Project
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,21 +16,25 @@
1616

1717
plugins {
1818
id 'com.android.application'
19-
id 'kotlin-android'
19+
id 'org.jetbrains.kotlin.android'
2020
id 'kotlin-kapt'
2121
}
2222

2323
android {
24-
compileSdkVersion 33
24+
namespace 'com.example.sqldemo'
25+
compileSdk 32
2526

2627
defaultConfig {
27-
applicationId "com.example.sqlbasics"
28-
minSdkVersion 19
29-
targetSdkVersion 33
28+
applicationId "com.example.sqldemo"
29+
minSdk 21
30+
targetSdk 32
3031
versionCode 1
3132
versionName "1.0"
3233

3334
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
35+
vectorDrawables {
36+
useSupportLibrary true
37+
}
3438
}
3539

3640
buildTypes {
@@ -46,23 +50,36 @@ android {
4650
kotlinOptions {
4751
jvmTarget = '1.8'
4852
}
53+
buildFeatures {
54+
compose true
55+
}
56+
composeOptions {
57+
kotlinCompilerExtensionVersion '1.1.1'
58+
}
59+
packagingOptions {
60+
resources {
61+
excludes += '/META-INF/{AL2.0,LGPL2.1}'
62+
}
63+
}
4964
}
5065

5166
dependencies {
5267
def room_version = '2.4.3'
5368

5469
implementation "androidx.room:room-runtime:$room_version"
5570
kapt "androidx.room:room-compiler:$room_version"
56-
57-
// optional - Kotlin Extensions and Coroutines support for Room
5871
implementation "androidx.room:room-ktx:$room_version"
5972

60-
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
61-
implementation 'androidx.core:core-ktx:1.9.0'
62-
implementation 'androidx.appcompat:appcompat:1.5.1'
63-
implementation 'com.google.android.material:material:1.7.0'
64-
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
73+
implementation 'androidx.core:core-ktx:1.7.0'
74+
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
75+
implementation 'androidx.activity:activity-compose:1.3.1'
76+
implementation "androidx.compose.ui:ui:$compose_ui_version"
77+
implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
78+
implementation 'androidx.compose.material:material:1.1.1'
6579
testImplementation 'junit:junit:4.13.2'
6680
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
6781
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
68-
}
82+
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
83+
debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
84+
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"
85+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.sqldemo
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.example.sqldemo", appContext.packageName)
23+
}
24+
}

app/src/main/AndroidManifest.xml

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,32 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<!--
3-
Copyright (C) 2021 The Android Open Source Project
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
-->
162
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
17-
package="com.example.sqlbasics">
3+
xmlns:tools="http://schemas.android.com/tools">
184

195
<application
206
android:allowBackup="true"
7+
android:dataExtractionRules="@xml/data_extraction_rules"
8+
android:fullBackupContent="@xml/backup_rules"
219
android:icon="@mipmap/ic_launcher"
2210
android:label="@string/app_name"
2311
android:roundIcon="@mipmap/ic_launcher_round"
2412
android:supportsRtl="true"
25-
android:theme="@style/Theme.SQLBasics">
26-
<activity android:name=".MainActivity"
27-
android:exported="true">
13+
android:theme="@style/Theme.SQLDemo"
14+
tools:targetApi="31">
15+
<activity
16+
android:name=".MainActivity"
17+
android:exported="true"
18+
android:label="@string/app_name"
19+
android:theme="@style/Theme.SQLDemo">
2820
<intent-filter>
2921
<action android:name="android.intent.action.MAIN" />
22+
3023
<category android:name="android.intent.category.LAUNCHER" />
3124
</intent-filter>
25+
26+
<meta-data
27+
android:name="android.app.lib_name"
28+
android:value="" />
3229
</activity>
3330
</application>
3431

35-
</manifest>
32+
</manifest>

app/src/main/assets/database/Email.db

12 KB
Binary file not shown.

app/src/main/java/com/example/sqlbasics/AppDatabase.kt renamed to app/src/main/java/com/example/sqldemo/AppDatabase.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021 The Android Open Source Project
2+
* Copyright (C) 2022 The Android Open Source Project
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,16 +13,17 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.example.sqlbasics
16+
17+
package com.example.sqldemo
1718

1819
import android.content.Context
1920
import androidx.room.Database
2021
import androidx.room.Room
2122
import androidx.room.RoomDatabase
2223

23-
@Database(entities = arrayOf(CaliforniaPark::class), version = 1)
24+
@Database(entities = arrayOf(Email::class), version = 1)
2425
abstract class AppDatabase: RoomDatabase() {
25-
abstract fun californiaParkDao(): CaliforniaParkDao
26+
abstract fun emailDao(): EmailDao
2627

2728
companion object {
2829
@Volatile
@@ -37,12 +38,12 @@ abstract class AppDatabase: RoomDatabase() {
3738
AppDatabase::class.java,
3839
"app_database"
3940
)
40-
.createFromAsset("database/sql_basics.db")
41+
.createFromAsset("database/Email.db")
4142
.build()
4243
INSTANCE = instance
4344

4445
instance
4546
}
4647
}
4748
}
48-
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021 The Android Open Source Project
2+
* Copyright (C) 2022 The Android Open Source Project
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,19 +13,20 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.example.sqlbasics
16+
17+
package com.example.sqldemo
1718

1819
import androidx.room.ColumnInfo
1920
import androidx.room.Entity
2021
import androidx.room.PrimaryKey
2122

22-
@Entity(tableName = "park")
23-
data class CaliforniaPark(
23+
@Entity(tableName = "email")
24+
data class Email(
2425
@PrimaryKey(autoGenerate = true) val id: Int,
25-
@ColumnInfo(name = "name") val name: String,
26-
@ColumnInfo(name = "city") val city: String,
27-
@ColumnInfo(name = "area_acres") val acres: Int,
28-
@ColumnInfo(name = "park_visitors") val visitors: Int?,
29-
@ColumnInfo(name = "established") val established: Long,
30-
@ColumnInfo(name = "type") val type: String
26+
@ColumnInfo(name = "subject") val subject: String,
27+
@ColumnInfo(name = "sender") val sender: String,
28+
@ColumnInfo(name = "folder") val folder: String,
29+
@ColumnInfo(name = "starred") val starred: Boolean,
30+
@ColumnInfo(name = "read") val read: Boolean,
31+
@ColumnInfo(name = "received") val received: Int
3132
)
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021 The Android Open Source Project
2+
* Copyright (C) 2022 The Android Open Source Project
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,16 +13,14 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.example.sqlbasics
16+
17+
package com.example.sqldemo
1718

1819
import androidx.room.Dao
19-
import androidx.room.Insert
2020
import androidx.room.Query
2121

2222
@Dao
23-
interface CaliforniaParkDao {
24-
@Insert
25-
suspend fun insertAll(parks: List<CaliforniaPark>)
26-
@Query("SELECT * FROM park")
27-
suspend fun getAll(): List<CaliforniaPark>
23+
interface EmailDao {
24+
@Query("SELECT * FROM email")
25+
suspend fun getAll(): List<Email>
2826
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (C) 2022 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.sqldemo
18+
19+
import android.os.Bundle
20+
import androidx.activity.ComponentActivity
21+
import androidx.activity.compose.setContent
22+
import androidx.compose.foundation.layout.Box
23+
import androidx.compose.foundation.layout.fillMaxSize
24+
import androidx.compose.material.MaterialTheme
25+
import androidx.compose.material.Surface
26+
import androidx.compose.material.Text
27+
import androidx.compose.ui.Alignment
28+
import androidx.compose.ui.Modifier
29+
import com.example.sqldemo.ui.theme.SQLDemoTheme
30+
import kotlinx.coroutines.GlobalScope
31+
import kotlinx.coroutines.launch
32+
33+
class MainActivity : ComponentActivity() {
34+
override fun onCreate(savedInstanceState: Bundle?) {
35+
super.onCreate(savedInstanceState)
36+
GlobalScope.launch {
37+
AppDatabase.getDatabase(applicationContext).emailDao().getAll()
38+
}
39+
setContent {
40+
SQLDemoTheme {
41+
// A surface container using the 'background' color from the theme
42+
Surface(
43+
modifier = Modifier.fillMaxSize(),
44+
color = MaterialTheme.colors.background
45+
) {
46+
Box(
47+
modifier = Modifier.fillMaxSize(),
48+
contentAlignment = Alignment.Center
49+
) {
50+
Text("The database is ready!")
51+
}
52+
}
53+
}
54+
}
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021 The Android Open Source Project
2+
* Copyright (C) 2022 The Android Open Source Project
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,19 +13,12 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.example.sqlbasics
1716

18-
import androidx.appcompat.app.AppCompatActivity
19-
import android.os.Bundle
20-
import kotlinx.coroutines.GlobalScope
21-
import kotlinx.coroutines.launch
17+
package com.example.sqldemo.ui.theme
2218

23-
class MainActivity : AppCompatActivity() {
24-
override fun onCreate(savedInstanceState: Bundle?) {
25-
super.onCreate(savedInstanceState)
26-
setContentView(R.layout.activity_main)
27-
GlobalScope.launch {
28-
AppDatabase.getDatabase(applicationContext).californiaParkDao().getAll()
29-
}
30-
}
31-
}
19+
import androidx.compose.ui.graphics.Color
20+
21+
val Purple200 = Color(0xFFBB86FC)
22+
val Purple500 = Color(0xFF6200EE)
23+
val Purple700 = Color(0xFF3700B3)
24+
val Teal200 = Color(0xFF03DAC5)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (C) 2022 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.sqldemo.ui.theme
18+
19+
import androidx.compose.foundation.shape.RoundedCornerShape
20+
import androidx.compose.material.Shapes
21+
import androidx.compose.ui.unit.dp
22+
23+
val Shapes = Shapes(
24+
small = RoundedCornerShape(4.dp),
25+
medium = RoundedCornerShape(4.dp),
26+
large = RoundedCornerShape(0.dp)
27+
)

0 commit comments

Comments
 (0)