Skip to content

Commit

Permalink
A couple UI changes
Browse files Browse the repository at this point in the history
  • Loading branch information
alt236 committed Jan 7, 2013
1 parent 1422568 commit af60dcf
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 49 deletions.
1 change: 1 addition & 0 deletions gen/com/martinadamek/jsonandroid/R.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static final class drawable {
}
public static final class id {
public static final int layout=0x7f050000;
public static final int text=0x7f050001;
}
public static final class layout {
public static final int main=0x7f030000;
Expand Down
12 changes: 12 additions & 0 deletions res/layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,16 @@
android:orientation="vertical"
android:padding="10dp" >

<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">

<TextView
android:id="@+id/text"
android:typeface="monospace"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</ScrollView>

</LinearLayout>
100 changes: 51 additions & 49 deletions src/com/martinadamek/jsonandroid/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,15 @@
import java.util.Map;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

private static final String TAG = MainActivity.class.getName();

private LinearLayout mLayout;
private LinearLayout.LayoutParams mLayoutParams;
private String mPath;

private TextView mTextView;

private final Runnable mTestTask = new Runnable() {
public void run() {

Expand All @@ -34,19 +30,15 @@ public void run() {

runOnUiThread(new Runnable() {
public void run() {

mLayout.removeAllViews();

writeToTextView("== Done!");
writeToTextView("\n");
List<String> keys = new ArrayList<String>(results.keySet());
Collections.sort(keys);

for (String key: keys) {
TextView textView = new TextView(MainActivity.this);
textView.setTypeface(Typeface.MONOSPACE);
textView.setText(key + ": " + results.get(key) + "ms");
mLayout.addView(textView, mLayoutParams);
writeToTextView(padRight(key, 12) + ": " + results.get(key) + "ms");
}

}
});

Expand All @@ -59,24 +51,43 @@ public void onCreate(Bundle savedInstanceState) {

setContentView(R.layout.main);

mLayout = (LinearLayout) findViewById(R.id.layout);
mLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);

mTextView = (TextView) findViewById(R.id.text);

mPath = "com/martinadamek/jsonandroid/public_timeline.json";

TextView textView = new TextView(MainActivity.this);
textView.setText("Running tests...");
mLayout.addView(textView, mLayoutParams);

mTextView.setText("Running tests...");
writeToTextView("-----------------");

new Thread(mTestTask).start();

}

private long test(final TestJson testJson, int repeats) {
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(mPath);

List<Map> result = testJson.parsePublicTimeline(inputStream);
verify(result);

long duration = 0;

for (int i = 0; i < repeats; i++) {
inputStream = getClass().getClassLoader().getResourceAsStream(mPath);
long start = System.currentTimeMillis();
testJson.parsePublicTimeline(inputStream);
duration += (System.currentTimeMillis() - start);
}

private void testImpl(TestJson testJson, Map<String, Long> results) {
return duration;
}

private void testImpl(final TestJson testJson, Map<String, Long> results) {
runOnUiThread(new Runnable() {
public void run() {
writeToTextView("== Running tests for '" + testJson.getName() + "'");
}
});

warmUp(testJson);

long duration = test(testJson, 1);
results.put("[1 run] " + testJson.getName(), duration);
duration = test(testJson, 5);
Expand All @@ -93,24 +104,26 @@ private void warmUp(final TestJson testJson) {
}
}

private long test(final TestJson testJson, int repeats) {
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(mPath);

List<Map> result = testJson.parsePublicTimeline(inputStream);
verify(result);
private void writeToTextView(String text){
mTextView.append("\n");
mTextView.append(text);
}

long duration = 0;
private static String map2json(Map map) {
StringBuilder sb = new StringBuilder("{");

for (int i = 0; i < repeats; i++) {
inputStream = getClass().getClassLoader().getResourceAsStream(mPath);
long start = System.currentTimeMillis();
testJson.parsePublicTimeline(inputStream);
duration += (System.currentTimeMillis() - start);
for (Object key: map.keySet()) {
sb.append('"').append(key).append('"').append(':').append('"').append(map.get(key)).append('"').append(",");
}

return duration;
sb.append("}");
return sb.toString();
}

private static String padRight(String s, int n) {
return String.format("%1$-" + n + "s", s);
}

private static void verify(List<Map> result) {
if (result.size() != 20) {
throw new IllegalStateException("Expected 20 but was " + result.size());
Expand All @@ -123,15 +136,4 @@ private static void verify(List<Map> result) {
}
}

private static String map2json(Map map) {
StringBuilder sb = new StringBuilder("{");

for (Object key: map.keySet()) {
sb.append('"').append(key).append('"').append(':').append('"').append(map.get(key)).append('"').append(",");
}

sb.append("}");
return sb.toString();
}

}

0 comments on commit af60dcf

Please sign in to comment.