/*
 * Copyright (C) 2015 Pedro Vicente Gomez Sanchez.
 *
 * 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.github.pedrovgs.sample;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.github.pedrovgs.lynx.LynxActivity;
import com.github.pedrovgs.lynx.LynxConfig;
import java.util.Random;

public class MainActivity extends ActionBarActivity {

  private static final int MAX_TRACES_TO_SHOW = 3000;
  private static final String LYNX_FILTER = "Lynx";
  private static final int SAMPLING_RATE = 200;

  private Thread logGeneratorThread;
  private boolean continueReading = true;
  private int traceCounter = 0;

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

    Button bt_show_lynx_activity = (Button) findViewById(R.id.bt_show_lynx_activity);
    bt_show_lynx_activity.setOnClickListener(new View.OnClickListener() {

      @Override public void onClick(View v) {
        LynxConfig lynxConfig = new LynxConfig();
        lynxConfig.setMaxNumberOfTracesToShow(MAX_TRACES_TO_SHOW)
            .setFilter(LYNX_FILTER)
            .setSamplingRate(SAMPLING_RATE);

        Context context = MainActivity.this;
        Intent lynxActivityIntent = LynxActivity.getIntent(context, lynxConfig);
        startActivity(lynxActivityIntent);
      }
    });

    final Button bt_show_lynx_view = (Button) findViewById(R.id.bt_show_lynx_view);
    bt_show_lynx_view.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        final View lynxView = findViewById(R.id.lynx_view);
        lynxView.setVisibility(View.VISIBLE);
      }
    });

    generateFiveRandomTracesPerSecond();
  }

  @Override protected void onDestroy() {
    super.onDestroy();
    continueReading = false;
  }

  private void generateFiveRandomTracesPerSecond() {
    final Random random = new Random();
    logGeneratorThread = new Thread(new Runnable() {
      @Override public void run() {
        while (continueReading) {
          int traceLevel = random.nextInt(6);
          switch (traceLevel) {
            case 0:
              Log.d("LynxDebug", traceCounter + " - Debug trace generated automatically");
              break;
            case 1:
              Log.w("LynxWarning", traceCounter + " - Warning trace generated automatically");
              break;
            case 2:
              Log.e("LynxError", traceCounter + " - Error trace generated automatically");
              break;
            case 3:
              Log.wtf("LynxWtf", traceCounter + " - WTF trace generated automatically");
              break;
            case 4:
              Log.i("LynxInfo", traceCounter + " - Info trace generated automatically");
            default:
              Log.v("LynxVerbose", traceCounter + " - Verbose trace generated automatically");
          }
          traceCounter++;
          try {
            Thread.sleep(200);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    });
    logGeneratorThread.start();
  }
}

@@ -647,40 +647,8 @@
 e;%0A%0A
-import android.content.Context;%0A
 impo
@@ -1637,364 +1637,25 @@
     
-LynxConfig lynxConfig = new LynxConfig();%0A        lynxConfig.setMaxNumberOfTracesToShow(MAX_TRACES_TO_SHOW)%0A            .setFilter(LYNX_FILTER)%0A            .setSamplingRate(SAMPLING_RATE);%0A%0A        Context context = MainActivity.this;%0A        Intent lynxActivityIntent = LynxActivity.getIntent(context, lynxConfig);%0A        startActivity(lynxActivityIntent
+openLynxActivity(
 );%0A 
@@ -2039,16 +2039,352 @@
 );%0A  %7D%0A%0A
+  private void openLynxActivity() %7B%0A    LynxConfig lynxConfig = new LynxConfig();%0A    lynxConfig.setMaxNumberOfTracesToShow(MAX_TRACES_TO_SHOW)%0A        .setFilter(LYNX_FILTER)%0A        .setSamplingRate(SAMPLING_RATE);%0A%0A    Intent lynxActivityIntent = LynxActivity.getIntent(this, lynxConfig);%0A    startActivity(lynxActivityIntent);%0A  %7D%0A%0A
   @Overr
@@ -2469,24 +2469,102 @@
 false;%0A  %7D%0A%0A
+  /**%0A   * Random traces generator used just for this demo application.%0A   */%0A
   private vo

Create openLynxActivity method in MainActivity