[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"?>
<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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="HardcodedText">
<ImageView
android:id="@+id/log_type"
android:layout_width="wrap_content"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_action_about"
tools:ignore="ContentDescription"/>
android:orientation="horizontal">
<TextView
android:id="@+id/log_tag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/log_type"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/log_type"
android:gravity="center_vertical|left"
android:text="iitcm"
android:textAppearance="?android:attr/textAppearance"
android:typeface="monospace"/>
<ImageView
android:id="@+id/log_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_about"
tools:ignore="ContentDescription"/>
<TextView
android:id="@+id/log_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/log_type"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/log_tag"
android:gravity="center_vertical|right"
android:text="13:37:42.000"
android:textAppearance="?android:attr/textAppearance"/>
<TextView
android:id="@+id/log_tag"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="center_vertical|left"
android:text="iitcm"
android:textAppearance="?android:attr/textAppearance"
android:typeface="monospace"/>
<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
android:id="@+id/log_msg"
android:layout_width="match_parent"
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: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_send_screenshot">Send screenshot</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="login_failed">Login failed.</string>
<string name="search_hint">Search Locations</string>

View File

@ -1,6 +1,5 @@
package com.cradle.iitc_mobile;
import android.annotation.SuppressLint;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
@ -12,50 +11,52 @@ import com.cradle.iitc_mobile.Log.Message;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
public class IITC_LogAdapter extends ArrayAdapter<Log.Message> implements Log.Receiver {
@SuppressLint("SimpleDateFormat")
private static final SimpleDateFormat FORMATTER = new SimpleDateFormat("HH:mm:ss.SSS");
private final IITC_Mobile mIitc;
private int mObservers = 0;
private IITC_Mobile mIitc;
public IITC_LogAdapter(IITC_Mobile iitc) {
public IITC_LogAdapter(final IITC_Mobile iitc) {
super(iitc, 0);
mIitc = iitc;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Message item = getItem(position);
View v = mIitc.getLayoutInflater().inflate(R.layout.view_log_msg, parent, false);
public View getView(final int position, View v, final ViewGroup parent) {
final Message item = getItem(position);
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()) {
case Log.ASSERT:
case Log.ERROR:
iv.setImageResource(R.drawable.ic_action_error_red);
holder.icon.setImageResource(R.drawable.ic_action_error_red);
break;
case Log.WARN:
iv.setImageResource(R.drawable.ic_action_warning_yellow);
holder.icon.setImageResource(R.drawable.ic_action_warning_yellow);
break;
default:
iv.setImageResource(R.drawable.ic_action_about);
holder.icon.setImageResource(R.drawable.ic_action_about);
}
TextView tv;
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()));
holder.tag.setText(item.getTag());
holder.time.setText(item.getDateString());
String msg = item.getMsg();
if (item.getTr() != null) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
item.getTr().printStackTrace(pw);
if (msg == null || msg.isEmpty())
@ -64,8 +65,7 @@ public class IITC_LogAdapter extends ArrayAdapter<Log.Message> implements Log.Re
msg += "\n" + sw.toString();
}
tv = (TextView) v.findViewById(R.id.log_msg);
tv.setText(msg);
holder.msg.setText(msg);
return v;
}
@ -75,27 +75,6 @@ public class IITC_LogAdapter extends ArrayAdapter<Log.Message> implements Log.Re
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
public void handle(final Message message) {
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.webkit.CookieManager;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;
@ -60,7 +63,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
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 SharedPreferences mSharedPrefs;
@ -132,6 +135,7 @@ public class IITC_Mobile extends Activity
});
mLvDebug.setAdapter(new IITC_LogAdapter(this));
mLvDebug.setOnItemLongClickListener(this);
// do something if user changed something in the settings
mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
@ -930,4 +934,33 @@ public class IITC_Mobile extends Activity
}
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;
import android.annotation.SuppressLint;
import android.webkit.ConsoleMessage;
import android.webkit.ConsoleMessage.MessageLevel;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
@ -12,6 +16,8 @@ import java.util.regex.Pattern;
public final class Log {
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 Pattern URL_PATTERN;
@ -268,6 +274,10 @@ public final class Log {
return mDate;
}
public String getDateString() {
return FORMATTER.format(mDate);
}
public String getMsg() {
return mMsg;
}
@ -283,6 +293,46 @@ public final class Log {
public Throwable getTr() {
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 {