package com.example.testapp;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.support.v4.app.NavUtils;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

/** Display activity. This activity displays text to the user. */
public class DisplayMessageActivity extends Activity {

	private final static int TEXT_SIZE = 40;

	private ClientSocket mClient;

	private TextView mTextView;

	public TextView getTextView() {
		return mTextView;
	}

	//testing whether this works...
	private void fontcolor(String text,int color) 
    {
        Spannable raw = new SpannableString(mTextView.getText());
        int index=TextUtils.indexOf(raw, text);
        while (index >= 0) 
        {
          raw.setSpan(new ForegroundColorSpan(color), index, index + text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
          index=TextUtils.indexOf(raw, text, index + text.length());
        }
        mTextView.setText(raw);
    }
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mClient = new ClientSocket();

		// Receive message
		Intent i = getIntent();
		String message = i.getStringExtra(MainActivity.EXTRA_MESSAGE);

		// Create the text view
		mTextView = new TextView(this);
		mTextView.setTextSize(TEXT_SIZE);
		
		//fontcolor("msg:",Color.GREEN);
		mTextView.setText("msg:");
		mTextView.setTextColor(Color.GREEN);
		mTextView.setText(message);
		
		// Make it look prettier
		mTextView.setMovementMethod(new ScrollingMovementMethod());
		mTextView.setBackgroundColor(Color.BLACK);
		mTextView.setTextColor(Color.CYAN);
		setContentView(mTextView);
        
		// Show the Up button in the action bar.
		setupActionBar();

		// Send data to server
		mClient.setServerIpAddress(message);

		String androidId = Secure.getString(getBaseContext()
				.getContentResolver(), Secure.ANDROID_ID);
		String deviceIdentifier = android.os.Build.MODEL + " (" + androidId
				+ ") connected\n";
		mClient.sendData(deviceIdentifier);
		String response = mClient.getResponse();

		// prints on the next line
		mTextView.setText(message + '\n' + response);
		setContentView(mTextView);
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Do nothing
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}

@@ -279,24 +279,78 @@
 p.NavUtils;%0A
+import android.test.suitebuilder.annotation.Suppress;%0A
 import andro
@@ -364,24 +364,24 @@
 .Spannable;%0A
-
 import andro
@@ -940,16 +940,17 @@
 %0A%09%7D%0A%0A%09//
+ 
 testing 
@@ -971,16 +971,27 @@
 orks...%0A
+%09@Suppress%0A
 %09private
@@ -1018,16 +1018,17 @@
 ng text,
+ 
 int colo
@@ -1034,23 +1034,12 @@
 or) 
-%0A    %7B%0A        
+%7B%0A%09%09
 Span
@@ -1092,24 +1092,18 @@
 ext());%0A
-        
+%09%09
 int inde
@@ -1099,25 +1099,27 @@
 %0A%09%09int index
-=
+ = 
 TextUtils.in
@@ -1136,24 +1136,18 @@
  text);%0A
-        
+%09%09
 while (i
@@ -1161,29 +1161,13 @@
  0) 
-%0A        %7B%0A          
+%7B%0A%09%09%09
 raw.
@@ -1212,17 +1212,22 @@
 , index,
- 
+%0A%09%09%09%09%09
 index + 
@@ -1280,24 +1280,19 @@
 E);%0A
-          
+%09%09%09
 index
-=
+ = 
 Text
@@ -1344,26 +1344,14 @@
 ));%0A
-        %7D%0A        
+%09%09%7D%0A%09%09
 mTex
@@ -1374,15 +1374,11 @@
 w);%0A
-    
+%09
 %7D%0A
-%09
 %0A%09@O
@@ -1477,19 +1477,17 @@
 State);%0A
-%09%09
 %0A
+
 %09%09mClien
@@ -1720,23 +1720,22 @@
 _SIZE);%0A
-%09%09
 %0A%09%09//
+ 
 fontcolo
@@ -1859,11 +1859,9 @@
 e);%0A
-%09%09
 %0A
+
 %09%09//
@@ -2061,16 +2061,8 @@
 w);%0A
-        
 %0A%09%09/

Fix warning and formatting.