Skip to content

Commit

Permalink
Fixed: Fix potential conflicting PendingIntent for execution commands…
Browse files Browse the repository at this point in the history
… sent to TermuxService due to same request code being used
  • Loading branch information
agnostic-apollo committed Sep 5, 2021
1 parent 705361e commit d9a172d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ dependencies {
implementation 'com.google.android.material:material:1.4.0'
implementation 'com.google.guava:guava:24.1-jre'

implementation 'com.termux.termux-app:termux-shared:f00738fe3a'
implementation 'com.termux.termux-app:termux-shared:ac32fbc53d'

// Use if below libraries are published locally by termux-app with `./gradlew publishReleasePublicationToMavenLocal` and used with `mavenLocal()`.
// If updates are done, republish there and sync project with gradle files here
Expand Down
29 changes: 28 additions & 1 deletion app/src/main/java/com/termux/tasker/utils/PluginUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import com.termux.shared.models.ResultConfig;
import com.termux.shared.models.ResultData;
import com.termux.shared.models.errors.Errno;
import com.termux.shared.settings.preferences.TermuxPreferenceConstants.TERMUX_TASKER_APP;
import com.termux.shared.settings.preferences.TermuxTaskerAppSharedPreferences;
import com.termux.shared.shell.ResultSender;
import com.termux.tasker.FireReceiver;
import com.termux.tasker.PluginResultsService;
Expand Down Expand Up @@ -156,7 +158,7 @@ public static void sendExecuteIntentToExecuteService(final Context context, fina
pluginResultsServiceIntent.putExtra(EXTRA_ORIGINAL_INTENT, originalIntent);

// Create PendingIntent that can be used by execution service to send result of commands back to PluginResultsService
PendingIntent pendingIntent = PendingIntent.getService(context, 1, pluginResultsServiceIntent, PendingIntent.FLAG_ONE_SHOT);
PendingIntent pendingIntent = PendingIntent.getService(context, getLastPendingIntentRequestCode(context), pluginResultsServiceIntent, PendingIntent.FLAG_ONE_SHOT);
executionIntent.putExtra(TERMUX_SERVICE.EXTRA_PENDING_INTENT, pendingIntent);
} else {
// If execute in background is not enabled, do not expect results back from execution service and return result now so that plugin action does not timeout
Expand Down Expand Up @@ -450,4 +452,29 @@ public static String checkIfTermuxTaskerAllowExternalAppsPolicyIsViolated(final
return errmsg;
}

/**
* Try to get the next unique {@link PendingIntent} request code that isn't already being used by
* the app and which would create a unique {@link PendingIntent} that doesn't conflict with that
* of any other execution commands.
*
* @param context The {@link Context} for operations.
* @return Returns the request code that should be safe to use.
*/
public synchronized static int getLastPendingIntentRequestCode(final Context context) {
if (context == null) return TERMUX_TASKER_APP.DEFAULT_VALUE_KEY_LAST_PENDING_INTENT_REQUEST_CODE;

TermuxTaskerAppSharedPreferences preferences = TermuxTaskerAppSharedPreferences.build(context);
if (preferences == null) return TERMUX_TASKER_APP.DEFAULT_VALUE_KEY_LAST_PENDING_INTENT_REQUEST_CODE;

int lastPendingIntentRequestCode = preferences.getLastPendingIntentRequestCode();

int nextPendingIntentRequestCode = lastPendingIntentRequestCode + 1;

if (nextPendingIntentRequestCode == Integer.MAX_VALUE || nextPendingIntentRequestCode < 0)
nextPendingIntentRequestCode = TERMUX_TASKER_APP.DEFAULT_VALUE_KEY_LAST_PENDING_INTENT_REQUEST_CODE;

preferences.setLastPendingIntentRequestCode(nextPendingIntentRequestCode);
return nextPendingIntentRequestCode;
}

}

0 comments on commit d9a172d

Please sign in to comment.