From fd7fdfb7cbf50eee734a6d98a35e76c44f692315 Mon Sep 17 00:00:00 2001
From: mimi89999 <michel@lebihan.pl>
Date: Wed, 13 Sep 2017 09:41:16 +0200
Subject: [PATCH] Remove GCM

---
 app/build.gradle                                   |  2 -
 .../notification/LibrusGCMRegistrationManager.kt   | 63 ----------------------
 .../api/notification/LibrusGcmListenerService.kt   | 50 -----------------
 .../java/com/wabadaba/dziennik/ui/MainActivity.kt  | 26 +--------
 build.gradle                                       |  1 -
 5 files changed, 1 insertion(+), 141 deletions(-)
 delete mode 100644 app/src/main/java/com/wabadaba/dziennik/api/notification/LibrusGCMRegistrationManager.kt
 delete mode 100644 app/src/main/java/com/wabadaba/dziennik/api/notification/LibrusGcmListenerService.kt

diff --git a/app/build.gradle b/app/build.gradle
index c96f432..0fda314 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -1,4 +1,3 @@
-apply plugin: 'com.google.gms.google-services'
 apply plugin: 'com.android.application'
 apply plugin: 'kotlin-android'
 apply plugin: 'kotlin-android-extensions'
@@ -65,7 +64,6 @@ dependencies {
     implementation 'com.android.support:support-v13:26.0.2'
     implementation 'com.android.support:support-v4:26.0.2'
 
-    implementation "com.google.android.gms:play-services-gcm:11.2.0"
 
     implementation 'com.android.support:preference-v14:26.0.2'
     implementation 'com.android.support:design:26.0.2'
diff --git a/app/src/main/java/com/wabadaba/dziennik/api/notification/LibrusGCMRegistrationManager.kt b/app/src/main/java/com/wabadaba/dziennik/api/notification/LibrusGCMRegistrationManager.kt
deleted file mode 100644
index 67f5afc..0000000
--- a/app/src/main/java/com/wabadaba/dziennik/api/notification/LibrusGCMRegistrationManager.kt
+++ /dev/null
@@ -1,63 +0,0 @@
-package com.wabadaba.dziennik.api.notification
-
-import android.content.Context
-import android.preference.PreferenceManager
-import com.bugsnag.android.Bugsnag
-import com.google.android.gms.gcm.GoogleCloudMessaging
-import com.google.android.gms.iid.InstanceID
-import com.wabadaba.dziennik.BuildConfig
-import com.wabadaba.dziennik.api.APIClient
-import com.wabadaba.dziennik.api.FullUser
-import com.wabadaba.dziennik.api.RxHttpClient
-import io.reactivex.Single
-import io.reactivex.schedulers.Schedulers
-
-class LibrusGCMRegistrationManager(private val currentUser: FullUser,
-                                   rxHttpClient: RxHttpClient,
-                                   val context: Context) {
-
-    private val senderId = BuildConfig.SENDER_ID
-
-    private val pref_key = "gcm_registered_users"
-    private val apiClient = APIClient(currentUser.authInfo, rxHttpClient)
-
-    fun register() {
-        if (!isRegistered()) {
-            getRegistrationToken()
-                    .flatMap { regToken -> apiClient.pushDevices(regToken) }
-                    .doOnSuccess { this.setRegistered() }
-                    .subscribeOn(Schedulers.io())
-                    .subscribe(
-                            {
-                                println("GCM registration successful")
-                            },
-                            { e ->
-                                println("GCM registration failed")
-                                Bugsnag.notify(e)
-                            })
-        } else {
-            println("User $currentUser already registered for GCM")
-        }
-    }
-
-
-    private fun isRegistered(): Boolean {
-        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
-        return prefs.getStringSet(pref_key, emptySet())
-                .contains(currentUser.login)
-    }
-
-    private fun setRegistered() {
-        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
-        val registered = prefs.getStringSet(pref_key, emptySet())
-        prefs.edit().putStringSet(pref_key, registered.plus(currentUser.login))
-                .apply()
-    }
-
-    private fun getRegistrationToken() = Single.fromCallable {
-        val instanceID = InstanceID.getInstance(context)
-        instanceID.getToken(senderId,
-                GoogleCloudMessaging.INSTANCE_ID_SCOPE, null)
-    }
-}
-
diff --git a/app/src/main/java/com/wabadaba/dziennik/api/notification/LibrusGcmListenerService.kt b/app/src/main/java/com/wabadaba/dziennik/api/notification/LibrusGcmListenerService.kt
deleted file mode 100644
index 6f02f0b..0000000
--- a/app/src/main/java/com/wabadaba/dziennik/api/notification/LibrusGcmListenerService.kt
+++ /dev/null
@@ -1,50 +0,0 @@
-package com.wabadaba.dziennik.api.notification
-
-import android.app.NotificationManager
-import android.app.PendingIntent
-import android.content.Context
-import android.content.Intent
-import android.os.Bundle
-import android.preference.PreferenceManager
-import android.support.v4.app.NotificationCompat
-import com.google.android.gms.gcm.GcmListenerService
-import com.wabadaba.dziennik.R
-import com.wabadaba.dziennik.ui.MainActivity
-
-
-@Suppress("DEPRECATION")
-class LibrusGcmListenerService : GcmListenerService() {
-    override fun onMessageReceived(p0: String, data: Bundle) {
-
-        val prefs = PreferenceManager.getDefaultSharedPreferences(this)
-        if (!prefs.getBoolean("enable_notifications", true)) {
-            println("received message but notifications are disabled")
-            return
-        }
-
-        println("received message, sending notification...")
-
-        val message = data.getString("message")
-        val user = data.getString("userId")
-
-        val intent = Intent(this, MainActivity::class.java)
-        val pendingIntent = PendingIntent.getActivity(
-                this,
-                0,
-                intent,
-                PendingIntent.FLAG_UPDATE_CURRENT
-        )
-
-        val notification = NotificationCompat.Builder(this)
-                .setSmallIcon(R.drawable.ic_school_black_24dp)
-                .setContentTitle(message)
-                .setContentText(user)
-                .setContentIntent(pendingIntent)
-                .setAutoCancel(true)
-                .setPriority(NotificationCompat.PRIORITY_HIGH)
-                .build()
-
-        val notifyManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
-        notifyManager.notify(System.currentTimeMillis().toInt(), notification)
-    }
-}
\ No newline at end of file
diff --git a/app/src/main/java/com/wabadaba/dziennik/ui/MainActivity.kt b/app/src/main/java/com/wabadaba/dziennik/ui/MainActivity.kt
index 42e3c80..19cc291 100644
--- a/app/src/main/java/com/wabadaba/dziennik/ui/MainActivity.kt
+++ b/app/src/main/java/com/wabadaba/dziennik/ui/MainActivity.kt
@@ -19,8 +19,6 @@ import co.zsmb.materialdrawerkt.draweritems.profile.profileSetting
 import com.amulyakhare.textdrawable.TextDrawable
 import com.amulyakhare.textdrawable.util.ColorGenerator
 import com.bugsnag.android.Bugsnag
-import com.google.android.gms.common.ConnectionResult
-import com.google.android.gms.common.GoogleApiAvailability
 import com.mikepenz.materialdrawer.Drawer
 import com.mikepenz.materialdrawer.model.DividerDrawerItem
 import com.mikepenz.materialdrawer.model.PrimaryDrawerItem
@@ -28,7 +26,6 @@ import com.mikepenz.materialdrawer.model.ProfileDrawerItem
 import com.wabadaba.dziennik.MainApplication
 import com.wabadaba.dziennik.R
 import com.wabadaba.dziennik.api.*
-import com.wabadaba.dziennik.api.notification.LibrusGCMRegistrationManager
 import com.wabadaba.dziennik.di.ViewModelFactory
 import com.wabadaba.dziennik.ui.login.LoginActivity
 import com.wabadaba.dziennik.vo.LuckyNumber
@@ -113,7 +110,7 @@ class MainActivity : AppCompatActivity(), LifecycleRegistryOwner {
 
         viewModel = ViewModelProviders.of(this, viewModelFactory).get(MainViewModel::class.java)
 
-        if (checkGooglePlayServicesAvailable()) {
+        if (true) {
             userRepository.allUsers.observeOn(AndroidSchedulers.mainThread())
                     .subscribe { users ->
                         if (users.isEmpty()) {
@@ -129,27 +126,6 @@ class MainActivity : AppCompatActivity(), LifecycleRegistryOwner {
                         }
                     }
         }
-        registerGCM()
-    }
-
-    private fun registerGCM() {
-        userRepository.currentUser.subscribe {
-            println("Registering user $it for GCM")
-            LibrusGCMRegistrationManager(it, rxHttpClient, this)
-                    .register()
-        }
-    }
-
-    private fun checkGooglePlayServicesAvailable(): Boolean {
-        val googleApiAvailability = GoogleApiAvailability.getInstance()
-        val status = googleApiAvailability.isGooglePlayServicesAvailable(this)
-        if (status != ConnectionResult.SUCCESS) {
-            if (googleApiAvailability.isUserResolvableError(status)) {
-                googleApiAvailability.getErrorDialog(this, status, 2404).show()
-            }
-            return false
-        }
-        return true
     }
 
     @Suppress("DEPRECATION")
diff --git a/build.gradle b/build.gradle
index 4148edd..ba94c66 100644
--- a/build.gradle
+++ b/build.gradle
@@ -9,7 +9,6 @@ buildscript {
     dependencies {
         classpath 'com.android.tools.build:gradle:3.0.0-beta5'
         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
-        classpath 'com.google.gms:google-services:3.1.0'
 
         // NOTE: Do not place your application dependencies here; they belong
         // in the individual module build.gradle files
-- 
2.11.0

