Skip to content

Commit 1c5ea95

Browse files
rburchellRobin Burchell
authored andcommitted
Implement more full resource policy features.
* Handle released by manager properly. Signal resourcesReleasedByManager is different from signal handleResourcesLost and needs to be handled separately. Signal handleResourcesLost means some other client has acquired the resources, thus the resources are lost from us, but if the other client frees the resources, we will automatically get the resources back. With resourcesReleasedByManager we lose the resources, and resource manager releases them as well (same as if client would call release()), so only way to re-acquire resources in latter case is calling acquire() again. This distinction is useful for example in cases where user is listening to music with headphones connected and removes the headphones, then the music player shouldn't continue playback until user requests so. But if user is listening to music when phone call is received, it is convenient to resume the playback automatically after the call. * Implement availability changed. Availability changed is information whether resources the client is interested in are available (no higher priority resource classes have acquired the resources). * Implement missing resources released. Emit resourcesReleased signal when receiving corresponding signal from libresource-qt. * Add mechanism to change used resource class. Check for environment for special variable, and if the variable has proper data, use that as the resource class. Can be set with for example qputenv("NEMO_RESOURCE_CLASS_OVERRIDE", "game"); Change-Id: I8e92f40cc5c01b6b94f54c3fa0f7a07775e87df0 Done-with: Juho Hämäläinen <juho.hamalainen@tieto.com> Reviewed-by: Andrew den Exter <andrew.den.exter@qinetic.com.au>
1 parent 0ed18d8 commit 1c5ea95

File tree

3 files changed

+123
-18
lines changed

3 files changed

+123
-18
lines changed

src/plugins/resourcepolicy/resourcepolicyimpl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ ResourcePolicyImpl::ResourcePolicyImpl(QObject *parent)
6161
ResourcePolicyImpl::~ResourcePolicyImpl()
6262
{
6363
ResourcePolicyInt *set = globalResourcePolicyInt;
64-
set->removeClient(this);
64+
if (!globalResourcePolicyInt.isDestroyed())
65+
set->removeClient(this);
6566
}
6667

6768
bool ResourcePolicyImpl::isVideoEnabled() const

src/plugins/resourcepolicy/resourcepolicyint.cpp

Lines changed: 113 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
#include "resourcepolicyimpl.h"
5050

5151
#include <QMap>
52+
#include <QByteArray>
53+
#include <QString>
5254

5355
static int clientid = 0;
5456

@@ -57,26 +59,51 @@ ResourcePolicyInt::ResourcePolicyInt(QObject *parent)
5759
, m_acquired(0)
5860
, m_status(Initial)
5961
, m_video(0)
62+
, m_available(false)
6063
, m_resourceSet(0)
6164
{
62-
m_resourceSet = new ResourcePolicy::ResourceSet("player", this);
63-
m_resourceSet->setAlwaysReply();
65+
const char *resourceClass = "player";
66+
67+
QByteArray envVar = qgetenv("NEMO_RESOURCE_CLASS_OVERRIDE");
68+
if (!envVar.isEmpty()) {
69+
QString data(envVar);
70+
// Only allow few resource classes
71+
if (data == "navigator" ||
72+
data == "call" ||
73+
data == "camera" ||
74+
data == "game" ||
75+
data == "player" ||
76+
data == "event")
77+
resourceClass = envVar.constData();
78+
}
6479

65-
ResourcePolicy::AudioResource *audioResource = new ResourcePolicy::AudioResource("player");
66-
audioResource->setProcessID(QCoreApplication::applicationPid());
67-
audioResource->setStreamTag("media.name", "*");
68-
m_resourceSet->addResourceObject(audioResource);
80+
#ifdef RESOURCE_DEBUG
81+
qDebug() << "##### Using resource class " << resourceClass;
82+
#endif
6983

70-
m_resourceSet->update();
84+
m_resourceSet = new ResourcePolicy::ResourceSet(resourceClass, this);
85+
m_resourceSet->setAlwaysReply();
7186

7287
connect(m_resourceSet, SIGNAL(resourcesGranted(QList<ResourcePolicy::ResourceType>)),
7388
this, SLOT(handleResourcesGranted()));
7489
connect(m_resourceSet, SIGNAL(resourcesDenied()),
7590
this, SLOT(handleResourcesDenied()));
91+
connect(m_resourceSet, SIGNAL(resourcesReleased()),
92+
this, SLOT(handleResourcesReleased()));
7693
connect(m_resourceSet, SIGNAL(lostResources()),
7794
this, SLOT(handleResourcesLost()));
7895
connect(m_resourceSet, SIGNAL(resourcesReleasedByManager()),
79-
this, SLOT(handleResourcesLost()));
96+
this, SLOT(handleResourcesReleasedByManager()));
97+
98+
connect(m_resourceSet, SIGNAL(resourcesBecameAvailable(const QList<ResourcePolicy::ResourceType>)),
99+
this, SLOT(handleResourcesBecameAvailable(const QList<ResourcePolicy::ResourceType>)));
100+
101+
ResourcePolicy::AudioResource *audioResource = new ResourcePolicy::AudioResource(resourceClass);
102+
103+
audioResource->setProcessID(QCoreApplication::applicationPid());
104+
audioResource->setStreamTag("media.name", "*");
105+
m_resourceSet->addResourceObject(audioResource);
106+
m_resourceSet->update();
80107
}
81108

82109
ResourcePolicyInt::~ResourcePolicyInt()
@@ -113,7 +140,7 @@ void ResourcePolicyInt::removeClient(ResourcePolicyImpl *client)
113140
m_clients.erase(i);
114141
}
115142

116-
if (m_acquired == 0) {
143+
if (m_acquired == 0 && m_status != Initial) {
117144
#ifdef RESOURCE_DEBUG
118145
qDebug() << "##### Remove client, acquired = 0, release";
119146
#endif
@@ -221,10 +248,11 @@ void ResourcePolicyInt::release(const ResourcePolicyImpl *client)
221248
#ifdef RESOURCE_DEBUG
222249
qDebug() << "##### " << i.value().id << ": RELEASE, acquired (" << m_acquired << ")";
223250
#endif
251+
emit i.value().client->resourcesReleased();
224252
}
225253
}
226254

227-
if (m_acquired == 0) {
255+
if (m_acquired == 0 && m_status != Initial) {
228256
#ifdef RESOURCE_DEBUG
229257
qDebug() << "##### " << i.value().id << ": RELEASE call resourceSet->release()";
230258
#endif
@@ -248,9 +276,10 @@ bool ResourcePolicyInt::isGranted(const ResourcePolicyImpl *client) const
248276

249277
bool ResourcePolicyInt::isAvailable() const
250278
{
251-
// TODO: is this used? what is it for?
252-
qWarning() << Q_FUNC_INFO << "Stub";
253-
return true;
279+
#ifdef RESOURCE_DEBUG
280+
qDebug() << "##### isAvailable " << m_available;
281+
#endif
282+
return m_available;
254283
}
255284

256285
void ResourcePolicyInt::handleResourcesGranted()
@@ -288,24 +317,91 @@ void ResourcePolicyInt::handleResourcesDenied()
288317
}
289318
}
290319

320+
void ResourcePolicyInt::handleResourcesReleased()
321+
{
322+
m_status = Initial;
323+
m_acquired = 0;
324+
QMap<const ResourcePolicyImpl*, clientEntry>::iterator i = m_clients.begin();
325+
while (i != m_clients.end()) {
326+
if (i.value().status == GrantedResource) {
327+
#ifdef RESOURCE_DEBUG
328+
qDebug() << "##### " << i.value().id << ": HANDLE RELEASED, acquired (" << m_acquired << ") emitting resourcesReleased()";
329+
#endif
330+
i.value().status = Initial;
331+
emit i.value().client->resourcesReleased();
332+
}
333+
++i;
334+
}
335+
}
336+
291337
void ResourcePolicyInt::handleResourcesLost()
292338
{
293-
if (m_status != Initial) {
294-
m_status = Initial;
339+
// If resources were granted switch to acquiring state,
340+
// so that if the resources are freed elsewhere we
341+
// will acquire them again properly.
342+
if (m_status == GrantedResource)
343+
m_status = RequestedResource;
344+
345+
m_acquired = 0;
346+
347+
QMap<const ResourcePolicyImpl*, clientEntry>::iterator i = m_clients.begin();
348+
while (i != m_clients.end()) {
349+
if (i.value().status == GrantedResource) {
350+
#ifdef RESOURCE_DEBUG
351+
qDebug() << "##### " << i.value().id << ": HANDLE LOST, acquired (" << m_acquired << ") emitting resourcesLost()";
352+
#endif
353+
i.value().status = RequestedResource;
354+
emit i.value().client->resourcesLost();
355+
}
356+
++i;
295357
}
358+
}
359+
360+
void ResourcePolicyInt::handleResourcesReleasedByManager()
361+
{
362+
if (m_status != Initial)
363+
m_status = Initial;
296364

297365
m_acquired = 0;
298-
m_resourceSet->release();
299366

300367
QMap<const ResourcePolicyImpl*, clientEntry>::iterator i = m_clients.begin();
301368
while (i != m_clients.end()) {
302369
if (i.value().status != Initial) {
303370
#ifdef RESOURCE_DEBUG
304-
qDebug() << "##### " << i.value().id << ": HANDLE LOST, acquired (" << m_acquired << ") emitting resourcesLost()";
371+
qDebug() << "##### " << i.value().id << ": HANDLE RELEASEDBYMANAGER, acquired (" << m_acquired << ") emitting resourcesLost()";
305372
#endif
306373
i.value().status = Initial;
307374
emit i.value().client->resourcesLost();
308375
}
309376
++i;
310377
}
311378
}
379+
380+
void ResourcePolicyInt::handleResourcesBecameAvailable(const QList<ResourcePolicy::ResourceType> &resources)
381+
{
382+
bool available = false;
383+
384+
for (int i = 0; i < resources.size(); ++i) {
385+
if (resources.at(i) == ResourcePolicy::AudioPlaybackType)
386+
available = true;
387+
}
388+
389+
availabilityChanged(available);
390+
}
391+
392+
void ResourcePolicyInt::availabilityChanged(bool available)
393+
{
394+
if (available == m_available)
395+
return;
396+
397+
m_available = available;
398+
399+
QMap<const ResourcePolicyImpl*, clientEntry>::const_iterator i = m_clients.constBegin();
400+
while (i != m_clients.constEnd()) {
401+
#ifdef RESOURCE_DEBUG
402+
qDebug() << "##### " << i.value().id << ": emitting availabilityChanged(" << m_available << ")";
403+
#endif
404+
emit i.value().client->availabilityChanged(m_available);
405+
++i;
406+
}
407+
}

src/plugins/resourcepolicy/resourcepolicyint.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
#include <QObject>
4747
#include <QMap>
4848

49+
#include <policy/resource-set.h>
50+
#include <policy/resource.h>
4951
#include <private/qmediaresourceset_p.h>
5052
#include "resourcepolicyimpl.h"
5153

@@ -86,14 +88,20 @@ class ResourcePolicyInt : public QObject
8688
private slots:
8789
void handleResourcesGranted();
8890
void handleResourcesDenied();
91+
void handleResourcesReleased();
8992
void handleResourcesLost();
93+
void handleResourcesReleasedByManager();
94+
void handleResourcesBecameAvailable(const QList<ResourcePolicy::ResourceType> &resources);
9095

9196
private:
97+
void availabilityChanged(bool available);
98+
9299
QMap<const ResourcePolicyImpl*, clientEntry> m_clients;
93100

94101
int m_acquired;
95102
ResourceStatus m_status;
96103
int m_video;
104+
bool m_available;
97105
ResourcePolicy::ResourceSet *m_resourceSet;
98106
};
99107

0 commit comments

Comments
 (0)