mirror of
https://github.com/QuasarApp/QtAndroidTools.git
synced 2025-04-30 15:04:30 +00:00
Added method for get drive files list
This commit is contained in:
parent
8c20913be8
commit
2625fb01ea
@ -71,7 +71,7 @@ class QAndroidGoogleAccount : public QObject, public QAndroidActivityResultRecei
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const QAndroidGoogleAccount const *m_pAccount;
|
const QAndroidGoogleAccount *const m_pAccount;
|
||||||
};
|
};
|
||||||
|
|
||||||
QAndroidGoogleAccount();
|
QAndroidGoogleAccount();
|
||||||
|
@ -46,6 +46,86 @@ QAndroidGoogleDrive* QAndroidGoogleDrive::instance()
|
|||||||
return m_pInstance;
|
return m_pInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool QAndroidGoogleDrive::authenticate(const QString &AppName, const QString &ScopeName)
|
||||||
|
{
|
||||||
|
if(m_JavaGoogleDrive.isValid())
|
||||||
|
{
|
||||||
|
m_isAuthenticated = m_JavaGoogleDrive.callMethod<jboolean>("authenticate",
|
||||||
|
"(Ljava/lang/String;Ljava/lang/String;)Z",
|
||||||
|
QAndroidJniObject::fromString(AppName).object<jstring>(),
|
||||||
|
QAndroidJniObject::fromString(ScopeName).object<jstring>()
|
||||||
|
);
|
||||||
|
emit isAuthenticatedChanged();
|
||||||
|
return m_isAuthenticated;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariantList QAndroidGoogleDrive::getFilesList(const QString &Query)
|
||||||
|
{
|
||||||
|
QAndroidJniEnvironment JniEnv;
|
||||||
|
QVariantList FilesList;
|
||||||
|
|
||||||
|
if(m_JavaGoogleDrive.isValid())
|
||||||
|
{
|
||||||
|
const QAndroidJniObject FilesListObj = m_JavaGoogleDrive.callObjectMethod("listFiles",
|
||||||
|
"(Ljava/lang/String;)[Lcom/falsinsoft/qtandroidtools/AndroidGoogleDrive$DriveFile;",
|
||||||
|
QAndroidJniObject::fromString(Query).object<jstring>()
|
||||||
|
);
|
||||||
|
if(FilesListObj.isValid())
|
||||||
|
{
|
||||||
|
const jobjectArray FilesListJObjArray = FilesListObj.object<jobjectArray>();
|
||||||
|
const int FilesNum = JniEnv->GetArrayLength(FilesListJObjArray);
|
||||||
|
|
||||||
|
for(int i = 0; i < FilesNum; i++)
|
||||||
|
{
|
||||||
|
const QAndroidJniObject JniObject = JniEnv->GetObjectArrayElement(FilesListJObjArray, i);
|
||||||
|
QAndroidJniObject ParentsListObj;
|
||||||
|
QVariantList FileParents;
|
||||||
|
QVariantMap FileInfo;
|
||||||
|
|
||||||
|
FileInfo["id"] = JniObject.getObjectField<jstring>("id").toString();
|
||||||
|
FileInfo["name"] = JniObject.getObjectField<jstring>("name").toString();
|
||||||
|
FileInfo["mimeType"] = JniObject.getObjectField<jstring>("mimeType").toString();
|
||||||
|
ParentsListObj = JniObject.getObjectField("parents", "[Ljava/lang/String;");
|
||||||
|
if(ParentsListObj.isValid())
|
||||||
|
{
|
||||||
|
const jobjectArray ParentsListJObjArray = ParentsListObj.object<jobjectArray>();
|
||||||
|
const int ParentsNum = JniEnv->GetArrayLength(ParentsListJObjArray);
|
||||||
|
|
||||||
|
for(int p = 0; p < ParentsNum; p++)
|
||||||
|
{
|
||||||
|
FileParents << QAndroidJniObject(JniEnv->GetObjectArrayElement(ParentsListJObjArray, p)).toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
FileInfo["parents"] = FileParents;
|
||||||
|
|
||||||
|
FilesList << FileInfo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return FilesList;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString QAndroidGoogleDrive::getRootId()
|
||||||
|
{
|
||||||
|
QString RootId;
|
||||||
|
|
||||||
|
if(m_JavaGoogleDrive.isValid())
|
||||||
|
{
|
||||||
|
const QAndroidJniObject RootIdObj = m_JavaGoogleDrive.callObjectMethod("getRootId",
|
||||||
|
"()Ljava/lang/String;"
|
||||||
|
);
|
||||||
|
if(RootIdObj.isValid())
|
||||||
|
{
|
||||||
|
RootId = RootIdObj.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return RootId;
|
||||||
|
}
|
||||||
|
|
||||||
void QAndroidGoogleDrive::LoadScopeDefinitions()
|
void QAndroidGoogleDrive::LoadScopeDefinitions()
|
||||||
{
|
{
|
||||||
const char ScopesClass[] = "com/google/api/services/drive/DriveScopes";
|
const char ScopesClass[] = "com/google/api/services/drive/DriveScopes";
|
||||||
|
@ -36,6 +36,7 @@ class QAndroidGoogleDrive : public QObject
|
|||||||
Q_PROPERTY(QString SCOPE_DRIVE_PHOTOS_READONLY MEMBER SCOPE_DRIVE_PHOTOS_READONLY CONSTANT)
|
Q_PROPERTY(QString SCOPE_DRIVE_PHOTOS_READONLY MEMBER SCOPE_DRIVE_PHOTOS_READONLY CONSTANT)
|
||||||
Q_PROPERTY(QString SCOPE_DRIVE_READONLY MEMBER SCOPE_DRIVE_READONLY CONSTANT)
|
Q_PROPERTY(QString SCOPE_DRIVE_READONLY MEMBER SCOPE_DRIVE_READONLY CONSTANT)
|
||||||
Q_PROPERTY(QString SCOPE_DRIVE_SCRIPTS MEMBER SCOPE_DRIVE_SCRIPTS CONSTANT)
|
Q_PROPERTY(QString SCOPE_DRIVE_SCRIPTS MEMBER SCOPE_DRIVE_SCRIPTS CONSTANT)
|
||||||
|
Q_PROPERTY(bool isAuthenticated READ isAuthenticated NOTIFY isAuthenticatedChanged)
|
||||||
Q_DISABLE_COPY(QAndroidGoogleDrive)
|
Q_DISABLE_COPY(QAndroidGoogleDrive)
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
@ -54,9 +55,19 @@ public:
|
|||||||
static QObject* qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine);
|
static QObject* qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine);
|
||||||
static QAndroidGoogleDrive* instance();
|
static QAndroidGoogleDrive* instance();
|
||||||
|
|
||||||
|
Q_INVOKABLE bool authenticate(const QString &AppName, const QString &ScopeName);
|
||||||
|
Q_INVOKABLE QVariantList getFilesList(const QString &Query = QString());
|
||||||
|
Q_INVOKABLE QString getRootId();
|
||||||
|
|
||||||
|
bool isAuthenticated() { return m_isAuthenticated; }
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void isAuthenticatedChanged();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const QAndroidJniObject m_JavaGoogleDrive;
|
const QAndroidJniObject m_JavaGoogleDrive;
|
||||||
static QAndroidGoogleDrive *m_pInstance;
|
static QAndroidGoogleDrive *m_pInstance;
|
||||||
|
bool m_isAuthenticated = false;
|
||||||
QString m_ScopeList[8];
|
QString m_ScopeList[8];
|
||||||
|
|
||||||
void LoadScopeDefinitions();
|
void LoadScopeDefinitions();
|
||||||
|
@ -53,6 +53,7 @@ import java.net.URL;
|
|||||||
|
|
||||||
public class AndroidGoogleAccount
|
public class AndroidGoogleAccount
|
||||||
{
|
{
|
||||||
|
private static final String TAG = "AndroidGoogleAccount";
|
||||||
private final Activity mActivityInstance;
|
private final Activity mActivityInstance;
|
||||||
private GoogleSignInClient mGoogleSignInClient = null;
|
private GoogleSignInClient mGoogleSignInClient = null;
|
||||||
|
|
||||||
@ -248,10 +249,10 @@ public class AndroidGoogleAccount
|
|||||||
switch(e.getStatusCode())
|
switch(e.getStatusCode())
|
||||||
{
|
{
|
||||||
case GoogleSignInStatusCodes.DEVELOPER_ERROR:
|
case GoogleSignInStatusCodes.DEVELOPER_ERROR:
|
||||||
Log.d("AndroidGoogleAccount", "DEVELOPER_ERROR -> Have you signed your project on Android console?");
|
Log.d(TAG, "DEVELOPER_ERROR -> Have you signed your project on Android console?");
|
||||||
break;
|
break;
|
||||||
case GoogleSignInStatusCodes.SIGN_IN_REQUIRED:
|
case GoogleSignInStatusCodes.SIGN_IN_REQUIRED:
|
||||||
Log.d("AndroidGoogleAccount", "SIGN_IN_REQUIRED -> You have to signin by select account before use this call");
|
Log.d(TAG, "SIGN_IN_REQUIRED -> You have to signin by select account before use this call");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
signInSuccessfully = false;
|
signInSuccessfully = false;
|
||||||
|
@ -40,16 +40,21 @@ import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
|
|||||||
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
|
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
|
||||||
import com.google.android.gms.common.api.Scope;
|
import com.google.android.gms.common.api.Scope;
|
||||||
import com.google.api.client.extensions.android.http.AndroidHttp;
|
import com.google.api.client.extensions.android.http.AndroidHttp;
|
||||||
|
import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException;
|
||||||
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
|
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
|
||||||
import com.google.api.client.json.gson.GsonFactory;
|
import com.google.api.client.json.gson.GsonFactory;
|
||||||
import com.google.api.services.drive.Drive;
|
import com.google.api.services.drive.Drive;
|
||||||
import com.google.api.services.drive.DriveScopes;
|
import com.google.api.services.drive.DriveScopes;
|
||||||
import com.google.api.services.drive.model.File;
|
import com.google.api.services.drive.model.File;
|
||||||
|
import com.google.api.services.drive.model.FileList;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
public class AndroidGoogleDrive
|
public class AndroidGoogleDrive
|
||||||
{
|
{
|
||||||
|
private static final String TAG = "AndroidGoogleDrive";
|
||||||
private final Activity mActivityInstance;
|
private final Activity mActivityInstance;
|
||||||
private Drive mDriveService = null;
|
private Drive mDriveService = null;
|
||||||
|
|
||||||
@ -77,6 +82,96 @@ public class AndroidGoogleDrive
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Log.d(TAG, "You have to signin by select account before use this call!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isAuthenticated()
|
||||||
|
{
|
||||||
|
return (mDriveService != null) ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DriveFile[] listFiles(String Query)
|
||||||
|
{
|
||||||
|
if(mDriveService != null)
|
||||||
|
{
|
||||||
|
DriveFile[] DriveFileList;
|
||||||
|
File[] FileList;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
FileList = mDriveService.files()
|
||||||
|
.list()
|
||||||
|
.setQ(Query)
|
||||||
|
.setSpaces("drive")
|
||||||
|
.setFields("files(id, name, mimeType, parents)")
|
||||||
|
.execute()
|
||||||
|
.getFiles()
|
||||||
|
.toArray(new File[0]);
|
||||||
|
}
|
||||||
|
catch(UserRecoverableAuthIOException e)
|
||||||
|
{
|
||||||
|
Log.d(TAG, "Authorization scope not requested at signin!");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch(IOException e)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
DriveFileList = new DriveFile[FileList.length];
|
||||||
|
for(int i = 0; i < FileList.length; i++)
|
||||||
|
{
|
||||||
|
DriveFile FileData = new DriveFile();
|
||||||
|
final File FileInfo = FileList[i];
|
||||||
|
|
||||||
|
FileData.id = FileInfo.getId();
|
||||||
|
FileData.name = FileInfo.getName();
|
||||||
|
FileData.mimeType = FileInfo.getMimeType();
|
||||||
|
FileData.parents = (FileInfo.getParents() != null) ? FileInfo.getParents().toArray(new String[0]) : null;
|
||||||
|
|
||||||
|
DriveFileList[i] = FileData;
|
||||||
|
}
|
||||||
|
|
||||||
|
return DriveFileList;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRootId()
|
||||||
|
{
|
||||||
|
if(mDriveService != null)
|
||||||
|
{
|
||||||
|
File FileInfo;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
FileInfo = mDriveService.files()
|
||||||
|
.get("root")
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
catch(UserRecoverableAuthIOException e)
|
||||||
|
{
|
||||||
|
Log.d(TAG, "Authorization scope not requested at signin!");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch(IOException e)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return FileInfo.getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class DriveFile
|
||||||
|
{
|
||||||
|
public String id;
|
||||||
|
public String name;
|
||||||
|
public String mimeType;
|
||||||
|
public String[] parents;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,4 +91,5 @@
|
|||||||
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
|
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
|
||||||
<uses-permission android:name="android.permission.READ_CONTACTS"/>
|
<uses-permission android:name="android.permission.READ_CONTACTS"/>
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
|
||||||
</manifest>
|
</manifest>
|
||||||
|
@ -65,12 +65,12 @@ Page {
|
|||||||
Button {
|
Button {
|
||||||
anchors.horizontalCenter: parent.horizontalCenter
|
anchors.horizontalCenter: parent.horizontalCenter
|
||||||
text: "sigIn"
|
text: "sigIn"
|
||||||
onClicked: QtAndroidGoogleAccount.signIn()
|
onClicked: QtAndroidGoogleAccount.signIn(QtAndroidGoogleDrive.SCOPE_DRIVE)
|
||||||
}
|
}
|
||||||
Button {
|
Button {
|
||||||
anchors.horizontalCenter: parent.horizontalCenter
|
anchors.horizontalCenter: parent.horizontalCenter
|
||||||
text: "sigIn select account"
|
text: "sigIn select account"
|
||||||
onClicked: QtAndroidGoogleAccount.signInSelectAccount()
|
onClicked: QtAndroidGoogleAccount.signInSelectAccount(QtAndroidGoogleDrive.SCOPE_DRIVE)
|
||||||
}
|
}
|
||||||
Button {
|
Button {
|
||||||
anchors.horizontalCenter: parent.horizontalCenter
|
anchors.horizontalCenter: parent.horizontalCenter
|
||||||
|
@ -8,8 +8,82 @@ Page {
|
|||||||
id: page
|
id: page
|
||||||
padding: 0
|
padding: 0
|
||||||
|
|
||||||
|
Column {
|
||||||
|
width: parent.width * 0.9
|
||||||
|
height: parent.height * 0.9
|
||||||
|
anchors.centerIn: parent
|
||||||
|
spacing: 5
|
||||||
|
|
||||||
Button {
|
Button {
|
||||||
anchors.horizontalCenter: parent.horizontalCenter
|
anchors.horizontalCenter: parent.horizontalCenter
|
||||||
text: "test"
|
text: "Authenticate"
|
||||||
|
onClicked: QtAndroidGoogleDrive.authenticate("QtAndroidTools", QtAndroidGoogleDrive.SCOPE_DRIVE)
|
||||||
|
}
|
||||||
|
|
||||||
|
Button {
|
||||||
|
anchors.horizontalCenter: parent.horizontalCenter
|
||||||
|
enabled: QtAndroidGoogleDrive.isAuthenticated
|
||||||
|
text: "Get files list"
|
||||||
|
onClicked: {
|
||||||
|
var filesList = QtAndroidGoogleDrive.getFilesList();
|
||||||
|
var rootId = QtAndroidGoogleDrive.getRootId();
|
||||||
|
|
||||||
|
filesListModel.clear();
|
||||||
|
for(var i = 0; i < filesList.length; i++)
|
||||||
|
{
|
||||||
|
var data = filesList[i];
|
||||||
|
var parentId = "null";
|
||||||
|
|
||||||
|
if(data.parents.length > 0)
|
||||||
|
{
|
||||||
|
if(data.parents[0] === rootId)
|
||||||
|
parentId = "root";
|
||||||
|
else
|
||||||
|
parentId = data.parents[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
filesListModel.append({ "id": data.id,
|
||||||
|
"name": data.name,
|
||||||
|
"mimeType": data.mimeType,
|
||||||
|
"parentId": parentId
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Rectangle {
|
||||||
|
width: parent.width
|
||||||
|
height: parent.height * 0.4
|
||||||
|
border.width: 1
|
||||||
|
border.color: "black"
|
||||||
|
clip: true
|
||||||
|
|
||||||
|
ListModel { id: filesListModel }
|
||||||
|
|
||||||
|
ListView {
|
||||||
|
id: filesListView
|
||||||
|
width: parent.width * 0.95
|
||||||
|
height: parent.height
|
||||||
|
anchors.centerIn: parent
|
||||||
|
model: filesListModel
|
||||||
|
boundsBehavior: Flickable.StopAtBounds
|
||||||
|
delegate: Item {
|
||||||
|
width: filesListView.width
|
||||||
|
height: fileInfoColumn.implicitHeight + 10
|
||||||
|
Column {
|
||||||
|
id: fileInfoColumn
|
||||||
|
Text { font.pixelSize: 13; text: '<b>Id:</b> ' + id }
|
||||||
|
Text { font.pixelSize: 13; text: '<b>Name:</b> ' + name }
|
||||||
|
Text { font.pixelSize: 13; text: '<b>MimeType:</b> ' + mimeType }
|
||||||
|
Text { font.pixelSize: 13; text: '<b>ParentId:</b> ' + parentId }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ScrollBar.vertical: ScrollBar {
|
||||||
|
width: 10
|
||||||
|
anchors.left: parent.right
|
||||||
|
policy: ScrollBar.AlwaysOn
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user