diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2021-07-18 12:04:45 -0700 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2021-07-18 12:04:45 -0700 |
commit | a983b2648017bb0069e73c6a5ab07356db3859e5 (patch) | |
tree | f3ab27c39ab45d90c2dcaa7311c2051226291919 /app/src/google | |
parent | 3b399c41ad64e156f363213078c4a983fe93ef4a (diff) | |
download | etbsa-traccar-manager-android-a983b2648017bb0069e73c6a5ab07356db3859e5.tar.gz etbsa-traccar-manager-android-a983b2648017bb0069e73c6a5ab07356db3859e5.tar.bz2 etbsa-traccar-manager-android-a983b2648017bb0069e73c6a5ab07356db3859e5.zip |
Convert to Kotlin
Diffstat (limited to 'app/src/google')
4 files changed, 109 insertions, 120 deletions
diff --git a/app/src/google/java/org/traccar/manager/GoogleMainApplication.java b/app/src/google/java/org/traccar/manager/GoogleMainApplication.java deleted file mode 100644 index fe0c65f..0000000 --- a/app/src/google/java/org/traccar/manager/GoogleMainApplication.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton.tananaev@gmail.com) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.manager; - -import android.app.Application; -import android.app.NotificationChannel; -import android.app.NotificationManager; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; -import android.os.Build; -import androidx.localbroadcastmanager.content.LocalBroadcastManager; - -import com.google.android.gms.tasks.OnSuccessListener; -import com.google.firebase.analytics.FirebaseAnalytics; -import com.google.firebase.iid.FirebaseInstanceId; -import com.google.firebase.iid.InstanceIdResult; - -public class GoogleMainApplication extends Application { - - private FirebaseAnalytics firebaseAnalytics; - - @Override - public void onCreate() { - super.onCreate(); - firebaseAnalytics = FirebaseAnalytics.getInstance(this); - IntentFilter intentFilter = new IntentFilter(MainFragment.EVENT_LOGIN); - LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, intentFilter); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel channel = new NotificationChannel( - getString(R.string.notification_channel_id), getString(R.string.notification_channel), - NotificationManager.IMPORTANCE_DEFAULT); - ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).createNotificationChannel(channel); - } - } - - private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() { - @Override - public void onSuccess(InstanceIdResult instanceIdResult) { - broadcastToken(instanceIdResult.getToken()); - } - }); - } - }; - - public void broadcastToken(String token) { - Intent intent = new Intent(MainFragment.EVENT_TOKEN); - intent.putExtra(MainFragment.KEY_TOKEN, token); - LocalBroadcastManager.getInstance(this).sendBroadcast(intent); - } - -} diff --git a/app/src/google/java/org/traccar/manager/GoogleMainApplication.kt b/app/src/google/java/org/traccar/manager/GoogleMainApplication.kt new file mode 100644 index 0000000..cfeaf6e --- /dev/null +++ b/app/src/google/java/org/traccar/manager/GoogleMainApplication.kt @@ -0,0 +1,64 @@ +/* + * Copyright 2017 - 2021 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.manager + +import android.app.Application +import android.app.NotificationChannel +import android.app.NotificationManager +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.IntentFilter +import android.os.Build +import androidx.localbroadcastmanager.content.LocalBroadcastManager +import com.google.firebase.analytics.FirebaseAnalytics +import com.google.firebase.messaging.FirebaseMessaging + +class GoogleMainApplication : Application() { + + private lateinit var firebaseAnalytics: FirebaseAnalytics + + override fun onCreate() { + super.onCreate() + firebaseAnalytics = FirebaseAnalytics.getInstance(this) + val intentFilter = IntentFilter(MainFragment.EVENT_LOGIN) + LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, intentFilter) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val channel = NotificationChannel( + getString(R.string.notification_channel_id), + getString(R.string.notification_channel), + NotificationManager.IMPORTANCE_DEFAULT + ) + (getSystemService(NOTIFICATION_SERVICE) as NotificationManager).createNotificationChannel(channel) + } + } + + private val broadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + FirebaseMessaging.getInstance().token.addOnCompleteListener { + if (it.isComplete) { + broadcastToken(it.result) + } + } + } + } + + fun broadcastToken(token: String?) { + val intent = Intent(MainFragment.EVENT_TOKEN) + intent.putExtra(MainFragment.KEY_TOKEN, token) + LocalBroadcastManager.getInstance(this).sendBroadcast(intent) + } +} diff --git a/app/src/google/java/org/traccar/manager/ManagerMessagingService.java b/app/src/google/java/org/traccar/manager/ManagerMessagingService.java deleted file mode 100644 index 7548ce5..0000000 --- a/app/src/google/java/org/traccar/manager/ManagerMessagingService.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2018 Anton Tananaev (anton.tananaev@gmail.com) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.manager; - -import android.app.NotificationManager; -import android.app.PendingIntent; -import android.content.Intent; -import androidx.core.app.NotificationCompat; - -import com.google.firebase.messaging.FirebaseMessagingService; -import com.google.firebase.messaging.RemoteMessage; - -public class ManagerMessagingService extends FirebaseMessagingService { - - @Override - public void onMessageReceived(RemoteMessage remoteMessage) { - super.onMessageReceived(remoteMessage); - - PendingIntent pendingIntent = PendingIntent.getActivity( - this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_ONE_SHOT); - NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getString(R.string.notification_channel_id)) - .setSmallIcon(R.drawable.ic_stat_notify) - .setContentTitle(getString(R.string.app_name)) - .setContentText(remoteMessage.getNotification().getBody()) - .setAutoCancel(true) - .setContentIntent(pendingIntent);; - ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(remoteMessage.hashCode(), builder.build()); - } - - @Override - public void onNewToken(String token) { - super.onNewToken(token); - - ((GoogleMainApplication) getApplication()).broadcastToken(token); - } - -} diff --git a/app/src/google/java/org/traccar/manager/ManagerMessagingService.kt b/app/src/google/java/org/traccar/manager/ManagerMessagingService.kt new file mode 100644 index 0000000..165de63 --- /dev/null +++ b/app/src/google/java/org/traccar/manager/ManagerMessagingService.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2018 - 2021 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.manager + +import android.annotation.SuppressLint +import android.app.NotificationManager +import android.app.PendingIntent +import android.content.Intent +import androidx.core.app.NotificationCompat +import com.google.firebase.messaging.FirebaseMessagingService +import com.google.firebase.messaging.RemoteMessage + +class ManagerMessagingService : FirebaseMessagingService() { + + @SuppressLint("UnspecifiedImmutableFlag") + override fun onMessageReceived(remoteMessage: RemoteMessage) { + super.onMessageReceived(remoteMessage) + val pendingIntent = PendingIntent.getActivity(this, 0, Intent(this, MainActivity::class.java), PendingIntent.FLAG_ONE_SHOT) + val builder = NotificationCompat.Builder(this, getString(R.string.notification_channel_id)) + .setSmallIcon(R.drawable.ic_stat_notify) + .setContentTitle(getString(R.string.app_name)) + .setContentText(remoteMessage.notification?.body) + .setAutoCancel(true) + .setContentIntent(pendingIntent) + (getSystemService(NOTIFICATION_SERVICE) as NotificationManager).notify(remoteMessage.hashCode(), builder.build()) + } + + override fun onNewToken(token: String) { + super.onNewToken(token) + (application as GoogleMainApplication).broadcastToken(token) + } +} |