Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/app/app.iml
/PerformanceLab.iml
/.idea
.gradle
/local.properties
/.idea/workspace.xml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < N; i++) {
// Each card is laid out a little to the right of the previous one.
canvas.save();
myPaint.setColor(Color.RED / (i + 1));
myPaint.setStrokeWidth(10);
if (i < N - 1) {
canvas.clipRect(MARGIN + i * shift, MARGIN, MARGIN + (i + 1) * shift, SIZE);
}
canvas.drawRect(MARGIN + i * shift, MARGIN, SIZE + i * shift, SIZE, myPaint);
canvas.restore();
}
// Invalidate the whole view. Doing this calls onDraw() if the view is visible.
invalidate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import android.view.View;
import android.webkit.WebView;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
Expand Down Expand Up @@ -45,33 +47,21 @@ public void onClick(View v) {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public void dumpPopularRandomNumbersByRank() {
Trace.beginSection("Data Structures");
// First we need a sorted list of the numbers to iterate through.
Integer[] sortedNumbers = coolestRandomNumbers.clone();
Arrays.sort(sortedNumbers);
ArrayList<Integer> sortedNumbers = new ArrayList<>(coolestRandomNumbers);
Collections.sort(sortedNumbers);

// Great! Now because we have no rank lookup in the population-sorted array,
// take the random number in sorted order, and find its index in the array
// that's sorted by popularity. The index is the rank, so report that. Easy and efficient!
// Except that it's... you know... It's not.
for (int i = 0; i < sortedNumbers.length; i++) {
Integer currentNumber = sortedNumbers[i];
for (int j = 0; j < coolestRandomNumbers.length; j++) {
if (currentNumber.compareTo(coolestRandomNumbers[j]) == 0) {
Log.i("Popularity Dump", currentNumber + ": #" + j);
}
}
for (Integer value : sortedNumbers) {
Log.i("Popularity Dump", value + ": #" + coolestRandomNumbers.indexOf(value));
}
Trace.endSection();
}

public static Integer[] coolestRandomNumbers = new Integer[3000];
static int temp;
public static List<Integer> coolestRandomNumbers = new ArrayList<>();

static {
Random randomGenerator = new Random();
for (int i = 0; i < 3000; i++) {
temp = randomGenerator.nextInt();
coolestRandomNumbers[i] = temp;
coolestRandomNumbers.add(randomGenerator.nextInt());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;
Expand All @@ -18,6 +19,9 @@ public class FibActivity extends AppCompatActivity {
public static final int POSITION_IN_FIB_SEQUENCE = 20;
private TextView textView;

private static final int CACHE_SIZE = 1000;
private int[] cache = new int[CACHE_SIZE];

public static void start(Context context) {
context.startActivity(new Intent(context, FibActivity.class));
}
Expand All @@ -30,7 +34,19 @@ protected void onCreate(Bundle savedInstanceState) {
findViewById(R.id.fib_progress).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText(String.valueOf(computeFibonacci(POSITION_IN_FIB_SEQUENCE)));
new Thread() {
@Override
public void run() {
super.run();
final int result = computeFibonacci(POSITION_IN_FIB_SEQUENCE);
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
textView.setText(String.valueOf(result));
}
});
}
}.start();
}
});
WebView webView = (WebView) findViewById(R.id.anim_view);
Expand All @@ -40,13 +56,20 @@ public void onClick(View v) {
}

public int computeFibonacci(int positionInFibSequence) {
Log.i(TAG, "inside fib" + positionInFibSequence);
if (positionInFibSequence < CACHE_SIZE) {
if (cache[positionInFibSequence] != 0) {
return cache[positionInFibSequence];
}
}
int result;
if (positionInFibSequence <= 2) {
return 1;
result = 1;
} else {
return computeFibonacci(positionInFibSequence - 1)
result = computeFibonacci(positionInFibSequence - 1)
+ computeFibonacci(positionInFibSequence - 2);
}
cache[positionInFibSequence] = result;
return result;
}

}
11 changes: 5 additions & 6 deletions app/src/main/res/layout/backgrounds_item.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:layout_margin="10dp">
android:layout_margin="10dp"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/title"
android:background="@android:color/white"/>
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>
12 changes: 5 additions & 7 deletions app/src/main/res/layout/backgrounds_layout.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/backgrounds_title"
android:background="@android:color/white"/>
android:text="@string/backgrounds_title" />

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/backgrounds_list"
android:background="@android:color/white"/>
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>
52 changes: 21 additions & 31 deletions app/src/main/res/layout/hierarchy_layout.xml
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_marginTop="@dimen/activity_vertical_margin">

<!-- Version 1. Uses nested LinearLayouts -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<ImageView
android:id="@+id/chat_author_avatar2"
android:layout_width="@dimen/avatar_dimen"
android:layout_height="@dimen/avatar_dimen"
android:layout_margin="@dimen/avatar_layout_margin"
android:src="@mipmap/ic_launcher" />

<TextView
android:id="@+id/chat_author_text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:orientation="horizontal">

<ImageView
android:id="@+id/chat_author_avatar1"
android:layout_width="@dimen/avatar_dimen"
android:layout_height="@dimen/avatar_dimen"
android:layout_margin="@dimen/avatar_layout_margin"
android:src="@mipmap/ic_launcher" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/line1_text" />
android:layout_toEndOf="@id/chat_author_avatar2"
android:text="@string/line1_text" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/line2_text" />
</LinearLayout>
</LinearLayout>

</LinearLayout>
<TextView
android:id="@+id/chat_author_linetext2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/chat_author_text2"
android:layout_toEndOf="@id/chat_author_avatar2"
android:text="@string/line2_text" />
</RelativeLayout>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.android.tools.build:gradle:2.0.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip