Added code for download Google account photo

This commit is contained in:
FalsinSoft 2019-09-27 16:13:24 +02:00
parent b872cc3acc
commit e2273b1f48
5 changed files with 100 additions and 26 deletions

View File

@ -21,6 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. * SOFTWARE.
*/ */
#include <QUrl>
#include "QAndroidGoogleAccount.h" #include "QAndroidGoogleAccount.h"
QAndroidGoogleAccount *QAndroidGoogleAccount::m_pInstance = nullptr; QAndroidGoogleAccount *QAndroidGoogleAccount::m_pInstance = nullptr;
@ -30,16 +31,17 @@ QAndroidGoogleAccount::QAndroidGoogleAccount() : m_JavaGoogleAccount("com/falsin
QtAndroid::androidActivity().object<jobject>()) QtAndroid::androidActivity().object<jobject>())
{ {
m_pInstance = this; m_pInstance = this;
connect(&m_NetworkAccessManager, &QNetworkAccessManager::finished, this, &QAndroidGoogleAccount::AccountPhotoDownloaded);
LoadLastSignedInAccountInfo(); LoadLastSignedInAccountInfo();
} }
QObject* QAndroidGoogleAccount::qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine) 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() QAndroidGoogleAccount* QAndroidGoogleAccount::instance()
@ -72,33 +74,60 @@ void QAndroidGoogleAccount::LoadLastSignedInAccountInfo()
); );
if(AccountInfoObj.isValid()) if(AccountInfoObj.isValid())
{ {
QString PhotoUrl;
m_LastSignedInAccountInfo.Id = AccountInfoObj.getObjectField<jstring>("id").toString(); m_LastSignedInAccountInfo.Id = AccountInfoObj.getObjectField<jstring>("id").toString();
m_LastSignedInAccountInfo.DisplayName = AccountInfoObj.getObjectField<jstring>("displayName").toString(); m_LastSignedInAccountInfo.DisplayName = AccountInfoObj.getObjectField<jstring>("displayName").toString();
m_LastSignedInAccountInfo.Email = AccountInfoObj.getObjectField<jstring>("email").toString(); m_LastSignedInAccountInfo.Email = AccountInfoObj.getObjectField<jstring>("email").toString();
m_LastSignedInAccountInfo.FamilyName = AccountInfoObj.getObjectField<jstring>("familyName").toString(); m_LastSignedInAccountInfo.FamilyName = AccountInfoObj.getObjectField<jstring>("familyName").toString();
m_LastSignedInAccountInfo.GivenName = AccountInfoObj.getObjectField<jstring>("givenName").toString(); m_LastSignedInAccountInfo.GivenName = AccountInfoObj.getObjectField<jstring>("givenName").toString();
PhotoUrl = AccountInfoObj.getObjectField<jstring>("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; 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) void QAndroidGoogleAccount::ActivityResult(int RequestCode, int ResultCode, const QAndroidJniObject &Data)
{ {
Q_UNUSED(ResultCode); Q_UNUSED(ResultCode)
if(RequestCode == m_SignInId) if(RequestCode == m_SignInId)
{ {
if(m_JavaGoogleAccount.isValid()) if(m_JavaGoogleAccount.isValid())
{ {
m_JavaGoogleAccount.callMethod<void>("signInIntentDataResult", const bool SignInSuccessfully = m_JavaGoogleAccount.callMethod<jboolean>("signInIntentDataResult",
"(Landroid/content/Intent;)V", "(Landroid/content/Intent;)V",
Data.object() Data.object()
); );
LoadLastSignedInAccountInfo(); if(SignInSuccessfully == true)
{
LoadLastSignedInAccountInfo();
}
} }
} }
} }

View File

@ -23,11 +23,16 @@
*/ */
#pragma once #pragma once
#include <QQuickImageProvider>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QtAndroidExtras> #include <QtAndroidExtras>
#include <QQmlEngine> #include <QQmlEngine>
#include <QPixmap>
#include <QImage> #include <QImage>
struct QAndroidGoogleLastSignedInAccountInfo struct QAndroidGoogleAccountInfo
{ {
Q_GADGET Q_GADGET
Q_PROPERTY(QString id MEMBER Id) Q_PROPERTY(QString id MEMBER Id)
@ -35,23 +40,37 @@ struct QAndroidGoogleLastSignedInAccountInfo
Q_PROPERTY(QString email MEMBER Email) Q_PROPERTY(QString email MEMBER Email)
Q_PROPERTY(QString familyName MEMBER FamilyName) Q_PROPERTY(QString familyName MEMBER FamilyName)
Q_PROPERTY(QString givenName MEMBER GivenName) Q_PROPERTY(QString givenName MEMBER GivenName)
Q_PROPERTY(QImage photo MEMBER Photo)
public: public:
QString Id; QString Id;
QString DisplayName; QString DisplayName;
QString Email; QString Email;
QString FamilyName; QString FamilyName;
QString GivenName; QString GivenName;
QImage Photo;
}; };
Q_DECLARE_METATYPE(QAndroidGoogleLastSignedInAccountInfo) Q_DECLARE_METATYPE(QAndroidGoogleAccountInfo)
class QAndroidGoogleAccount : public QObject class QAndroidGoogleAccount : public QObject
{ {
Q_PROPERTY(QAndroidGoogleLastSignedInAccountInfo lastSignedInAccount READ getLastSignedInAccountInfo) Q_PROPERTY(QAndroidGoogleAccountInfo lastSignedInAccount READ getLastSignedInAccountInfo NOTIFY lastSignedInAccountInfoChanged)
Q_DISABLE_COPY(QAndroidGoogleAccount) Q_DISABLE_COPY(QAndroidGoogleAccount)
Q_OBJECT 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(); QAndroidGoogleAccount();
public: public:
@ -60,14 +79,24 @@ public:
static QAndroidGoogleAccount* instance(); static QAndroidGoogleAccount* instance();
Q_INVOKABLE bool signIn(); Q_INVOKABLE bool signIn();
const QAndroidGoogleLastSignedInAccountInfo& getLastSignedInAccountInfo() const;
const QAndroidGoogleAccountInfo& getLastSignedInAccountInfo() const;
signals:
void lastSignedInAccountInfoChanged();
private slots:
void AccountPhotoDownloaded(QNetworkReply *pReply);
private: private:
const QAndroidJniObject m_JavaGoogleAccount; const QAndroidJniObject m_JavaGoogleAccount;
static QAndroidGoogleAccount *m_pInstance; static QAndroidGoogleAccount *m_pInstance;
const int m_SignInId = 9001; 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 ActivityResult(int RequestCode, int ResultCode, const QAndroidJniObject &Data);
void LoadLastSignedInAccountInfo(); void LoadLastSignedInAccountInfo();
QPixmap GetAccountPhoto() const;
}; };

View File

@ -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.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient; 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.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInStatusCodes;
import com.google.android.gms.common.Scopes; import com.google.android.gms.common.Scopes;
import com.google.android.gms.common.SignInButton; import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.ApiException; 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.OnCompleteListener;
import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.Task;
import java.io.FileNotFoundException;
import java.io.IOException;
public class AndroidGoogleAccount public class AndroidGoogleAccount
{ {
private final Activity mActivityInstance; private final Activity mActivityInstance;
@ -62,7 +60,6 @@ public class AndroidGoogleAccount
private void getSignInClient(Activity ActivityInstance) private void getSignInClient(Activity ActivityInstance)
{ {
GoogleSignInOptions SignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) GoogleSignInOptions SignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestProfile()
.requestEmail() .requestEmail()
.build(); .build();
@ -75,14 +72,20 @@ public class AndroidGoogleAccount
if(mLastSignedInAccount != null) if(mLastSignedInAccount != null)
{ {
final Uri PhotoUrl = mLastSignedInAccount.getPhotoUrl(); Uri PhotoUrl;
Account = new AccountInfo();
Account = new AccountInfo();
Account.id = mLastSignedInAccount.getId(); Account.id = mLastSignedInAccount.getId();
Account.displayName = mLastSignedInAccount.getDisplayName(); Account.displayName = mLastSignedInAccount.getDisplayName();
Account.email = mLastSignedInAccount.getEmail(); Account.email = mLastSignedInAccount.getEmail();
Account.familyName = mLastSignedInAccount.getFamilyName(); Account.familyName = mLastSignedInAccount.getFamilyName();
Account.givenName = mLastSignedInAccount.getGivenName(); Account.givenName = mLastSignedInAccount.getGivenName();
PhotoUrl = mLastSignedInAccount.getPhotoUrl();
if(PhotoUrl != null)
Account.photoUrl = PhotoUrl.toString();
else
Account.photoUrl = new String();
} }
return Account; return Account;
@ -93,7 +96,7 @@ public class AndroidGoogleAccount
return mGoogleSignInClient.getSignInIntent(); return mGoogleSignInClient.getSignInIntent();
} }
public void signInIntentDataResult(Intent Data) public boolean signInIntentDataResult(Intent Data)
{ {
Task<GoogleSignInAccount> SignInTask = GoogleSignIn.getSignedInAccountFromIntent(Data); Task<GoogleSignInAccount> SignInTask = GoogleSignIn.getSignedInAccountFromIntent(Data);
@ -102,11 +105,18 @@ public class AndroidGoogleAccount
try try
{ {
mLastSignedInAccount = SignInTask.getResult(ApiException.class); mLastSignedInAccount = SignInTask.getResult(ApiException.class);
return true;
} }
catch(ApiException e) 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 public static class AccountInfo

View File

@ -1,4 +1,4 @@
QT += quick quickcontrols2 svg QT += quick quickcontrols2 svg network
CONFIG += c++11 CONFIG += c++11
TARGET = QtAndroidToolsDemo TARGET = QtAndroidToolsDemo

View File

@ -62,6 +62,12 @@ Page {
text: QtAndroidGoogleAccount.lastSignedInAccount.givenName text: QtAndroidGoogleAccount.lastSignedInAccount.givenName
} }
Image {
width: 200
height: 200
source: "image://LastSignedInAccountPhoto/"
}
Button { Button {
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
text: "sigIn" text: "sigIn"