/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.android.mobileperf.compute;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;

import java.util.Arrays;
import java.util.Random;


public class MemoryChurnActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_memory_churn);

        Button theButtonThatDoesArrayStuff = (Button) findViewById(R.id.memchurn_do_array_stuff);
        theButtonThatDoesArrayStuff.setText("Do interesting things with Arrays.");

        theButtonThatDoesArrayStuff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // It's not.
                imPrettySureSortingIsFree();
            }
        });

        // Blah blah productivity YARR
        WebView webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.loadUrl("file:///android_asset/shiver_me_timbers.gif");
    }

    /**
     * Sorts and prints every row of a 2D array, one element at a time.
     */
    public void imPrettySureSortingIsFree() {
        // Throw together a nice, big 2D array of random numbers.
        int dimension = 300;
        int[][] lotsOfInts = new int[dimension][dimension];
        Random randomGenerator = new Random();
        for(int i = 0; i < lotsOfInts.length; i++) {
            for (int j = 0; j < lotsOfInts[i].length; j++) {
                lotsOfInts[i][j] = randomGenerator.nextInt();
            }
        }

        // Now go through and dump the sorted version of each row to output!
        for(int i = 0; i < lotsOfInts.length; i++) {
            String rowAsStr = "";
            for (int j = 0; j < lotsOfInts[i].length; j++) {
                // Clearly, the only reasonable way to construct a string is one character at a
                // time, with lots and lots of convenient concatenation.
                rowAsStr += getSorted(lotsOfInts[i])[j];
                if(j < (lotsOfInts[i].length - 1)){
                    rowAsStr += ", ";
                }
            }
            Log.i("CachingActivityExercise", "Row " + i + ": " + rowAsStr);
        }
    }

    /**
     * Helper method, returns the sorted copy of an array.
     * @param input the unsorted array
     * @return the sorted array
     */
    public int[] getSorted(int[] input) {
        int[] clone = input.clone();
        Arrays.sort(clone);
        return clone;
    }
}

@@ -2454,66 +2454,464 @@
 put!
-%0A        for(int i = 0; i %3C lotsOfInts.length; i++) %7B%0A    
+ This time, use a%0A        // StringBuilder object so that we can construct one String per row, instead of wasting%0A        // String objects with ridiculous concatenation that never ends.%0A        // You may notice that the dancing pirate still seems to have lost the beat here. You would%0A        // be correct. What else could you do now to like that pirate keep his groove? What have you%0A        // learned?%0A        StringBuilder sb = new StringBuilder();%0A
     
@@ -2944,37 +2944,32 @@
         
-   
- 
 for
- 
 (int 
-j
+i
  = 0; 
-j
+i
  %3C lotsO
@@ -2965,35 +2965,32 @@
 ; i %3C lotsOfInts
-%5Bi%5D
 .length; j++) %7B%0A
@@ -2974,33 +2974,33 @@
 sOfInts.length; 
-j
+i
 ++) %7B%0A          
@@ -3005,164 +3005,127 @@
     
-    // Clearly, the only reasonable way to construct a string is one character at a%0A                // time, with lots and lots of convenient concatenation.
+sb.delete(0, rowAsStr.length()); // clear the previous row%0A            for (int j = 0; j %3C lotsOfInts%5Bi%5D.length; j++) %7B
 %0A   
@@ -3137,28 +3137,26 @@
         
-rowAsStr += 
+sb.append(
 getSorte
@@ -3174,16 +3174,17 @@
 s%5Bi%5D)%5Bj%5D
+)
 ;%0A      
@@ -3253,24 +3253,23 @@
     
-rowAsStr += 
+sb.append(
 %22, %22
+)
 ;%0A  
@@ -3290,32 +3290,70 @@
 %7D%0A            %7D%0A
+            rowAsStr = sb.toString();%0A
             Log.

Fix Memory Churn activity