feat(mobile): keep screen up and use persistent instance id

This commit is contained in:
wpetit 2024-01-12 15:15:24 +01:00
parent 5d0311b731
commit 22070ba33a
6 changed files with 60 additions and 13 deletions

View File

@ -16,7 +16,7 @@ test: test-go ## Executing tests
test-go: deps
( set -o allexport && source .env && set +o allexport && go test -v -count=1 $(GOTEST_ARGS) ./... )
build: build-desktop build-mobile build-client ## Build artefacts
build: build-desktop build-android build-client ## Build artefacts
build-desktop: deps ## Build executable
CGO_ENABLED=0 go build \
@ -30,12 +30,12 @@ build-client: deps ## Build executable
-o ./bin/client \
./cmd/client
build-mobile: tools/gogio/bin/gogio deps ## Build executable
build-android: tools/gogio/bin/gogio deps ## Build executable
mkdir -p dist
GOOS=android CGO_CFLAGS="-I${JDK_PATH}/include -I${JDK_PATH}/include/linux -w" tools/gogio/bin/gogio -target android -buildmode archive -o android/app/libs/mobile.aar -x ./cmd/mobile
( cd android && ./gradlew assembleDebug )
install-android: build-mobile
install-android: build-android
adb install android/app/build/outputs/apk/debug/app-debug.apk
adb shell monkey -p com.cadoles.arcast_player -c android.intent.category.LAUNCHER 1

Binary file not shown.

Binary file not shown.

View File

@ -6,8 +6,11 @@
<uses-feature android:glEsVersion="0x00030000"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"

View File

@ -13,11 +13,13 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.cadoles.arcast_player.ui.theme.ArcastplayerTheme
import org.gioui.GioActivity
import android.view.WindowManager
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
startActivity(Intent(this, GioActivity::class.java))
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
}

View File

@ -6,6 +6,7 @@ package main
import (
"context"
"os"
"sync"
"forge.cadoles.com/arcad/arcast/pkg/browser/gioui"
"forge.cadoles.com/arcad/arcast/pkg/server"
@ -14,18 +15,15 @@ import (
"gioui.org/layout"
"gioui.org/op"
"github.com/gioui-plugins/gio-plugins/plugin"
"github.com/gioui-plugins/gio-plugins/safedata"
"github.com/gioui-plugins/gio-plugins/safedata/giosafedata"
"github.com/gioui-plugins/gio-plugins/webviewer/webview"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/logger"
// Android permissions
_ "net"
_ "gioui.org/app/permission/networkstate"
_ "gioui.org/app/permission/storage"
_ "gioui.org/app/permission/wakelock"
)
const instanceIDSecretIdentifier = "instance_id"
func main() {
ctx := context.Background()
@ -36,6 +34,12 @@ func main() {
browser := gioui.NewBrowser(window)
var safeDataConfig safedata.Config
var safeDataConfigWaigGroup sync.WaitGroup
var initSafeDataConfig sync.Once
safeDataConfigWaigGroup.Add(1)
go func() {
ops := new(op.Ops)
for {
@ -50,16 +54,30 @@ func main() {
gtx := layout.NewContext(ops, evt)
browser.Layout(gtx)
evt.Frame(gtx.Ops)
case app.ViewEvent:
initSafeDataConfig.Do(func() {
defer safeDataConfigWaigGroup.Done()
safeDataConfig = giosafedata.NewConfigFromViewEvent(window, evt, "com.cadoles.arcast_player")
})
}
}
}()
go func() {
for {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
server := server.New(browser)
safeDataConfigWaigGroup.Wait()
safe := safedata.NewSafeData(safeDataConfig)
instanceID, err := getInstanceIDFromSafeData(safe)
if err != nil {
logger.Fatal(ctx, "could not retrieve instance id", logger.CapturedE(errors.WithStack(err)))
}
server := server.New(browser, server.WithInstanceID(instanceID))
if err := server.Start(); err != nil {
logger.Fatal(ctx, "could not start server", logger.CapturedE(errors.WithStack(err)))
@ -79,3 +97,27 @@ func main() {
app.Main()
}
func getInstanceIDFromSafeData(safe *safedata.SafeData) (string, error) {
instanceIDSecret, err := safe.Get(instanceIDSecretIdentifier)
if err != nil && err.Error() != "not found" {
return "", errors.Wrapf(err, "could not retrieve instance id secret")
}
var instanceID string
if len(instanceIDSecret.Data) > 0 {
instanceID = string(instanceIDSecret.Data)
} else {
instanceID = server.NewRandomInstanceID()
instanceIDSecret.Identifier = instanceIDSecretIdentifier
instanceIDSecret.Data = []byte(instanceID)
instanceIDSecret.Description = "Arcast player instance identifier"
if err := safe.Set(instanceIDSecret); err != nil {
return "", errors.Wrapf(err, "could not save instance id secret")
}
}
return instanceID, nil
}