Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!(android): migrate source code to kotlin #123

Merged
merged 57 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
c880fca
feat!(android): add kotlin support
erisu Jul 22, 2021
786c733
refactor!: convert PushInstanceIDListenerService to Kotlin
erisu Jul 22, 2021
1459826
refactor!: convert PushDismissedHandler to Kotlin
erisu Jul 22, 2021
0469513
refactor!: convert BackgroundActionButtonHandler to Kotlin
erisu Jul 22, 2021
f225fd2
refactor!: convert PushConstants to Kotlin
erisu Jul 22, 2021
4f2917c
refactor!: convert BackgroundHandlerActivity to Kotlin
erisu Jul 22, 2021
c6bc80e
refactor!: convert PushHandlerActivity to Kotlin
erisu Jul 22, 2021
0bbb17d
refactor!: convert PushPlugin to Kotlin
erisu Jul 22, 2021
76b0437
refactor!: convert FCMService to Kotlin
erisu Jul 22, 2021
b4d07d9
fix: target-dir path for kotlin files
erisu Jul 22, 2021
73bee19
refactor: BackgroundActionButtonHandler
erisu Jul 22, 2021
e469530
refactor: PushInstanceIDListenerService
erisu Jul 22, 2021
b280a89
refactor: BackgroundHandlerActivity
erisu Jul 22, 2021
6ca4704
refactor: PushDismissedHandler
erisu Jul 22, 2021
628877a
refactor: PushHandlerActivity
erisu Jul 22, 2021
d484094
refactor: PushConstants
erisu Jul 22, 2021
66cc630
refactor: PushPlugin
erisu Jul 22, 2021
aa9aae2
refactor: PushPlugin - separate INITIALIZE action
erisu Jul 22, 2021
b90a20d
refactor: PushPlugin - separate UNREGISTER action
erisu Jul 22, 2021
ac3e6b2
refactor: PushPlugin - separate HAS_PERMISSION action
erisu Jul 22, 2021
a2fcfc3
refactor: PushPlugin - separate SET_APPLICATION_ICON_BADGE_NUMBER action
erisu Jul 22, 2021
688fdb3
refactor: PushPlugin cleanup
erisu Jul 22, 2021
fa95b5e
refactor: PushPlugin - separate GET_APPLICATION_ICON_BADGE_NUMBER & C…
erisu Jul 22, 2021
8423dac
refactor: PushPlugin - separate SUBSCRIBE action
erisu Jul 22, 2021
585274a
refactor: PushPlugin cleanup
erisu Jul 22, 2021
2671b93
refactor: PushPlugin - separate UNSUBSCRIBE action
erisu Jul 22, 2021
72de808
refactor: PushPlugin - separate CREATE_CHANNEL action
erisu Jul 22, 2021
14ecfe8
refactor: PushPlugin - separate DELETE_CHANNEL action
erisu Jul 22, 2021
90f6111
refactor: PushPlugin - separate LIST_CHANNELS action
erisu Jul 22, 2021
654b4c3
refactor: PushPlugin - separate CLEAR_NOTIFICATION action
erisu Jul 22, 2021
2e49bc7
refactor: Invalid Execute Action Log
erisu Jul 22, 2021
bdf80b4
refactor: PushPlugin
erisu Jul 22, 2021
56c6fa5
refactor: remove unused sendError
erisu Jul 22, 2021
24c1950
refactor: remove unused import
erisu Jul 22, 2021
13b5bb5
refactor: PushPlugin - moved compainion object to top
erisu Jul 22, 2021
7c5b9e6
refactor: PushPlugin - commenting
erisu Jul 22, 2021
79bea9c
refactor: PushPlugin - move onPause super call to bottom
erisu Jul 22, 2021
2706662
refactor: PushPlugin - removed unused var registration_id
erisu Jul 22, 2021
dca9b83
refactor: PushPlugin - removed getStringResourceByName
erisu Jul 22, 2021
71ae5cc
refactor: PushPlugin - convertBundleToJson
erisu Jul 22, 2021
823e22a
chore(lint): remove comments from private methods (CommentOverPrivate…
erisu Jul 22, 2021
30529b2
chore(lint): move comments from private methods inside (CommentOverPr…
erisu Jul 22, 2021
dde024e
refactor: FCMService - reformat
erisu Jul 23, 2021
8fc9b4e
refactor: FCMService - reformat again
erisu Jul 23, 2021
ba75005
refactor: FCMService - reformat some more
erisu Jul 23, 2021
0fe3b21
refactor: add IS_ENABLED to PushConstants
erisu Jul 23, 2021
e3d53b0
refactor: FCMService LOG_TAG to TAG
erisu Jul 23, 2021
0c46f18
chore: remove lint warning CommentOverPrivateProperty
erisu Jul 23, 2021
49a594c
chore: fix some lint warnings in FCMService
erisu Jul 23, 2021
2037e40
chore: removing duplicated code
erisu Jul 23, 2021
360df16
chore: removing duplicated code (pushSharedPref)
erisu Jul 23, 2021
ec643b7
refactor: FCMService continues
erisu Jul 23, 2021
5fd16ec
refactor: FCMService continues
erisu Jul 23, 2021
16cf9ca
fix: remove AppCompatActivity reference
erisu Jul 28, 2021
1adef43
chore: type cast cordova.activity as Activity
erisu Jul 29, 2021
c030692
fix: remove some code warnings in FCMService
erisu Aug 14, 2021
7d65012
chore: update tag to include plugin name for better logcat filtering
erisu Aug 14, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions hooks/android/beforeCompile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { join } = require('path');
const { existsSync, readFileSync, writeFileSync } = require('fs');
const { parseElementtreeSync: ParseElementtreeSync } = require('cordova-common/src/util/xml-helpers');
const platform = require('cordova-android');

module.exports = function (context) {
if (!isExecutable()) {
console.log('[cordova-plugin-push::before-compile] skipping before_compile hookscript.');
return;
}

const buildGradleFilePath = join(context.opts.projectRoot, 'platforms/android/build.gradle');

if (!existsSync(buildGradleFilePath)) {
console.log('[cordova-plugin-push::before-compile] could not find "build.gradle" file.');
return;
}

updateBuildGradle(context, buildGradleFilePath);
};

/**
* This hookscript is executable only when the platform version less then 10.x
* @returns Boolean
*/
function isExecutable () {
const majorVersion = parseInt(platform.version(), 10);
return majorVersion < 10 && majorVersion >= 9;
}

function getPluginKotlinVersion (context) {
const pluginConfig = new ParseElementtreeSync(join(context.opts.projectRoot, 'plugins/@havesource/cordova-plugin-push/plugin.xml'));

return pluginConfig
.findall('./platform[@name="android"]').pop()
.findall('./config-file[@target="config.xml"]').pop()
.findall('preference').filter(
elem => elem.attrib.name.toLowerCase() === 'GradlePluginKotlinVersion'.toLowerCase()
).pop().attrib.value;
}

function updateBuildGradle (context, buildGradleFilePath) {
const kotlinVersion = getPluginKotlinVersion(context);
const updateContent = readFileSync(buildGradleFilePath, 'utf8')
.replace(/ext.kotlin_version = ['"](.*)['"]/g, `ext.kotlin_version = '${kotlinVersion}'`);

writeFileSync(buildGradleFilePath, updateContent);

console.log(`[cordova-plugin-push::before-compile] updated "build.gradle" file with kotlin version set to: ${kotlinVersion}`);
}
21 changes: 13 additions & 8 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
</engines>

<platform name="android">
<hook type="before_compile" src="hooks/android/beforeCompile.js"/>

<config-file target="res/xml/config.xml" parent="/*">
<feature name="PushNotification">
<param name="android-package" value="com.adobe.phonegap.push.PushPlugin"/>
Expand Down Expand Up @@ -61,6 +63,9 @@
<preference name="AndroidXEnabled" value="true" />
<preference name="GradlePluginGoogleServicesEnabled" value="true" />
<preference name="GradlePluginGoogleServicesVersion" value="4.3.8" />

<preference name="GradlePluginKotlinEnabled" value="true" />
<preference name="GradlePluginKotlinVersion" value="1.5.20" />
</config-file>

<preference name="ANDROIDX_CORE_VERSION" default="1.6.+"/>
Expand All @@ -70,14 +75,14 @@
<framework src="me.leolin:ShortcutBadger:1.1.22@aar"/>
<framework src="com.google.firebase:firebase-messaging:$FCM_VERSION"/>

<source-file src="src/android/com/adobe/phonegap/push/FCMService.java" target-dir="src/com/adobe/phonegap/push/"/>
<source-file src="src/android/com/adobe/phonegap/push/PushConstants.java" target-dir="src/com/adobe/phonegap/push/"/>
<source-file src="src/android/com/adobe/phonegap/push/PushHandlerActivity.java" target-dir="src/com/adobe/phonegap/push/"/>
<source-file src="src/android/com/adobe/phonegap/push/BackgroundHandlerActivity.java" target-dir="src/com/adobe/phonegap/push/"/>
<source-file src="src/android/com/adobe/phonegap/push/PushInstanceIDListenerService.java" target-dir="src/com/adobe/phonegap/push/"/>
<source-file src="src/android/com/adobe/phonegap/push/PushPlugin.java" target-dir="src/com/adobe/phonegap/push/"/>
<source-file src="src/android/com/adobe/phonegap/push/BackgroundActionButtonHandler.java" target-dir="src/com/adobe/phonegap/push/"/>
<source-file src="src/android/com/adobe/phonegap/push/PushDismissedHandler.java" target-dir="src/com/adobe/phonegap/push/"/>
<source-file src="src/android/com/adobe/phonegap/push/FCMService.kt" target-dir="java/com/adobe/phonegap/push/"/>
<source-file src="src/android/com/adobe/phonegap/push/PushConstants.kt" target-dir="java/com/adobe/phonegap/push/"/>
<source-file src="src/android/com/adobe/phonegap/push/PushHandlerActivity.kt" target-dir="java/com/adobe/phonegap/push/"/>
<source-file src="src/android/com/adobe/phonegap/push/BackgroundHandlerActivity.kt" target-dir="java/com/adobe/phonegap/push/"/>
<source-file src="src/android/com/adobe/phonegap/push/PushInstanceIDListenerService.kt" target-dir="java/com/adobe/phonegap/push/"/>
<source-file src="src/android/com/adobe/phonegap/push/PushPlugin.kt" target-dir="java/com/adobe/phonegap/push/"/>
<source-file src="src/android/com/adobe/phonegap/push/BackgroundActionButtonHandler.kt" target-dir="java/com/adobe/phonegap/push/"/>
<source-file src="src/android/com/adobe/phonegap/push/PushDismissedHandler.kt" target-dir="java/com/adobe/phonegap/push/"/>
</platform>

<platform name="browser">
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.adobe.phonegap.push

import android.annotation.SuppressLint
import android.app.NotificationManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.core.app.RemoteInput

/**
* Background Action Button Handler
*/
@Suppress("HardCodedStringLiteral")
@SuppressLint("LongLogTag", "LogConditional")
class BackgroundActionButtonHandler : BroadcastReceiver() {
companion object {
private const val TAG: String = "${PushPlugin.PREFIX_TAG} (BackgroundActionButtonHandler)"
}

/**
* @param context
* @param intent
*/
override fun onReceive(context: Context, intent: Intent) {
val notId = intent.getIntExtra(PushConstants.NOT_ID, 0)
Log.d(TAG, "Not ID: $notId")

val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.cancel(FCMService.getAppName(context), notId)

intent.extras?.let { extras ->
Log.d(TAG, "Intent Extras: $extras")
extras.getBundle(PushConstants.PUSH_BUNDLE)?.apply {
putBoolean(PushConstants.FOREGROUND, false)
putBoolean(PushConstants.COLDSTART, false)
putString(
PushConstants.ACTION_CALLBACK,
extras.getString(PushConstants.CALLBACK)
)

RemoteInput.getResultsFromIntent(intent)?.let { remoteInputResults ->
val results = remoteInputResults.getCharSequence(PushConstants.INLINE_REPLY).toString()
Log.d(TAG, "Inline Reply: $results")

putString(PushConstants.INLINE_REPLY, results)
}
}

PushPlugin.sendExtras(extras)
}
}
}
125 changes: 0 additions & 125 deletions src/android/com/adobe/phonegap/push/BackgroundHandlerActivity.java

This file was deleted.

Loading