[mobile] improve debug view

- ListView should be significantly faster thanks to the ViewHolder pattern
- ability to remove/copy items be long-pressing them
This commit is contained in:
fkloft 2014-08-13 11:41:42 +02:00
parent ad07f8685c
commit e3542dabbf
6 changed files with 185 additions and 82 deletions

View File

@ -1,51 +1,49 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="HardcodedText"> tools:ignore="HardcodedText">
<ImageView <LinearLayout
android:id="@+id/log_type" android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentLeft="true" android:orientation="horizontal">
android:layout_alignParentTop="true"
android:src="@drawable/ic_action_about"
tools:ignore="ContentDescription"/>
<TextView <ImageView
android:id="@+id/log_tag" android:id="@+id/log_type"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignBottom="@+id/log_type" android:src="@drawable/ic_action_about"
android:layout_alignParentTop="true" tools:ignore="ContentDescription"/>
android:layout_toRightOf="@+id/log_type"
android:gravity="center_vertical|left"
android:text="iitcm"
android:textAppearance="?android:attr/textAppearance"
android:typeface="monospace"/>
<TextView <TextView
android:id="@+id/log_time" android:id="@+id/log_tag"
android:layout_width="wrap_content" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignBottom="@+id/log_type" android:layout_gravity="center_vertical"
android:layout_alignParentRight="true" android:layout_weight="1"
android:layout_alignParentTop="true" android:gravity="center_vertical|left"
android:layout_toRightOf="@+id/log_tag" android:text="iitcm"
android:gravity="center_vertical|right" android:textAppearance="?android:attr/textAppearance"
android:text="13:37:42.000" android:typeface="monospace"/>
android:textAppearance="?android:attr/textAppearance"/>
<TextView
android:id="@+id/log_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical|right"
android:text="13:37:42.000"
android:textAppearance="?android:attr/textAppearance"/>
</LinearLayout>
<TextView <TextView
android:id="@+id/log_msg" android:id="@+id/log_msg"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/log_type"
android:text="iitcm://script/total-conversion-build.user.js:1337: Some random message" android:text="iitcm://script/total-conversion-build.user.js:1337: Some random message"
android:typeface="monospace"/> android:typeface="monospace"/>
</RelativeLayout> </LinearLayout>

13
mobile/res/menu/debug.xml Normal file
View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_copy"
android:icon="@drawable/ic_action_copy"
android:title="@string/menu_copy"/>
<item
android:id="@+id/menu_delete"
android:icon="@drawable/ic_action_discard"
android:title="@string/menu_delete"/>
</menu>

View File

@ -213,6 +213,8 @@
<string name="menu_debug">Debug</string> <string name="menu_debug">Debug</string>
<string name="menu_send_screenshot">Send screenshot</string> <string name="menu_send_screenshot">Send screenshot</string>
<string name="menu_plugins_add">Add external plugins</string> <string name="menu_plugins_add">Add external plugins</string>
<string name="menu_copy">Copy</string>
<string name="menu_delete">Delete</string>
<string name="choose_account_to_login">Choose account to login</string> <string name="choose_account_to_login">Choose account to login</string>
<string name="login_failed">Login failed.</string> <string name="login_failed">Login failed.</string>
<string name="search_hint">Search Locations</string> <string name="search_hint">Search Locations</string>

View File

@ -1,6 +1,5 @@
package com.cradle.iitc_mobile; package com.cradle.iitc_mobile;
import android.annotation.SuppressLint;
import android.database.DataSetObserver; import android.database.DataSetObserver;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -12,50 +11,52 @@ import com.cradle.iitc_mobile.Log.Message;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.text.SimpleDateFormat;
public class IITC_LogAdapter extends ArrayAdapter<Log.Message> implements Log.Receiver { public class IITC_LogAdapter extends ArrayAdapter<Log.Message> implements Log.Receiver {
@SuppressLint("SimpleDateFormat") private final IITC_Mobile mIitc;
private static final SimpleDateFormat FORMATTER = new SimpleDateFormat("HH:mm:ss.SSS");
private int mObservers = 0; private int mObservers = 0;
private IITC_Mobile mIitc;
public IITC_LogAdapter(IITC_Mobile iitc) { public IITC_LogAdapter(final IITC_Mobile iitc) {
super(iitc, 0); super(iitc, 0);
mIitc = iitc; mIitc = iitc;
} }
@Override @Override
public View getView(int position, View convertView, ViewGroup parent) { public View getView(final int position, View v, final ViewGroup parent) {
Message item = getItem(position); final Message item = getItem(position);
View v = mIitc.getLayoutInflater().inflate(R.layout.view_log_msg, parent, false);
ViewHolder holder;
if (v != null && v.getTag() != null && v.getTag() instanceof ViewHolder) {
holder = (ViewHolder) v.getTag();
} else {
v = mIitc.getLayoutInflater().inflate(R.layout.view_log_msg, parent, false);
holder = new ViewHolder();
holder.icon = (ImageView) v.findViewById(R.id.log_type);
holder.tag = (TextView) v.findViewById(R.id.log_tag);
holder.time = (TextView) v.findViewById(R.id.log_time);
holder.msg = (TextView) v.findViewById(R.id.log_msg);
v.setTag(holder);
}
ImageView iv = (ImageView) v.findViewById(R.id.log_type);
switch (item.getPriority()) { switch (item.getPriority()) {
case Log.ASSERT: case Log.ASSERT:
case Log.ERROR: case Log.ERROR:
iv.setImageResource(R.drawable.ic_action_error_red); holder.icon.setImageResource(R.drawable.ic_action_error_red);
break; break;
case Log.WARN: case Log.WARN:
iv.setImageResource(R.drawable.ic_action_warning_yellow); holder.icon.setImageResource(R.drawable.ic_action_warning_yellow);
break; break;
default: default:
iv.setImageResource(R.drawable.ic_action_about); holder.icon.setImageResource(R.drawable.ic_action_about);
} }
TextView tv; holder.tag.setText(item.getTag());
holder.time.setText(item.getDateString());
tv = (TextView) v.findViewById(R.id.log_tag);
tv.setText(item.getTag());
tv = (TextView) v.findViewById(R.id.log_time);
tv.setText(FORMATTER.format(item.getDate()));
String msg = item.getMsg(); String msg = item.getMsg();
if (item.getTr() != null) { if (item.getTr() != null) {
StringWriter sw = new StringWriter(); final StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw); final PrintWriter pw = new PrintWriter(sw);
item.getTr().printStackTrace(pw); item.getTr().printStackTrace(pw);
if (msg == null || msg.isEmpty()) if (msg == null || msg.isEmpty())
@ -64,8 +65,7 @@ public class IITC_LogAdapter extends ArrayAdapter<Log.Message> implements Log.Re
msg += "\n" + sw.toString(); msg += "\n" + sw.toString();
} }
tv = (TextView) v.findViewById(R.id.log_msg); holder.msg.setText(msg);
tv.setText(msg);
return v; return v;
} }
@ -75,27 +75,6 @@ public class IITC_LogAdapter extends ArrayAdapter<Log.Message> implements Log.Re
return 1; return 1;
} }
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
super.unregisterDataSetObserver(observer);
mObservers--;
if (mObservers < 1) {
clear();
Log.removeReceiver(this);
}
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
super.registerDataSetObserver(observer);
if (mObservers < 1)
Log.addReceiver(this);
mObservers++;
}
@Override @Override
public void handle(final Message message) { public void handle(final Message message) {
mIitc.runOnUiThread(new Runnable() { mIitc.runOnUiThread(new Runnable() {
@ -105,4 +84,32 @@ public class IITC_LogAdapter extends ArrayAdapter<Log.Message> implements Log.Re
} }
}); });
} }
@Override
public void registerDataSetObserver(final DataSetObserver observer) {
super.registerDataSetObserver(observer);
if (mObservers < 1)
Log.addReceiver(this);
mObservers++;
}
@Override
public void unregisterDataSetObserver(final DataSetObserver observer) {
super.unregisterDataSetObserver(observer);
mObservers--;
if (mObservers < 1) {
clear();
Log.removeReceiver(this);
}
}
private class ViewHolder {
private ImageView icon;
private TextView msg;
private TextView tag;
private TextView time;
}
} }

View File

@ -32,9 +32,12 @@ import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.webkit.CookieManager; import android.webkit.CookieManager;
import android.webkit.WebView; import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ListView; import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.SearchView; import android.widget.SearchView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -60,7 +63,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class IITC_Mobile extends Activity public class IITC_Mobile extends Activity
implements OnSharedPreferenceChangeListener, NfcAdapter.CreateNdefMessageCallback { implements OnSharedPreferenceChangeListener, NfcAdapter.CreateNdefMessageCallback, OnItemLongClickListener {
private static final String mIntelUrl = "https://www.ingress.com/intel"; private static final String mIntelUrl = "https://www.ingress.com/intel";
private SharedPreferences mSharedPrefs; private SharedPreferences mSharedPrefs;
@ -132,6 +135,7 @@ public class IITC_Mobile extends Activity
}); });
mLvDebug.setAdapter(new IITC_LogAdapter(this)); mLvDebug.setAdapter(new IITC_LogAdapter(this));
mLvDebug.setOnItemLongClickListener(this);
// do something if user changed something in the settings // do something if user changed something in the settings
mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
@ -930,4 +934,33 @@ public class IITC_Mobile extends Activity
} }
return new NdefMessage(records); return new NdefMessage(records);
} }
@Override
public boolean onItemLongClick(final AdapterView<?> parent, final View view, final int position, final long id) {
if (parent == mLvDebug) {
final IITC_LogAdapter adapter = ((IITC_LogAdapter) parent.getAdapter());
final Log.Message item = adapter.getItem(position);
final PopupMenu popupMenu = new PopupMenu(this, view);
popupMenu.getMenuInflater().inflate(R.menu.debug, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(final MenuItem menuitem) {
switch (menuitem.getItemId()) {
case R.id.menu_copy:
mIitcWebView.getJSInterface().copy(item.toString());
return true;
case R.id.menu_delete:
adapter.remove(item);
return true;
}
return false;
}
});
popupMenu.show();
}
return false;
}
} }

View File

@ -1,8 +1,12 @@
package com.cradle.iitc_mobile; package com.cradle.iitc_mobile;
import android.annotation.SuppressLint;
import android.webkit.ConsoleMessage; import android.webkit.ConsoleMessage;
import android.webkit.ConsoleMessage.MessageLevel; import android.webkit.ConsoleMessage.MessageLevel;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
@ -12,6 +16,8 @@ import java.util.regex.Pattern;
public final class Log { public final class Log {
private static final HashMap<ConsoleMessage.MessageLevel, Integer> CONSOLE_MAPPING; private static final HashMap<ConsoleMessage.MessageLevel, Integer> CONSOLE_MAPPING;
@SuppressLint("SimpleDateFormat")
private static final SimpleDateFormat FORMATTER = new SimpleDateFormat("HH:mm:ss.SSS");
private static final List<Receiver> RECEIVERS = new LinkedList<Log.Receiver>(); private static final List<Receiver> RECEIVERS = new LinkedList<Log.Receiver>();
private static final Pattern URL_PATTERN; private static final Pattern URL_PATTERN;
@ -268,6 +274,10 @@ public final class Log {
return mDate; return mDate;
} }
public String getDateString() {
return FORMATTER.format(mDate);
}
public String getMsg() { public String getMsg() {
return mMsg; return mMsg;
} }
@ -283,6 +293,46 @@ public final class Log {
public Throwable getTr() { public Throwable getTr() {
return mTr; return mTr;
} }
@Override
public String toString() {
String priority;
switch (mPriority) {
case Log.ASSERT:
priority = "ASSERT";
break;
case Log.DEBUG:
priority = "DEBUG";
break;
case Log.ERROR:
priority = "ERROR";
break;
case Log.INFO:
priority = "INFO";
case Log.WARN:
priority = "WARN";
break;
case Log.VERBOSE:
priority = "VERBOSE";
break;
default:
priority = "UNKNOWN";
}
String msg = mMsg;
if (mTr != null) {
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
mTr.printStackTrace(pw);
if (msg == null || msg.isEmpty())
msg = sw.toString();
else
msg += "\n" + sw.toString();
}
return getDateString() + " " + priority + " " + getTag() + "\n" + msg;
}
} }
public static interface Receiver { public static interface Receiver {