From 8655dc9927ab9596c493595bfa0ee4d51a9805b2 Mon Sep 17 00:00:00 2001 From: Caesar2011 Date: Fri, 15 Feb 2019 17:56:29 +0100 Subject: [PATCH] Version 37 reconstructed --- .../java/de/sebse/fuplanner/MainActivity.java | 78 +++++++++++++------ .../de/sebse/fuplanner/services/kvv/KVV.java | 10 +-- .../fuplanner/services/kvv/KVVListener.java | 3 +- .../sebse/fuplanner/services/kvv/Login.java | 4 +- 4 files changed, 62 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/de/sebse/fuplanner/MainActivity.java b/app/src/main/java/de/sebse/fuplanner/MainActivity.java index 7f32506..c8540bd 100644 --- a/app/src/main/java/de/sebse/fuplanner/MainActivity.java +++ b/app/src/main/java/de/sebse/fuplanner/MainActivity.java @@ -9,6 +9,7 @@ import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; +import android.text.TextUtils; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -26,6 +27,7 @@ import java.util.Iterator; import java.util.Locale; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.annotation.StringRes; import androidx.arch.core.util.Function; import androidx.appcompat.app.ActionBarDrawerToggle; @@ -52,6 +54,7 @@ import de.sebse.fuplanner.services.kvv.KVV; import de.sebse.fuplanner.services.kvv.KVVListener; import de.sebse.fuplanner.services.kvv.Login; import de.sebse.fuplanner.services.kvv.sync.KVVContentProvider; +import de.sebse.fuplanner.services.kvv.types.LoginTokenBB; import de.sebse.fuplanner.services.kvv.types.LoginTokenKVV; import de.sebse.fuplanner.services.kvv.types.Modules; import de.sebse.fuplanner.services.news.NewsManager; @@ -91,9 +94,11 @@ public class MainActivity extends AppCompatActivity private final Logger log = new Logger(this); private NavigationView mNavigationView; + private boolean mAlreadyCreated = false; + // KVV service private ArrayList mCallbacks = new ArrayList<>(); - private KVV mKVV; + @Nullable private KVV mKVV; boolean mBound = false; /** Defines callbacks for service binding, passed to bindService() */ private ServiceConnection mConnection = new ServiceConnection() { @@ -115,6 +120,7 @@ public class MainActivity extends AppCompatActivity public void onServiceDisconnected(ComponentName arg0) { mKVV.removeListener("mainactivity"); mBound = false; + mKVV = null; } }; @@ -140,14 +146,12 @@ public class MainActivity extends AppCompatActivity int desiredPage = getDefaultFragmentAfterLogin(); String desiredData = ""; Intent intent = getIntent(); - if (intent != null) { - if (CustomNotificationManager.NOTIFICATION_TYPE_NAVIGATE.equals(intent.getStringExtra(CustomNotificationManager.NOTIFICATION_INTENT))) { - int page = intent.getIntExtra(CustomNotificationManager.NOTIFICATION_PAGE, 0); - if (page == FRAGMENT_STARTUP || page == FRAGMENT_NONE) - page = getDefaultFragmentAfterLogin(); - desiredPage = page; - desiredData = intent.getStringExtra(CustomNotificationManager.NOTIFICATION_DATA); - } + if (intent != null && CustomNotificationManager.NOTIFICATION_TYPE_NAVIGATE.equals(intent.getStringExtra(CustomNotificationManager.NOTIFICATION_INTENT))) { + int page = intent.getIntExtra(CustomNotificationManager.NOTIFICATION_PAGE, 0); + if (page == FRAGMENT_STARTUP || page == FRAGMENT_NONE) + page = getDefaultFragmentAfterLogin(); + desiredPage = page; + desiredData = intent.getStringExtra(CustomNotificationManager.NOTIFICATION_DATA); } else if (savedInstanceState != null) { desiredPage = savedInstanceState.getInt(ARG_FRAGMENT_PAGE, desiredPage); desiredData = savedInstanceState.getString(ARG_FRAGMENT_STATUS, desiredData); @@ -173,7 +177,7 @@ public class MainActivity extends AppCompatActivity if (!mAccountManager.hasAccounts(AccountGeneral.ACCOUNT_TYPE)) { desiredPage = getDefaultFragmentAfterLogout(); desiredData = ""; - mAccountManager.getTokenByType(AccountGeneral.ACCOUNT_TYPE, AccountGeneral.AUTHTOKEN_TYPE_KVV, null, null); + mAccountManager.getTokenByType(AccountGeneral.ACCOUNT_TYPE, AccountGeneral.AUTHTOKEN_TYPE_BLACKBOARD, null, null); updateNavigation(); changeFragment(desiredPage, desiredData); } else { @@ -192,6 +196,12 @@ public class MainActivity extends AppCompatActivity }); } + if (!mAlreadyCreated) { + Intent serviceIntent = new Intent(this, KVV.class); + bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE); + } + mAlreadyCreated = true; + if (!Preferences.getBoolean(this, R.string.pref_set_auto_sync_on_startup)) { registerSync(true); Preferences.setBoolean(this, R.string.pref_set_auto_sync_on_startup, true); @@ -199,25 +209,34 @@ public class MainActivity extends AppCompatActivity registerSync(false); } CustomNotificationManager.createNotificationChannel(this); - /*getKVV().modules().list().recv(list -> { - Modules.Module module = list.getByIndex(0); - CustomNotificationManager.sendNotification(this, "Test", module.title, FRAGMENT_MODULES_DETAILS, module.getID()+"."+ModulePart.getPageByPart(ModulePart.EVENT)); - }, log::e);*/ + /*getKVV(kvv -> { + kvv.modules().list().recv(list -> { + Modules.Module module = list.getByIndex(0); + CustomNotificationManager.sendNotification(this, "Test", module.title, FRAGMENT_MODULES_DETAILS, module.getID()+"."+ ModulePart.getPageByPart(ModulePart.EVENT)); + }, log::e); + });*/ } @Override protected void onStart() { super.onStart(); - Intent intent = new Intent(this, KVV.class); - bindService(intent, mConnection, Context.BIND_AUTO_CREATE); + if (!mAlreadyCreated) { + Intent serviceIntent = new Intent(this, KVV.class); + bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE); + } + mAlreadyCreated = true; } @Override protected void onStop() { super.onStop(); - mKVV.removeListener("mainactivity"); - unbindService(mConnection); + if (mKVV != null) + mKVV.removeListener("mainactivity"); + if (mBound) + unbindService(mConnection); + mKVV = null; mBound = false; + mAlreadyCreated = false; } @Override @@ -291,7 +310,7 @@ public class MainActivity extends AppCompatActivity } else { mDoubleBackToExitPressedOnce = System.currentTimeMillis(); showToast(R.string.back_to_exit); - //getTokenForAccountCreateIfNeeded(AccountGeneral.ACCOUNT_TYPE, AccountGeneral.AUTHTOKEN_TYPE_KVV); + //getTokenForAccountCreateIfNeeded(AccountGeneral.ACCOUNT_TYPE, AccountGeneral.AUTHTOKEN_TYPE_BLACKBOARD); } }); } @@ -442,7 +461,7 @@ public class MainActivity extends AppCompatActivity setRefreshFailedBanner(false); updateNavigation(); changeFragment(getDefaultFragmentAfterLogout()); - //mAccountManager.getTokenByType(AccountGeneral.ACCOUNT_TYPE, AccountGeneral.AUTHTOKEN_TYPE_KVV, null, null); + //mAccountManager.getTokenByType(AccountGeneral.ACCOUNT_TYPE, AccountGeneral.AUTHTOKEN_TYPE_BLACKBOARD, null, null); } private void toLoginState(String fullName, String email, int newFragment) { @@ -615,7 +634,7 @@ public class MainActivity extends AppCompatActivity if (drawer.isDrawerOpen(GravityCompat.START) && !isDrawerFixed) { drawer.closeDrawer(GravityCompat.START); } - mAccountManager.getTokenByType(AccountGeneral.ACCOUNT_TYPE, AccountGeneral.AUTHTOKEN_TYPE_KVV, null, null); + mAccountManager.getTokenByType(AccountGeneral.ACCOUNT_TYPE, AccountGeneral.AUTHTOKEN_TYPE_BLACKBOARD, null, null); }); } @@ -770,8 +789,21 @@ public class MainActivity extends AppCompatActivity @Override - public void onLogin(LoginTokenKVV token, boolean isOnlyRefresh) { - toLoginState(token.getFullName(), token.getEmail(), getDefaultFragmentAfterLogin()); + public void onLogin(LoginTokenKVV tokenKVV, LoginTokenBB tokenBB, boolean isOnlyRefresh) { + String fullName = ""; + if (tokenKVV != null && !TextUtils.isEmpty(tokenKVV.getFullName())) { + fullName = tokenKVV.getFullName(); + } else if (tokenBB != null && !TextUtils.isEmpty(tokenBB.getUsername())) { + fullName = tokenBB.getUsername(); + } + + String email = ""; + if (tokenKVV != null && !TextUtils.isEmpty(tokenKVV.getEmail())) { + email = tokenKVV.getFullName(); + } else if (tokenBB != null && !TextUtils.isEmpty(tokenBB.getUsername())) { + email = tokenBB.getUsername() + "@zedat.fu-berlin.de"; + } + toLoginState(fullName, email, getDefaultFragmentAfterLogin()); } @Override diff --git a/app/src/main/java/de/sebse/fuplanner/services/kvv/KVV.java b/app/src/main/java/de/sebse/fuplanner/services/kvv/KVV.java index a87c469..a954bdc 100644 --- a/app/src/main/java/de/sebse/fuplanner/services/kvv/KVV.java +++ b/app/src/main/java/de/sebse/fuplanner/services/kvv/KVV.java @@ -12,9 +12,9 @@ import org.jetbrains.annotations.NotNull; import java.util.HashMap; +import de.sebse.fuplanner.services.kvv.types.LoginTokenBB; import de.sebse.fuplanner.services.kvv.types.LoginTokenKVV; import de.sebse.fuplanner.tools.CustomAccountManager; -import de.sebse.fuplanner.tools.logging.Logger; public class KVV extends Service { private final HashMap addons = new HashMap<>(); @@ -36,9 +36,9 @@ public class KVV extends Service { } @Override - public void onLogin(LoginTokenKVV token, boolean isOnlyRefresh) { + public void onLogin(LoginTokenKVV tokenKVV, LoginTokenBB tokenBB, boolean isOnlyRefresh) { for (KVVListener listener : mListeners.values()) - listener.onLogin(token, isOnlyRefresh); + listener.onLogin(tokenKVV, tokenBB, isOnlyRefresh); } @Override @@ -60,10 +60,6 @@ public class KVV extends Service { } }; - public KVV() { - Logger log = new Logger(this); - } - // Binder given to clients private final IBinder mBinder = new LocalBinder(); diff --git a/app/src/main/java/de/sebse/fuplanner/services/kvv/KVVListener.java b/app/src/main/java/de/sebse/fuplanner/services/kvv/KVVListener.java index 2ee107a..dbb6b9c 100644 --- a/app/src/main/java/de/sebse/fuplanner/services/kvv/KVVListener.java +++ b/app/src/main/java/de/sebse/fuplanner/services/kvv/KVVListener.java @@ -2,12 +2,13 @@ package de.sebse.fuplanner.services.kvv; import com.android.volley.NetworkResponse; +import de.sebse.fuplanner.services.kvv.types.LoginTokenBB; import de.sebse.fuplanner.services.kvv.types.LoginTokenKVV; import de.sebse.fuplanner.services.kvv.types.Modules; import de.sebse.fuplanner.tools.CustomAccountManager; public interface KVVListener { - default void onLogin(LoginTokenKVV token, boolean isOnlyRefresh) {} + default void onLogin(LoginTokenKVV tokenKVV, LoginTokenBB tokenBB, boolean isOnlyRefresh) {} default void onLogout() {} diff --git a/app/src/main/java/de/sebse/fuplanner/services/kvv/Login.java b/app/src/main/java/de/sebse/fuplanner/services/kvv/Login.java index 36ccd86..8711582 100644 --- a/app/src/main/java/de/sebse/fuplanner/services/kvv/Login.java +++ b/app/src/main/java/de/sebse/fuplanner/services/kvv/Login.java @@ -164,8 +164,8 @@ public class Login extends HTTPService { private boolean handleCallbacks(boolean isOnlyRefresh) { - if (mTokenKVV != null) { - mListener.onLogin(mTokenKVV, isOnlyRefresh); + if (mTokenKVV != null || mTokenBB != null) { + mListener.onLogin(mTokenKVV, mTokenBB, isOnlyRefresh); return true; } else { mListener.onLogout();