4
0
mirror of https://github.com/QuasarApp/QtAndroidTools.git synced 2025-04-30 06:54:31 +00:00

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

@ -21,6 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <QUrl>
#include "QAndroidGoogleAccount.h"
QAndroidGoogleAccount *QAndroidGoogleAccount::m_pInstance = nullptr;
@ -30,16 +31,17 @@ QAndroidGoogleAccount::QAndroidGoogleAccount() : m_JavaGoogleAccount("com/falsin
QtAndroid::androidActivity().object<jobject>())
{
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<jstring>("id").toString();
m_LastSignedInAccountInfo.DisplayName = AccountInfoObj.getObjectField<jstring>("displayName").toString();
m_LastSignedInAccountInfo.Email = AccountInfoObj.getObjectField<jstring>("email").toString();
m_LastSignedInAccountInfo.FamilyName = AccountInfoObj.getObjectField<jstring>("familyName").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;
}
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<void>("signInIntentDataResult",
"(Landroid/content/Intent;)V",
Data.object()
);
LoadLastSignedInAccountInfo();
const bool SignInSuccessfully = m_JavaGoogleAccount.callMethod<jboolean>("signInIntentDataResult",
"(Landroid/content/Intent;)V",
Data.object()
);
if(SignInSuccessfully == true)
{
LoadLastSignedInAccountInfo();
}
}
}
}

@ -23,11 +23,16 @@
*/
#pragma once
#include <QQuickImageProvider>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QtAndroidExtras>
#include <QQmlEngine>
#include <QPixmap>
#include <QImage>
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;
};

@ -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<GoogleSignInAccount> 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

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

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