Skip to content

Commit c0d4dca

Browse files
Santosh KumarSantosh Kumar
Santosh Kumar
authored and
Santosh Kumar
committed
Initial commit
0 parents  commit c0d4dca

Some content is hidden

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

54 files changed

+1288
-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

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/compiler.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/jarRepositories.xml

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/.gitignore

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

app/build.gradle

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
plugins {
2+
id 'com.android.application'
3+
id 'kotlin-android'
4+
}
5+
6+
android {
7+
compileSdkVersion 30
8+
buildToolsVersion "30.0.0"
9+
10+
defaultConfig {
11+
applicationId "com.sample.easyworkmanager"
12+
minSdkVersion 16
13+
targetSdkVersion 30
14+
versionCode 1
15+
versionName "1.0"
16+
17+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
18+
}
19+
20+
buildTypes {
21+
release {
22+
minifyEnabled false
23+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
24+
}
25+
}
26+
compileOptions {
27+
sourceCompatibility JavaVersion.VERSION_1_8
28+
targetCompatibility JavaVersion.VERSION_1_8
29+
}
30+
kotlinOptions {
31+
jvmTarget = '1.8'
32+
}
33+
}
34+
35+
dependencies {
36+
37+
implementation project(':workmanager')
38+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
39+
implementation 'androidx.core:core-ktx:1.3.1'
40+
implementation 'androidx.appcompat:appcompat:1.2.0'
41+
implementation 'com.google.android.material:material:1.2.0'
42+
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
43+
testImplementation 'junit:junit:4.13'
44+
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
45+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
46+
implementation 'com.squareup.okhttp3:okhttp:4.8.1'
47+
}

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.sample.easyworkmanager
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.sample.easyworkmanager", appContext.packageName)
23+
}
24+
}

app/src/main/AndroidManifest.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.sample.easyworkmanager">
4+
5+
<uses-permission android:name="android.permission.INTERNET"/>
6+
7+
<application
8+
android:allowBackup="true"
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:usesCleartextTraffic="true"
14+
android:theme="@style/Theme.EasyWorkManager">
15+
<activity android:name=".MainActivity">
16+
<intent-filter>
17+
<action android:name="android.intent.action.MAIN" />
18+
19+
<category android:name="android.intent.category.LAUNCHER" />
20+
</intent-filter>
21+
</activity>
22+
</application>
23+
24+
</manifest>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.sample.easyworkmanager
2+
3+
import android.os.Build
4+
import android.os.Bundle
5+
import android.util.Log
6+
import androidx.annotation.RequiresApi
7+
import androidx.appcompat.app.AppCompatActivity
8+
import com.library.workmanager.ITaskExecutionCallback
9+
import com.library.workmanager.TaskScheduler
10+
import com.library.workmanager.WorkType
11+
import java.net.HttpURLConnection
12+
import java.net.URL
13+
14+
class MainActivity : AppCompatActivity(),ITaskExecutionCallback {
15+
override fun onCreate(savedInstanceState: Bundle?) {
16+
super.onCreate(savedInstanceState)
17+
val min=20*60000
18+
setContentView(R.layout.activity_main)
19+
20+
//Build Task scheduler with different options.
21+
TaskScheduler.Builder(this,this).
22+
setListener(this).setWorkType(WorkType.PERIODIC)
23+
.setPeriodicTime(min.toLong()).build()
24+
}
25+
26+
override fun onTaskExecutionInProgress() {
27+
Log.e("MainActivity","onTaskExecutionInProgress...")
28+
}
29+
30+
override fun onTaskExecutionCompleted() {
31+
Log.e("MainActivity","onTaskExecutionCompleted.")
32+
}
33+
34+
override fun onTaskExecutionFailed() {
35+
Log.e("MainActivity","onTaskExecutionFailed.")
36+
}
37+
38+
@RequiresApi(Build.VERSION_CODES.N)
39+
override fun startBackgroundTask() {
40+
Log.e("MainActivity","startBackgroundTask.")
41+
fetchDataFromApi()
42+
}
43+
44+
/**
45+
* Sample api call needs to be called on @startBackgroundTask().
46+
*/
47+
@RequiresApi(Build.VERSION_CODES.N)
48+
fun fetchDataFromApi() {
49+
val url = URL("http://www.google.com/")
50+
51+
with(url.openConnection() as HttpURLConnection) {
52+
// optional default is GET
53+
requestMethod = "GET"
54+
55+
println("\nSent 'GET' request to URL : $url; Response Code : $responseCode")
56+
57+
inputStream.bufferedReader().use {
58+
//To use this we @RequiresApi(Build.VERSION_CODES.N)
59+
it.lines().forEach { line ->
60+
Log.e("MainActivity",line)
61+
}
62+
}
63+
}
64+
}
65+
66+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.sample.easyworkmanager.custom
2+
3+
import android.os.Build
4+
import android.os.Bundle
5+
import android.util.Log
6+
import androidx.annotation.RequiresApi
7+
import androidx.appcompat.app.AppCompatActivity
8+
import com.library.workmanager.ITaskExecutionCallback
9+
import com.library.workmanager.WorkType
10+
import com.sample.easyworkmanager.R
11+
import java.net.HttpURLConnection
12+
import java.net.URL
13+
14+
class CustomWorkManagerActivity:AppCompatActivity(), ITaskExecutionCallback {
15+
16+
override fun onCreate(savedInstanceState: Bundle?) {
17+
super.onCreate(savedInstanceState)
18+
setContentView(R.layout.activity_main)
19+
20+
//Initialize your scheduler.
21+
val ownTaskScheduler= OwnTaskScheduler(
22+
this,
23+
this,
24+
this,
25+
WorkType.CUSTOM
26+
)
27+
// call your request.
28+
ownTaskScheduler.scheduleCustomRequest()
29+
}
30+
31+
override fun onTaskExecutionInProgress() {
32+
Log.e("CustMainActivity","onTaskExecutionInProgress...")
33+
}
34+
35+
override fun onTaskExecutionCompleted() {
36+
Log.e("CustMainActivity","onTaskExecutionCompleted.")
37+
}
38+
39+
override fun onTaskExecutionFailed() {
40+
Log.e("CustMainActivity","onTaskExecutionFailed.")
41+
}
42+
43+
@RequiresApi(Build.VERSION_CODES.N)
44+
override fun startBackgroundTask() {
45+
Log.e("CustMainActivity","startBackgroundTask.")
46+
fetchDataFromApi()
47+
}
48+
49+
/**
50+
* Sample api call needs to be called on @startBackgroundTask().
51+
*/
52+
@RequiresApi(Build.VERSION_CODES.N)
53+
fun fetchDataFromApi() {
54+
val url = URL("http://www.google.com/")
55+
56+
with(url.openConnection() as HttpURLConnection) {
57+
// optional default is GET
58+
requestMethod = "GET"
59+
60+
println("\nSent 'GET' request to URL : $url; Response Code : $responseCode")
61+
62+
inputStream.bufferedReader().use {
63+
//To use this we @RequiresApi(Build.VERSION_CODES.N)
64+
it.lines().forEach { line ->
65+
Log.e("CustMainActivity",line)
66+
}
67+
}
68+
}
69+
}
70+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.sample.easyworkmanager.custom
2+
3+
import android.content.Context
4+
import androidx.lifecycle.LifecycleOwner
5+
import androidx.work.Constraints
6+
import androidx.work.NetworkType
7+
import androidx.work.OneTimeWorkRequest
8+
import com.library.workmanager.BackGroundWorker
9+
import com.library.workmanager.ITaskExecutionCallback
10+
import com.library.workmanager.TaskScheduler
11+
import com.library.workmanager.WorkType
12+
13+
/**
14+
* Your own custom scheduler which can have other constraints and other type of work request.
15+
* Just extend {TaskScheduler class}.
16+
*/
17+
class OwnTaskScheduler(
18+
context: Context,
19+
lifecycleOwner: LifecycleOwner,
20+
iTaskExecutionCallback: ITaskExecutionCallback,
21+
workType: WorkType
22+
) : TaskScheduler(context, lifecycleOwner, iTaskExecutionCallback, workType) {
23+
24+
private lateinit var oneTimeWorkRequest: OneTimeWorkRequest
25+
26+
//Your own constraints.
27+
override var constraints: Constraints =
28+
Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()
29+
30+
//Your own request type.
31+
override fun scheduleCustomRequest() {
32+
oneTimeWorkRequest =
33+
OneTimeWorkRequest.Builder(BackGroundWorker::class.java)
34+
.setConstraints(constraints)
35+
.build()
36+
workManager.enqueue(oneTimeWorkRequest)
37+
//Set the callbacks to listen to changes.
38+
listenToCallbacks(oneTimeWorkRequest.id)
39+
}
40+
41+
}

0 commit comments

Comments
 (0)