Skip to content

Commit

Permalink
[Profile] Avoid displaying completion notification when it's triggere…
Browse files Browse the repository at this point in the history
…d through automation

Signed-off-by: Muntashir Al-Islam <muntashirakon@riseup.net>
MuntashirAkon committed Jan 10, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent c3440d6 commit febb98d
Showing 5 changed files with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -8,6 +8,8 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Objects;

import io.github.muntashirakon.AppManager.BaseActivity;
import io.github.muntashirakon.AppManager.profiles.ProfileApplierActivity;

@@ -44,17 +46,19 @@ private void handleRequest(@NonNull Intent intent) {
return;
}

switch (feature) {
switch (Objects.requireNonNull(feature)) {
case "profile":
launchProfile(intent);
break;
default:
throw new RuntimeException("Invalid feature: " + feature);
}
finish();
}

public void launchProfile(@NonNull Intent intent) {
String profileId = intent.getStringExtra(ProfileApplierActivity.EXTRA_PROFILE_ID);
String state = intent.getStringExtra(ProfileApplierActivity.EXTRA_STATE);
startActivity(ProfileApplierActivity.getShortcutIntent(getApplicationContext(), profileId, null, state));
startActivity(ProfileApplierActivity.getAutomationIntent(getApplicationContext(), profileId, state));
}
}
Original file line number Diff line number Diff line change
@@ -223,7 +223,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (id == android.R.id.home) {
onBackPressed();
} else if (id == R.id.action_apply) {
Intent intent = ProfileApplierActivity.getShortcutIntent(this, model.getProfileName(), null, null);
Intent intent = ProfileApplierActivity.getApplierIntent(this, model.getProfileName());
startActivity(intent);
} else if (id == R.id.action_save) {
model.save(false);
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@ public class ProfileApplierActivity extends BaseActivity {

public static final String EXTRA_PROFILE_ID = "prof";
public static final String EXTRA_STATE = "state";
private static final String EXTRA_NOTIFY = "notify";

@StringDef({ST_SIMPLE, ST_ADVANCED})
public @interface ShortcutType {
@@ -73,13 +74,43 @@ public static Intent getShortcutIntent(@NonNull Context context,
return intent;
}

@NonNull
public static Intent getAutomationIntent(@NonNull Context context,
@NonNull String profileId,
@Nullable String state) {
// Compatibility: Old shortcuts still store profile name instead of profile ID.
String realProfileId = ProfileManager.getProfileIdCompat(profileId);
Intent intent = new Intent(context, ProfileApplierActivity.class);
intent.putExtra(EXTRA_PROFILE_ID, realProfileId);
if (state != null) { // State => Automatic trigger
intent.putExtra(EXTRA_SHORTCUT_TYPE, ST_SIMPLE);
intent.putExtra(EXTRA_STATE, state);
// Avoid issuing completion notification
intent.putExtra(EXTRA_NOTIFY, false);
} else { // Manual trigger
intent.putExtra(EXTRA_SHORTCUT_TYPE, ST_ADVANCED);
}
return intent;
}

@NonNull
public static Intent getApplierIntent(@NonNull Context context, @NonNull String profileId) {
// Compatibility: Old shortcuts still store profile name instead of profile ID.
String realProfileId = ProfileManager.getProfileIdCompat(profileId);
Intent intent = new Intent(context, ProfileApplierActivity.class);
intent.putExtra(EXTRA_PROFILE_ID, realProfileId);
intent.putExtra(EXTRA_SHORTCUT_TYPE, ST_ADVANCED);
return intent;
}

public static class ProfileApplierInfo {
public AppsProfile profile;
public String profileId;
@ShortcutType
public String shortcutType;
@Nullable
public String state;
public boolean notify;
}

private final Queue<Intent> mQueue = new LinkedList<>();
@@ -118,9 +149,10 @@ private void next() {
return;
}
@ShortcutType
String shortcutType = getIntent().getStringExtra(EXTRA_SHORTCUT_TYPE);
String profileId = getIntent().getStringExtra(EXTRA_PROFILE_ID);
String profileState = getIntent().getStringExtra(EXTRA_STATE);
String shortcutType = intent.getStringExtra(EXTRA_SHORTCUT_TYPE);
String profileId = intent.getStringExtra(EXTRA_PROFILE_ID);
String profileState = intent.getStringExtra(EXTRA_STATE);
boolean notify = intent.getBooleanExtra(EXTRA_NOTIFY, true);
if (shortcutType == null || profileId == null) {
// Invalid shortcut
return;
@@ -129,6 +161,7 @@ private void next() {
info.profileId = profileId;
info.shortcutType = shortcutType;
info.state = profileState;
info.notify = notify;
mViewModel.loadProfile(info);
}

@@ -145,6 +178,7 @@ private void handleShortcut(@Nullable ProfileApplierInfo info) {
intent.putExtra(ProfileApplierService.EXTRA_PROFILE_NAME, info.profile.name);
// There must be a state
intent.putExtra(ProfileApplierService.EXTRA_PROFILE_STATE, Objects.requireNonNull(state));
intent.putExtra(ProfileApplierService.EXTRA_NOTIFY, info.notify);
ContextCompat.startForegroundService(this, intent);
next();
break;
@@ -165,6 +199,7 @@ private void handleShortcut(@Nullable ProfileApplierInfo info) {
aIntent.putExtra(ProfileApplierService.EXTRA_PROFILE_ID, info.profileId);
aIntent.putExtra(ProfileApplierService.EXTRA_PROFILE_NAME, info.profile.name);
aIntent.putExtra(ProfileApplierService.EXTRA_PROFILE_STATE, selectedState);
aIntent.putExtra(ProfileApplierService.EXTRA_NOTIFY, info.notify);
ContextCompat.startForegroundService(this, aIntent);
})
.setNegativeButton(R.string.cancel, null)
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@ public class ProfileApplierService extends ForegroundService {
public static final String EXTRA_PROFILE_ID = "prof";
public static final String EXTRA_PROFILE_NAME = "name";
public static final String EXTRA_PROFILE_STATE = "state";
public static final String EXTRA_NOTIFY = "notify";
/**
* Notification channel ID
*/
@@ -39,6 +40,7 @@ public class ProfileApplierService extends ForegroundService {
private String mProfileName;
@Nullable
private String mProfileId;
private boolean mNotify = true;
private QueuedProgressHandler mProgressHandler;
private NotificationProgressHandler.NotificationInfo mNotificationInfo;
private PowerManager.WakeLock mWakeLock;
@@ -60,6 +62,7 @@ public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
if (intent != null) {
mProfileId = intent.getStringExtra(EXTRA_PROFILE_ID);
mProfileName = intent.getStringExtra(EXTRA_PROFILE_NAME);
mNotify = intent.getBooleanExtra(EXTRA_NOTIFY, true);
}
NotificationManagerInfo notificationManagerInfo = new NotificationManagerInfo(CHANNEL_ID,
"Profile Applier", NotificationManagerCompat.IMPORTANCE_LOW);
@@ -152,6 +155,6 @@ private void sendNotification(int result, boolean requiresRestart) {
PendingIntent.FLAG_ONE_SHOT, false);
notificationInfo.addAction(0, getString(R.string.restart_device), pendingIntent);
}
mProgressHandler.onResult(notificationInfo);
mProgressHandler.onResult(mNotify ? notificationInfo : null);
}
}
Original file line number Diff line number Diff line change
@@ -237,7 +237,7 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
popupMenu.setOnMenuItemClickListener(item -> {
int id = item.getItemId();
if (id == R.id.action_apply) {
Intent intent = ProfileApplierActivity.getShortcutIntent(mActivity, profile.profileId, null, null);
Intent intent = ProfileApplierActivity.getApplierIntent(mActivity, profile.profileId);
mActivity.startActivity(intent);
} else if (id == R.id.action_delete) {
new MaterialAlertDialogBuilder(mActivity)

0 comments on commit febb98d

Please sign in to comment.