diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index 268b9ca5..8882bed9 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -57,6 +57,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Deque; import java.util.List; +import java.util.UUID; import java.util.stream.Collectors; import awais.instagrabber.BuildConfig; @@ -85,8 +86,8 @@ import awais.instagrabber.utils.TextUtils; import awais.instagrabber.utils.Utils; import awais.instagrabber.utils.emoji.EmojiParser; import awais.instagrabber.viewmodels.AppStateViewModel; -import awais.instagrabber.webservices.RetrofitFactory; import awais.instagrabber.viewmodels.DirectInboxViewModel; +import awais.instagrabber.webservices.RetrofitFactory; import awais.instagrabber.webservices.SearchService; import retrofit2.Call; import retrofit2.Callback; @@ -137,9 +138,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage RetrofitFactory.setup(this); super.onCreate(savedInstanceState); binding = ActivityMainBinding.inflate(getLayoutInflater()); - final String cookie = settingsHelper.getString(Constants.COOKIE); - CookieUtils.setupCookies(cookie); - isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) != 0; + setupCookie(); if (settingsHelper.getBoolean(Constants.FLAG_SECURE)) getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE); setContentView(binding.getRoot()); @@ -165,7 +164,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage new ViewModelProvider(this).get(AppStateViewModel.class); // Just initiate the App state here final Intent intent = getIntent(); handleIntent(intent); - if (!TextUtils.isEmpty(cookie) && settingsHelper.getBoolean(Constants.CHECK_ACTIVITY)) { + if (isLoggedIn && settingsHelper.getBoolean(Constants.CHECK_ACTIVITY)) { bindActivityCheckerService(); } getSupportFragmentManager().addOnBackStackChangedListener(this); @@ -180,6 +179,26 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage initDmUnreadCount(); } + private void setupCookie() { + final String cookie = settingsHelper.getString(Constants.COOKIE); + long userId = 0; + String csrfToken = null; + if (!TextUtils.isEmpty(cookie)) { + userId = CookieUtils.getUserIdFromCookie(cookie); + csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie); + } + if (TextUtils.isEmpty(cookie) || userId == 0 || TextUtils.isEmpty(csrfToken)) { + isLoggedIn = false; + return; + } + final String deviceUuid = settingsHelper.getString(Constants.DEVICE_UUID); + if (TextUtils.isEmpty(deviceUuid)) { + settingsHelper.putString(Constants.DEVICE_UUID, UUID.randomUUID().toString()); + } + CookieUtils.setupCookies(cookie); + isLoggedIn = true; + } + private void initDmService() { if (!isLoggedIn) return; final boolean enabled = settingsHelper.getBoolean(PreferenceKeys.PREF_ENABLE_DM_AUTO_REFRESH); @@ -916,9 +935,9 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage return currentTabs; } -// public boolean isNavRootInCurrentTabs(@IdRes final int navRootId) { -// return showBottomViewDestinations.stream().anyMatch(id -> id == navRootId); -// } + // public boolean isNavRootInCurrentTabs(@IdRes final int navRootId) { + // return showBottomViewDestinations.stream().anyMatch(id -> id == navRootId); + // } private void setNavBarDMUnreadCountBadge(final int unseenCount) { final BadgeDrawable badge = binding.bottomNavView.getOrCreateBadge(R.id.direct_messages_nav_graph); diff --git a/app/src/main/java/awais/instagrabber/managers/InboxManager.java b/app/src/main/java/awais/instagrabber/managers/InboxManager.java index 98e12b46..efca55f5 100644 --- a/app/src/main/java/awais/instagrabber/managers/InboxManager.java +++ b/app/src/main/java/awais/instagrabber/managers/InboxManager.java @@ -82,8 +82,12 @@ public final class InboxManager { final long userId = CookieUtils.getUserIdFromCookie(cookie); final String deviceUuid = settingsHelper.getString(Constants.DEVICE_UUID); final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie); - if (TextUtils.isEmpty(csrfToken) || userId <= 0 || TextUtils.isEmpty(deviceUuid)) { - throw new IllegalArgumentException("User is not logged in!"); + if (TextUtils.isEmpty(csrfToken)) { + throw new IllegalArgumentException("csrfToken is empty!"); + } else if (userId == 0) { + throw new IllegalArgumentException("user id invalid"); + } else if (TextUtils.isEmpty(deviceUuid)) { + throw new IllegalArgumentException("device uuid is empty!"); } service = DirectMessagesService.getInstance(csrfToken, userId, deviceUuid); diff --git a/app/src/main/java/awais/instagrabber/managers/ThreadManager.java b/app/src/main/java/awais/instagrabber/managers/ThreadManager.java index 8efff504..c76728e3 100644 --- a/app/src/main/java/awais/instagrabber/managers/ThreadManager.java +++ b/app/src/main/java/awais/instagrabber/managers/ThreadManager.java @@ -156,9 +156,9 @@ public final class ThreadManager { viewerId = CookieUtils.getUserIdFromCookie(cookie); final String deviceUuid = settingsHelper.getString(Constants.DEVICE_UUID); final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie); - if (TextUtils.isEmpty(csrfToken) || viewerId <= 0 || TextUtils.isEmpty(deviceUuid)) { - throw new IllegalArgumentException("User is not logged in!"); - } + // if (TextUtils.isEmpty(csrfToken) || viewerId <= 0 || TextUtils.isEmpty(deviceUuid)) { + // throw new IllegalArgumentException("User is not logged in!"); + // } service = DirectMessagesService.getInstance(csrfToken, viewerId, deviceUuid); mediaService = MediaService.getInstance(deviceUuid, csrfToken, viewerId); friendshipService = FriendshipService.getInstance(deviceUuid, csrfToken, viewerId);