diff --git a/src/Kp2aBusinessLogic/Io/OneDrive2FileStorage.cs b/src/Kp2aBusinessLogic/Io/OneDrive2FileStorage.cs index 55d2a187..b57a62e7 100644 --- a/src/Kp2aBusinessLogic/Io/OneDrive2FileStorage.cs +++ b/src/Kp2aBusinessLogic/Io/OneDrive2FileStorage.cs @@ -322,20 +322,37 @@ namespace keepass2android.Io private async Task TryGetMsGraphClient(String path, bool tryConnect) { + String userId = OneDrive2ItemLocation.FromString(path).User.Id; + + logDebug("TryGetMsGraphClient for " + userId); if (mClientByUser.ContainsKey(userId)) { + logDebug("TryGetMsGraphClient found user " + userId); GraphServiceClientWithState clientWithState = mClientByUser[userId]; - if (!(clientWithState.RequiresUserInteraction || (clientWithState.TokenExpiryDate < DateTime.Now) || (clientWithState.Client == null))) + if (!(clientWithState.RequiresUserInteraction || (clientWithState.TokenExpiryDate < DateTime.Now) || + (clientWithState.Client == null))) + { + logDebug("TryGetMsGraphClient returning client"); return clientWithState.Client; + } + else + { + logDebug("not returning client because " + clientWithState.RequiresUserInteraction + " " + + (clientWithState.TokenExpiryDate < DateTime.Now) + " " + (clientWithState.Client == null)); + } } if (tryConnect) { + logDebug("trying to connect..."); if (await TryLoginSilent(path) != null) { + logDebug("trying to connect ok"); return mClientByUser[userId].Client; } + logDebug("trying to connect failed"); } + logDebug("TryGetMsGraphClient for " + userId + " returns null"); return null; } @@ -367,7 +384,7 @@ namespace keepass2android.Io if (authenticationResult.Account == null) throw new Exception("authenticationResult.Account == null!"); mClientByUser[authenticationResult.Account.HomeAccountId.Identifier] = clientWithState; - + logDebug("buildClient ok."); return clientWithState.Client; } @@ -375,7 +392,9 @@ namespace keepass2android.Io private void logDebug(string str) { - Log.Debug("KP2A", str); +#if DEBUG + Log.Debug("KP2A", "OneDrive2: " + str); +#endif } @@ -858,14 +877,17 @@ namespace keepass2android.Io public async void OnStart(IFileStorageSetupActivity activity) { - + logDebug("OneDrive2.OnStart"); if (activity.ProcessName.Equals(FileStorageSetupDefs.ProcessNameFileUsageSetup)) activity.State.PutString(FileStorageSetupDefs.ExtraPath, activity.Ioc.Path); string rootPathForUser = await TryLoginSilent(activity.Ioc.Path); if (rootPathForUser != null) { + logDebug("rootPathForUser not null"); FinishActivityWithSuccess(activity, rootPathForUser); + return; } + logDebug("rootPathForUser null"); try { @@ -893,13 +915,14 @@ namespace keepass2android.Io private async Task TryLoginSilent(string iocPath) { - + logDebug("Login Silent for " + iocPath); IAccount account = null; try { if (IsConnected(iocPath)) { + logDebug("Login Silent ok, connected"); return iocPath; } String userId = OneDrive2ItemLocation.FromString(iocPath).User?.Id; @@ -928,7 +951,9 @@ namespace keepass2android.Io /*User me = await graphClient.Me.Request().WithForceRefresh(true).GetAsync(); logDebug("received name " + me.DisplayName);*/ - return BuildRootPathForUser(authResult); + var rootFolder = BuildRootPathForUser(authResult); + logDebug("Found RootPath for user"); + return rootFolder; } catch (MsalUiRequiredException ex)