Skip to content

Commit

Permalink
Fix potential null exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
agnostic-apollo committed Mar 9, 2021
1 parent 324a69f commit dbf8477
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
27 changes: 19 additions & 8 deletions app/src/main/java/com/termux/app/TermuxActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ public final class TermuxActivity extends Activity implements ServiceConnection
private static final String BROADCAST_TERMUX_OPENED = TermuxConstants.TERMUX_PACKAGE_NAME + ".app.OPENED";

/** The main view of the activity showing the terminal. Initialized in onCreate(). */
@SuppressWarnings("NullableProblems")
@NonNull
TerminalView mTerminalView;

ExtraKeysView mExtraKeysView;
Expand Down Expand Up @@ -366,7 +364,7 @@ void sendOpenedBroadcast() {
for (ResolveInfo info : matches) {
Intent explicitBroadcast = new Intent(broadcast);
ComponentName cname = new ComponentName(info.activityInfo.applicationInfo.packageName,
info.activityInfo.name);
info.activityInfo.name);
explicitBroadcast.setComponent(cname);
sendBroadcast(explicitBroadcast);
}
Expand Down Expand Up @@ -488,7 +486,10 @@ public View getView(int position, View convertView, @NonNull ViewGroup parent) {
}

TerminalSession sessionAtRow = getItem(position);
boolean sessionRunning = sessionAtRow.isRunning();
if (sessionAtRow == null) return row;

boolean sessionRunning = false;
sessionRunning = sessionAtRow.isRunning();

TextView firstLineView = row.findViewById(R.id.row_line);
if (mIsUsingBlackUI) {
Expand Down Expand Up @@ -577,6 +578,7 @@ public void switchToSession(boolean forward) {

@SuppressLint("InflateParams")
void renameSession(final TerminalSession sessionToRename) {
if (sessionToRename == null) return;
DialogUtils.textInput(this, R.string.session_rename_title, sessionToRename.mSessionName, R.string.session_rename_positive_button, text -> {
sessionToRename.mSessionName = text;
mListViewAdapter.notifyDataSetChanged();
Expand Down Expand Up @@ -629,6 +631,7 @@ protected void onStop() {
getDrawer().closeDrawers();
}

@SuppressLint("RtlHardcoded")
@Override
public void onBackPressed() {
if (getDrawer().isDrawerOpen(Gravity.LEFT)) {
Expand Down Expand Up @@ -825,7 +828,10 @@ static LinkedHashSet<CharSequence> extractUrls(String text) {
}

void showUrlSelection() {
String text = getCurrentTermSession().getEmulator().getScreen().getTranscriptTextWithFullLinesJoined();
String text = null;
if (getCurrentTermSession() != null) {
text = getCurrentTermSession().getEmulator().getScreen().getTranscriptTextWithFullLinesJoined();
}
LinkedHashSet<CharSequence> urlSet = extractUrls(text);
if (urlSet.isEmpty()) {
new AlertDialog.Builder(this).setMessage(R.string.select_url_no_found).show();
Expand Down Expand Up @@ -896,11 +902,14 @@ public boolean onContextItemSelected(MenuItem item) {
return true;
case CONTEXTMENU_KILL_PROCESS_ID:
final AlertDialog.Builder b = new AlertDialog.Builder(this);
final TerminalSession terminalSession = getCurrentTermSession();
if (terminalSession == null) return true;

b.setIcon(android.R.drawable.ic_dialog_alert);
b.setMessage(R.string.confirm_kill_process);
b.setPositiveButton(android.R.string.yes, (dialog, id) -> {
dialog.dismiss();
getCurrentTermSession().finishIfRunning();
terminalSession.finishIfRunning();
});
b.setNegativeButton(android.R.string.no, null);
b.show();
Expand Down Expand Up @@ -952,7 +961,7 @@ public boolean onContextItemSelected(MenuItem item) {
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUESTCODE_PERMISSION_STORAGE && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
TermuxInstaller.setupStorageSymlinks(this);
}
Expand All @@ -969,7 +978,9 @@ void doPaste() {
if (clipData == null) return;
CharSequence paste = clipData.getItemAt(0).coerceToText(this);
if (!TextUtils.isEmpty(paste))
getCurrentTermSession().getEmulator().paste(paste.toString());
if (getCurrentTermSession() != null) {
getCurrentTermSession().getEmulator().paste(paste.toString());
}
}

/** The current session as stored or the last one if that does not exist. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public abstract class TerminalOutput {

/** Write a string using the UTF-8 encoding to the terminal client. */
public final void write(String data) {
if (data == null) return;
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
write(bytes, 0, bytes.length);
}
Expand Down

0 comments on commit dbf8477

Please sign in to comment.