Check for authenticated user before a subscription sync.

This commit is contained in:
sictiru 2021-12-29 20:48:20 -08:00 committed by Samuel Clay
parent 7f5c44ca26
commit 790223e1ff
3 changed files with 17 additions and 2 deletions

View file

@ -46,8 +46,7 @@ class InitActivity : AppCompatActivity() {
}
private fun preferenceCheck() {
val preferences = getSharedPreferences(PrefConstants.PREFERENCES, MODE_PRIVATE)
if (preferences.getString(PrefConstants.PREF_COOKIE, null) != null) {
if (PrefsUtils.hasCookie(this)) {
val mainIntent = Intent(this, Main::class.java)
startActivity(mainIntent)
} else {

View file

@ -9,6 +9,7 @@ import com.newsblur.subscription.SubscriptionManagerImpl
import com.newsblur.util.AppConstants
import com.newsblur.util.Log
import com.newsblur.util.NBScope
import com.newsblur.util.PrefsUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -24,6 +25,11 @@ class SubscriptionSyncService : JobService() {
override fun onStartJob(params: JobParameters?): Boolean {
Log.d(this, "onStartJob")
if (!PrefsUtils.hasCookie(this)) {
// no user authenticated
return false
}
NBScope.launch(Dispatchers.Default) {
val subscriptionManager = SubscriptionManagerImpl(this@SubscriptionSyncService, this)
val job = subscriptionManager.syncActiveSubscription()

View file

@ -1026,4 +1026,14 @@ public class PrefsUtils {
editor.putBoolean(PrefConstants.IN_APP_REVIEW, true);
editor.commit();
}
/**
* Check for logged in user.
* @return whether a cookie is stored on disk
* which gets saved when a user is authenticated.
*/
public static boolean hasCookie(Context context) {
SharedPreferences preferences = context.getSharedPreferences(PrefConstants.PREFERENCES, Context.MODE_PRIVATE);
return preferences.getString(PrefConstants.PREF_COOKIE, null) != null;
}
}