diff --git a/QtAndroidTools/QAndroidGoogleAccount.cpp b/QtAndroidTools/QAndroidGoogleAccount.cpp index d26c401..ad07feb 100644 --- a/QtAndroidTools/QAndroidGoogleAccount.cpp +++ b/QtAndroidTools/QAndroidGoogleAccount.cpp @@ -21,6 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ +#include #include "QAndroidGoogleAccount.h" QAndroidGoogleAccount *QAndroidGoogleAccount::m_pInstance = nullptr; @@ -30,16 +31,17 @@ QAndroidGoogleAccount::QAndroidGoogleAccount() : m_JavaGoogleAccount("com/falsin QtAndroid::androidActivity().object()) { m_pInstance = this; - + connect(&m_NetworkAccessManager, &QNetworkAccessManager::finished, this, &QAndroidGoogleAccount::AccountPhotoDownloaded); LoadLastSignedInAccountInfo(); } QObject* QAndroidGoogleAccount::qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine) { - Q_UNUSED(engine); - Q_UNUSED(scriptEngine); + Q_UNUSED(scriptEngine) - return new QAndroidGoogleAccount(); + QAndroidGoogleAccount *pAndroidGoogleAccount = new QAndroidGoogleAccount(); + engine->addImageProvider("LastSignedInAccountPhoto", new AccountPhotoImageProvider(pAndroidGoogleAccount)); + return pAndroidGoogleAccount; } QAndroidGoogleAccount* QAndroidGoogleAccount::instance() @@ -72,33 +74,60 @@ void QAndroidGoogleAccount::LoadLastSignedInAccountInfo() ); if(AccountInfoObj.isValid()) { + QString PhotoUrl; + m_LastSignedInAccountInfo.Id = AccountInfoObj.getObjectField("id").toString(); m_LastSignedInAccountInfo.DisplayName = AccountInfoObj.getObjectField("displayName").toString(); m_LastSignedInAccountInfo.Email = AccountInfoObj.getObjectField("email").toString(); m_LastSignedInAccountInfo.FamilyName = AccountInfoObj.getObjectField("familyName").toString(); m_LastSignedInAccountInfo.GivenName = AccountInfoObj.getObjectField("givenName").toString(); + + PhotoUrl = AccountInfoObj.getObjectField("photoUrl").toString(); + if(PhotoUrl.isEmpty() == false) + { + const QNetworkRequest PhotoDownloadRequest(PhotoUrl); + m_NetworkAccessManager.get(PhotoDownloadRequest); + } + + emit lastSignedInAccountInfoChanged(); } } } -const QAndroidGoogleLastSignedInAccountInfo& QAndroidGoogleAccount::getLastSignedInAccountInfo() const +const QAndroidGoogleAccountInfo& QAndroidGoogleAccount::getLastSignedInAccountInfo() const { return m_LastSignedInAccountInfo; } +QPixmap QAndroidGoogleAccount::GetAccountPhoto() const +{ + return m_LastSignedInAccountPhoto; +} + +void QAndroidGoogleAccount::AccountPhotoDownloaded(QNetworkReply *pReply) +{ + if(pReply->error() == QNetworkReply::NoError) + { + m_LastSignedInAccountPhoto = QPixmap(pReply->readAll()); + } +} + void QAndroidGoogleAccount::ActivityResult(int RequestCode, int ResultCode, const QAndroidJniObject &Data) { - Q_UNUSED(ResultCode); + Q_UNUSED(ResultCode) if(RequestCode == m_SignInId) { if(m_JavaGoogleAccount.isValid()) { - m_JavaGoogleAccount.callMethod("signInIntentDataResult", - "(Landroid/content/Intent;)V", - Data.object() - ); - LoadLastSignedInAccountInfo(); + const bool SignInSuccessfully = m_JavaGoogleAccount.callMethod("signInIntentDataResult", + "(Landroid/content/Intent;)V", + Data.object() + ); + if(SignInSuccessfully == true) + { + LoadLastSignedInAccountInfo(); + } } } } diff --git a/QtAndroidTools/QAndroidGoogleAccount.h b/QtAndroidTools/QAndroidGoogleAccount.h index a45c490..d7b37d6 100644 --- a/QtAndroidTools/QAndroidGoogleAccount.h +++ b/QtAndroidTools/QAndroidGoogleAccount.h @@ -23,11 +23,16 @@ */ #pragma once +#include +#include +#include +#include #include #include +#include #include -struct QAndroidGoogleLastSignedInAccountInfo +struct QAndroidGoogleAccountInfo { Q_GADGET Q_PROPERTY(QString id MEMBER Id) @@ -35,23 +40,37 @@ struct QAndroidGoogleLastSignedInAccountInfo Q_PROPERTY(QString email MEMBER Email) Q_PROPERTY(QString familyName MEMBER FamilyName) Q_PROPERTY(QString givenName MEMBER GivenName) - Q_PROPERTY(QImage photo MEMBER Photo) public: QString Id; QString DisplayName; QString Email; QString FamilyName; QString GivenName; - QImage Photo; }; -Q_DECLARE_METATYPE(QAndroidGoogleLastSignedInAccountInfo) +Q_DECLARE_METATYPE(QAndroidGoogleAccountInfo) class QAndroidGoogleAccount : public QObject { - Q_PROPERTY(QAndroidGoogleLastSignedInAccountInfo lastSignedInAccount READ getLastSignedInAccountInfo) + Q_PROPERTY(QAndroidGoogleAccountInfo lastSignedInAccount READ getLastSignedInAccountInfo NOTIFY lastSignedInAccountInfoChanged) Q_DISABLE_COPY(QAndroidGoogleAccount) Q_OBJECT + class AccountPhotoImageProvider : public QQuickImageProvider + { + public: + AccountPhotoImageProvider(QAndroidGoogleAccount *pAccount) : QQuickImageProvider(QQuickImageProvider::Pixmap), m_pAccount(pAccount) {} + + QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) + { + Q_UNUSED(id) + Q_UNUSED(size) + return m_pAccount->GetAccountPhoto().scaled(requestedSize); + } + + private: + const QAndroidGoogleAccount const *m_pAccount; + }; + QAndroidGoogleAccount(); public: @@ -60,14 +79,24 @@ public: static QAndroidGoogleAccount* instance(); Q_INVOKABLE bool signIn(); - const QAndroidGoogleLastSignedInAccountInfo& getLastSignedInAccountInfo() const; + + const QAndroidGoogleAccountInfo& getLastSignedInAccountInfo() const; + +signals: + void lastSignedInAccountInfoChanged(); + +private slots: + void AccountPhotoDownloaded(QNetworkReply *pReply); private: const QAndroidJniObject m_JavaGoogleAccount; static QAndroidGoogleAccount *m_pInstance; const int m_SignInId = 9001; - QAndroidGoogleLastSignedInAccountInfo m_LastSignedInAccountInfo; + QAndroidGoogleAccountInfo m_LastSignedInAccountInfo; + QNetworkAccessManager m_NetworkAccessManager; + QPixmap m_LastSignedInAccountPhoto; void ActivityResult(int RequestCode, int ResultCode, const QAndroidJniObject &Data); void LoadLastSignedInAccountInfo(); + QPixmap GetAccountPhoto() const; }; diff --git a/QtAndroidTools/src/com/falsinsoft/qtandroidtools/AndroidGoogleAccount.java b/QtAndroidTools/src/com/falsinsoft/qtandroidtools/AndroidGoogleAccount.java index 041036e..8330147 100644 --- a/QtAndroidTools/src/com/falsinsoft/qtandroidtools/AndroidGoogleAccount.java +++ b/QtAndroidTools/src/com/falsinsoft/qtandroidtools/AndroidGoogleAccount.java @@ -36,6 +36,7 @@ import com.google.android.gms.auth.api.signin.GoogleSignIn; import com.google.android.gms.auth.api.signin.GoogleSignInAccount; import com.google.android.gms.auth.api.signin.GoogleSignInClient; import com.google.android.gms.auth.api.signin.GoogleSignInOptions; +import com.google.android.gms.auth.api.signin.GoogleSignInStatusCodes; import com.google.android.gms.common.Scopes; import com.google.android.gms.common.SignInButton; import com.google.android.gms.common.api.ApiException; @@ -43,9 +44,6 @@ import com.google.android.gms.common.api.Scope; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; -import java.io.FileNotFoundException; -import java.io.IOException; - public class AndroidGoogleAccount { private final Activity mActivityInstance; @@ -62,7 +60,6 @@ public class AndroidGoogleAccount private void getSignInClient(Activity ActivityInstance) { GoogleSignInOptions SignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) - .requestProfile() .requestEmail() .build(); @@ -75,14 +72,20 @@ public class AndroidGoogleAccount if(mLastSignedInAccount != null) { - final Uri PhotoUrl = mLastSignedInAccount.getPhotoUrl(); - Account = new AccountInfo(); + Uri PhotoUrl; + Account = new AccountInfo(); Account.id = mLastSignedInAccount.getId(); Account.displayName = mLastSignedInAccount.getDisplayName(); Account.email = mLastSignedInAccount.getEmail(); Account.familyName = mLastSignedInAccount.getFamilyName(); Account.givenName = mLastSignedInAccount.getGivenName(); + + PhotoUrl = mLastSignedInAccount.getPhotoUrl(); + if(PhotoUrl != null) + Account.photoUrl = PhotoUrl.toString(); + else + Account.photoUrl = new String(); } return Account; @@ -93,7 +96,7 @@ public class AndroidGoogleAccount return mGoogleSignInClient.getSignInIntent(); } - public void signInIntentDataResult(Intent Data) + public boolean signInIntentDataResult(Intent Data) { Task SignInTask = GoogleSignIn.getSignedInAccountFromIntent(Data); @@ -102,11 +105,18 @@ public class AndroidGoogleAccount try { mLastSignedInAccount = SignInTask.getResult(ApiException.class); + return true; } catch(ApiException e) { + if(e.getStatusCode() == GoogleSignInStatusCodes.DEVELOPER_ERROR) + { + Log.d("AndroidGoogleAccount", "DEVELOPER_ERROR -> Have you signed your project on Android console?"); + } } } + + return false; } public static class AccountInfo diff --git a/QtAndroidToolsDemo/QtAndroidToolsDemo.pro b/QtAndroidToolsDemo/QtAndroidToolsDemo.pro index c7327d9..0ffb5cf 100644 --- a/QtAndroidToolsDemo/QtAndroidToolsDemo.pro +++ b/QtAndroidToolsDemo/QtAndroidToolsDemo.pro @@ -1,4 +1,4 @@ -QT += quick quickcontrols2 svg +QT += quick quickcontrols2 svg network CONFIG += c++11 TARGET = QtAndroidToolsDemo diff --git a/QtAndroidToolsDemo/tools/AndroidGoogleAccount.qml b/QtAndroidToolsDemo/tools/AndroidGoogleAccount.qml index c75ceb7..c8bfe6e 100644 --- a/QtAndroidToolsDemo/tools/AndroidGoogleAccount.qml +++ b/QtAndroidToolsDemo/tools/AndroidGoogleAccount.qml @@ -62,6 +62,12 @@ Page { text: QtAndroidGoogleAccount.lastSignedInAccount.givenName } + Image { + width: 200 + height: 200 + source: "image://LastSignedInAccountPhoto/" + } + Button { anchors.horizontalCenter: parent.horizontalCenter text: "sigIn"