Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Santosh Kumar authored and Santosh Kumar committed Sep 2, 2020
0 parents commit c0d4dca
Show file tree
Hide file tree
Showing 54 changed files with 1,288 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
47 changes: 47 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
}

android {
compileSdkVersion 30
buildToolsVersion "30.0.0"

defaultConfig {
applicationId "com.sample.easyworkmanager"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"

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'
}
}

dependencies {

implementation project(':workmanager')
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.squareup.okhttp3:okhttp:4.8.1'
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.sample.easyworkmanager

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.sample.easyworkmanager", appContext.packageName)
}
}
24 changes: 24 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.easyworkmanager">

<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:usesCleartextTraffic="true"
android:theme="@style/Theme.EasyWorkManager">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
66 changes: 66 additions & 0 deletions app/src/main/java/com/sample/easyworkmanager/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.sample.easyworkmanager

import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.library.workmanager.ITaskExecutionCallback
import com.library.workmanager.TaskScheduler
import com.library.workmanager.WorkType
import java.net.HttpURLConnection
import java.net.URL

class MainActivity : AppCompatActivity(),ITaskExecutionCallback {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val min=20*60000
setContentView(R.layout.activity_main)

//Build Task scheduler with different options.
TaskScheduler.Builder(this,this).
setListener(this).setWorkType(WorkType.PERIODIC)
.setPeriodicTime(min.toLong()).build()
}

override fun onTaskExecutionInProgress() {
Log.e("MainActivity","onTaskExecutionInProgress...")
}

override fun onTaskExecutionCompleted() {
Log.e("MainActivity","onTaskExecutionCompleted.")
}

override fun onTaskExecutionFailed() {
Log.e("MainActivity","onTaskExecutionFailed.")
}

@RequiresApi(Build.VERSION_CODES.N)
override fun startBackgroundTask() {
Log.e("MainActivity","startBackgroundTask.")
fetchDataFromApi()
}

/**
* Sample api call needs to be called on @startBackgroundTask().
*/
@RequiresApi(Build.VERSION_CODES.N)
fun fetchDataFromApi() {
val url = URL("http://www.google.com/")

with(url.openConnection() as HttpURLConnection) {
// optional default is GET
requestMethod = "GET"

println("\nSent 'GET' request to URL : $url; Response Code : $responseCode")

inputStream.bufferedReader().use {
//To use this we @RequiresApi(Build.VERSION_CODES.N)
it.lines().forEach { line ->
Log.e("MainActivity",line)
}
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.sample.easyworkmanager.custom

import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.library.workmanager.ITaskExecutionCallback
import com.library.workmanager.WorkType
import com.sample.easyworkmanager.R
import java.net.HttpURLConnection
import java.net.URL

class CustomWorkManagerActivity:AppCompatActivity(), ITaskExecutionCallback {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

//Initialize your scheduler.
val ownTaskScheduler= OwnTaskScheduler(
this,
this,
this,
WorkType.CUSTOM
)
// call your request.
ownTaskScheduler.scheduleCustomRequest()
}

override fun onTaskExecutionInProgress() {
Log.e("CustMainActivity","onTaskExecutionInProgress...")
}

override fun onTaskExecutionCompleted() {
Log.e("CustMainActivity","onTaskExecutionCompleted.")
}

override fun onTaskExecutionFailed() {
Log.e("CustMainActivity","onTaskExecutionFailed.")
}

@RequiresApi(Build.VERSION_CODES.N)
override fun startBackgroundTask() {
Log.e("CustMainActivity","startBackgroundTask.")
fetchDataFromApi()
}

/**
* Sample api call needs to be called on @startBackgroundTask().
*/
@RequiresApi(Build.VERSION_CODES.N)
fun fetchDataFromApi() {
val url = URL("http://www.google.com/")

with(url.openConnection() as HttpURLConnection) {
// optional default is GET
requestMethod = "GET"

println("\nSent 'GET' request to URL : $url; Response Code : $responseCode")

inputStream.bufferedReader().use {
//To use this we @RequiresApi(Build.VERSION_CODES.N)
it.lines().forEach { line ->
Log.e("CustMainActivity",line)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.sample.easyworkmanager.custom

import android.content.Context
import androidx.lifecycle.LifecycleOwner
import androidx.work.Constraints
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequest
import com.library.workmanager.BackGroundWorker
import com.library.workmanager.ITaskExecutionCallback
import com.library.workmanager.TaskScheduler
import com.library.workmanager.WorkType

/**
* Your own custom scheduler which can have other constraints and other type of work request.
* Just extend {TaskScheduler class}.
*/
class OwnTaskScheduler(
context: Context,
lifecycleOwner: LifecycleOwner,
iTaskExecutionCallback: ITaskExecutionCallback,
workType: WorkType
) : TaskScheduler(context, lifecycleOwner, iTaskExecutionCallback, workType) {

private lateinit var oneTimeWorkRequest: OneTimeWorkRequest

//Your own constraints.
override var constraints: Constraints =
Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()

//Your own request type.
override fun scheduleCustomRequest() {
oneTimeWorkRequest =
OneTimeWorkRequest.Builder(BackGroundWorker::class.java)
.setConstraints(constraints)
.build()
workManager.enqueue(oneTimeWorkRequest)
//Set the callbacks to listen to changes.
listenToCallbacks(oneTimeWorkRequest.id)
}

}
Loading

0 comments on commit c0d4dca

Please sign in to comment.