Skip to content
Snippets Groups Projects
Commit a755a3b9 authored by Rohith Aralikatti's avatar Rohith Aralikatti
Browse files

Added Lab 8

parent da4ebe98
No related branches found
No related tags found
No related merge requests found
Showing
with 566 additions and 0 deletions
.idea/
.gradle/
build/
*.log
local.properties
File added
File added
/build
\ No newline at end of file
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'androidx.navigation.safeargs'
}
android {
compileSdkVersion build_versions.compile_sdk
defaultConfig {
minSdkVersion build_versions.min_sdk
targetSdkVersion build_versions.target_sdk
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
}
}
dependencies {
implementation deps.core_ktx
implementation deps.activity.activity_ktx
implementation deps.fragment.fragment_ktx
implementation deps.app_compat
implementation deps.material
implementation deps.constraint_layout
implementation deps.lifecycle.livedata_ktx
implementation deps.lifecycle.viewmodel_ktx
implementation deps.ktor.client_core
implementation deps.ktor.client_cio
implementation deps.gson
}
\ No newline at end of file
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="course.labs.networking">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.App">
<activity
android:name="course.labs.networking.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
\ No newline at end of file
package course.labs.networking
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
if (savedInstanceState == null) {
// TODO
// Initialize MainFragment
}
}
}
\ No newline at end of file
package course.labs.networking
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import course.labs.networking.MainViewModel.Status.*
import course.labs.networking.databinding.MainFragmentBinding
class MainFragment : Fragment() {
// Binding to XML layout
private lateinit var binding: MainFragmentBinding
// Defining the viewModel
private val viewModel by viewModels<MainViewModel>()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflating the fragment binding and returning the binding root
binding = MainFragmentBinding.inflate(inflater, container, false)
return binding.root
}
/**
* Called by the framework once the view has been created.
* This is the appropriate place to update the view or add
* listeners before the view is actually displayed.
**/
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Register an observer to the viewModel's LiveData and respond to status updates.
// 1. When the GET request is in progress, display the appropriate message.
// 2. When the bitmap is downloaded, display the QR code.
// 3. If an error is obtained, display the error message.
// 4. After steps 2 and 3, re-enable the button that downloads the QR code.
viewModel.statusLiveData.observe(viewLifecycleOwner) { status ->
when (status) {
is Progress -> {
// TODO
// Display a message saying that the GET request/download is in progress.
}
is Result -> {
// Display the image and update the text view.
binding.info.text = null
binding.info.setCompoundDrawablesWithIntrinsicBounds(null,null,null,
BitmapDrawable(resources,status.image)
)
// TODO
// Re-enable button.
}
is Error -> {
// TODO
// Display the error resource string and
// exception message in text view.
// TODO
// Re-enable button.
}
}
}
/**
* Set the button OnClickListener to call the network request
* defined in the viewModel
* **/
binding.button.setOnClickListener {
// TODO
// Disable button.
// TODO
// Clear output text from the previous request.
// TODO
// Use viewModel to send asynchronous network request.
}
}
}
\ No newline at end of file
package course.labs.networking
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import io.ktor.client.*
import io.ktor.client.request.*
import kotlinx.coroutines.*
class MainViewModel : ViewModel() {
// Defining a pair of LiveData and MutableLiveData variables of type Status
// The Status variable will be updated to display the downloaded bitmap, the status of the network request and
// error messages if any
private val _statusLiveData = MutableLiveData<Status>()
val statusLiveData: LiveData<Status>
get() = _statusLiveData
/** Keep track of coroutine so that duplicate requests are ignored. */
private var job: Job? = null
/**
* Defining a sealed class to store the status of the network request,
* the downloaded bitmap and the error message, if any
*/
sealed class Status {
data class Progress(val progress: Int) : Status()
data class Result(val image: Bitmap) : Status()
data class Error(val errorResId: Int, val e: Exception) : Status()
}
/**
* sendNetworkRequest() is a non-blocking function called by the UI (Fragment) when the
* user clicks the send button. This function starts a coroutine
* that sends a network request and then posts downloaded bitmap
* on the LiveData feed that can be observed by the calling
* Fragment.
*/
fun sendNetworkRequest() {
// TODO:
// Ignore button click if a request is still active.
// TODO:
// Update the Progress field of the Status LiveData feed and say that the GET request is being performed
// Launch a new coroutine to run network request in the background.
job = viewModelScope.launch {
try {
// TODO:
// Run the suspending network request to download the bitmap and store it in a variable
// TODO:
// Post the downloaded bitmap to the Result field of the Status LiveData Feed
} catch (e: Exception) {
// TODO:
// Something went wrong ... post an error message to the Error field of the Status LiveData feed.
}
}
}
/**
* makeNetworkCall() is a suspending helper function that performs the network request
* specified by the passed [url] and returns a Bitmap.
*/
private suspend fun makeNetworkCall(url: String): Bitmap =
withContext(Dispatchers.IO) {
// For testing purposes, simulate network delay
delay(2000)
// TODO:
// Construct a new Ktor HttpClient to perform the GET
// request and then return the resulting Bitmap
// Use the decodeStream function from BitmapFactory to decode the HttpClient output into a Bitmap
// Resize the Bitmap to dimension (SIZE, SIZE) using the createScaledBitmap function
// Replace the line below with the code to download the bitmap
// The line below has only been included so that the initial app compiles without errors
Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.RGB_565)
}
/**
* Constants
* Use these constants in the makeNetworkCall() function
* Download a square image of dimension (SIZE, SIZE) specified in URL below
*/
companion object {
private const val URL =
"https://image-charts.com/chart?chs=150x150&cht=qr&chl=https://www.cs.umd.edu/class/fall2022/cmsc436/&choe=UTF-8"
private const val SIZE = 450
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/rippleColor">
<item>
<selector>
<item android:state_checked="true">
<color android:color="?attr/colorControlHighlight" />
</item>
</selector>
</item>
</ripple>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path
android:fillColor="#3DDC84"
android:pathData="M0,0h108v108h-108z" />
<path
android:fillColor="#00000000"
android:pathData="M9,0L9,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,0L19,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M29,0L29,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M39,0L39,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M49,0L49,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M59,0L59,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M69,0L69,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M79,0L79,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M89,0L89,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M99,0L99,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,9L108,9"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,19L108,19"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,29L108,29"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,39L108,39"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,49L108,49"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,59L108,59"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,69L108,69"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,79L108,79"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,89L108,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,99L108,99"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,29L89,29"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,39L89,39"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,49L89,49"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,59L89,59"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,69L89,69"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,79L89,79"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M29,19L29,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M39,19L39,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M49,19L49,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M59,19L59,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M69,19L69,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M79,19L79,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
</vector>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path android:pathData="M31,63.928c0,0 6.4,-11 12.1,-13.1c7.2,-2.6 26,-1.4 26,-1.4l38.1,38.1L107,108.928l-32,-1L31,63.928z">
<aapt:attr name="android:fillColor">
<gradient
android:endX="85.84757"
android:endY="92.4963"
android:startX="42.9492"
android:startY="49.59793"
android:type="linear">
<item
android:color="#44000000"
android:offset="0.0" />
<item
android:color="#00000000"
android:offset="1.0" />
</gradient>
</aapt:attr>
</path>
<path
android:fillColor="#FFFFFF"
android:fillType="nonZero"
android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z"
android:strokeWidth="1"
android:strokeColor="#00000000" />
</vector>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/activity_margin">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginBottom="24dp"
android:text="@string/get_qr_string"
android:textColor="#212121"
app:layout_constraintBottom_toTopOf="@+id/info"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/click_send_to_issue_a_network_request"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button"
android:gravity="start"
android:textColor="@color/secondary_text"
android:contentDescription="QR Code">
</TextView>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>
\ No newline at end of file
Labs/Lab8_Networking/app/src/main/res/mipmap-hdpi/ic_launcher.webp

1.37 KiB

Labs/Lab8_Networking/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp

2.83 KiB

Labs/Lab8_Networking/app/src/main/res/mipmap-mdpi/ic_launcher.webp

982 B

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment