Sfoglia il codice sorgente

app/android: add a ForegroundService so the app stays awake and doesn't get killed when in the background (disconnecting the p2p network)

darkfi 1 anno fa
parent
commit
a1a32db8d8

+ 9 - 0
bin/app/Cargo.toml

@@ -113,3 +113,12 @@ assets = "assets"
 name = "android.permission.INTERNET"
 [[package.metadata.android.permission]]
 name = "android.permission.ACCESS_NETWORK_STATE"
+[[package.metadata.android.permission]]
+name = "android.permission.FOREGROUND_SERVICE"
+[[package.metadata.android.permission]]
+name = "android.permission.FOREGROUND_SERVICE_REMOTE_MESSAGING"
+
+[[package.metadata.android.service]]
+name = ".ForegroundService"
+foreground_service_type = "remoteMessaging"
+exported = false

+ 14 - 10
bin/app/Dockerfile

@@ -1,7 +1,7 @@
 FROM docker.io/archlinux
 
 RUN pacman -Syu --noconfirm
-RUN pacman -S --noconfirm jdk8-openjdk unzip wget cmake openssl pkgconf gcc git
+RUN pacman -S --noconfirm jdk17-openjdk unzip wget cmake openssl pkgconf gcc git zip
 
 # github override HOME, so here we are
 ENV RUSTUP_HOME=/usr/local/rustup \
@@ -23,14 +23,17 @@ ENV ANDROID_HOME /opt/android-sdk-linux
 ENV JAVA_HOME /usr/lib/jvm/default
 RUN mkdir ${ANDROID_HOME} && \
     cd ${ANDROID_HOME} && \
-    wget -q https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip && \
-    unzip -q sdk-tools-linux-4333796.zip && \
-    rm sdk-tools-linux-4333796.zip && \
+    wget -q https://dl.google.com/android/repository/commandlinetools-linux-13114758_latest.zip && \
+    unzip -q commandlinetools-linux-13114758_latest.zip && \
+    rm commandlinetools-linux-13114758_latest.zip && \
+    mv cmdline-tools latest && \
+    mkdir cmdline-tools/ && \
+    mv latest cmdline-tools/ && \
     chown -R root:root /opt
 RUN mkdir -p ~/.android && touch ~/.android/repositories.cfg
-RUN yes | ${ANDROID_HOME}/tools/bin/sdkmanager "platform-tools" | grep -v = || true
-RUN yes | ${ANDROID_HOME}/tools/bin/sdkmanager "platforms;android-31" | grep -v = || true
-RUN yes | ${ANDROID_HOME}/tools/bin/sdkmanager "build-tools;31.0.0"  | grep -v = || true
+RUN yes | ${ANDROID_HOME}/cmdline-tools/latest/bin/sdkmanager "platform-tools" | grep -v = || true
+RUN yes | ${ANDROID_HOME}/cmdline-tools/latest/bin/sdkmanager "platforms;android-36" | grep -v = || true
+RUN yes | ${ANDROID_HOME}/cmdline-tools/latest/bin/sdkmanager "build-tools;36.0.0-rc5"  | grep -v = || true
 RUN ${ANDROID_HOME}/tools/bin/sdkmanager --update | grep -v = || true
 
 # Install Android NDK
@@ -42,15 +45,16 @@ ENV NDK_HOME /usr/local/android-ndk-r25
 
 # Copy contents to container. Should only use this on a clean directory
 WORKDIR /root/
-RUN git clone https://github.com/not-fl3/cargo-quad-apk cargo-apk
+RUN git clone https://github.com/narodnik/cargo-quad-apk cargo-apk
+#RUN git clone https://github.com/not-fl3/cargo-quad-apk cargo-apk
 # For deterministic builds, we want a deterministic toolchain
-RUN cd /root/cargo-apk && git checkout f3b865610b79a2b1b9d2b90600c36390a9e19569
+#RUN cd /root/cargo-apk && git checkout f3b865610b79a2b1b9d2b90600c36390a9e19569
 
 # Install binary
 RUN cargo install --path /root/cargo-apk
 
 # Add build-tools to PATH, for apksigner
-ENV PATH="/opt/android-sdk-linux/build-tools/31.0.0/:${PATH}"
+ENV PATH="/opt/android-sdk-linux/build-tools/36.0.0-rc5/:${PATH}"
 
 # Lets cache packages for faster builds
 RUN git clone --depth=1 https://codeberg.org/darkrenaissance/darkfi

+ 1 - 1
bin/app/Makefile

@@ -88,7 +88,7 @@ install-apk:
 
 # Useful for dev
 cli:
-	podman run -v $(shell pwd)/../../:/root/darkfi -w /root/darkfi/bin/app/ -it apk bash
+	podman run -v /home/narodnik/src/stuff/cargo-quad-apk:/root/capk -v $(shell pwd)/../../:/root/darkfi -w /root/darkfi/bin/app/ -it apk bash
 
 fmt:
 	$(CARGO) +nightly fmt

+ 76 - 0
bin/app/java/ForegroundService.java

@@ -0,0 +1,76 @@
+/* This file is part of DarkFi (https://dark.fi)
+ *
+ * Copyright (C) 2020-2025 Dyne.org foundation
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package TARGET_PACKAGE_NAME;
+
+import android.app.Service;
+import android.content.pm.ServiceInfo;
+import android.app.Notification;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
+import android.content.Intent;
+import android.os.Build;
+import android.os.IBinder;
+import android.util.Log;
+
+public class ForegroundService extends Service {
+
+    @Override
+    public void onCreate() {
+        super.onCreate();
+        createNotificationChannel();
+    }
+
+    @Override
+    public int onStartCommand(Intent intent, int flags, int startId) {
+        //Log.d("darkfi", "ForegroundService::onStartCommand()");
+        Notification.Builder builder = 
+            new Notification.Builder(this, "darkfi_service");
+
+        Notification notification = builder
+            .setContentTitle("DarkFi")
+            .setContentText("Running p2p network...")
+            .setSmallIcon(android.R.drawable.ic_dialog_info)
+            .build();
+
+        startForeground(
+            100,
+            notification,
+            ServiceInfo.FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING
+        );
+
+        return START_STICKY;
+    }
+
+    @Override
+    public IBinder onBind(Intent intent) {
+        return null;
+    }
+
+    private void createNotificationChannel() {
+        //Log.d("darkfi", "ForegroundService::createNotificationChannel()");
+        NotificationChannel channel = new NotificationChannel(
+            "darkfi_service",
+            "DarkFi Foreground Service",
+            NotificationManager.IMPORTANCE_DEFAULT
+        );
+        NotificationManager manager = getSystemService(NotificationManager.class);
+        manager.createNotificationChannel(channel);
+    }
+}
+

+ 4 - 0
bin/app/java/MainActivity.java

@@ -169,5 +169,9 @@ view.setFocusable(false);
 view.setFocusableInTouchMode(false);
 view.clearFocus();
 
+// Start a foreground service so the app stays awake
+Intent serviceIntent = new Intent(this, ForegroundService.class);
+startForegroundService(serviceIntent);
+
 //% END
 

+ 1 - 0
bin/app/quad.toml

@@ -3,5 +3,6 @@ java_files = [
     "java/autosuggest/CustomInputConnection.java",
     "java/autosuggest/InvisibleInputView.java",
     #"java/autosuggest/InvisibleInputManager.java",
+    "java/ForegroundService.java",
 ]