Integrate push notifications

This commit is contained in:
nielsandriesse 2020-04-17 12:11:27 +10:00
parent e68c2d1cfe
commit 0a3dbc55a8
10 changed files with 93 additions and 51 deletions

View File

@ -205,7 +205,7 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
// Loki - Set up public chat manager // Loki - Set up public chat manager
lokiPublicChatManager = new LokiPublicChatManager(this); lokiPublicChatManager = new LokiPublicChatManager(this);
updatePublicChatProfilePictureIfNeeded(); updatePublicChatProfilePictureIfNeeded();
setUpFCMIfNeeded(); registerForFCMIfNeeded();
} }
@Override @Override
@ -462,7 +462,7 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
}, this); }, this);
} }
public void setUpFCMIfNeeded() { public void registerForFCMIfNeeded() {
Context context = this; Context context = this;
FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(task -> { FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(task -> {
if (!task.isSuccessful()) { if (!task.isSuccessful()) {
@ -471,7 +471,12 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
} }
String token = task.getResult().getToken(); String token = task.getResult().getToken();
String userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context); String userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context);
if (userHexEncodedPublicKey == null) return;
if (TextSecurePreferences.isUsingFCM(this)) {
LokiPushNotificationManager.register(token, userHexEncodedPublicKey, context); LokiPushNotificationManager.register(token, userHexEncodedPublicKey, context);
} else {
LokiPushNotificationManager.unregister(token, context);
}
}); });
} }

View File

@ -17,7 +17,8 @@ object LokiPushNotificationManager {
private const val tokenExpirationInterval = 2 * 24 * 60 * 60 * 1000 private const val tokenExpirationInterval = 2 * 24 * 60 * 60 * 1000
fun disableFCM(token: String, context: Context?) { @JvmStatic
fun unregister(token: String, context: Context?) {
val parameters = mapOf( "token" to token ) val parameters = mapOf( "token" to token )
val url = "${server}/register" val url = "${server}/register"
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))

View File

@ -9,11 +9,9 @@ import android.widget.TextView.OnEditorActionListener
import android.widget.Toast import android.widget.Toast
import kotlinx.android.synthetic.main.activity_display_name.* import kotlinx.android.synthetic.main.activity_display_name.*
import network.loki.messenger.R import network.loki.messenger.R
import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.BaseActionBarActivity import org.thoughtcrime.securesms.BaseActionBarActivity
import org.thoughtcrime.securesms.database.DatabaseFactory import org.thoughtcrime.securesms.loki.redesign.utilities.push
import org.thoughtcrime.securesms.loki.redesign.utilities.setUpActionBarSessionLogo import org.thoughtcrime.securesms.loki.redesign.utilities.setUpActionBarSessionLogo
import org.thoughtcrime.securesms.loki.redesign.utilities.show
import org.thoughtcrime.securesms.util.TextSecurePreferences import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.signalservice.api.crypto.ProfileCipher import org.whispersystems.signalservice.api.crypto.ProfileCipher
@ -54,20 +52,7 @@ class DisplayNameActivity : BaseActionBarActivity() {
val inputMethodManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager val inputMethodManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(displayNameEditText.windowToken, 0) inputMethodManager.hideSoftInputFromWindow(displayNameEditText.windowToken, 0)
TextSecurePreferences.setProfileName(this, displayName) TextSecurePreferences.setProfileName(this, displayName)
TextSecurePreferences.setHasSeenWelcomeScreen(this, true) val intent = Intent(this, PNModeActivity::class.java)
TextSecurePreferences.setPromptedPushRegistration(this, true) push(intent)
val application = ApplicationContext.getInstance(this)
application.setUpStorageAPIIfNeeded()
application.setUpP2PAPI()
val publicChatAPI = ApplicationContext.getInstance(this).lokiPublicChatAPI
if (publicChatAPI != null) {
// TODO: This won't be necessary anymore when we don't auto-join the Loki Public Chat anymore
application.createDefaultPublicChatsIfNeeded()
val servers = DatabaseFactory.getLokiThreadDatabase(this).getAllPublicChatServers()
servers.forEach { publicChatAPI.setDisplayName(displayName, it) }
}
val intent = Intent(this, HomeActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
show(intent)
} }
} }

View File

@ -32,6 +32,7 @@ import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.database.ThreadDatabase import org.thoughtcrime.securesms.database.ThreadDatabase
import org.thoughtcrime.securesms.database.model.ThreadRecord import org.thoughtcrime.securesms.database.model.ThreadRecord
import org.thoughtcrime.securesms.loki.getColorWithID import org.thoughtcrime.securesms.loki.getColorWithID
import org.thoughtcrime.securesms.loki.redesign.dialogs.PNModeBottomSheet
import org.thoughtcrime.securesms.loki.redesign.utilities.push import org.thoughtcrime.securesms.loki.redesign.utilities.push
import org.thoughtcrime.securesms.loki.redesign.utilities.show import org.thoughtcrime.securesms.loki.redesign.utilities.show
import org.thoughtcrime.securesms.loki.redesign.views.ConversationView import org.thoughtcrime.securesms.loki.redesign.views.ConversationView
@ -156,32 +157,20 @@ class HomeActivity : PassphraseRequiredActionBarActivity, ConversationClickListe
if (hasViewedSeed || !isMasterDevice) { if (hasViewedSeed || !isMasterDevice) {
seedReminderView.visibility = View.GONE seedReminderView.visibility = View.GONE
} }
// if (!TextSecurePreferences.getHasSeenOpenGroupSuggestionSheet(this)) { if (!TextSecurePreferences.hasSeenPNModeSheet(this)) {
// val bottomSheet = OpenGroupSuggestionBottomSheet() val bottomSheet = PNModeBottomSheet()
// bottomSheet.onJoinTapped = { bottomSheet.onConfirmTapped = { isUsingFCM ->
// TextSecurePreferences.setHasSeenOpenGroupSuggestionSheet(this) TextSecurePreferences.setHasSeenPNModeSheet(this, true)
// bottomSheet.dismiss() TextSecurePreferences.setIsUsingFCM(this, isUsingFCM)
// // TODO: Duplication of the code in JoinPublicChatActivity ApplicationContext.getInstance(this).registerForFCMIfNeeded()
// val application = ApplicationContext.getInstance(this) bottomSheet.dismiss()
// val channel: Long = 1 }
// val displayName = TextSecurePreferences.getProfileName(this) bottomSheet.onSkipTapped = {
// val lokiPublicChatAPI = application.lokiPublicChatAPI!! TextSecurePreferences.setHasSeenPNModeSheet(this, true)
// val url = "https://chat.getsession.org" bottomSheet.dismiss()
// application.lokiPublicChatManager.addChat(url, channel).successUi { }
// lokiPublicChatAPI.getMessages(channel, url) bottomSheet.show(supportFragmentManager, bottomSheet.tag)
// lokiPublicChatAPI.setDisplayName(displayName, url) }
// lokiPublicChatAPI.join(channel, url)
// val profileKey: ByteArray = ProfileKeyUtil.getProfileKey(this)
// val profileUrl: String? = TextSecurePreferences.getProfileAvatarUrl(this)
// lokiPublicChatAPI.setProfilePicture(url, profileKey, profileUrl)
// }
// }
// bottomSheet.onDismissTapped = {
// TextSecurePreferences.setHasSeenOpenGroupSuggestionSheet(this)
// bottomSheet.dismiss()
// }
// bottomSheet.show(supportFragmentManager, bottomSheet.tag)
// }
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

View File

@ -1,15 +1,25 @@
package org.thoughtcrime.securesms.loki.redesign.activities package org.thoughtcrime.securesms.loki.redesign.activities
import android.app.AlertDialog
import android.content.Intent
import android.graphics.drawable.TransitionDrawable import android.graphics.drawable.TransitionDrawable
import android.os.Bundle import android.os.Bundle
import android.os.Handler
import android.support.annotation.DrawableRes import android.support.annotation.DrawableRes
import android.view.View import android.view.View
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_display_name.registerButton import kotlinx.android.synthetic.main.activity_display_name.registerButton
import kotlinx.android.synthetic.main.activity_home.*
import kotlinx.android.synthetic.main.activity_pn_mode.* import kotlinx.android.synthetic.main.activity_pn_mode.*
import network.loki.messenger.R import network.loki.messenger.R
import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.BaseActionBarActivity import org.thoughtcrime.securesms.BaseActionBarActivity
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.loki.redesign.utilities.setUpActionBarSessionLogo import org.thoughtcrime.securesms.loki.redesign.utilities.setUpActionBarSessionLogo
import org.thoughtcrime.securesms.loki.redesign.utilities.show
import org.thoughtcrime.securesms.util.GroupUtil
import org.thoughtcrime.securesms.util.TextSecurePreferences
class PNModeActivity : BaseActionBarActivity() { class PNModeActivity : BaseActionBarActivity() {
private var selectedOptionView: LinearLayout? = null private var selectedOptionView: LinearLayout? = null
@ -71,7 +81,32 @@ class PNModeActivity : BaseActionBarActivity() {
} }
private fun register() { private fun register() {
// TODO: Implement if (selectedOptionView == null) {
val dialog = AlertDialog.Builder(this)
dialog.setMessage(R.string.activity_pn_mode_no_option_picked_dialog_title)
dialog.setPositiveButton(R.string.ok) { _, _ -> }
dialog.create().show()
return
}
val displayName = TextSecurePreferences.getProfileName(this)
TextSecurePreferences.setHasSeenWelcomeScreen(this, true)
TextSecurePreferences.setPromptedPushRegistration(this, true)
TextSecurePreferences.setIsUsingFCM(this, (selectedOptionView == fcmOptionView))
TextSecurePreferences.setHasSeenPNModeSheet(this, true) // Shouldn't be shown to users who've done the new onboarding
val application = ApplicationContext.getInstance(this)
application.setUpStorageAPIIfNeeded()
application.setUpP2PAPI()
val publicChatAPI = ApplicationContext.getInstance(this).lokiPublicChatAPI
if (publicChatAPI != null) {
// TODO: This won't be necessary anymore when we don't auto-join the Loki Public Chat anymore
application.createDefaultPublicChatsIfNeeded()
val servers = DatabaseFactory.getLokiThreadDatabase(this).getAllPublicChatServers()
servers.forEach { publicChatAPI.setDisplayName(displayName, it) }
}
application.registerForFCMIfNeeded()
val intent = Intent(this, HomeActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
show(intent)
} }
// endregion // endregion
} }

View File

@ -1,5 +1,6 @@
package org.thoughtcrime.securesms.loki.redesign.dialogs package org.thoughtcrime.securesms.loki.redesign.dialogs
import android.content.DialogInterface
import android.graphics.drawable.TransitionDrawable import android.graphics.drawable.TransitionDrawable
import android.os.Bundle import android.os.Bundle
import android.support.annotation.DrawableRes import android.support.annotation.DrawableRes
@ -10,6 +11,7 @@ import android.view.ViewGroup
import android.widget.LinearLayout import android.widget.LinearLayout
import kotlinx.android.synthetic.main.fragment_pn_mode_bottom_sheet.* import kotlinx.android.synthetic.main.fragment_pn_mode_bottom_sheet.*
import network.loki.messenger.R import network.loki.messenger.R
import org.thoughtcrime.securesms.util.TextSecurePreferences
class PNModeBottomSheet : BottomSheetDialogFragment() { class PNModeBottomSheet : BottomSheetDialogFragment() {
private var selectedOptionView: LinearLayout? = null private var selectedOptionView: LinearLayout? = null
@ -33,6 +35,11 @@ class PNModeBottomSheet : BottomSheetDialogFragment() {
skipButton.setOnClickListener { onSkipTapped?.invoke() } skipButton.setOnClickListener { onSkipTapped?.invoke() }
} }
override fun onDismiss(dialog: DialogInterface?) {
TextSecurePreferences.setHasSeenPNModeSheet(context, true)
super.onDismiss(dialog)
}
// region Animation // region Animation
private fun performTransition(@DrawableRes transitionID: Int, subject: View) { private fun performTransition(@DrawableRes transitionID: Int, subject: View) {
val drawable = resources.getDrawable(transitionID, context!!.theme) as TransitionDrawable val drawable = resources.getDrawable(transitionID, context!!.theme) as TransitionDrawable

View File

@ -6,7 +6,6 @@ import nl.komponents.kovenant.functional.map
import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.database.DatabaseFactory import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.jobs.PushContentReceiveJob import org.thoughtcrime.securesms.jobs.PushContentReceiveJob
import org.thoughtcrime.securesms.loki.LokiPushNotificationManager
import org.thoughtcrime.securesms.service.PersistentAlarmManagerListener import org.thoughtcrime.securesms.service.PersistentAlarmManagerListener
import org.thoughtcrime.securesms.util.TextSecurePreferences import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope
@ -29,6 +28,7 @@ class BackgroundPollWorker : PersistentAlarmManagerListener() {
} }
override fun onAlarm(context: Context, scheduledTime: Long): Long { override fun onAlarm(context: Context, scheduledTime: Long): Long {
if (TextSecurePreferences.isUsingFCM(context)) { return 0L }
if (scheduledTime != 0L) { if (scheduledTime != 0L) {
val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context) val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context)
val lokiAPIDatabase = DatabaseFactory.getLokiAPIDatabase(context) val lokiAPIDatabase = DatabaseFactory.getLokiAPIDatabase(context)

View File

@ -14,6 +14,7 @@ import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.text.TextUtils; import android.text.TextUtils;
import org.thoughtcrime.securesms.ApplicationContext;
import org.thoughtcrime.securesms.components.SwitchPreferenceCompat; import org.thoughtcrime.securesms.components.SwitchPreferenceCompat;
import org.thoughtcrime.securesms.notifications.MessageNotifier; import org.thoughtcrime.securesms.notifications.MessageNotifier;
import org.thoughtcrime.securesms.notifications.NotificationChannels; import org.thoughtcrime.securesms.notifications.NotificationChannels;
@ -32,6 +33,16 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme
public void onCreate(Bundle paramBundle) { public void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle); super.onCreate(paramBundle);
// Set up FCM toggle
String fcmKey = "pref_key_use_fcm";
((SwitchPreferenceCompat)findPreference(fcmKey)).setChecked(TextSecurePreferences.isUsingFCM(getContext()));
this.findPreference(fcmKey)
.setOnPreferenceChangeListener((preference, newValue) -> {
TextSecurePreferences.setIsUsingFCM(getContext(), (boolean) newValue);
ApplicationContext.getInstance(getContext()).registerForFCMIfNeeded();
return true;
});
Preference ledBlinkPref = this.findPreference(TextSecurePreferences.LED_BLINK_PREF); Preference ledBlinkPref = this.findPreference(TextSecurePreferences.LED_BLINK_PREF);
if (NotificationChannels.supported()) { if (NotificationChannels.supported()) {

View File

@ -15,7 +15,7 @@ class PushNotificationService : FirebaseMessagingService() {
override fun onNewToken(token: String) { override fun onNewToken(token: String) {
super.onNewToken(token) super.onNewToken(token)
Log.d("Loki", "New FCM token: $token.") Log.d("Loki", "New FCM token: $token.")
val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this) val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this) ?: return
LokiPushNotificationManager.register(token, userHexEncodedPublicKey, this) LokiPushNotificationManager.register(token, userHexEncodedPublicKey, this)
} }

View File

@ -189,6 +189,7 @@ public class TextSecurePreferences {
private static final String IS_USING_FCM = "pref_is_using_fcm"; private static final String IS_USING_FCM = "pref_is_using_fcm";
private static final String FCM_TOKEN = "pref_fcm_token"; private static final String FCM_TOKEN = "pref_fcm_token";
private static final String LAST_FCM_TOKEN_UPLOAD_TIME = "pref_last_fcm_token_upload_time"; private static final String LAST_FCM_TOKEN_UPLOAD_TIME = "pref_last_fcm_token_upload_time";
private static final String HAS_SEEN_PN_MODE_SHEET = "pref_has_seen_pn_mode_sheet";
public static boolean isUsingFCM(Context context) { public static boolean isUsingFCM(Context context) {
return getBooleanPreference(context, IS_USING_FCM, false); return getBooleanPreference(context, IS_USING_FCM, false);
@ -213,6 +214,14 @@ public class TextSecurePreferences {
public static void setLastFCMUploadTime(Context context, long value) { public static void setLastFCMUploadTime(Context context, long value) {
setLongPreference(context, LAST_FCM_TOKEN_UPLOAD_TIME, value); setLongPreference(context, LAST_FCM_TOKEN_UPLOAD_TIME, value);
} }
public static boolean hasSeenPNModeSheet(Context context) {
return getBooleanPreference(context, HAS_SEEN_PN_MODE_SHEET, false);
}
public static void setHasSeenPNModeSheet(Context context, boolean value) {
setBooleanPreference(context, HAS_SEEN_PN_MODE_SHEET, value);
}
// endregion // endregion
public static boolean isScreenLockEnabled(@NonNull Context context) { public static boolean isScreenLockEnabled(@NonNull Context context) {