starts a go service
This commit is contained in:
parent
b287d0b27e
commit
8a666a7f37
|
@ -63,7 +63,46 @@
|
|||
</activity>
|
||||
|
||||
<!-- For adding service(s) please check: https://wiki.qt.io/AndroidServices -->
|
||||
<service
|
||||
android:name="ca.openprivacy.cwtch.ui.CwtchService"
|
||||
android:process=":cwtchService">
|
||||
<!--
|
||||
<intent-filter>
|
||||
<action android:name="ca.openprivacy.cwtch.ui.CwtchService"/>
|
||||
</intent-filter>
|
||||
-->
|
||||
|
||||
<!-- Application arguments -->
|
||||
<meta-data android:name="android.app.arguments" android:value="-service"/>
|
||||
<!-- Application arguments -->
|
||||
|
||||
<meta-data android:name="android.app.lib_name" android:value="go"/>
|
||||
<meta-data android:name="android.app.qt_sources_resource_id" android:resource="@array/qt_sources"/>
|
||||
<meta-data android:name="android.app.repository" android:value="default"/>
|
||||
<meta-data android:name="android.app.qt_libs_resource_id" android:resource="@array/qt_libs"/>
|
||||
<meta-data android:name="android.app.bundled_libs_resource_id" android:resource="@array/bundled_libs"/>
|
||||
<!-- Deploy Qt libs as part of package -->
|
||||
<meta-data android:name="android.app.bundle_local_qt_libs" android:value="1"/>
|
||||
<meta-data android:name="android.app.bundled_in_lib_resource_id" android:resource="@array/bundled_in_lib"/>
|
||||
<meta-data android:name="android.app.bundled_in_assets_resource_id" android:resource="@array/bundled_in_assets"/>
|
||||
<!-- Run with local libs -->
|
||||
<meta-data android:name="android.app.use_local_qt_libs" android:value="1"/>
|
||||
<meta-data android:name="android.app.libs_prefix" android:value="/data/local/tmp/qt/"/>
|
||||
<meta-data android:name="android.app.load_local_libs" android:value="plugins/platforms/android/libqtforandroid.so:plugins/bearer/libqandroidbearer.so:plugins/mediaservice/libqtmedia_android.so:lib/libQt5MultimediaQuick.so:lib/libQt5QuickParticles.so"/>
|
||||
<meta-data android:name="android.app.load_local_jars" android:value="jar/QtAndroid.jar:jar/QtAndroidBearer.jar:jar/QtMultimedia.jar"/>
|
||||
<meta-data android:name="android.app.static_init_classes" android:value="org.qtproject.qt5.android.multimedia.QtMultimediaUtils"/>
|
||||
<!-- Used to specify custom system library path to run with local system libs -->
|
||||
<!-- <meta-data android:name="android.app.system_libs_prefix" android:value="/system/lib/"/> -->
|
||||
<!-- Messages maps -->
|
||||
<meta-data android:value="@string/ministro_not_found_msg" android:name="android.app.ministro_not_found_msg"/>
|
||||
<meta-data android:value="@string/ministro_needed_msg" android:name="android.app.ministro_needed_msg"/>
|
||||
<meta-data android:value="@string/fatal_error_msg" android:name="android.app.fatal_error_msg"/>
|
||||
<!-- Messages maps -->
|
||||
|
||||
<!-- Background running -->
|
||||
<!-- <meta-data android:name="android.app.background_running" android:value="true"/> -->
|
||||
<!-- Background running -->
|
||||
</service>
|
||||
|
||||
|
||||
|
||||
|
@ -75,6 +114,7 @@
|
|||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
|
||||
<uses-permission-sdk-23 android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
|
||||
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>
|
||||
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
package ca.openprivcy.cwtch.ui;
|
||||
package ca.openprivacy.cwtch.ui;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.NotificationChannel;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import android.util.Log;
|
||||
import android.os.Bundle;
|
||||
import android.content.ComponentName;
|
||||
|
||||
import static android.app.Notification.DEFAULT_LIGHTS;
|
||||
import static android.app.Notification.DEFAULT_SOUND;
|
||||
|
@ -16,7 +18,7 @@ import static android.content.Context.NOTIFICATION_SERVICE;
|
|||
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
|
||||
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
|
||||
import static android.os.Build.VERSION.SDK_INT;
|
||||
import static android.app.Notification.CATEGORY_SERVICE;SS
|
||||
import static android.app.Notification.CATEGORY_SERVICE;
|
||||
|
||||
public class CwtchActivity extends org.qtproject.qt5.android.bindings.QtActivity
|
||||
{
|
||||
|
@ -37,12 +39,22 @@ public class CwtchActivity extends org.qtproject.qt5.android.bindings.QtActivity
|
|||
private static String CONTENT_NOTIFICATION_ID_NAME = "content";
|
||||
|
||||
|
||||
public CwtchActivity()
|
||||
{
|
||||
public CwtchActivity() {
|
||||
m_instance = this;
|
||||
}
|
||||
|
||||
/*Intent intent = new Intent(this, CwtchService.class);
|
||||
startService(intent);*/
|
||||
// https://github.com/bbernhard/qtandroidservices_example/blob/master/source/java/MyCustomAppActivity.java
|
||||
@Override
|
||||
public void onCreate(Bundle bundle) {
|
||||
super.onCreate(bundle);
|
||||
Log.i("CwtchActivity", "Starting service!");
|
||||
Intent serviceIntent = new Intent(this, ca.openprivacy.cwtch.ui.CwtchService.class);
|
||||
ComponentName ret = startService(serviceIntent);
|
||||
if (ret == null) {
|
||||
Log.i("CwtchActivity", "Started Service: FAILED with null");
|
||||
} else {
|
||||
Log.i("CwtchActivity", "Started Service: " + ret.flattenToString());
|
||||
}
|
||||
}
|
||||
|
||||
public static void notify(String s)
|
||||
|
|
|
@ -1,34 +0,0 @@
|
|||
package ca.openprivcy.cwtch.ui;
|
||||
|
||||
import android.app.IntentService;
|
||||
import android.content.Intent;
|
||||
import android.util.Log;
|
||||
|
||||
public class CwtchIntentService extends IntentService {
|
||||
|
||||
/**
|
||||
* A constructor is required, and must call the super <code><a href="/reference/android/app/IntentService.html#IntentService(java.lang.String)">IntentService(String)</a></code>
|
||||
* constructor with a name for the worker thread.
|
||||
*/
|
||||
public CwtchIntentService() {
|
||||
super("CwtchIntentService");
|
||||
}
|
||||
|
||||
/**
|
||||
* The IntentService calls this method from the default worker thread with
|
||||
* the intent that started the service. When this method returns, IntentService
|
||||
* stops the service, as appropriate.
|
||||
*/
|
||||
@Override
|
||||
protected void onHandleIntent(Intent intent) {
|
||||
// Normally we would do some work here, like download a file.
|
||||
// For our sample, we just sleep for 5 seconds.
|
||||
try {
|
||||
Log.i("CwtchIntentService", "onHandleIntent!!!!!!");
|
||||
} catch (Exception e) { //InterruptedException e) {
|
||||
// Restore interrupt status.
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package ca.openprivacy.cwtch.ui;
|
||||
|
||||
|
||||
import android.app.IntentService;
|
||||
import android.content.Intent;
|
||||
import android.util.Log;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import org.qtproject.qt5.android.bindings.QtService;
|
||||
|
||||
// https://github.com/bbernhard/qtandroidservices_example
|
||||
public class CwtchService extends QtService {
|
||||
|
||||
}
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
package ca.openprivcy.cwtch.ui;
|
||||
|
||||
public class Runnable implements java.lang.Runnable {
|
||||
static {
|
||||
System.loadLibrary("go");
|
||||
}
|
||||
public String ObjectName;
|
||||
|
||||
public static native void qtrun(qt.java.lang.Runnable me);
|
||||
@Override
|
||||
public void run() {
|
||||
qtrun(this);
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
package android
|
||||
|
||||
import (
|
||||
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
||||
"github.com/therecipe/qt/androidextras"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CwtchService struct {
|
||||
androidextras.QAndroidService
|
||||
//core.QObject
|
||||
|
||||
//_ func() `constructor:"init"`
|
||||
}
|
||||
|
||||
func (cs *CwtchService) onCreate() {
|
||||
log.Infof("CwtchService onCreate()\n")
|
||||
|
||||
go func() {
|
||||
i := 0
|
||||
for true {
|
||||
time.Sleep(2 * time.Second)
|
||||
log.Infof("CwtchService thread iteration %d\n", i)
|
||||
i++
|
||||
}
|
||||
}()
|
||||
}
|
|
@ -1,98 +0,0 @@
|
|||
package android
|
||||
|
||||
import "C"
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/therecipe/qt"
|
||||
"github.com/therecipe/qt/androidextras"
|
||||
)
|
||||
|
||||
type Runnable struct {
|
||||
ptr *androidextras.QAndroidJniObject
|
||||
}
|
||||
|
||||
type Runnable_ITF interface {
|
||||
Runnable_PTR() *Runnable
|
||||
}
|
||||
|
||||
func (p *Runnable) Pointer() *androidextras.QAndroidJniObject {
|
||||
return p.ptr
|
||||
}
|
||||
|
||||
func (p *Runnable) SetPointer(ptr *androidextras.QAndroidJniObject) {
|
||||
p.ptr = ptr
|
||||
}
|
||||
|
||||
func (ptr *Runnable) ConnectRun(f func()) {
|
||||
if ptr.Pointer() != nil && ptr.Pointer().IsValid() {
|
||||
|
||||
qt.ConnectSignal(ptr.Pointer().ToString(), "Runnable::run", unsafe.Pointer(&f))
|
||||
}
|
||||
}
|
||||
|
||||
func (ptr *Runnable) DisconnectRun() {
|
||||
if ptr.Pointer() != nil && ptr.Pointer().IsValid() {
|
||||
|
||||
qt.DisconnectSignal(ptr.Pointer().ToString(), "Runnable::run")
|
||||
}
|
||||
}
|
||||
|
||||
//export Java_qt_java_lang_Runnable_qtrun
|
||||
func Java_qt_java_lang_Runnable_qtrun(_, _, this unsafe.Pointer) {
|
||||
if signal := qt.GetSignal(androidextras.NewQAndroidJniObject6(this).ToString(), "Runnable::run"); signal != nil {
|
||||
(*(*func())(signal))()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (ptr *Runnable) Run() {
|
||||
if ptr.Pointer() != nil && ptr.Pointer().IsValid() {
|
||||
ptr.Pointer().CallMethodVoid2("run", "()V")
|
||||
}
|
||||
}
|
||||
|
||||
func PointerFromRunnable(ptr Runnable_ITF) *androidextras.QAndroidJniObject {
|
||||
if ptr != nil {
|
||||
return ptr.Runnable_PTR().Pointer()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewRunnableFromPointer(ptr androidextras.QAndroidJniObject_ITF) *Runnable {
|
||||
var n = new(Runnable)
|
||||
n.SetPointer(ptr.QAndroidJniObject_PTR())
|
||||
return n
|
||||
}
|
||||
|
||||
func (ptr *Runnable) Runnable_PTR() *Runnable {
|
||||
return ptr
|
||||
}
|
||||
|
||||
func Runnable__IsClassAvailable() bool {
|
||||
return androidextras.QAndroidJniObject_IsClassAvailable("java/lang/Runnable")
|
||||
}
|
||||
|
||||
func Runnable__IsQtClassAvailable() bool {
|
||||
return androidextras.QAndroidJniObject_IsClassAvailable("qt/java/lang/Runnable")
|
||||
}
|
||||
|
||||
func (ptr *Runnable) SetObjectName(n string) {
|
||||
if ptr.Pointer() != nil && ptr.Pointer().IsValid() {
|
||||
ptr.Pointer().SetField2("ObjectName", "Ljava/lang/String;", n)
|
||||
}
|
||||
}
|
||||
|
||||
func (ptr *Runnable) ObjectName() string {
|
||||
if ptr.Pointer() != nil && ptr.Pointer().IsValid() {
|
||||
return ptr.Pointer().GetObjectField2("ObjectName", "Ljava/lang/String;").ToString()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func NewQtRunnable() *Runnable {
|
||||
return NewRunnableFromPointer(androidextras.NewQAndroidJniObject3("qt/java/lang/Runnable", "()V"))
|
||||
}
|
||||
func NewRunnable() *Runnable {
|
||||
return NewRunnableFromPointer(androidextras.NewQAndroidJniObject3("java/lang/Runnable", "()V"))
|
||||
}
|
167
main.go
167
main.go
|
@ -21,6 +21,7 @@ import (
|
|||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
const androidBaseDir = "/data/data/ca.openprivacy.cwtch.ui/"
|
||||
|
@ -36,8 +37,10 @@ func init() {
|
|||
}
|
||||
|
||||
func main() {
|
||||
log.Infoln("ui main()\n")
|
||||
flagDebug := flag.Bool("debug", false, "turn on extra logging. WARNING: THIS MAY EXPOSE PRIVATE INFORMATION IN CONSOLE OUTPUT!")
|
||||
flagLocal := flag.Bool("local", false, "load user interface from the local folder \"qml\" instead of the built-in UI")
|
||||
flagService := flag.Bool("service", false, "indicate this process should run as an android service")
|
||||
flag.Parse()
|
||||
|
||||
if *flagDebug {
|
||||
|
@ -46,83 +49,113 @@ func main() {
|
|||
log.SetLevel(log.LevelInfo)
|
||||
}
|
||||
|
||||
// our globals
|
||||
gcd := gothings.NewGrandCentralDispatcher(nil)
|
||||
gcd.SetOs(runtime.GOOS)
|
||||
if buildVer != "" {
|
||||
gcd.SetVersion(buildVer)
|
||||
gcd.SetBuildDate(buildDate)
|
||||
log.Infof("cwtch main args: %v\n", os.Args)
|
||||
|
||||
if *flagService {
|
||||
log.Infoln("I am the service")
|
||||
torpath := path.Join(androidBaseDir, "lib/libtor.so")
|
||||
log.Infoln("Starting Tor...")
|
||||
acn, _ := connectivity.StartTor(the.CwtchDir, torpath)
|
||||
log.Infoln("Starting a cwtch app...")
|
||||
the.CwtchApp = libapp.NewApp(acn, the.CwtchDir)
|
||||
|
||||
log.Infoln("Sleepinging???")
|
||||
for true {
|
||||
time.Sleep(5 * time.Second)
|
||||
log.Infoln("Wake up, repeat")
|
||||
}
|
||||
|
||||
} else {
|
||||
gcd.SetVersion("development")
|
||||
gcd.SetBuildDate("now")
|
||||
}
|
||||
gcd.UIState = gothings.NewUIState(gcd)
|
||||
the.AcknowledgementIDs = make(map[string][]*the.AckId)
|
||||
gcd.OutgoingMessages = make(chan gobjects.Letter, 1000)
|
||||
log.Infoln("I am the application")
|
||||
|
||||
//TODO: put theme stuff somewhere better
|
||||
gcd.SetThemeScale(1.0)
|
||||
app := gui.NewQGuiApplication(len(os.Args), os.Args)
|
||||
|
||||
// this is to load local qml files quickly when developing
|
||||
var qmlSource *core.QUrl
|
||||
if *flagLocal {
|
||||
qmlSource = core.QUrl_FromLocalFile("./qml/main.qml")
|
||||
} else {
|
||||
qmlSource = core.NewQUrl3("qrc:/qml/main.qml", 0)
|
||||
}
|
||||
// our globals
|
||||
gcd := gothings.NewGrandCentralDispatcher(nil)
|
||||
gcd.SetOs(runtime.GOOS)
|
||||
if buildVer != "" {
|
||||
gcd.SetVersion(buildVer)
|
||||
gcd.SetBuildDate(buildDate)
|
||||
} else {
|
||||
gcd.SetVersion("development")
|
||||
gcd.SetBuildDate("now")
|
||||
}
|
||||
gcd.UIState = gothings.NewUIState(gcd)
|
||||
the.AcknowledgementIDs = make(map[string][]*the.AckId)
|
||||
gcd.OutgoingMessages = make(chan gobjects.Letter, 1000)
|
||||
|
||||
app := gui.NewQGuiApplication(len(os.Args), os.Args)
|
||||
app.SetWindowIcon(gui.NewQIcon5(":/qml/images/cwtch-icon.png"))
|
||||
|
||||
// load english first so it becomes the default in case we don't have a .ts for the user's locale, or if it contains unfinished strings
|
||||
translator := core.NewQTranslator(nil)
|
||||
translator.Load("translation_en", ":/i18n/", "", "")
|
||||
core.QCoreApplication_InstallTranslator(translator)
|
||||
|
||||
gcd.Translator = core.NewQTranslator(nil)
|
||||
gcd.Translator.Load("translation_"+core.QLocale_System().Name(), ":/i18n/", "", "")
|
||||
core.QCoreApplication_InstallTranslator(gcd.Translator)
|
||||
|
||||
core.QCoreApplication_SetAttribute(core.Qt__AA_EnableHighDpiScaling, true)
|
||||
quickcontrols2.QQuickStyle_SetStyle("Universe")
|
||||
engine := qml.NewQQmlApplicationEngine(nil)
|
||||
gcd.QMLEngine = engine
|
||||
|
||||
// prevent qt from initiating network connections (possible deanon attempts!)
|
||||
factory := qml.NewQQmlNetworkAccessManagerFactory()
|
||||
factory.ConnectCreate(func(parent *core.QObject) *network.QNetworkAccessManager {
|
||||
nam := network.NewQNetworkAccessManager(parent)
|
||||
nam.SetNetworkAccessible(network.QNetworkAccessManager__NotAccessible)
|
||||
proxy := network.NewQNetworkProxy()
|
||||
proxy.SetHostName("0.0.0.0")
|
||||
nam.SetProxy(proxy)
|
||||
//nam.ConnectCreateRequest(func(op network.QNetworkAccessManager__Operation, originalReq *network.QNetworkRequest, outgoingData *core.QIODevice) *network.QNetworkReply {
|
||||
// log.Errorf("network access request detected - possible remote content insertion bug!!!")
|
||||
// return nil
|
||||
//})
|
||||
return nam
|
||||
})
|
||||
engine.SetNetworkAccessManagerFactory(factory)
|
||||
|
||||
// variables we want to access from inside qml
|
||||
if runtime.GOOS == "android" {
|
||||
gcd.SetThemeScale(2.9)
|
||||
} else {
|
||||
//TODO: put theme stuff somewhere better
|
||||
gcd.SetThemeScale(1.0)
|
||||
|
||||
// this is to load local qml files quickly when developing
|
||||
var qmlSource *core.QUrl
|
||||
if *flagLocal {
|
||||
qmlSource = core.QUrl_FromLocalFile("./qml/main.qml")
|
||||
} else {
|
||||
qmlSource = core.NewQUrl3("qrc:/qml/main.qml", 0)
|
||||
}
|
||||
|
||||
app.SetWindowIcon(gui.NewQIcon5(":/qml/images/cwtch-icon.png"))
|
||||
|
||||
// load english first so it becomes the default in case we don't have a .ts for the user's locale, or if it contains unfinished strings
|
||||
translator := core.NewQTranslator(nil)
|
||||
translator.Load("translation_en", ":/i18n/", "", "")
|
||||
core.QCoreApplication_InstallTranslator(translator)
|
||||
|
||||
gcd.Translator = core.NewQTranslator(nil)
|
||||
gcd.Translator.Load("translation_"+core.QLocale_System().Name(), ":/i18n/", "", "")
|
||||
core.QCoreApplication_InstallTranslator(gcd.Translator)
|
||||
|
||||
core.QCoreApplication_SetAttribute(core.Qt__AA_EnableHighDpiScaling, true)
|
||||
quickcontrols2.QQuickStyle_SetStyle("Universe")
|
||||
engine := qml.NewQQmlApplicationEngine(nil)
|
||||
gcd.QMLEngine = engine
|
||||
|
||||
// prevent qt from initiating network connections (possible deanon attempts!)
|
||||
factory := qml.NewQQmlNetworkAccessManagerFactory()
|
||||
factory.ConnectCreate(func(parent *core.QObject) *network.QNetworkAccessManager {
|
||||
nam := network.NewQNetworkAccessManager(parent)
|
||||
nam.SetNetworkAccessible(network.QNetworkAccessManager__NotAccessible)
|
||||
proxy := network.NewQNetworkProxy()
|
||||
proxy.SetHostName("0.0.0.0")
|
||||
nam.SetProxy(proxy)
|
||||
//nam.ConnectCreateRequest(func(op network.QNetworkAccessManager__Operation, originalReq *network.QNetworkRequest, outgoingData *core.QIODevice) *network.QNetworkReply {
|
||||
// log.Errorf("network access request detected - possible remote content insertion bug!!!")
|
||||
// return nil
|
||||
//})
|
||||
return nam
|
||||
})
|
||||
engine.SetNetworkAccessManagerFactory(factory)
|
||||
|
||||
// variables we want to access from inside qml
|
||||
if runtime.GOOS == "android" {
|
||||
gcd.SetThemeScale(2.9)
|
||||
} else {
|
||||
gcd.SetThemeScale(1.0)
|
||||
}
|
||||
engine.RootContext().SetContextProperty("gcd", gcd)
|
||||
|
||||
var androidCwtchActivity = android.NewCwtchActivity(nil)
|
||||
engine.RootContext().SetContextProperty("androidCwtchActivity", androidCwtchActivity)
|
||||
/*if runtime.GOOS == "android" {
|
||||
androidCwtchActivity.StartCwtchService()
|
||||
}*/
|
||||
|
||||
//engine.addImportPath(QStringLiteral("qrc:/"));
|
||||
//engine.load(QUrl(QStringLiteral("qrc:/source/qml/main.qml")));
|
||||
|
||||
engine.Load(qmlSource)
|
||||
|
||||
the.ACN = nil
|
||||
go loadNetworkingAndFiles(gcd)
|
||||
|
||||
app.Exec()
|
||||
}
|
||||
engine.RootContext().SetContextProperty("gcd", gcd)
|
||||
|
||||
var androidCwtchActivity = android.NewCwtchActivity(nil)
|
||||
engine.RootContext().SetContextProperty("androidCwtchActivity", androidCwtchActivity)
|
||||
|
||||
engine.Load(qmlSource)
|
||||
the.ACN = nil
|
||||
go loadNetworkingAndFiles(gcd)
|
||||
app.Exec()
|
||||
|
||||
if the.ACN != nil {
|
||||
the.ACN.Close()
|
||||
}
|
||||
log.Infoln("Exiting main()")
|
||||
}
|
||||
|
||||
func loadNetworkingAndFiles(gcd *gothings.GrandCentralDispatcher) {
|
||||
|
|
Reference in New Issue