Commit f7956338 authored by Sergey Galin's avatar Sergey Galin

Add QAndroidPartialWakeLocker

parent 1c9c8d71
/*
Offscreen Android Views library for Qt
Authors:
Vyacheslav O. Koscheev <vok1980@gmail.com>
Sergey A. Galin <sergey.galin@gmail.com>
Distrbuted under The BSD License
Copyright (c) 2017, DoubleGIS, LLC.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the DoubleGIS, LLC nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "QAndroidPartialWakeLocker.h"
#include <QAndroidQPAPluginGap.h>
#include <TJniObjectLinker.h>
static const char * const c_full_class_name_ = "ru/dublgis/androidhelpers/WakeLocker";
static const JNINativeMethod methods[] =
{
{"getContext", "()Landroid/content/Context;", (void*)QAndroidQPAPluginGap::getCurrentContext},
};
JNI_LINKER_IMPL(QAndroidPartialWakeLocker, c_full_class_name_, methods)
QAndroidPartialWakeLocker & QAndroidPartialWakeLocker::instance()
{
static QAndroidPartialWakeLocker obj;
return obj;
}
QAndroidPartialWakeLocker::QAndroidPartialWakeLocker()
: QLocks::QLockedObject(true)
, jniLinker_(new JniObjectLinker(this))
{
if (isJniReady())
{
// PowerManager.PARTIAL_WAKE_LOCK
jni()->callVoid("setMode", static_cast<jint>(0x00000001));
}
}
QAndroidPartialWakeLocker::~QAndroidPartialWakeLocker()
{
}
void QAndroidPartialWakeLocker::lock()
{
if (isJniReady())
{
jni()->callBool("Lock");
}
Q_ASSERT(isLocked());
}
void QAndroidPartialWakeLocker::unlock()
{
if (isJniReady())
{
jni()->callVoid("Unlock");
}
}
bool QAndroidPartialWakeLocker::isLocked()
{
if (isJniReady())
{
return jni()->callBool("IsLocked");
}
return false;
}
/*
Offscreen Android Views library for Qt
Authors:
Vyacheslav O. Koscheev <vok1980@gmail.com>
Sergey A. Galin <sergey.galin@gmail.com>
Distrbuted under The BSD License
Copyright (c) 2017, DoubleGIS, LLC.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the DoubleGIS, LLC nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <QJniHelpers.h>
#include "QLocks/QLockedObject.h"
#include <IJniObjectLinker.h>
/*!
* Class for keeping CPU running.
* See: https://developer.android.com/reference/android/os/PowerManager.html#PARTIAL_WAKE_LOCK
* To lock it call method getLock() and keep returned value while you need the lock.
*/
class QAndroidPartialWakeLocker: public QLocks::QLockedObject
{
Q_DISABLE_COPY(QAndroidPartialWakeLocker)
JNI_LINKER_DECL(QAndroidPartialWakeLocker)
public:
static QAndroidPartialWakeLocker & instance();
private:
QAndroidPartialWakeLocker();
virtual ~QAndroidPartialWakeLocker();
public:
bool isLocked();
private:
virtual void lock();
virtual void unlock();
};
/*
Offscreen Android Views library for Qt
Author:
Authors:
Vyacheslav O. Koscheev <vok1980@gmail.com>
Sergey A. Galin <sergey.galin@gmail.com>
Distrbuted under The BSD License
Copyright (c) 2015, DoubleGIS, LLC.
Copyright (c) 2017, DoubleGIS, LLC.
All rights reserved.
Redistribution and use in source and binary forms, with or without
......@@ -40,7 +41,7 @@
#include <TJniObjectLinker.h>
static const char * const c_full_class_name_ = "ru/dublgis/androidhelpers/ScreenLocker";
static const char * const c_full_class_name_ = "ru/dublgis/androidhelpers/WakeLocker";
static const JNINativeMethod methods[] =
{
......@@ -62,6 +63,11 @@ QAndroidScreenLocker::QAndroidScreenLocker()
: QLocks::QLockedObject(true)
, jniLinker_(new JniObjectLinker(this))
{
if (isJniReady())
{
// PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE
jni()->callVoid("setMode", static_cast<jint>(0x0000000a | 0x20000000));
}
}
......
/*
Offscreen Android Views library for Qt
Author:
Authors:
Vyacheslav O. Koscheev <vok1980@gmail.com>
Sergey A. Galin <sergey.galin@gmail.com>
Distrbuted under The BSD License
Copyright (c) 2015, DoubleGIS, LLC.
Copyright (c) 2017, DoubleGIS, LLC.
All rights reserved.
Redistribution and use in source and binary forms, with or without
......@@ -42,8 +43,8 @@
/*!
* Class for locking WiFi manager.
* To lock it call method getLock() and keep returned value until you need this lock.
* Class for disabling automatic dimming of the device screen.
* To lock it call method getLock() and keep returned value while you need the lock.
*/
class QAndroidScreenLocker: public QLocks::QLockedObject
{
......@@ -51,7 +52,7 @@ class QAndroidScreenLocker: public QLocks::QLockedObject
JNI_LINKER_DECL(QAndroidScreenLocker)
public:
static QAndroidScreenLocker& instance();
static QAndroidScreenLocker & instance();
private:
QAndroidScreenLocker();
......
......@@ -28,10 +28,12 @@ HEADERS += \
$$PWD/QAndroidDesktopUtils.h \
$$PWD/QAndroidStorages_.h \
$$PWD/QAndroidStorages_p.h
$$PWD/QAndroidPartialWakeLocker.h
SOURCES += \
$$PWD/QAndroidSharedPreferences.cpp \
$$PWD/QAndroidScreenLocker.cpp \
$$PWD/QAndroidPartialWakeLocker.cpp \
$$PWD/QAndroidWiFiLocker.cpp \
$$PWD/Mobility/QAndroidCellDataProvider.cpp \
$$PWD/Mobility/QAndroidWifiDataProvider.cpp \
......
......@@ -3,10 +3,11 @@
Author:
Vyacheslav O. Koscheev <vok1980@gmail.com>
Sergey A.Galin <sergey.galin@gmail.com>
Distrbuted under The BSD License
Copyright (c) 2015, DoubleGIS, LLC.
Copyright (c) 2017, DoubleGIS, LLC.
All rights reserved.
Redistribution and use in source and binary forms, with or without
......@@ -33,27 +34,36 @@
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/
package ru.dublgis.androidhelpers;
import android.content.Context;
import android.os.PowerManager;
class ScreenLocker
class WakeLocker
{
public static final String TAG = "Grym/ScreenLocker";
public static final String TAG = "Grym/WakeLocker";
private volatile long native_ptr_ = 0;
private PowerManager.WakeLock mLock = null;
private int mMode = PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE;
public ScreenLocker(long native_ptr)
public WakeLocker(final long native_ptr)
{
Log.i(TAG, "ScreenLocker constructor");
Log.i(TAG, "WakeLocker constructor");
native_ptr_ = native_ptr;
}
// From C++
// This function should be called only once before lock/unlock.
public void setMode(final int lockMode)
{
mMode = lockMode;
}
//! Called from C++ to notify us that the associated C++ object is being destroyed.
public void cppDestroyed()
{
......@@ -63,29 +73,25 @@ class ScreenLocker
private void createLock()
{
Log.i(TAG, "createLock");
try
Log.i(TAG, "createLock " + mMode);
try
{
PowerManager powerManager = (PowerManager)getContext().getSystemService(Context.POWER_SERVICE);
final PowerManager powerManager = (PowerManager)getContext().getSystemService(Context.POWER_SERVICE);
if (powerManager != null)
{
mLock = powerManager.newWakeLock(
PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, TAG);
mLock = powerManager.newWakeLock(mMode, TAG);
}
else
{
Log.w(TAG, "PowerManager is null! No control over backlight or suspend.");
}
}
catch (Exception e)
catch (final Throwable e)
{
Log.e(TAG, "Exception in PowerController (power manager init): " + e);
}
}
public boolean Lock()
{
if (null == mLock)
......@@ -95,7 +101,7 @@ class ScreenLocker
if (null == mLock)
{
Log.e(TAG, "no mLock");
Log.e(TAG, "Failed to create the lock!");
return false;
}
......@@ -106,9 +112,9 @@ class ScreenLocker
mLock.acquire();
}
}
catch (Exception e)
catch (final Throwable e)
{
Log.e(TAG, "Failed to acquire lock: " + e);
Log.e(TAG, "Failed to acquire lock for mode " + mMode + ": " + e);
}
return IsLocked();
......@@ -118,31 +124,31 @@ class ScreenLocker
public boolean IsLocked()
{
boolean ret = mLock != null && mLock.isHeld();
Log.i(TAG, "IsLocked = " + ret);
Log.i(TAG, "IsLocked: " + ret + " (mode: " + mMode + ")");
return ret;
}
public void Unlock()
{
Log.i(TAG, "Unlock");
Log.i(TAG, "Unlock " + mMode);
if (null == mLock)
{
Log.e(TAG, "no mLock");
Log.e(TAG, "Trying to unlock when not locked!");
return;
}
try
try
{
if (mLock.isHeld())
{
mLock.release();
}
}
catch (Exception e)
catch (final Throwable e)
{
Log.e(TAG, "Failed to release lock: " + e);
Log.e(TAG, "Failed to release lock for mode " + mMode + ": " + e);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment