QtDataSync  4.2.0
A simple offline-first synchronisation framework, to synchronize data of Qt applications between devices
Public Slots | Signals | Public Member Functions | Static Public Attributes | Protected Member Functions | Properties | List of all members
QtDataSync::AndroidBackgroundService Class Referenceabstract

An extension of QtService to create a synchronization service for android. More...

#include <androidbackgroundservice.h>

+ Inheritance diagram for QtDataSync::AndroidBackgroundService:

Public Slots

void setWaitFullSync (bool waitFullSync)
 WRITE accessor for AndroidBackgroundService::waitFullSync.
 
- Public Slots inherited from QtService::Service
void quit ()
 
void reload ()
 
void setTerminalActive (bool terminalActive)
 
void setTerminalMode (TerminalMode terminalMode)
 
void setGlobalTerminal (bool globalTerminal)
 
void setStartWithTerminal (bool startWithTerminal)
 

Signals

void waitFullSyncChanged (bool waitFullSync, QPrivateSignal)
 NOTIFY accessor for AndroidBackgroundService::waitFullSync.
 
- Signals inherited from QtService::Service
void started (bool success)
 
Q_DECL_DEPRECATED void started ()
 
void stopped (int exitCode=EXIT_SUCCESS)
 
void reloaded (bool success)
 
Q_DECL_DEPRECATED void reloaded ()
 
void paused (bool success)
 
Q_DECL_DEPRECATED void paused ()
 
void resumed (bool success)
 
Q_DECL_DEPRECATED void resumed ()
 
void terminalActiveChanged (bool terminalActive, QPrivateSignal)
 
void terminalModeChanged (TerminalMode terminalMode, QPrivateSignal)
 
void globalTerminalChanged (bool globalTerminal, QPrivateSignal)
 
void startWithTerminalChanged (bool startWithTerminal, QPrivateSignal)
 

Public Member Functions

 AndroidBackgroundService (int &argc, char **argv, int=QCoreApplication::ApplicationFlags)
 Default constructor.
 
bool waitFullSync () const
 READ accessor for AndroidBackgroundService::waitFullSync.
 
- Public Member Functions inherited from QtService::Service
 Service (int &argc, char **argv, int=QCoreApplication::ApplicationFlags)
 
int exec ()
 
Q_INVOKABLE QList< int > getSockets (const QByteArray &socketName)
 
Q_INVOKABLE int getSocket ()
 
QString backend () const
 
QDir runtimeDir () const
 
bool isTerminalActive () const
 
TerminalMode terminalMode () const
 
bool isGlobalTerminal () const
 
bool startWithTerminal () const
 
- Public Member Functions inherited from QObject
virtual const QMetaObjectmetaObject () const const
 
virtual void * qt_metacast (const char *)
 
virtual int qt_metacall (QMetaObject::Call, int, void **)
 
 QObject (QObject *parent)
 
virtual bool event (QEvent *e)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
QString objectName () const const
 
void setObjectName (const QString &name)
 
bool isWidgetType () const const
 
bool isWindowType () const const
 
bool signalsBlocked () const const
 
bool blockSignals (bool block)
 
QThreadthread () const const
 
void moveToThread (QThread *targetThread)
 
int startTimer (int interval, Qt::TimerType timerType)
 
int startTimer (std::chrono::milliseconds time, Qt::TimerType timerType)
 
void killTimer (int id)
 
findChild (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegExp &regExp, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const
 
const QObjectList & children () const const
 
void setParent (QObject *parent)
 
void installEventFilter (QObject *filterObj)
 
void removeEventFilter (QObject *obj)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const
 
bool disconnect (const char *signal, const QObject *receiver, const char *method) const const
 
bool disconnect (const QObject *receiver, const char *method) const const
 
void dumpObjectTree ()
 
void dumpObjectInfo ()
 
void dumpObjectTree () const const
 
void dumpObjectInfo () const const
 
bool setProperty (const char *name, const QVariant &value)
 
QVariant property (const char *name) const const
 
QList< QByteArraydynamicPropertyNames () const const
 
void destroyed (QObject *obj)
 
void objectNameChanged (const QString &objectName)
 
QObjectparent () const const
 
bool inherits (const char *className) const const
 
void deleteLater ()
 

Static Public Attributes

static const QString BackgroundSyncAction
 The Intent Action used to start the background synchronization.
 
static const QString RegisterSyncAction
 The Intent Action used to register the service for background synchronization.
 
static const int ForegroundNotificationId
 The ID of the Foreground notification shown by the service while synchronizing.
 

Protected Member Functions

virtual QString setupName () const
 Returns the name of the setup to be synchronized. More...
 
virtual void prepareSetup (Setup &setup)
 Prepares the setup to be synchronized. More...
 
CommandResult onStart () override
 Inherits QtService::Service::onStart.
 
CommandResult onStop (int &exitCode) override
 Inherits QtService::Service::onStop.
 
virtual void onSyncCompleted (SyncManager::SyncState state)
 Is called by the service as soon the the synchronization has been completed. More...
 
void exitAfterSync ()
 Tells the service to quit itself after a synchronization. More...
 
virtual int onStartCommand (const QAndroidIntent &intent, int flags, int startId)
 Is called on the android thread to deliver the start command. More...
 
virtual QAndroidJniObject createForegroundNotification ()=0
 Is called on the android thread to create a foreground notification. More...
 
virtual bool stopSelf (int startId)
 Is called by the service to stop itself once finished, based on the id it was started with. More...
 
- Protected Member Functions inherited from QtService::Service
virtual bool preStart ()
 
virtual CommandResult onReload ()
 
virtual CommandResult onPause ()
 
virtual CommandResult onResume ()
 
virtual QVariant onCallback (const QByteArray &kind, const QVariantList &args)
 
virtual bool verifyCommand (const QStringList &arguments)
 
void addCallback (const QByteArray &kind, const std::function< QVariant(QVariantList)> &fn)
 
void addCallback (const QByteArray &kind, const TFunction &fn)
 
void addCallback (const QByteArray &kind, TReturn(TClass::*fn)(TArgs...), std::enable_if_t< std::is_base_of< QtService::Service, TClass >::value, void * >=nullptr)
 
- Protected Member Functions inherited from QObject
QObjectsender () const const
 
int senderSignalIndex () const const
 
int receivers (const char *signal) const const
 
bool isSignalConnected (const QMetaMethod &signal) const const
 
virtual void timerEvent (QTimerEvent *event)
 
virtual void childEvent (QChildEvent *event)
 
virtual void customEvent (QEvent *event)
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 

Properties

bool waitFullSync
 Specify whether the service should wait for a full synchronization or only the download. More...
 
- Properties inherited from QtService::Service
QString backend
 
QDir runtimeDir
 
bool terminalActive
 
TerminalMode terminalMode
 
bool globalTerminal
 
bool startWithTerminal
 
- Properties inherited from QObject
 objectName
 

Additional Inherited Members

- Public Types inherited from QtService::Service
enum  CommandResult
 
enum  TerminalMode
 
- Static Public Member Functions inherited from QtService::Service
static Serviceinstance ()
 
- Static Public Member Functions inherited from QObject
QString tr (const char *sourceText, const char *disambiguation, int n)
 
QString trUtf8 (const char *sourceText, const char *disambiguation, int n)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, Functor functor)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
 
bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method)
 
bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method)
 
bool disconnect (const QMetaObject::Connection &connection)
 
bool disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method)
 
- Public Attributes inherited from QtService::Service
 OperationCompleted
 
 OperationPending
 
 OperationFailed
 
 OperationExit
 
 QT_SERVICE_POST_ENUM_DEPRECATED
 
 ReadOnly
 
 WriteOnly
 
 ReadWritePassive
 
 ReadWriteActive
 
- Protected Slots inherited from QtService::Service
virtual void terminalConnected (Terminal *terminal)
 

Detailed Description

An extension of QtService to create a synchronization service for android.

This service is part of the QtDataSyncAndroid module and can be used as Android service to create a background synchronizations service without much effort. However, a few additional setup steps are needed. This class only extends the QtService implementation of an android service to add the synchronization aspect.

Warning
This class alone is not sufficient to make synchronization possible. Have a look at the Android Background synchronization to learn how to add background synchronization to your project.
See also
AndroidSyncControl, QtService::Service, Android Backend

Definition at line 19 of file androidbackgroundservice.h.

Member Function Documentation

◆ createForegroundNotification()

QtDataSync::AndroidBackgroundService::createForegroundNotification ( )
protectedpure virtual

Is called on the android thread to create a foreground notification.

Returns
An Android Notification object, ready to be presented as foreground notification

You must implement this method to create a working service, as since Android 8, all services that want to run must show a foreground notification while running in order to not be killed by the system. A basic sample could be the following.

The code to create the notification was first written in java:

public static Notification createFgNotification(Context context) {
return new NotificationCompat.Builder(context, ForegroundChannelId)
.setContentTitle("AndroidSync Synchronization Service")
.setContentText("Synchronizing…")
.setContentInfo("AndroidSync")
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.icon))
.setSmallIcon(R.drawable.icon)
.setLocalOnly(true)
.setOngoing(true)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.build();
}

And that method is then simply called from the implementation:

QAndroidJniObject SyncService::createForegroundNotification()
{
return QAndroidJniObject::callStaticObjectMethod("de/skycoder42/qtdatasync/sample/androidsync/SvcHelper",
"createFgNotification",
"(Landroid/content/Context;)Landroid/app/Notification;",
QtAndroid::androidService().object());
}
See also
android.app.Notification

◆ exitAfterSync()

QtDataSync::AndroidBackgroundService::exitAfterSync ( )
protected

Tells the service to quit itself after a synchronization.

Call this method if you reimplement onSyncCompleted(). It will quit the service gracefully, honoring the order of start commands, if multiple have been used. Internally, this method calls stopSelf() with the most recently used startId of a BackgroundSyncAction.

See also
AndroidBackgroundService::onSyncCompleted, AndroidBackgroundService::stopSelf, AndroidBackgroundService::BackgroundSyncAction

◆ onStartCommand()

QtDataSync::AndroidBackgroundService::onStartCommand ( const QAndroidIntent &  intent,
int  flags,
int  startId 
)
protectedvirtual

Is called on the android thread to deliver the start command.

Parameters
intentThe intent that was used to start the service
flagsThe flags that were used to start the service
startIdThe id of this start request
Returns
A value allowed by android to indicate the servies state

This method is registered within QtService via QtService::Service::addCallback. Check Android Backend documentation for details on the method itself. Its bascially just a forwarding of the Android Service method.

Only implement this if you need to do custom stuff here. You should always call this base implementation in your overload, as this method internally handles the synchronization intents.

Attention
This method is called synchronously on the android thread. This means you should not interact with service class at all. Only use queued invokes to schedule things to be done on the actual main thread.
See also
Android Backend, QtService::Service::addCallback, AndroidBackgroundService::stopSelf, android.app.Service.onStartCommand

◆ onSyncCompleted()

QtDataSync::AndroidBackgroundService::onSyncCompleted ( SyncManager::SyncState  state)
protectedvirtual

Is called by the service as soon the the synchronization has been completed.

Parameters
stateThe state in which the synchronization finished

This method is called as soon as the datasync instance has finished the data synchronization. You can override it to perform additional operations with the data before quitting the service.

The default implementation only calls exitAfterSync() to quit the service gracefully. You must do the same in your implementation. You do not have to call it from within the method, but must call it eventually. Otherwise it will keep running indefinitely

Possible states can be:

  • Uploading (only if waitFullSync is set to false)
  • Synchronized
  • Error
  • Disconnected
See also
AndroidBackgroundService::waitFullSync, AndroidBackgroundService::exitAfterSync

◆ prepareSetup()

QtDataSync::AndroidBackgroundService::prepareSetup ( Setup setup)
protectedvirtual

Prepares the setup to be synchronized.

Parameters
setupThe setup to be prepared

You should override this method to configure the setup before creation (i.e. set properties on it). The default implementation does nothing.

See also
QtDataSync::Setup, AndroidBackgroundService::setupName

◆ setupName()

QtDataSync::AndroidBackgroundService::setupName ( ) const
protectedvirtual

Returns the name of the setup to be synchronized.

Returns
The name of the setup

The default implementation returns QtDataSync::DefaultSetup. You can override the method if you need the service to create the setup under a different name.

See also
QtDataSync::DefaultSetup, AndroidBackgroundService::prepareSetup

◆ stopSelf()

QtDataSync::AndroidBackgroundService::stopSelf ( int  startId)
protectedvirtual

Is called by the service to stop itself once finished, based on the id it was started with.

Parameters
startIdThe id of the start command that was finished and thus is beeing stopped
Returns
true if the service stop was accepted, false if not

Internally this method simply calls stopSelfResult on the android service. You can reimplement it if you want to customize how to quit the service.

See also
AndroidBackgroundService::exitAfterSync, AndroidBackgroundService::onStartCommand, android.app.Service.stopSelfResult](https://developer.android.com/reference/android/app/Service.html#stopSelfResult(int))

Property Documentation

◆ waitFullSync

QtDataSync::AndroidBackgroundService::waitFullSync
readwrite

Specify whether the service should wait for a full synchronization or only the download.

Default: true

If set to true, the serivce will internally call SyncManager::runOnSynchronized() with the onSyncCompleted() as handler. If disable, SyncManager::runOnDownloaded() is used instead. Check the documentation of these two methods.

Accessors
READwaitFullSync()
WRITEsetWaitFullSync()
NOTIFYwaitFullSyncChanged()
See also
AndroidBackgroundService::onSyncCompleted, SyncManager::runOnSynchronized, SyncManager::runOnDownloaded

Definition at line 24 of file androidbackgroundservice.h.


The documentation for this class was generated from the following files: