Commit 3d5ec89f authored by Vyacheslav Koscheev's avatar Vyacheslav Koscheev

WiFi locker created

parent 4bfb0e3b
/*
Offscreen Android Views library for Qt
Author:
Vyacheslav O. Koscheev <vok1980@gmail.com>
Distrbuted under The BSD License
Copyright (c) 2015, 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 "QAndroidWiFiLocker.h"
#include <QAndroidQPAPluginGap.h>
static const char * const c_full_class_name_ = "ru/dublgis/androidhelpers/WifiLocker";
QAndroidWiFiLocker& QAndroidWiFiLocker::instance()
{
static QAndroidWiFiLocker obj;
return obj;
}
QAndroidWiFiLocker::QAndroidWiFiLocker()
{
preloadJavaClasses();
// Creating Java object
java_handler_.reset(new QJniObject(c_full_class_name_, "J",
jlong(reinterpret_cast<void*>(this))));
}
QAndroidWiFiLocker::~QAndroidWiFiLocker()
{
if (java_handler_)
{
java_handler_->callVoid("cppDestroyed");
java_handler_.reset();
}
}
void QAndroidWiFiLocker::preloadJavaClasses()
{
static volatile bool preloaded_ = false;
if (!preloaded_)
{
preloaded_ = true;
QAndroidQPAPluginGap::preloadJavaClasses();
QAndroidQPAPluginGap::preloadJavaClass(c_full_class_name_);
QJniClass ov(c_full_class_name_);
static const JNINativeMethod methods[] =
{
{"getContext", "()Landroid/content/Context;", (void*)QAndroidQPAPluginGap::getCurrentContext},
};
ov.registerNativeMethods(methods, sizeof(methods));
}
}
void QAndroidWiFiLocker::lock()
{
java_handler_->callBool("Lock");
Q_ASSERT(isLocked());
}
void QAndroidWiFiLocker::unlock()
{
java_handler_->callVoid("Unlock");
}
bool QAndroidWiFiLocker::isLocked()
{
return java_handler_->callBool("IsLocked");
}
/*
Offscreen Android Views library for Qt
Author:
Vyacheslav O. Koscheev <vok1980@gmail.com>
Distrbuted under The BSD License
Copyright (c) 2015, 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"
/*!
* Class for locking WiFi manager.
* To lock it call method getLock() and keep returned value until you need this lock.
*/
class QAndroidWiFiLocker: public QLocks::QLockedObject
{
Q_DISABLE_COPY(QAndroidWiFiLocker)
public:
static QAndroidWiFiLocker& instance();
private:
QAndroidWiFiLocker();
virtual ~QAndroidWiFiLocker();
public:
bool isLocked();
static void preloadJavaClasses();
private:
virtual void lock();
virtual void unlock();
private:
QScopedPointer<QJniObject> java_handler_;
};
/*
Offscreen Android Views library for Qt
Author:
Vyacheslav O. Koscheev <vok1980@gmail.com>
Distrbuted under The BSD License
Copyright (c) 2015, 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.
*/
package ru.dublgis.androidhelpers;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.util.Log;
class WifiLocker
{
public static final String TAG = "Grym/WifiLocker";
private long native_ptr_ = 0;
private WifiManager.WifiLock mWifiLock = null;
public WifiLocker(long native_ptr)
{
Log.i(TAG, "WifiLocker constructor");
native_ptr_ = native_ptr;
}
//! Called from C++ to notify us that the associated C++ object is being destroyed.
public void cppDestroyed()
{
native_ptr_ = 0;
}
private void createLock()
{
Log.i(TAG, "createLock");
try
{
WifiManager wifiManager = (WifiManager)getContext().getSystemService(Context.WIFI_SERVICE);
if (wifiManager != null)
{
int mode = android.os.Build.VERSION.SDK_INT >= 12 ?
WifiManager.WIFI_MODE_FULL_HIGH_PERF :
WifiManager.WIFI_MODE_FULL;
mWifiLock = wifiManager.createWifiLock(mode, TAG);
}
else
{
Log.w(TAG, "WiFiManager is null! No control over WiFi connection state.");
}
}
catch (Exception e)
{
Log.e(TAG, "Exception in WifiLocker (WiFi manager init): "+e);
}
}
public boolean Lock()
{
if (null == mWifiLock)
{
createLock();
}
if (null == mWifiLock)
{
Log.e(TAG, "no mWifiLock");
return false;
}
mWifiLock.acquire();
return IsLocked();
}
public boolean IsLocked()
{
boolean ret = mWifiLock != null && mWifiLock.isHeld();
Log.i(TAG, "IsLocked = " + ret);
return ret;
}
public void Unlock()
{
Log.i(TAG, "Unlock");
mWifiLock.release();
}
public native Context getContext();
}
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