native-update-capacitor 0.0.0-stage → 4.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AI-INTEGRATION-GUIDE.md +578 -0
- package/LICENSE +25 -0
- package/NativeUpdate.podspec +19 -0
- package/README.md +574 -2
- package/android/build.gradle +125 -0
- package/android/consumer-rules.pro +9 -0
- package/android/gradle/wrapper/gradle-wrapper.properties +8 -0
- package/android/gradle.properties +17 -0
- package/android/manifest-additions.xml +28 -0
- package/android/proguard-rules.pro +26 -0
- package/android/settings.gradle +2 -0
- package/android/src/main/AndroidManifest.xml +13 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/AppReviewPlugin.kt +159 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/AppUpdatePlugin.kt +314 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundNotificationManager.kt +406 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdateManager.kt +48 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdatePlugin.kt +343 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdateWorker.kt +236 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/JsonOptional.kt +26 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/LiveUpdatePlugin.kt +1249 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/NativeUpdatePlugin.kt +404 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/NotificationActionReceiver.kt +107 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/SecurityManager.kt +240 -0
- package/android/variables.gradle +5 -0
- package/cli/commands/backend-create.js +623 -0
- package/cli/commands/bundle-create.js +145 -0
- package/cli/commands/bundle-sign.js +64 -0
- package/cli/commands/bundle-verify.js +58 -0
- package/cli/commands/init.js +146 -0
- package/cli/commands/keys-generate.js +58 -0
- package/cli/commands/monitor.js +89 -0
- package/cli/commands/remote-shared.js +262 -0
- package/cli/commands/remote.js +622 -0
- package/cli/commands/server-start.js +96 -0
- package/cli/index.js +605 -0
- package/cli/package.json +21 -0
- package/dist/esm/app-review/app-review-manager.d.ts +24 -0
- package/dist/esm/app-review/app-review-manager.js +194 -0
- package/dist/esm/app-review/index.d.ts +5 -0
- package/dist/esm/app-review/index.js +5 -0
- package/dist/esm/app-review/platform-review-handler.d.ts +20 -0
- package/dist/esm/app-review/platform-review-handler.js +137 -0
- package/dist/esm/app-review/review-conditions-checker.d.ts +22 -0
- package/dist/esm/app-review/review-conditions-checker.js +154 -0
- package/dist/esm/app-review/review-rate-limiter.d.ts +23 -0
- package/dist/esm/app-review/review-rate-limiter.js +163 -0
- package/dist/esm/app-review/types.d.ts +41 -0
- package/dist/esm/app-review/types.js +1 -0
- package/dist/esm/app-update/app-update-checker.d.ts +58 -0
- package/dist/esm/app-update/app-update-checker.js +227 -0
- package/dist/esm/app-update/app-update-installer.d.ts +20 -0
- package/dist/esm/app-update/app-update-installer.js +134 -0
- package/dist/esm/app-update/app-update-manager.d.ts +28 -0
- package/dist/esm/app-update/app-update-manager.js +198 -0
- package/dist/esm/app-update/app-update-notifier.d.ts +14 -0
- package/dist/esm/app-update/app-update-notifier.js +99 -0
- package/dist/esm/app-update/index.d.ts +6 -0
- package/dist/esm/app-update/index.js +6 -0
- package/dist/esm/app-update/platform-app-update.d.ts +19 -0
- package/dist/esm/app-update/platform-app-update.js +138 -0
- package/dist/esm/app-update/types.d.ts +58 -0
- package/dist/esm/app-update/types.js +11 -0
- package/dist/esm/background-update/background-scheduler.d.ts +63 -0
- package/dist/esm/background-update/background-scheduler.js +332 -0
- package/dist/esm/background-update/index.d.ts +3 -0
- package/dist/esm/background-update/index.js +2 -0
- package/dist/esm/background-update/notification-manager.d.ts +29 -0
- package/dist/esm/background-update/notification-manager.js +88 -0
- package/dist/esm/config/support.d.ts +9 -0
- package/dist/esm/config/support.js +9 -0
- package/dist/esm/core/analytics.d.ts +83 -0
- package/dist/esm/core/analytics.js +151 -0
- package/dist/esm/core/cache-manager.d.ts +72 -0
- package/dist/esm/core/cache-manager.js +274 -0
- package/dist/esm/core/config.d.ts +69 -0
- package/dist/esm/core/config.js +107 -0
- package/dist/esm/core/errors.d.ts +56 -0
- package/dist/esm/core/errors.js +85 -0
- package/dist/esm/core/event-emitter.d.ts +30 -0
- package/dist/esm/core/event-emitter.js +80 -0
- package/dist/esm/core/logger.d.ts +21 -0
- package/dist/esm/core/logger.js +108 -0
- package/dist/esm/core/performance.d.ts +53 -0
- package/dist/esm/core/performance.js +153 -0
- package/dist/esm/core/plugin-manager.d.ts +88 -0
- package/dist/esm/core/plugin-manager.js +224 -0
- package/dist/esm/core/security.d.ts +123 -0
- package/dist/esm/core/security.js +361 -0
- package/dist/esm/core/update-check.d.ts +121 -0
- package/dist/esm/core/update-check.js +127 -0
- package/dist/esm/definitions.d.ts +872 -0
- package/dist/esm/definitions.js +104 -0
- package/dist/esm/index.d.ts +14 -0
- package/dist/esm/index.js +18 -0
- package/dist/esm/live-update/bundle-manager.d.ts +102 -0
- package/dist/esm/live-update/bundle-manager.js +344 -0
- package/dist/esm/live-update/certificate-pinning.d.ts +38 -0
- package/dist/esm/live-update/certificate-pinning.js +77 -0
- package/dist/esm/live-update/delta-processor.d.ts +84 -0
- package/dist/esm/live-update/delta-processor.js +257 -0
- package/dist/esm/live-update/download-manager.d.ts +106 -0
- package/dist/esm/live-update/download-manager.js +412 -0
- package/dist/esm/live-update/rollout-checker.d.ts +86 -0
- package/dist/esm/live-update/rollout-checker.js +304 -0
- package/dist/esm/live-update/update-manager.d.ts +63 -0
- package/dist/esm/live-update/update-manager.js +321 -0
- package/dist/esm/live-update/version-manager.d.ts +93 -0
- package/dist/esm/live-update/version-manager.js +390 -0
- package/dist/esm/plugin.d.ts +3 -0
- package/dist/esm/plugin.js +848 -0
- package/dist/esm/security/crypto.d.ts +88 -0
- package/dist/esm/security/crypto.js +228 -0
- package/dist/esm/security/validator.d.ts +60 -0
- package/dist/esm/security/validator.js +142 -0
- package/dist/esm/types/app-identity.d.ts +36 -0
- package/dist/esm/types/app-identity.js +1 -0
- package/dist/esm/types/rollout.d.ts +91 -0
- package/dist/esm/types/rollout.js +25 -0
- package/dist/esm/web.d.ts +130 -0
- package/dist/esm/web.js +981 -0
- package/dist/plugin.cjs +1 -0
- package/dist/plugin.esm.js +1 -0
- package/dist/plugin.js +2 -0
- package/docs/CHANGELOG.md +993 -0
- package/docs/KNOWN_LIMITATIONS.md +235 -0
- package/docs/MIGRATION.md +391 -0
- package/docs/protocol/CLIENT-PROTOCOL.md +178 -0
- package/docs/server-requirements.md +211 -0
- package/ios/Plugin/AppReview/AppReviewPlugin.swift +158 -0
- package/ios/Plugin/AppUpdate/AppUpdatePlugin.swift +249 -0
- package/ios/Plugin/BackgroundUpdate/BackgroundNotificationManager.swift +333 -0
- package/ios/Plugin/BackgroundUpdate/BackgroundUpdatePlugin.swift +464 -0
- package/ios/Plugin/Info.plist +43 -0
- package/ios/Plugin/LiveUpdate/LiveUpdatePlugin.swift +1286 -0
- package/ios/Plugin/LiveUpdate/WebViewConfiguration.swift +45 -0
- package/ios/Plugin/NativeUpdatePlugin.m +53 -0
- package/ios/Plugin/NativeUpdatePlugin.swift +315 -0
- package/ios/Plugin/Security/SecurityManager.swift +289 -0
- package/package.json +135 -4
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
buildscript {
|
|
2
|
+
repositories {
|
|
3
|
+
google()
|
|
4
|
+
mavenCentral()
|
|
5
|
+
}
|
|
6
|
+
dependencies {
|
|
7
|
+
classpath 'com.android.tools.build:gradle:8.13.2'
|
|
8
|
+
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:2.2.20'
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
ext {
|
|
13
|
+
kotlinVersion = '2.2.20'
|
|
14
|
+
compileSdkVersion = 36
|
|
15
|
+
targetSdkVersion = 36
|
|
16
|
+
// Capacitor Android 8.5 declares API 26; the plugin cannot advertise or
|
|
17
|
+
// assemble against a lower manifest floor than its required peer.
|
|
18
|
+
minSdkVersion = 26
|
|
19
|
+
androidxAppCompatVersion = '1.7.1'
|
|
20
|
+
// 1.19 requires compileSdk 37 + AGP 9.1; Capacitor 8's supported line is AGP 8/API 36.
|
|
21
|
+
androidxCoreVersion = '1.17.0'
|
|
22
|
+
androidxWorkVersion = '2.11.2'
|
|
23
|
+
playAppUpdateVersion = '2.1.0'
|
|
24
|
+
playAppUpdateKtxVersion = '2.1.0'
|
|
25
|
+
playReviewVersion = '2.0.2'
|
|
26
|
+
playReviewKtxVersion = '2.0.2'
|
|
27
|
+
okhttpVersion = '5.4.0'
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
apply plugin: 'com.android.library'
|
|
31
|
+
apply plugin: 'kotlin-android'
|
|
32
|
+
|
|
33
|
+
android {
|
|
34
|
+
namespace = "com.aoneahsan.nativeupdate"
|
|
35
|
+
compileSdk = project.compileSdkVersion
|
|
36
|
+
|
|
37
|
+
defaultConfig {
|
|
38
|
+
minSdkVersion project.minSdkVersion
|
|
39
|
+
targetSdkVersion project.targetSdkVersion
|
|
40
|
+
versionCode 1
|
|
41
|
+
versionName "1.0"
|
|
42
|
+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
|
43
|
+
// Shipped to every consuming app's R8 run (ISSUE-09).
|
|
44
|
+
consumerProguardFiles 'consumer-rules.pro'
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
buildTypes {
|
|
48
|
+
release {
|
|
49
|
+
minifyEnabled false
|
|
50
|
+
// MUST be the -optimize variant. The current Android Gradle Plugin hard-rejects
|
|
51
|
+
// 'proguard-android.txt' (it bundles -dontoptimize), and the call is evaluated at
|
|
52
|
+
// CONFIGURATION time - so a consuming app's release build fails at evaluation of
|
|
53
|
+
// :native-update even with minifyEnabled false. Every other Capacitor plugin already
|
|
54
|
+
// uses the -optimize file. See docs/REPORTED-ISSUES.md ISSUE-01.
|
|
55
|
+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
lint {
|
|
60
|
+
abortOnError = true
|
|
61
|
+
warningsAsErrors = true
|
|
62
|
+
// Capacitor 8 consumers are still on AGP 8; AGP 9 is a deliberate v5 migration.
|
|
63
|
+
// KTX preference hints are style-only and do not affect correctness.
|
|
64
|
+
disable += ['AndroidGradlePluginVersion', 'GradleDependency', 'NewerVersionAvailable', 'UseKtx']
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
compileOptions {
|
|
68
|
+
sourceCompatibility JavaVersion.VERSION_21
|
|
69
|
+
targetCompatibility JavaVersion.VERSION_21
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
kotlinOptions {
|
|
73
|
+
jvmTarget = '21'
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
testOptions {
|
|
77
|
+
unitTests {
|
|
78
|
+
includeAndroidResources = true
|
|
79
|
+
returnDefaultValues = true
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
repositories {
|
|
85
|
+
google()
|
|
86
|
+
mavenCentral()
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
dependencies {
|
|
90
|
+
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
|
91
|
+
implementation project(':capacitor-android')
|
|
92
|
+
|
|
93
|
+
// Kotlin
|
|
94
|
+
implementation 'org.jetbrains.kotlin:kotlin-stdlib:2.2.20'
|
|
95
|
+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.11.0'
|
|
96
|
+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.11.0'
|
|
97
|
+
|
|
98
|
+
// AndroidX
|
|
99
|
+
implementation "androidx.appcompat:appcompat:${androidxAppCompatVersion}"
|
|
100
|
+
implementation "androidx.core:core-ktx:${androidxCoreVersion}"
|
|
101
|
+
implementation "androidx.work:work-runtime-ktx:${androidxWorkVersion}"
|
|
102
|
+
|
|
103
|
+
// Google Play Services - Using explicit versions to avoid resolution issues
|
|
104
|
+
implementation 'com.google.android.play:app-update:2.1.0'
|
|
105
|
+
implementation 'com.google.android.play:app-update-ktx:2.1.0'
|
|
106
|
+
implementation "com.google.android.play:review:${playReviewVersion}"
|
|
107
|
+
implementation "com.google.android.play:review-ktx:${playReviewKtxVersion}"
|
|
108
|
+
|
|
109
|
+
// OkHttp for secure network requests
|
|
110
|
+
implementation "com.squareup.okhttp3:okhttp:${okhttpVersion}"
|
|
111
|
+
implementation "com.squareup.okhttp3:okhttp-tls:${okhttpVersion}"
|
|
112
|
+
|
|
113
|
+
// JSON parsing
|
|
114
|
+
implementation 'com.google.code.gson:gson:2.14.0'
|
|
115
|
+
|
|
116
|
+
// Testing
|
|
117
|
+
testImplementation 'junit:junit:4.13.2'
|
|
118
|
+
testImplementation 'org.mockito:mockito-core:5.23.0'
|
|
119
|
+
testImplementation 'org.mockito.kotlin:mockito-kotlin:6.3.0'
|
|
120
|
+
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.11.0'
|
|
121
|
+
testImplementation 'com.google.truth:truth:1.4.5'
|
|
122
|
+
testImplementation 'org.robolectric:robolectric:4.16.1'
|
|
123
|
+
androidTestImplementation 'androidx.test.ext:junit:1.3.0'
|
|
124
|
+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.7.0'
|
|
125
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Rules applied to every app that builds with native-update (android/build.gradle -> consumerProguardFiles), so an
|
|
2
|
+
# app's release build with R8 (minifyEnabled true) needs no rules of its own. ISSUE-09.
|
|
3
|
+
|
|
4
|
+
# The plugin is reached through Capacitor's bridge by reflection.
|
|
5
|
+
-keep class com.aoneahsan.nativeupdate.** { *; }
|
|
6
|
+
|
|
7
|
+
# com.google.android.play:review-ktx references a compile-time-only nullness annotation that is not on any app's
|
|
8
|
+
# runtime classpath; without this R8 stops the release build with "Missing class ...NoNullnessRewrite".
|
|
9
|
+
-dontwarn com.google.android.gms.common.annotation.NoNullnessRewrite
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# Gradle Wrapper Properties
|
|
2
|
+
distributionBase=GRADLE_USER_HOME
|
|
3
|
+
distributionPath=wrapper/dists
|
|
4
|
+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.5-all.zip
|
|
5
|
+
networkTimeout=10000
|
|
6
|
+
validateDistributionUrl=true
|
|
7
|
+
zipStoreBase=GRADLE_USER_HOME
|
|
8
|
+
zipStorePath=wrapper/dists
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Project-wide Gradle settings.
|
|
2
|
+
|
|
3
|
+
# IDE (e.g. Android Studio) users:
|
|
4
|
+
# Gradle settings configured through the IDE *will override*
|
|
5
|
+
# any settings specified in this file.
|
|
6
|
+
|
|
7
|
+
# For more details on how to configure your build environment visit
|
|
8
|
+
# http://www.gradle.org/docs/current/userguide/build_environment.html
|
|
9
|
+
|
|
10
|
+
# Enable AndroidX
|
|
11
|
+
android.useAndroidX=true
|
|
12
|
+
|
|
13
|
+
# Automatically convert third-party libraries to use AndroidX
|
|
14
|
+
android.enableJetifier=true
|
|
15
|
+
|
|
16
|
+
# Kotlin code style
|
|
17
|
+
kotlin.code.style=official
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
IMPORTANT: These elements MUST be added to your app's AndroidManifest.xml inside the <application> tag
|
|
3
|
+
when using background update features of the native-update plugin.
|
|
4
|
+
|
|
5
|
+
Without these additions:
|
|
6
|
+
1. Background updates will crash with ServiceNotFoundException
|
|
7
|
+
2. Update notification buttons won't work (they'll appear but won't respond to taps)
|
|
8
|
+
3. Downloads won't continue when app is in background
|
|
9
|
+
|
|
10
|
+
Location: android/app/src/main/AndroidManifest.xml (in your app, not in the plugin)
|
|
11
|
+
-->
|
|
12
|
+
|
|
13
|
+
<!-- WorkManager service for background updates -->
|
|
14
|
+
<service
|
|
15
|
+
android:name="androidx.work.impl.foreground.SystemForegroundService"
|
|
16
|
+
android:foregroundServiceType="dataSync"
|
|
17
|
+
tools:node="merge" />
|
|
18
|
+
|
|
19
|
+
<!-- Broadcast receiver for notification actions -->
|
|
20
|
+
<receiver
|
|
21
|
+
android:name="com.aoneahsan.nativeupdate.NotificationActionReceiver"
|
|
22
|
+
android:exported="false">
|
|
23
|
+
<intent-filter>
|
|
24
|
+
<action android:name="com.aoneahsan.nativeupdate.UPDATE_NOW" />
|
|
25
|
+
<action android:name="com.aoneahsan.nativeupdate.UPDATE_LATER" />
|
|
26
|
+
<action android:name="com.aoneahsan.nativeupdate.DISMISS" />
|
|
27
|
+
</intent-filter>
|
|
28
|
+
</receiver>
|
|
@@ -0,0 +1,26 @@
|
|
|
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
|
+
# Uncomment this to preserve the line number information for
|
|
9
|
+
# debugging stack traces.
|
|
10
|
+
-keepattributes SourceFile,LineNumberTable
|
|
11
|
+
|
|
12
|
+
# If you keep the line number information, uncomment this to
|
|
13
|
+
# hide the original source file name.
|
|
14
|
+
#-renamesourcefileattribute SourceFile
|
|
15
|
+
|
|
16
|
+
# Capacitor Native Update Plugin
|
|
17
|
+
-keep class com.aoneahsan.nativeupdate.** { *; }
|
|
18
|
+
|
|
19
|
+
# OkHttp
|
|
20
|
+
-dontwarn okhttp3.**
|
|
21
|
+
-dontwarn okio.**
|
|
22
|
+
-dontwarn javax.annotation.**
|
|
23
|
+
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase
|
|
24
|
+
|
|
25
|
+
# Google Play Services
|
|
26
|
+
-keep class com.google.android.play.** { *; }
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
|
2
|
+
|
|
3
|
+
<!-- Required permissions -->
|
|
4
|
+
<uses-permission android:name="android.permission.INTERNET" />
|
|
5
|
+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
|
6
|
+
|
|
7
|
+
<!-- Background update permissions -->
|
|
8
|
+
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
|
9
|
+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
|
10
|
+
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
|
11
|
+
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
|
12
|
+
|
|
13
|
+
</manifest>
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
package com.aoneahsan.nativeupdate
|
|
2
|
+
|
|
3
|
+
import android.app.Activity
|
|
4
|
+
import android.content.Context
|
|
5
|
+
import com.getcapacitor.JSObject
|
|
6
|
+
import com.getcapacitor.PluginCall
|
|
7
|
+
import com.google.android.play.core.review.ReviewInfo
|
|
8
|
+
import com.google.android.play.core.review.ReviewManager
|
|
9
|
+
import com.google.android.play.core.review.ReviewManagerFactory
|
|
10
|
+
import java.util.concurrent.TimeUnit
|
|
11
|
+
|
|
12
|
+
class AppReviewPlugin(
|
|
13
|
+
private val activity: Activity,
|
|
14
|
+
private val context: Context
|
|
15
|
+
) {
|
|
16
|
+
private val reviewManager: ReviewManager = ReviewManagerFactory.create(context)
|
|
17
|
+
private var config: JSObject? = null
|
|
18
|
+
private val prefs = context.getSharedPreferences("native_update_review", Context.MODE_PRIVATE)
|
|
19
|
+
|
|
20
|
+
companion object {
|
|
21
|
+
private const val PREF_INSTALL_DATE = "install_date"
|
|
22
|
+
private const val PREF_LAST_REVIEW_REQUEST = "last_review_request"
|
|
23
|
+
private const val PREF_LAUNCH_COUNT = "launch_count"
|
|
24
|
+
private const val PREF_REVIEW_SHOWN_COUNT = "review_shown_count"
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
init {
|
|
28
|
+
// Initialize install date if not set
|
|
29
|
+
if (!prefs.contains(PREF_INSTALL_DATE)) {
|
|
30
|
+
prefs.edit().putLong(PREF_INSTALL_DATE, System.currentTimeMillis()).apply()
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Increment launch count
|
|
34
|
+
incrementLaunchCount()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
fun configure(config: JSObject) {
|
|
38
|
+
this.config = config
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
fun requestReview(call: PluginCall) {
|
|
42
|
+
// First check if we can request a review
|
|
43
|
+
val canRequestResult = checkCanRequestReview()
|
|
44
|
+
|
|
45
|
+
if (!canRequestResult.first) {
|
|
46
|
+
val result = JSObject()
|
|
47
|
+
result.put("shown", false)
|
|
48
|
+
result.put("displayed", false)
|
|
49
|
+
result.put("error", canRequestResult.second)
|
|
50
|
+
call.resolve(result)
|
|
51
|
+
return
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Request review info
|
|
55
|
+
val request = reviewManager.requestReviewFlow()
|
|
56
|
+
|
|
57
|
+
request.addOnCompleteListener { task ->
|
|
58
|
+
if (task.isSuccessful) {
|
|
59
|
+
val reviewInfo = task.result
|
|
60
|
+
launchReviewFlow(reviewInfo, call)
|
|
61
|
+
} else {
|
|
62
|
+
val result = JSObject()
|
|
63
|
+
result.put("shown", false)
|
|
64
|
+
result.put("displayed", false)
|
|
65
|
+
result.put("error", "Failed to request review flow")
|
|
66
|
+
call.resolve(result)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
fun canRequestReview(call: PluginCall) {
|
|
72
|
+
val (allowed, reason) = checkCanRequestReview()
|
|
73
|
+
|
|
74
|
+
val result = JSObject()
|
|
75
|
+
result.put("allowed", allowed)
|
|
76
|
+
// The typed name (CanRequestReviewResult.canRequest); "allowed" stays for one minor release (ISSUE-11).
|
|
77
|
+
result.put("canRequest", allowed)
|
|
78
|
+
if (!allowed) {
|
|
79
|
+
result.put("reason", reason)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
call.resolve(result)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private fun launchReviewFlow(reviewInfo: ReviewInfo, call: PluginCall) {
|
|
86
|
+
val flow = reviewManager.launchReviewFlow(activity, reviewInfo)
|
|
87
|
+
|
|
88
|
+
flow.addOnCompleteListener { _ ->
|
|
89
|
+
// The flow has finished. The API does not indicate whether the user
|
|
90
|
+
// reviewed or not, or even whether the review dialog was shown.
|
|
91
|
+
// Update last review request time
|
|
92
|
+
prefs.edit().putLong(PREF_LAST_REVIEW_REQUEST, System.currentTimeMillis()).apply()
|
|
93
|
+
|
|
94
|
+
// Increment shown count
|
|
95
|
+
val shownCount = prefs.getInt(PREF_REVIEW_SHOWN_COUNT, 0)
|
|
96
|
+
prefs.edit().putInt(PREF_REVIEW_SHOWN_COUNT, shownCount + 1).apply()
|
|
97
|
+
|
|
98
|
+
val result = JSObject()
|
|
99
|
+
result.put("shown", true)
|
|
100
|
+
result.put("displayed", true)
|
|
101
|
+
call.resolve(result)
|
|
102
|
+
}.addOnFailureListener { e ->
|
|
103
|
+
val result = JSObject()
|
|
104
|
+
result.put("shown", false)
|
|
105
|
+
result.put("displayed", false)
|
|
106
|
+
result.put("error", e.message)
|
|
107
|
+
call.resolve(result)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private fun checkCanRequestReview(): Pair<Boolean, String?> {
|
|
112
|
+
val now = System.currentTimeMillis()
|
|
113
|
+
val installDate = prefs.getLong(PREF_INSTALL_DATE, now)
|
|
114
|
+
val lastReviewRequest = prefs.getLong(PREF_LAST_REVIEW_REQUEST, 0)
|
|
115
|
+
val launchCount = prefs.getInt(PREF_LAUNCH_COUNT, 0)
|
|
116
|
+
val shownCount = prefs.getInt(PREF_REVIEW_SHOWN_COUNT, 0)
|
|
117
|
+
|
|
118
|
+
// Check if debug mode is enabled
|
|
119
|
+
val debugMode = config?.getBool("debugMode") ?: false
|
|
120
|
+
if (debugMode) {
|
|
121
|
+
return Pair(true, null)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Check minimum days since install
|
|
125
|
+
val minDaysSinceInstall = config?.getInteger("minimumDaysSinceInstall") ?: 7
|
|
126
|
+
val daysSinceInstall = TimeUnit.MILLISECONDS.toDays(now - installDate)
|
|
127
|
+
if (daysSinceInstall < minDaysSinceInstall) {
|
|
128
|
+
return Pair(false, "Not enough days since install")
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Check minimum days since last prompt
|
|
132
|
+
if (lastReviewRequest > 0) {
|
|
133
|
+
val minDaysSinceLastPrompt = config?.getInteger("minimumDaysSinceLastPrompt") ?: 90
|
|
134
|
+
val daysSinceLastPrompt = TimeUnit.MILLISECONDS.toDays(now - lastReviewRequest)
|
|
135
|
+
if (daysSinceLastPrompt < minDaysSinceLastPrompt) {
|
|
136
|
+
return Pair(false, "Too soon since last review request")
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Check minimum launch count
|
|
141
|
+
val minLaunchCount = config?.getInteger("minimumLaunchCount") ?: 3
|
|
142
|
+
if (launchCount < minLaunchCount) {
|
|
143
|
+
return Pair(false, "Not enough app launches")
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Google Play has internal quotas that we can't check directly
|
|
147
|
+
// But we can limit our own requests to be safe
|
|
148
|
+
if (shownCount >= 3) {
|
|
149
|
+
return Pair(false, "Review quota exceeded")
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return Pair(true, null)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private fun incrementLaunchCount() {
|
|
156
|
+
val currentCount = prefs.getInt(PREF_LAUNCH_COUNT, 0)
|
|
157
|
+
prefs.edit().putInt(PREF_LAUNCH_COUNT, currentCount + 1).apply()
|
|
158
|
+
}
|
|
159
|
+
}
|