Skip to content
This repository was archived by the owner on Jan 14, 2022. It is now read-only.

Commit e68b375

Browse files
committed
Merge branch 'session-cleanup' of github.com:Alexander--/Android-Terminal-Emulator into Alexander---session-cleanup
2 parents 6a6e116 + 8a8c017 commit e68b375

File tree

11 files changed

+260
-237
lines changed

11 files changed

+260
-237
lines changed

build.gradle

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2+
task wrapper (type:Wrapper) {
3+
gradleVersion = '2.2.1'
4+
distributionUrl = 'https://services.gradle.org/distributions/gradle-2.2.1-all.zip'
5+
}
6+
27
buildscript {
38
repositories {
49
jcenter()
10+
mavenCentral()
511
}
612
dependencies {
713
classpath 'com.android.tools.build:gradle:1.0.0'
14+
classpath 'com.jakewharton.sdkmanager:gradle-plugin:0.12.0'
815
}
916
}
1017

@@ -13,3 +20,12 @@ allprojects {
1320
jcenter()
1421
}
1522
}
23+
24+
subprojects {
25+
def androidHome
26+
27+
if ((androidHome = System.env.'ANDROID_HOME')
28+
&& (androidHome = androidHome as File).exists()
29+
&& androidHome.canWrite())
30+
apply plugin: 'android-sdk-manager'
31+
}

term/src/main/java/jackpal/androidterm/BoundSession.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import android.text.TextUtils;
55
import jackpal.androidterm.util.TermSettings;
66

7-
public class BoundSession extends GenericTermSession {
7+
class BoundSession extends GenericTermSession {
88
private final String issuerTitle;
99

10+
private boolean fullyInitialized;
11+
1012
BoundSession(ParcelFileDescriptor ptmxFd, TermSettings settings, String issuerTitle) {
1113
super(ptmxFd, settings, true);
1214

@@ -24,4 +26,16 @@ public String getTitle() {
2426
? issuerTitle
2527
: issuerTitle + " — " + extraTitle;
2628
}
29+
30+
@Override
31+
public void initializeEmulator(int columns, int rows) {
32+
super.initializeEmulator(columns, rows);
33+
34+
fullyInitialized = true;
35+
}
36+
37+
@Override
38+
boolean isFailFast() {
39+
return !fullyInitialized;
40+
}
2741
}

term/src/main/java/jackpal/androidterm/Exec.java

Lines changed: 2 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -36,88 +36,8 @@ public class Exec
3636
System.loadLibrary("jackpal-androidterm5");
3737
}
3838

39-
private static Field descriptorField;
39+
static native void setPtyWindowSizeInternal(int fd, int row, int col, int xpixel, int ypixel) throws IOException;
4040

41-
private static void cacheDescField() throws NoSuchFieldException {
42-
if (descriptorField != null)
43-
return;
44-
45-
descriptorField = FileDescriptor.class.getDeclaredField("descriptor");
46-
descriptorField.setAccessible(true);
47-
}
48-
49-
private static int getIntFd(ParcelFileDescriptor parcelFd) throws IOException {
50-
if (Build.VERSION.SDK_INT >= 12)
51-
return FdHelperHoneycomb.getFd(parcelFd);
52-
else {
53-
try {
54-
cacheDescField();
55-
56-
return descriptorField.getInt(parcelFd.getFileDescriptor());
57-
} catch (Exception e) {
58-
throw new IOException("Unable to obtain file descriptor on this OS version: " + e.getMessage());
59-
}
60-
}
61-
}
62-
63-
/**
64-
* Set the widow size for a given pty. Allows programs
65-
* connected to the pty learn how large their screen is.
66-
*/
67-
public static void setPtyWindowSize(ParcelFileDescriptor fd, int row, int col, int xpixel, int ypixel) {
68-
// If the tty goes away too quickly, this may get called after it's descriptor is closed
69-
if (!fd.getFileDescriptor().valid())
70-
return;
71-
72-
try {
73-
setPtyWindowSizeInternal(getIntFd(fd), row, col, xpixel, ypixel);
74-
} catch (IOException e) {
75-
// pretend that everything is ok...
76-
Log.e("exec", "Failed to set window size due to " + e.getMessage());
77-
}
78-
}
79-
80-
/**
81-
* Set or clear UTF-8 mode for a given pty. Used by the terminal driver
82-
* to implement correct erase behavior in cooked mode (Linux >= 2.6.4).
83-
*/
84-
public static void setPtyUTF8Mode(ParcelFileDescriptor fd, boolean utf8Mode) {
85-
// If the tty goes away too quickly, this may get called after it's descriptor is closed
86-
if (!fd.getFileDescriptor().valid())
87-
return;
88-
89-
try {
90-
setPtyUTF8ModeInternal(getIntFd(fd), utf8Mode);
91-
} catch (IOException e) {
92-
// pretend that everything is ok...
93-
Log.e("exec", "Failed to set UTF mode due to " + e.getMessage());
94-
}
95-
}
96-
97-
/**
98-
* Close a given file descriptor.
99-
*/
100-
public static void close(ParcelFileDescriptor fd) {
101-
try {
102-
fd.close();
103-
} catch (IOException e) {
104-
// ok
105-
}
106-
}
107-
108-
/**
109-
* Send SIGHUP to a process group, SIGHUP notifies a terminal client, that the terminal have been disconnected,
110-
* and usually results in client's death, unless it's process is a daemon or have been somehow else detached
111-
* from the terminal (for example, by the "nohup" utility).
112-
*/
113-
public static void hangupProcessGroup(int processId) {
114-
TermExec.sendSignal(-processId, 1);
115-
}
116-
117-
private static native void setPtyWindowSizeInternal(int fd, int row, int col, int xpixel, int ypixel)
118-
throws IOException;
119-
120-
private static native void setPtyUTF8ModeInternal(int fd, boolean utf8Mode)
121-
throws IOException;
41+
static native void setPtyUTF8ModeInternal(int fd, boolean utf8Mode) throws IOException;
12242
}
12343

term/src/main/java/jackpal/androidterm/GenericTermSession.java

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package jackpal.androidterm;
1818

1919
import java.io.*;
20+
import java.lang.reflect.Field;
2021
import java.util.ArrayList;
2122

23+
import android.os.Build;
2224
import android.os.Handler;
2325
import android.os.Message;
2426
import android.os.ParcelFileDescriptor;
@@ -35,10 +37,14 @@
3537
* A terminal session, consisting of a TerminalEmulator, a TranscriptScreen,
3638
* and the I/O streams used to talk to the process.
3739
*/
38-
public class GenericTermSession extends TermSession {
40+
class GenericTermSession extends TermSession {
3941
//** Set to true to force into 80 x 24 for testing with vttest. */
4042
private static final boolean VTTEST_MODE = false;
4143

44+
private static Field descriptorField;
45+
46+
private final long createdAt;
47+
4248
// A cookie which uniquely identifies this session.
4349
private String mHandle;
4450

@@ -53,7 +59,7 @@ public class GenericTermSession extends TermSession {
5359

5460
private UpdateCallback mUTF8ModeNotify = new UpdateCallback() {
5561
public void onUpdate() {
56-
Exec.setPtyUTF8Mode(mTermFd, getUTF8Mode());
62+
setPtyUTF8Mode(getUTF8Mode());
5763
}
5864
};
5965

@@ -62,6 +68,8 @@ public void onUpdate() {
6268

6369
this.mTermFd = mTermFd;
6470

71+
this.createdAt = System.currentTimeMillis();
72+
6573
updatePrefs(settings);
6674
}
6775

@@ -79,7 +87,7 @@ public void initializeEmulator(int columns, int rows) {
7987
}
8088
super.initializeEmulator(columns, rows);
8189

82-
Exec.setPtyUTF8Mode(mTermFd, getUTF8Mode());
90+
setPtyUTF8Mode(getUTF8Mode());
8391
setUTF8ModeUpdateCallback(mUTF8ModeNotify);
8492
}
8593

@@ -90,7 +98,7 @@ public void updateSize(int columns, int rows) {
9098
rows = 24;
9199
}
92100
// Inform the attached pty of our new size:
93-
Exec.setPtyWindowSize(mTermFd, rows, columns, 0, 0);
101+
setPtyWindowSize(rows, columns, 0, 0);
94102
super.updateSize(columns, rows);
95103
}
96104

@@ -117,7 +125,12 @@ protected void onProcessExit() {
117125

118126
@Override
119127
public void finish() {
120-
Exec.close(mTermFd);
128+
try {
129+
mTermFd.close();
130+
} catch (IOException e) {
131+
// ok
132+
}
133+
121134
super.finish();
122135
}
123136

@@ -130,7 +143,7 @@ public void finish() {
130143
* unset or an empty string.
131144
*/
132145
public String getTitle(String defaultTitle) {
133-
String title = super.getTitle();
146+
String title = getTitle();
134147
if (title != null && title.length() > 0) {
135148
return title;
136149
} else {
@@ -148,4 +161,76 @@ public void setHandle(String handle) {
148161
public String getHandle() {
149162
return mHandle;
150163
}
164+
165+
@Override
166+
public String toString() {
167+
return getClass().getSimpleName() + '(' + createdAt + ',' + mHandle + ')';
168+
}
169+
170+
/**
171+
* Set the widow size for a given pty. Allows programs
172+
* connected to the pty learn how large their screen is.
173+
*/
174+
void setPtyWindowSize(int row, int col, int xpixel, int ypixel) {
175+
// If the tty goes away too quickly, this may get called after it's descriptor is closed
176+
if (!mTermFd.getFileDescriptor().valid())
177+
return;
178+
179+
try {
180+
Exec.setPtyWindowSizeInternal(getIntFd(mTermFd), row, col, xpixel, ypixel);
181+
} catch (IOException e) {
182+
Log.e("exec", "Failed to set window size: " + e.getMessage());
183+
184+
if (isFailFast())
185+
throw new IllegalStateException(e);
186+
}
187+
}
188+
189+
/**
190+
* Set or clear UTF-8 mode for a given pty. Used by the terminal driver
191+
* to implement correct erase behavior in cooked mode (Linux >= 2.6.4).
192+
*/
193+
void setPtyUTF8Mode(boolean utf8Mode) {
194+
// If the tty goes away too quickly, this may get called after it's descriptor is closed
195+
if (!mTermFd.getFileDescriptor().valid())
196+
return;
197+
198+
try {
199+
Exec.setPtyUTF8ModeInternal(getIntFd(mTermFd), utf8Mode);
200+
} catch (IOException e) {
201+
Log.e("exec", "Failed to set UTF mode: " + e.getMessage());
202+
203+
if (isFailFast())
204+
throw new IllegalStateException(e);
205+
}
206+
}
207+
208+
/**
209+
* @return true, if failing to operate on file descriptor deserves an exception (never the case for ATE own shell)
210+
*/
211+
boolean isFailFast() {
212+
return false;
213+
}
214+
215+
private static void cacheDescField() throws NoSuchFieldException {
216+
if (descriptorField != null)
217+
return;
218+
219+
descriptorField = FileDescriptor.class.getDeclaredField("descriptor");
220+
descriptorField.setAccessible(true);
221+
}
222+
223+
private static int getIntFd(ParcelFileDescriptor parcelFd) throws IOException {
224+
if (Build.VERSION.SDK_INT >= 12)
225+
return FdHelperHoneycomb.getFd(parcelFd);
226+
else {
227+
try {
228+
cacheDescField();
229+
230+
return descriptorField.getInt(parcelFd.getFileDescriptor());
231+
} catch (Exception e) {
232+
throw new IOException("Unable to obtain file descriptor on this OS version: " + e.getMessage());
233+
}
234+
}
235+
}
151236
}

term/src/main/java/jackpal/androidterm/ShellTermSession.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,16 @@ private void onProcessExit(int result) {
207207

208208
@Override
209209
public void finish() {
210-
Exec.hangupProcessGroup(mProcId);
210+
hangupProcessGroup();
211211
super.finish();
212212
}
213+
214+
/**
215+
* Send SIGHUP to a process group, SIGHUP notifies a terminal client, that the terminal have been disconnected,
216+
* and usually results in client's death, unless it's process is a daemon or have been somehow else detached
217+
* from the terminal (for example, by the "nohup" utility).
218+
*/
219+
void hangupProcessGroup() {
220+
TermExec.sendSignal(-mProcId, 1);
221+
}
213222
}

0 commit comments

Comments
 (0)