Android: Fix app usually crashing when typing text into servers search box

This commit is contained in:
UnknownShadow200 2023-03-05 14:59:54 +11:00
parent 73beb52acb
commit 069325fcf8
6 changed files with 107 additions and 83 deletions

View file

@ -5,10 +5,8 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
@ -28,7 +26,6 @@ import android.graphics.Color;
import android.database.Cursor;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.net.Uri;
@ -42,9 +39,7 @@ import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
@ -70,6 +65,7 @@ import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
@ -181,12 +177,12 @@ public class MainActivity extends Activity
final static int CMD_LOW_MEMORY = 18;
final static int CMD_KEY_TEXT = 19;
final static int CMD_OFD_RESULT = 20;
final static int CMD_UI_CREATED = 21;
final static int CMD_UI_CLICKED = 22;
final static int CMD_UI_CHANGED = 23;
final static int CMD_UI_STRING = 24;
// ====================================================================
// ------------------------------ EVENTS ------------------------------
// ====================================================================
@ -428,7 +424,7 @@ public class MainActivity extends Activity
//native void processOnConfigChanged();
native void processOnLowMemory();
native void processOFDResult(String path);
native void processOnUICreated();
native void processOnUIClicked(int id);
native void processOnUIChanged(int id, int val);
@ -539,7 +535,7 @@ public class MainActivity extends Activity
}
ViewGroup.LayoutParams makeLayoutParams(int xMode, int xOffset, int yMode, int yOffset,
int width, int height) {
int width, int height) {
return new CC2DLayoutParams(xMode, xOffset, yMode, yOffset, width, height);
}
@ -622,10 +618,10 @@ public class MainActivity extends Activity
int width, int height) {
final Button btn = new Button(this);
final ViewGroup.LayoutParams lp = makeLayoutParams(xMode, xOffset, yMode, yOffset,
width, height);
width, height);
buttonUpdateBackground(btn, width, height);
btn.setTextColor(Color.WHITE);
buttonUpdateBackground(btn, width, height);
btn.setTextColor(Color.WHITE);
btn.setPadding(btn.getPaddingLeft(), 0, btn.getPaddingRight(), 0);
btn.setTransformationMethod(null); // get rid of all caps
@ -684,7 +680,7 @@ public class MainActivity extends Activity
int labelAdd(int xMode, int xOffset, int yMode, int yOffset) {
final TextView lbl = new TextView(this);
final ViewGroup.LayoutParams lp = makeLayoutParams(xMode, xOffset, yMode, yOffset,
_WRAP_CONTENT, _WRAP_CONTENT);
_WRAP_CONTENT, _WRAP_CONTENT);
lbl.setTextColor(Color.WHITE);
return showWidgetAsync(lbl, lp, null);
@ -705,7 +701,7 @@ public class MainActivity extends Activity
int width, int height, int flags, String placeholder) {
final EditText ipt = new EditText(this);
final ViewGroup.LayoutParams lp = makeLayoutParams(xMode, xOffset, yMode, yOffset,
width, height);
width, height);
ipt.setBackgroundColor(Color.WHITE);
ipt.setPadding(ipt.getPaddingLeft(), 0, ipt.getPaddingRight(), 0);
ipt.setHint(placeholder);
@ -733,20 +729,20 @@ public class MainActivity extends Activity
}
int lineAdd(int xMode, int xOffset, int yMode, int yOffset,
int width, int height, int color) {
int width, int height, int color) {
final View view = new View(this);
final ViewGroup.LayoutParams lp = makeLayoutParams(xMode, xOffset, yMode, yOffset,
width, height);
width, height);
view.setBackgroundColor(color);
return showWidgetAsync(view, lp, null);
}
int checkboxAdd(int xMode, int xOffset, int yMode, int yOffset,
String title, final boolean checked) {
String title, final boolean checked) {
final CheckBox cb = new CheckBox(this);
final CC2DLayoutParams lp = new CC2DLayoutParams(xMode, xOffset, yMode, yOffset,
_WRAP_CONTENT, _WRAP_CONTENT);
_WRAP_CONTENT, _WRAP_CONTENT);
cb.setText(title);
cb.setTextColor(Color.WHITE);
@ -776,10 +772,10 @@ public class MainActivity extends Activity
}
int sliderAdd(int xMode, int xOffset, int yMode, int yOffset,
int width, int height, int color) {
int width, int height, int color) {
final ProgressBar prg = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
final ViewGroup.LayoutParams lp = makeLayoutParams(xMode, xOffset, yMode, yOffset,
width, height);
width, height);
// https://stackoverflow.com/questions/39771796/change-horizontal-progress-bar-color
prg.getProgressDrawable().setColorFilter(color, android.graphics.PorterDuff.Mode.SRC_IN);
@ -799,7 +795,7 @@ public class MainActivity extends Activity
int color) {
final ListView list = new ListView(this);
final ViewGroup.LayoutParams lp = makeLayoutParams(xMode, xOffset, yMode, yOffset,
_MATCH_PARENT, _WRAP_CONTENT); //_MATCH_PARENT
_MATCH_PARENT, _WRAP_CONTENT); //_MATCH_PARENT
//list.setBackgroundColor(color);
/*list.setScrollIndicators(
View.SCROLL_INDICATOR_TOP |
@ -818,23 +814,50 @@ public class MainActivity extends Activity
});
}
native static int tableGetCount(int id);
native static String tableGetTitle(int id, int pos);
native static String tableGetDetails(int id, int pos);
native static int tableGetColor(int id, int pos, boolean selected);
native static int tableGetColor(int pos, boolean selected, boolean featured);
class TableEntry { public String title, details; public boolean featured; }
static ArrayList<TableEntry> table_entries = new ArrayList<TableEntry>();
void tableStartUpdate() {
table_entries.clear();
}
void tableAddEntry(String name, String details, boolean featured) {
TableEntry e = new TableEntry();
e.title = name;
e.details = details;
e.featured = featured;
table_entries.add(e);
}
void tableFinishUpdate(final int id) {
final Object[] entries = table_entries.toArray();
runOnUiThread(new Runnable() {
public void run() {
View view = findViewById(id);
if (view == null) return;
ListAdapter adapter = ((ListView)view).getAdapter();
((CCTableAdapter)adapter).entries = entries;
((CCTableAdapter)adapter).notifyDataSetChanged();
}
});
}
class CCTableAdapter extends BaseAdapter
{
public Object[] entries = new Object[0];
Context ctx;
int listID;
public CCTableAdapter(Context context, int id) {
ctx = context;
ctx = context;
listID = id;
}
@Override
public int getCount() { return tableGetCount(listID); }
public int getCount() { return entries.length; }
@Override
public String getItem(int position) { return ""; }
@ -854,20 +877,22 @@ public class MainActivity extends Activity
LinearLayout root = (LinearLayout)convertView;
LinearLayout text = (LinearLayout)root.getChildAt(1);
TextView title = (TextView)text.getChildAt(0);
TextView title = (TextView)text.getChildAt(0);
TextView details = (TextView)text.getChildAt(1);
title.setText(tableGetTitle(listID, position));
details.setText(tableGetDetails(listID, position));
TableEntry entry = (TableEntry)entries[position];
title.setText(entry.title);
details.setText(entry.details);
convertView.setBackgroundColor(tableGetColor(listID, position, false));
int color = tableGetColor(position, false, entry.featured);
convertView.setBackgroundColor(color);
return convertView;
}
View createRow(int position) {
ImageView image = new ImageView(ctx);
LinearLayout.LayoutParams imageLP = new LinearLayout.LayoutParams(
Pixels(FLAG_WIDTH), Pixels(FLAG_HEIGHT));
Pixels(FLAG_WIDTH), Pixels(FLAG_HEIGHT));
imageLP.gravity = Gravity.CENTER;
Bitmap bmp = Bitmap.createBitmap(FLAG_WIDTH, FLAG_HEIGHT, Bitmap.Config.ARGB_8888);
@ -1115,7 +1140,7 @@ public class MainActivity extends Activity
// getCacheDir - API level 1
return getCacheDir().getAbsolutePath();
}
public String getUUID() {
// getContentResolver - API level 1
// getString, ANDROID_ID - API level 3
@ -1234,7 +1259,7 @@ public class MainActivity extends Activity
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.getAttributes().layoutInDisplayCutoutMode =
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
} catch (NoSuchFieldError ex) {
ex.printStackTrace();
} catch (NoSuchMethodError ex) {
@ -1289,14 +1314,14 @@ public class MainActivity extends Activity
runOnUiThread(new Runnable() {
public void run() { setUIVisibility(FULLSCREEN_FLAGS); }
});
}
}
public void exitFullscreen() {
public void exitFullscreen() {
fullscreen = false;
runOnUiThread(new Runnable() {
public void run() { setUIVisibility(View.SYSTEM_UI_FLAG_VISIBLE); }
});
}
}
public String shareScreenshot(String path) {
try {

View file

@ -887,9 +887,10 @@ static void LTable_DrawHeaderBackground(struct LTable* w) {
static BitmapCol LBackend_TableRowColor(struct LTable* w, int row) {
struct ServerInfo* entry = row < w->rowsCount ? LTable_Get(row) : NULL;
cc_bool featured = entry && entry->featured;
cc_bool selected = entry && String_Equals(&entry->hash, w->selectedHash);
return LTable_RowColor(entry, row, selected);
return LTable_RowColor(entry, selected, featured);
}
/* Draws background behind each row in the table */

View file

@ -455,14 +455,48 @@ static void LBackend_TableShow(struct LTable* w) {
jvalue args[5];
LBackend_GetLayoutArgs(w, args);
args[4].i = ToAndroidColor(LTable_RowColor(NULL, 1, false));
args[4].i = ToAndroidColor(LTable_RowColor(1, false, false));
jmethodID method = JavaGetIMethod(env, "tableAdd", "(IIIII)I");
w->meta = (void*)JavaICall_Int(env, method, args);
LBackend_TableUpdate(w);
}
static jstring GetTableDetails(JNIEnv* env, struct ServerInfo* server) {
char buffer[NATIVE_STR_LEN];
cc_string text = String_FromArray(buffer);
String_Format2(&text, "%i/%i players, up for ", &server->players, &server->maxPlayers);
LTable_FormatUptime(&text, server->uptime);
if (server->software.length) String_Format1(&text, " | %s", &server->software);
return JavaMakeString(env, &text);
}
void LBackend_TableUpdate(struct LTable* w) {
JNIEnv* env; JavaGetCurrentEnv(env);
jvalue args[3];
jmethodID method;
method = JavaGetIMethod(env, "tableStartUpdate", "()V");
JavaICall_Void(env, method, args);
method = JavaGetIMethod(env, "tableAddEntry", "(Ljava/lang/String;Ljava/lang/String;Z)V");
for (int i = 0; i < w->rowsCount; i++)
{
struct ServerInfo* info = LTable_Get(i);
args[0].l = JavaMakeString(env, &info->name);
args[1].l = GetTableDetails(env, info);
args[2].z = info->featured;
JavaICall_Void(env, method, args);
(*env)->DeleteLocalRef(env, args[0].l);
(*env)->DeleteLocalRef(env, args[1].l);
}
method = JavaGetIMethod(env, "tableFinishUpdate", "(I)V");
args[0].i = (int)w->meta;
JavaICall_Void(env, method, args);
}
void LBackend_TableReposition(struct LTable* w) {
@ -477,41 +511,8 @@ void LBackend_TableMouseDown(struct LTable* w, int idx) { }
void LBackend_TableMouseMove(struct LTable* w, int idx) { }
void LBackend_TableMouseUp(struct LTable* w, int idx) { }
static jint JNICALL java_tableGetCount(JNIEnv* env, jobject o, jint id) {
struct LTable* tbl = (struct LTable*)FindWidgetForView(id);
return tbl ? tbl->rowsCount : 0;
}
static jstring JNICALL java_tableGetTitle(JNIEnv* env, jobject o, jint id, jint row) {
char buffer[NATIVE_STR_LEN];
cc_string text = String_FromArray(buffer);
struct LTable* tbl = (struct LTable*)FindWidgetForView(id);
struct ServerInfo* info = tbl && row < tbl->rowsCount ? LTable_Get(row) : NULL;
if (info) {
String_AppendString(&text, &info->name);
}
return JavaMakeString(env, &text);
}
static jstring JNICALL java_tableGetDetails(JNIEnv* env, jobject o, jint id, jint row) {
char buffer[NATIVE_STR_LEN];
cc_string text = String_FromArray(buffer);
struct LTable* tbl = (struct LTable*)FindWidgetForView(id);
struct ServerInfo* info = tbl && row < tbl->rowsCount ? LTable_Get(row) : NULL;
if (info) {
String_Format2(&text, "%i/%i players, up for ", &info->players, &info->maxPlayers);
LTable_FormatUptime(&text, info->uptime);
}
return JavaMakeString(env, &text);
}
static jint JNICALL java_tableGetColor(JNIEnv* env, jobject o, jint id, jint row, jboolean selected) {
struct LTable* tbl = (struct LTable*)FindWidgetForView(id);
struct ServerInfo* info = tbl && row < tbl->rowsCount ? LTable_Get(row) : NULL;
return ToAndroidColor(LTable_RowColor(info, row, selected));
static jint JNICALL java_tableGetColor(JNIEnv* env, jobject o, jint row, jboolean selected, jboolean featured) {
return ToAndroidColor(LTable_RowColor(row, selected, featured));
}
@ -602,10 +603,7 @@ static const JNINativeMethod methods[] = {
{ "processOnUIClicked", "(I)V", java_UIClicked },
{ "processOnUIChanged", "(II)V", java_UIChanged },
{ "processOnUIString", "(ILjava/lang/String;)V", java_UIString },
{ "tableGetCount", "(I)I", java_tableGetCount },
{ "tableGetTitle", "(II)Ljava/lang/String;", java_tableGetTitle },
{ "tableGetDetails", "(II)Ljava/lang/String;", java_tableGetDetails },
{ "tableGetColor", "(IIZ)I", java_tableGetColor },
{ "tableGetColor", "(IZZ)I", java_tableGetColor },
};
static void LBackend_InitHooks(void) {

View file

@ -721,14 +721,14 @@ void LTable_ShowSelected(struct LTable* w) {
LTable_ClampTopRow(w);
}
BitmapCol LTable_RowColor(struct ServerInfo* entry, int row, cc_bool selected) {
BitmapCol LTable_RowColor(int row, cc_bool selected, cc_bool featured) {
BitmapCol featSelColor = BitmapColor_RGB( 50, 53, 0);
BitmapCol featuredColor = BitmapColor_RGB(101, 107, 0);
BitmapCol selectedColor = BitmapColor_RGB( 40, 40, 40);
if (entry && entry->featured) {
if (featured) {
return selected ? featSelColor : featuredColor;
} else if (entry && selected) {
} else if (selected) {
return selectedColor;
}

View file

@ -236,5 +236,5 @@ int LTable_GetSelectedIndex(struct LTable* w);
void LTable_SetSelectedTo(struct LTable* w, int index);
void LTable_RowClick(struct LTable* w, int row);
/* Works out the background color of the given row */
BitmapCol LTable_RowColor(struct ServerInfo* entry, int row, cc_bool selected);
BitmapCol LTable_RowColor(int row, cc_bool selected, cc_bool featured);
#endif

View file

@ -1557,7 +1557,7 @@ void LBackend_TableMouseUp(struct LTable* w, int idx) { }
void LBackend_TableMouseMove(struct LTable* w, int idx) { }
static void LTable_UpdateCellColor(UIView* view, struct ServerInfo* server, int row, cc_bool selected) {
BitmapCol color = LTable_RowColor(server, row, selected);
BitmapCol color = LTable_RowColor(row, selected, server->featured);
if (color) {
view.backgroundColor = ToUIColor(color, 1.0f);
view.opaque = YES;