Skip to content

Commit

Permalink
Speculative fixes for keyboard crashes. Fixes #1821
Browse files Browse the repository at this point in the history
  • Loading branch information
bluemarvin committed Sep 14, 2019
1 parent 6090cf4 commit cfd18f2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Expand Up @@ -114,7 +114,7 @@ private int getParentFieldInt(Object obj, String fieldName) {
Field mField = getField(obj.getClass().getSuperclass(), fieldName);
mField.setAccessible(true);
return mField.getInt(obj);
} catch (IllegalAccessException e) {
} catch (IllegalAccessException | RuntimeException e) {
e.printStackTrace();
return 0;
}
Expand All @@ -125,7 +125,7 @@ private Object getFieldObject(Object obj, String fieldName) {
Field mField = getField(obj.getClass(), fieldName);
mField.setAccessible(true);
return mField.get(obj);
} catch (IllegalAccessException e) {
} catch (IllegalAccessException | RuntimeException e) {
e.printStackTrace();
return null;
}
Expand All @@ -135,7 +135,7 @@ private Object getParentFieldObject(Object obj, String fieldName) {
Field mField = getField(obj.getClass().getSuperclass(), fieldName);
mField.setAccessible(true);
return mField.get(this);
} catch (IllegalAccessException e) {
} catch (IllegalAccessException| RuntimeException e) {
e.printStackTrace();
return null;
}
Expand All @@ -157,14 +157,15 @@ public static Field getField(Class<?> clazz, String fieldName) {
}

public static void setParentField(Object obj, String fieldName, Object value) {
if (obj.getClass().getSuperclass() == null) {
return;
}
try {
Field privateField = obj.getClass().getSuperclass().getDeclaredField(fieldName);
privateField.setAccessible(true);
privateField.set(obj, value);

} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
Expand Down
Expand Up @@ -19,6 +19,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.ExtractedText;
import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.InputConnection;
import android.widget.ImageButton;
Expand Down Expand Up @@ -799,7 +800,12 @@ private String getTextBeforeCursor(InputConnection aConnection) {
return "";
}

String fullText = aConnection.getExtractedText(new ExtractedTextRequest(),0).text.toString();
ExtractedText extracted = aConnection.getExtractedText(new ExtractedTextRequest(),0);
if ((extracted == null) || extracted.text == null) {
return "";
}

String fullText = extracted.text.toString();
return aConnection.getTextBeforeCursor(fullText.length(),0).toString();
}

Expand Down

1 comment on commit cfd18f2

@daoshengmu
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the crash happens at ExtractedText extracted = aConnection.getExtractedText(new ExtractedTextRequest(),0); when there is a java.lang.NullPointerException. It looks good to me to do these string check to avoid null ptr access.

Please sign in to comment.