Skip to content

Commit

Permalink
[Profile] Replace individual extras with ProfileQueueItem
Browse files Browse the repository at this point in the history
Signed-off-by: Muntashir Al-Islam <[email protected]>
  • Loading branch information
MuntashirAkon committed Jan 26, 2025
1 parent e0768ab commit 7f9eed9
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ protected void onAuthenticated(@Nullable Bundle savedInstanceState) {
}

@Override
protected void onNewIntent(Intent intent) {
protected void onNewIntent(@NonNull Intent intent) {
synchronized (mQueue) {
mQueue.add(intent);
}
Expand Down Expand Up @@ -170,14 +170,13 @@ private void handleShortcut(@Nullable ProfileApplierInfo info) {
next();
return;
}
String state = info.state != null ? info.state : info.profile.state;
info.state = info.state != null ? info.state : info.profile.state;
switch (info.shortcutType) {
case ST_SIMPLE:
Intent intent = new Intent(this, ProfileApplierService.class);
intent.putExtra(ProfileApplierService.EXTRA_PROFILE_ID, info.profileId);
intent.putExtra(ProfileApplierService.EXTRA_PROFILE_NAME, info.profile.name);
// There must be a state
intent.putExtra(ProfileApplierService.EXTRA_PROFILE_STATE, Objects.requireNonNull(state));
Objects.requireNonNull(info.state);
Intent intent = new Intent(this, ProfileApplierService.class);
intent.putExtra(ProfileApplierService.EXTRA_QUEUE_ITEM, ProfileQueueItem.fromProfiledApplierInfo(info));
intent.putExtra(ProfileApplierService.EXTRA_NOTIFY, info.notify);
ContextCompat.startForegroundService(this, intent);
next();
Expand All @@ -193,12 +192,11 @@ private void handleShortcut(@Nullable ProfileApplierInfo info) {
.setSubtitle(R.string.choose_a_profile_state);
new SearchableSingleChoiceDialogBuilder<>(this, states, statesL)
.setTitle(titleBuilder.build())
.setSelection(state)
.setSelection(info.state)
.setPositiveButton(R.string.ok, (dialog, which, selectedState) -> {
info.state = selectedState;
Intent aIntent = new Intent(this, ProfileApplierService.class);
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_QUEUE_ITEM, ProfileQueueItem.fromProfiledApplierInfo(info));
aIntent.putExtra(ProfileApplierService.EXTRA_NOTIFY, info.notify);
ContextCompat.startForegroundService(this, aIntent);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.Intent;
import android.os.PowerManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.app.PendingIntentCompat;
Expand All @@ -18,6 +19,7 @@
import io.github.muntashirakon.AppManager.R;
import io.github.muntashirakon.AppManager.batchops.BatchOpsResultsActivity;
import io.github.muntashirakon.AppManager.batchops.BatchOpsService;
import io.github.muntashirakon.AppManager.intercept.IntentCompat;
import io.github.muntashirakon.AppManager.progress.NotificationProgressHandler;
import io.github.muntashirakon.AppManager.progress.NotificationProgressHandler.NotificationManagerInfo;
import io.github.muntashirakon.AppManager.progress.ProgressHandler;
Expand All @@ -27,20 +29,13 @@
import io.github.muntashirakon.AppManager.utils.NotificationUtils;

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_QUEUE_ITEM = "queue_item";
public static final String EXTRA_NOTIFY = "notify";
/**
* Notification channel ID
*/
public static final String CHANNEL_ID = BuildConfig.APPLICATION_ID + ".channel.PROFILE_APPLIER";

@Nullable
private String mProfileName;
@Nullable
private String mProfileId;
private boolean mNotify = true;
private QueuedProgressHandler mProgressHandler;
private NotificationProgressHandler.NotificationInfo mNotificationInfo;
private PowerManager.WakeLock mWakeLock;
Expand All @@ -59,11 +54,6 @@ public void onCreate() {
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
if (isWorking()) return super.onStartCommand(intent, flags, 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);
mProgressHandler = new NotificationProgressHandler(this,
Expand All @@ -81,46 +71,49 @@ public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
@Override
protected void onHandleIntent(@Nullable Intent intent) {
if (intent == null) return;
mProfileId = intent.getStringExtra(EXTRA_PROFILE_ID);
mProfileName = intent.getStringExtra(EXTRA_PROFILE_NAME);
if (mProfileId == null || mProfileName == null) return;
String state = intent.getStringExtra(EXTRA_PROFILE_STATE);
ProfileQueueItem item = IntentCompat.getParcelableExtra(intent, EXTRA_QUEUE_ITEM, ProfileQueueItem.class);
if (item == null) {
return;
}
boolean notify = intent.getBooleanExtra(EXTRA_NOTIFY, true);
try {
ProfileManager profileManager = new ProfileManager(mProfileId);
profileManager.applyProfile(state, mProgressHandler);
ProfileManager profileManager = new ProfileManager(item.getProfileId());
profileManager.applyProfile(item.getState(), mProgressHandler);
profileManager.conclude();
sendNotification(Activity.RESULT_OK, profileManager.requiresRestart());
sendNotification(item.getProfileName(), Activity.RESULT_OK, notify, profileManager.requiresRestart());
} catch (IOException e) {
sendNotification(Activity.RESULT_CANCELED, false);
sendNotification(item.getProfileName(), Activity.RESULT_CANCELED, notify, false);
}
}

@Override
protected void onQueued(@Nullable Intent intent) {
if (intent == null) return;
String profileName = intent.getStringExtra(EXTRA_PROFILE_NAME);
ProfileQueueItem item = IntentCompat.getParcelableExtra(intent, EXTRA_QUEUE_ITEM, ProfileQueueItem.class);
if (item == null) {
return;
}
Object notificationInfo = new NotificationProgressHandler.NotificationInfo()
.setAutoCancel(true)
.setTime(System.currentTimeMillis())
.setOperationName(getText(R.string.profiles))
.setTitle(profileName)
.setTitle(item.getProfileName())
.setBody(getString(R.string.added_to_queue));
mProgressHandler.onQueue(notificationInfo);
}

@Override
protected void onStartIntent(@Nullable Intent intent) {
if (intent == null) return;
mProfileId = intent.getStringExtra(EXTRA_PROFILE_ID);
mProfileName = intent.getStringExtra(EXTRA_PROFILE_NAME);
if (mProfileId != null) {
Intent notificationIntent = AppsProfileActivity.getProfileIntent(this, mProfileId);
ProfileQueueItem item = IntentCompat.getParcelableExtra(intent, EXTRA_QUEUE_ITEM, ProfileQueueItem.class);
if (item != null) {
Intent notificationIntent = AppsProfileActivity.getProfileIntent(this, item.getProfileId());
PendingIntent pendingIntent = PendingIntentCompat.getActivity(this, 0, notificationIntent,
0, false);
mNotificationInfo.setDefaultAction(pendingIntent);
}
// Set app name in the ongoing notification
mNotificationInfo.setTitle(mProfileName);
// Set profile name in the ongoing notification
mNotificationInfo.setTitle(item != null ? item.getProfileName() : null);
mProgressHandler.onProgressStart(-1, 0, mNotificationInfo);
}

Expand All @@ -134,13 +127,14 @@ public void onDestroy() {
super.onDestroy();
}

private void sendNotification(int result, boolean requiresRestart) {
private void sendNotification(@NonNull String profileName, int result, boolean notify,
boolean requiresRestart) {
NotificationProgressHandler.NotificationInfo notificationInfo = new NotificationProgressHandler
.NotificationInfo()
.setAutoCancel(true)
.setTime(System.currentTimeMillis())
.setOperationName(getText(R.string.profiles))
.setTitle(mProfileName);
.setTitle(profileName);
switch (result) {
case Activity.RESULT_CANCELED: // Failure
notificationInfo.setBody(getString(R.string.error));
Expand All @@ -155,6 +149,6 @@ private void sendNotification(int result, boolean requiresRestart) {
PendingIntent.FLAG_ONE_SHOT, false);
notificationInfo.addAction(0, getString(R.string.restart_device), pendingIntent);
}
mProgressHandler.onResult(mNotify ? notificationInfo : null);
mProgressHandler.onResult(notify ? notificationInfo : null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// SPDX-License-Identifier: GPL-3.0-or-later

package io.github.muntashirakon.AppManager.profiles;

import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Objects;

import io.github.muntashirakon.AppManager.profiles.ProfileApplierActivity.ProfileApplierInfo;
import io.github.muntashirakon.AppManager.profiles.struct.AppsProfile;

public class ProfileQueueItem implements Parcelable {
@NonNull
public static ProfileQueueItem fromProfiledApplierInfo(@NonNull ProfileApplierInfo info) {
return new ProfileQueueItem(info.profile, info.state);
}

@NonNull
private final String mProfileId;
@NonNull
private final String mProfileName;
@Nullable
private final String mState;

private ProfileQueueItem(@NonNull AppsProfile profile, @Nullable String state) {
mProfileId = profile.profileId;
mProfileName = profile.name;
mState = state;
}

protected ProfileQueueItem(@NonNull Parcel in) {
mProfileId = Objects.requireNonNull(in.readString());
mProfileName = Objects.requireNonNull(in.readString());
mState = in.readString();
}

@NonNull
public String getProfileId() {
return mProfileId;
}

@NonNull
public String getProfileName() {
return mProfileName;
}

@Nullable
public String getState() {
return mState;
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeString(mProfileId);
dest.writeString(mProfileName);
dest.writeString(mState);
}

public static final Creator<ProfileQueueItem> CREATOR = new Creator<ProfileQueueItem>() {
@NonNull
@Override
public ProfileQueueItem createFromParcel(@NonNull Parcel in) {
return new ProfileQueueItem(in);
}

@NonNull
@Override
public ProfileQueueItem[] newArray(int size) {
return new ProfileQueueItem[size];
}
};
}

0 comments on commit 7f9eed9

Please sign in to comment.