|
| 1 | +package io.neoterm.bridge; |
| 2 | + |
| 3 | +import android.content.ComponentName; |
| 4 | +import android.content.Intent; |
| 5 | + |
| 6 | +import java.util.Objects; |
| 7 | + |
| 8 | +/** |
| 9 | + * @author kiva |
| 10 | + */ |
| 11 | +public class Bridge { |
| 12 | + public static final String ACTION_EXECUTE = "neoterm.action.remote.execute"; |
| 13 | + public static final String ACTION_SILENT_RUN = "neoterm.action.remote.silent-run"; |
| 14 | + public static final String EXTRA_COMMAND = "neoterm.extra.remote.execute.command"; |
| 15 | + public static final String EXTRA_SESSION_ID = "neoterm.extra.remote.execute.session"; |
| 16 | + public static final String EXTRA_FOREGROUND = "neoterm.extra.remote.execute.foreground"; |
| 17 | + private static final String NEOTERM_PACKAGE = "io.neoterm"; |
| 18 | + private static final String NEOTERM_REMOTE_INTERFACE = "io.neoterm.ui.term.NeoTermRemoteInterface"; |
| 19 | + private static final ComponentName NEOTERM_COMPONENT = new ComponentName(NEOTERM_PACKAGE, NEOTERM_REMOTE_INTERFACE); |
| 20 | + |
| 21 | + private Bridge() throws IllegalAccessException { |
| 22 | + throw new IllegalAccessException(); |
| 23 | + } |
| 24 | + |
| 25 | + public static Intent createExecuteIntent(SessionId sessionId, |
| 26 | + String command, |
| 27 | + boolean foreground) { |
| 28 | + Objects.requireNonNull(command, "command"); |
| 29 | + Objects.requireNonNull(sessionId, "session id"); |
| 30 | + |
| 31 | + Intent intent = new Intent(ACTION_EXECUTE); |
| 32 | + intent.setComponent(NEOTERM_COMPONENT); |
| 33 | + intent.putExtra(EXTRA_COMMAND, command); |
| 34 | + intent.putExtra(EXTRA_SESSION_ID, sessionId.getSessionId()); |
| 35 | + intent.putExtra(EXTRA_FOREGROUND, foreground); |
| 36 | + return intent; |
| 37 | + } |
| 38 | + |
| 39 | + public static Intent createExecuteIntent(SessionId sessionId, String command) { |
| 40 | + return createExecuteIntent(sessionId, command, true); |
| 41 | + } |
| 42 | + |
| 43 | + public static Intent createExecuteIntent(String command) { |
| 44 | + return createExecuteIntent(SessionId.NEW_SESSION, command); |
| 45 | + } |
| 46 | + |
| 47 | + public static Intent createExecuteIntent(String command, boolean foreground) { |
| 48 | + return createExecuteIntent(SessionId.NEW_SESSION, command, foreground); |
| 49 | + } |
| 50 | + |
| 51 | + public static SessionId parseResult(Intent data) { |
| 52 | + Objects.requireNonNull(data, "data"); |
| 53 | + |
| 54 | + if (data.hasExtra(EXTRA_SESSION_ID)) { |
| 55 | + String handle = data.getStringExtra(EXTRA_SESSION_ID); |
| 56 | + return SessionId.of(handle); |
| 57 | + } |
| 58 | + return null; |
| 59 | + } |
| 60 | +} |
0 commit comments