Skip to content

Commit

Permalink
feat!(android): migrate source code to kotlin (#123)
Browse files Browse the repository at this point in the history
* feat!(android): add kotlin support
  * kotlin@1.5.20
  * add before_compile hookscript for cordova-android 9.x support
* refactor!: convert PushInstanceIDListenerService to Kotlin
* refactor!: convert PushDismissedHandler to Kotlin
* refactor!: convert BackgroundActionButtonHandler to Kotlin
* refactor!: convert PushConstants to Kotlin
* refactor!: convert BackgroundHandlerActivity to Kotlin
* refactor!: convert PushHandlerActivity to Kotlin
* refactor!: convert PushPlugin to Kotlin
* refactor!: convert FCMService to Kotlin
* fix: target-dir path for kotlin files
* refactor: BackgroundActionButtonHandler
* refactor: PushInstanceIDListenerService
* refactor: BackgroundHandlerActivity
* refactor: PushDismissedHandler
* refactor: PushHandlerActivity
* refactor: PushConstants
* refactor: PushPlugin
* refactor: PushPlugin - separate INITIALIZE action
* refactor: PushPlugin - separate UNREGISTER action
* refactor: PushPlugin - separate HAS_PERMISSION action
* refactor: PushPlugin - separate SET_APPLICATION_ICON_BADGE_NUMBER action
* refactor: PushPlugin - separate GET_APPLICATION_ICON_BADGE_NUMBER & CLEAR_ALL_NOTIFICATIONS action
* refactor: PushPlugin - separate SUBSCRIBE action
* refactor: PushPlugin - separate UNSUBSCRIBE action
* refactor: PushPlugin - separate CREATE_CHANNEL action
* refactor: PushPlugin - separate DELETE_CHANNEL action
* refactor: PushPlugin - separate LIST_CHANNELS action
* refactor: PushPlugin - separate CLEAR_NOTIFICATION action
* refactor: Invalid Execute Action Log
* refactor: PushPlugin
* refactor: remove unused sendError
* refactor: remove unused import
* refactor: PushPlugin - moved compainion object to top
* refactor: PushPlugin - commenting
* refactor: PushPlugin - move onPause super call to bottom
* refactor: PushPlugin - removed unused var registration_id
* refactor: PushPlugin - removed getStringResourceByName
* refactor: PushPlugin - convertBundleToJson
  * moved it internal to sendExtras to remove the conflict of choise private/internal.
* chore(lint): remove comments from private methods (CommentOverPrivateFunction)
* chore(lint): move comments from private methods inside (CommentOverPrivateFunction)
* refactor: FCMService - reformat
* refactor: FCMService - reformat again
* refactor: FCMService - reformat some more
* refactor: add IS_ENABLED to PushConstants
* refactor: FCMService LOG_TAG to TAG
* chore: remove lint warning CommentOverPrivateProperty
* chore: fix some lint warnings in FCMService
* chore: removing duplicated code
* chore: removing duplicated code (pushSharedPref)
* refactor: FCMService continue
* fix: remove AppCompatActivity reference
* chore: type cast cordova.activity as Activity
* fix: remove some code warnings in FCMService
* chore: update tag to include plugin name for better logcat filtering
  • Loading branch information
erisu committed Sep 15, 2021
1 parent ef26064 commit 1568abb
Show file tree
Hide file tree
Showing 18 changed files with 2,609 additions and 2,221 deletions.
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

0 comments on commit 1568abb

Please sign in to comment.