forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Expensify#54616 from callstack-internal/szymonrybc…
…zak/feat/android/setup-background-task feat(Android): setup background task
- Loading branch information
Showing
12 changed files
with
269 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
buildscript { | ||
// Buildscript is evaluated before everything else so we can't use getExtOrDefault | ||
ext.getExtOrDefault = {name -> | ||
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['<%- project.name -%>_' + name] | ||
} | ||
|
||
repositories { | ||
google() | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
// noinspection DifferentKotlinGradleVersion | ||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}" | ||
} | ||
} | ||
|
||
def reactNativeArchitectures() { | ||
def value = rootProject.getProperties().get("reactNativeArchitectures") | ||
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"] | ||
} | ||
|
||
def isNewArchitectureEnabled() { | ||
return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true" | ||
} | ||
|
||
apply plugin: "com.android.library" | ||
apply plugin: "kotlin-android" | ||
|
||
if (isNewArchitectureEnabled()) { | ||
apply plugin: "com.facebook.react" | ||
} | ||
|
||
def getExtOrIntegerDefault(name) { | ||
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["ReactNativeBackgroundTask_" + name]).toInteger() | ||
} | ||
|
||
android { | ||
namespace "com.expensify.reactnativebackgroundtask" | ||
|
||
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion") | ||
|
||
defaultConfig { | ||
minSdkVersion getExtOrIntegerDefault("minSdkVersion") | ||
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion") | ||
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString() | ||
|
||
} | ||
|
||
buildFeatures { | ||
buildConfig true | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
} | ||
} | ||
|
||
lintOptions { | ||
disable "GradleCompatible" | ||
} | ||
|
||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
|
||
sourceSets { | ||
main { | ||
if (isNewArchitectureEnabled()) { | ||
java.srcDirs += [ | ||
"src/newarch", | ||
// Codegen specs | ||
"generated/java", | ||
"generated/jni" | ||
] | ||
} else { | ||
java.srcDirs += ["src/oldarch"] | ||
} | ||
} | ||
} | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
google() | ||
} | ||
|
||
def kotlin_version = getExtOrDefault("kotlinVersion") | ||
|
||
dependencies { | ||
//noinspection GradleDynamicVersion | ||
implementation "com.facebook.react:react-android" | ||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" | ||
} | ||
|
||
if (isNewArchitectureEnabled()) { | ||
react { | ||
jsRootDir = file("../src/") | ||
libraryName = "ReactNativeBackgroundTask" | ||
codegenJavaPackageName = "com.expensify.reactnativebackgroundtask" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
ReactNativeBackgroundTask_kotlinVersion=1.9.24 | ||
ReactNativeBackgroundTask_minSdkVersion=23 | ||
ReactNativeBackgroundTask_targetSdkVersion=34 | ||
ReactNativeBackgroundTask_compileSdkVersion=34 | ||
ReactNativeBackgroundTask_ndkversion=26.1.10909125 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> | ||
</manifest> |
25 changes: 25 additions & 0 deletions
25
...ask/android/src/main/java/com/expensify/reactnativebackgroundtask/BackgroundJobService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.expensify.reactnativebackgroundtask | ||
|
||
import android.app.job.JobParameters | ||
import android.app.job.JobService | ||
import android.content.Intent | ||
import android.util.Log | ||
import com.facebook.react.ReactApplication | ||
|
||
class BackgroundJobService : JobService() { | ||
override fun onStartJob(params: JobParameters?): Boolean { | ||
val taskName = params?.extras?.getString("taskName") | ||
val intent = Intent("com.expensify.reactnativebackgroundtask.TASK_ACTION").apply { | ||
putExtra("taskName", taskName) | ||
} | ||
sendBroadcast(intent) | ||
|
||
// Job is done, return false if no more work is needed | ||
return false | ||
} | ||
|
||
override fun onStopJob(params: JobParameters?): Boolean { | ||
// Return true to reschedule the job | ||
return true | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
.../src/main/java/com/expensify/reactnativebackgroundtask/ReactNativeBackgroundTaskModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.expensify.reactnativebackgroundtask | ||
|
||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.bridge.ReactMethod | ||
import com.facebook.react.bridge.Promise | ||
import com.facebook.react.bridge.Callback | ||
import android.app.job.JobScheduler | ||
import android.app.job.JobInfo | ||
import android.content.BroadcastReceiver | ||
import android.content.ComponentName | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.IntentFilter | ||
import android.os.PersistableBundle | ||
import android.util.Log | ||
|
||
class ReactNativeBackgroundTaskModule internal constructor(context: ReactApplicationContext) : | ||
ReactNativeBackgroundTaskSpec(context) { | ||
|
||
private val taskReceiver = object : BroadcastReceiver() { | ||
override fun onReceive(context: Context?, intent: Intent?) { | ||
val taskName = intent?.getStringExtra("taskName") | ||
Log.d("ReactNativeBackgroundTaskModule", "Received task: $taskName") | ||
emitOnBackgroundTaskExecution(taskName) | ||
} | ||
} | ||
|
||
init { | ||
val filter = IntentFilter("com.expensify.reactnativebackgroundtask.TASK_ACTION") | ||
reactApplicationContext.registerReceiver(taskReceiver, filter) | ||
} | ||
|
||
override fun getName(): String { | ||
return NAME | ||
} | ||
|
||
@ReactMethod | ||
override fun defineTask(taskName: String, taskExecutor: Callback, promise: Promise) { | ||
try { | ||
val jobScheduler = reactApplicationContext.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler | ||
val componentName = ComponentName(reactApplicationContext, BackgroundJobService::class.java) | ||
|
||
val extras = PersistableBundle().apply { | ||
putString("taskName", taskName) | ||
} | ||
|
||
val jobInfo = JobInfo.Builder(taskName.hashCode() and 0xFFFFFF, componentName) | ||
.setPeriodic(15 * 60 * 1000L) // 15 minutes in milliseconds | ||
.setPersisted(true) // Job persists after reboot | ||
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) | ||
.setExtras(extras) | ||
.build() | ||
|
||
val resultCode = jobScheduler.schedule(jobInfo) | ||
if (resultCode == JobScheduler.RESULT_SUCCESS) { | ||
|
||
promise.resolve(null); | ||
} else { | ||
promise.reject("ERROR", "Failed to schedule job") | ||
} | ||
} catch (e: Exception) { | ||
promise.reject("ERROR", e.message) | ||
} | ||
} | ||
|
||
companion object { | ||
const val NAME = "ReactNativeBackgroundTask" | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...src/main/java/com/expensify/reactnativebackgroundtask/ReactNativeBackgroundTaskPackage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.expensify.reactnativebackgroundtask | ||
|
||
import com.facebook.react.TurboReactPackage | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.bridge.NativeModule | ||
import com.facebook.react.module.model.ReactModuleInfoProvider | ||
import com.facebook.react.module.model.ReactModuleInfo | ||
import java.util.HashMap | ||
|
||
class ReactNativeBackgroundTaskPackage : TurboReactPackage() { | ||
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? { | ||
return if (name == ReactNativeBackgroundTaskModule.NAME) { | ||
ReactNativeBackgroundTaskModule(reactContext) | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider { | ||
return ReactModuleInfoProvider { | ||
val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap() | ||
val isTurboModule: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED | ||
moduleInfos[ReactNativeBackgroundTaskModule.NAME] = ReactModuleInfo( | ||
ReactNativeBackgroundTaskModule.NAME, | ||
ReactNativeBackgroundTaskModule.NAME, | ||
false, // canOverrideExistingModule | ||
false, // needsEagerInit | ||
true, // hasConstants | ||
false, // isCxxModule | ||
isTurboModule // isTurboModule | ||
) | ||
moduleInfos | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
modules/background-task/android/src/newarch/ReactNativeBackgroundTaskSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.expensify.reactnativebackgroundtask | ||
|
||
import com.facebook.react.bridge.ReactApplicationContext | ||
|
||
abstract class ReactNativeBackgroundTaskSpec internal constructor(context: ReactApplicationContext) : | ||
NativeReactNativeBackgroundTaskSpec(context) { | ||
} |
11 changes: 11 additions & 0 deletions
11
modules/background-task/android/src/oldarch/ReactNativeBackgroundTaskSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.expensify.reactnativebackgroundtask | ||
|
||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule | ||
import com.facebook.react.bridge.Promise | ||
|
||
abstract class ReactNativeBackgroundTaskSpec internal constructor(context: ReactApplicationContext) : | ||
ReactContextBaseJavaModule(context) { | ||
|
||
abstract fun defineTask(taskName: String, taskExecutor: Callback, promise: Promise) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// This file is intentionally empty as Background Tasks are currently only implemented | ||
// for native mobile platforms. See `index.native.ts` for the native implementation. |