Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically resume shares on device start #178

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
package="info.varden.hauk">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>


<application
android:allowBackup="true"
Expand Down Expand Up @@ -45,14 +48,27 @@
</activity>

<receiver
android:name=".global.Receiver"
android:name=".global.ExportedReceiver"
android:exported="true"
tools:ignore="ExportedReceiver">
<intent-filter>
<action android:name="info.varden.hauk.START_ALONE_THEN_SHARE_VIA" />
<action android:name="info.varden.hauk.START_ALONE_THEN_MAKE_TOAST" />
</intent-filter>
</receiver>

<receiver
Copy link
Contributor

@licaon-kter licaon-kter Jun 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm this one

Copy link
Contributor

@licaon-kter licaon-kter Jun 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My local patch was missing the receiver on built hence it was erroring out on reboot, my bad

I'm using this snippet

        <receiver
            android:name=".global.RebootReceiver"
            android:enabled="true"
            android:exported="false">

            <intent-filter android:directBootAware="true">
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <action android:name="android.intent.action.REBOOT"/>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

        </receiver>

and works fine in my testing on Android 13

android:name=".global.RebootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

</receiver>

<receiver
android:name=".notify.CopyLinkReceiver"
android:exported="false">
Expand Down
1 change: 1 addition & 0 deletions android/app/src/main/java/info/varden/hauk/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public enum Constants {
public static final Preference<String> PREF_NICKNAME = new Preference.String("nickname", "");
public static final Preference<Integer> PREF_DURATION_UNIT = new Preference.Integer("durUnit", Constants.DURATION_UNIT_MINUTES);
public static final Preference<Boolean> PREF_ALLOW_ADOPTION = new Preference.Boolean("allowAdoption", true);
public static final Preference<Boolean> PREF_RESTART_ON_BOOT = new Preference.Boolean("restartOnBoot", true);
public static final Preference<NightModeStyle> PREF_NIGHT_MODE = new Preference.Enum<>("nightMode", NightModeStyle.FOLLOW_SYSTEM);
public static final Preference<Boolean> PREF_CONFIRM_STOP = new Preference.Boolean("confirmStop", true);
public static final Preference<Boolean> PREF_HIDE_LOGO = new Preference.Boolean("hideLogo", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
* @since 1.3
* @author Marius Lindvall
*/
public final class Receiver extends BroadcastReceiver {
public final class ExportedReceiver extends BroadcastReceiver {
@SuppressWarnings("HardCodedStringLiteral")
private static final String ACTION_START_SHARING_ALONE_WITH_MENU = "info.varden.hauk.START_ALONE_THEN_SHARE_VIA";
@SuppressWarnings("HardCodedStringLiteral")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package info.varden.hauk.global;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Toast;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.SocketAddress;

import info.varden.hauk.Constants;
import info.varden.hauk.R;
import info.varden.hauk.global.ui.AuthorizationActivity;
import info.varden.hauk.global.ui.DisplayShareDialogListener;
import info.varden.hauk.global.ui.toast.GNSSStatusUpdateListenerImpl;
import info.varden.hauk.global.ui.toast.SessionInitiationResponseHandlerImpl;
import info.varden.hauk.global.ui.toast.ShareListenerImpl;
import info.varden.hauk.http.ConnectionParameters;
import info.varden.hauk.http.SessionInitiationPacket;
import info.varden.hauk.http.security.CertificateValidationPolicy;
import info.varden.hauk.manager.SessionManager;
import info.varden.hauk.struct.AdoptabilityPreference;
import info.varden.hauk.system.LocationPermissionsNotGrantedException;
import info.varden.hauk.system.LocationServicesDisabledException;
import info.varden.hauk.system.preferences.PreferenceManager;
import info.varden.hauk.utils.DeprecationMigrator;
import info.varden.hauk.utils.Log;
import info.varden.hauk.utils.TimeUtils;
// Reboot broadcast receiver for Hauk. If enabled resumes a share session if one exists on device restart
public final class RebootReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// Subsequent calls may result in data being read from preferences. We should ensure that
// all deprecated preferences have been migrated before we continue.
new DeprecationMigrator(context).migrate();

PreferenceManager prefs = new PreferenceManager(context);
if(prefs.get(Constants.PREF_RESTART_ON_BOOT))
resumeShare(context,intent);
}
private void resumeShare(Context context, Intent intent) {
Log.d("Trying to resume shares...");
new BroadcastSessionManager(context).resumeShares();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,15 @@ public final void resumeShares(ResumePrompt prompt) {
}
}

public final void resumeShares() {
if (pusher != null) {
Log.d("Pusher is non-null (%s), stopping and nulling it before calling service relauncher", pusher); //NON-NLS
this.ctx.stopService(pusher);
pusher = null;
}
this.resumable.tryResumeShare(new ServiceRelauncher(this, this.resumable));
}

/**
* A preparation step for initiating sessions. Checks location services status and instantiates
* a response handler for the session initiation packet.
Expand Down Expand Up @@ -448,6 +457,7 @@ private boolean hasLocationPermission() {
}
}


/**
* The GNSS status handler that the {@link SessionManager} itself uses to receive status updates
* from the GNSS listeners. Used to propagate events further upstream.
Expand Down
4 changes: 4 additions & 0 deletions android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@
<string name="tls_validation_no_anchor_onion">Disable trust anchor validation for .onion hosts (not recommended)</string>
<string name="tls_validation_none_for_onion">Disable trust anchor and hostname validation for .onion hosts (not recommended)</string>

<string name="pref_restartOnBoot_title">Resume shares on device restart</string>
<string name="pref_restartOnBoot_summaryOn">Resume shares on boot</string>
<string name="pref_restartOnBoot_summaryOff">Do not resume shares on boot</string>

<!-- Appearance settings -->
<string name="prefs_header_appearance">Appearance</string>
<string name="prefs_nightMode_title">Night mode</string>
Expand Down
5 changes: 5 additions & 0 deletions android/app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
app:entries="@array/tls_validation_types"
app:entryValues="@array/tls_validation_type_values" />

<SwitchPreference
app:key="restartOnBoot"
app:title="@string/pref_restartOnBoot_title"
app:summaryOff="@string/pref_restartOnBoot_summaryOff"
app:summaryOn="@string/pref_restartOnBoot_summaryOn" />
</PreferenceCategory>

<PreferenceCategory app:title="@string/prefs_header_appearance">
Expand Down