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

Add exit action to notification (fixes #1121) #1210

Merged
merged 13 commits into from
Dec 10, 2024
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE_PREFERENCES" />
</intent-filter>
<intent-filter>
<action android:name="com.github.catfriend1.syncthingandroid.MainActivity.EXIT" />
</intent-filter>
</activity>
<activity
android:name=".activities.RecentChangesActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ public class MainActivity extends SyncthingActivity
private static final int FOLDER_FRAGMENT_ID = 0;
private static final int DEVICE_FRAGMENT_ID = 1;
private static final int STATUS_FRAGMENT_ID = 2;

/**
* Intent action to exit app.
*/
public static final String ACTION_EXIT =
"com.github.catfriend1.syncthingandroid.MainActivity.EXIT";

/**
* Time after first start when usage reporting dialog should be shown.
Expand All @@ -110,6 +116,7 @@ public class MainActivity extends SyncthingActivity
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;

private Intent mLastIntent;
private Boolean oneTimeShot = true;

@Inject SharedPreferences mPreferences;
Expand Down Expand Up @@ -253,6 +260,14 @@ public void onCreate(Bundle savedInstanceState) {

onNewIntent(getIntent());
}


@Override
protected void onNewIntent(Intent intent) {
mLastIntent = intent;
super.onNewIntent(intent);
};


/**
* Updates the ViewPager to show tabs depending on the service state.
Expand Down Expand Up @@ -348,6 +363,16 @@ public void onResume() {
}

startUIRefreshHandler();

String action = mLastIntent.getAction();
if (action != null) {
if (ACTION_EXIT.equals(action)) {
Log.i(TAG, "Exit app requested by notification action");
stopService(new Intent(this, SyncthingService.class));
finishAndRemoveTask();
}
}

super.onResume();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,27 @@ public void updatePersistentNotification(SyncthingService service,
*/
int idToShow = syncthingRunning ? ID_PERSISTENT : ID_PERSISTENT_WAITING;
int idToCancel = syncthingRunning ? ID_PERSISTENT_WAITING : ID_PERSISTENT;
Intent intent = new Intent(mContext, MainActivity.class);

Intent openAppIntent = new Intent(mContext, MainActivity.class);

Intent exitIntent = new Intent(mContext, MainActivity.class);
exitIntent.setAction(MainActivity.ACTION_EXIT);
PendingIntent exitPendingIntent = PendingIntent.getActivity(
mContext,
0,
exitIntent,
FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT
);

NotificationChannel channel = syncthingRunning ? mPersistentChannel : mPersistentChannelWaiting;
NotificationCompat.Builder builder = getNotificationBuilder(channel)
.setContentTitle(text)
.setSmallIcon(R.drawable.ic_stat_notify)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setPriority(NotificationCompat.PRIORITY_MIN)
.setContentIntent(PendingIntent.getActivity(mContext, 0, intent, FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT));
.setContentIntent(PendingIntent.getActivity(mContext, 0, openAppIntent, FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT))
.addAction(R.drawable.baseline_close_24, mContext.getString(R.string.exit), exitPendingIntent);
if (!appShutdownInProgress) {
if (startForegroundService) {
Log.v(TAG, "Starting foreground service or updating notification");
Expand Down
Loading