package com.corneliudascalu.glass.phone;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.method.ScrollingMovementMethod;
import android.util.Pair;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import de.greenrobot.event.EventBus;


public class MainActivity extends ActionBarActivity {

    @InjectView(R.id.text)
    TextView textView;

    private DateTimeFormatter formatter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this, this);
        DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
        formatter = builder.appendHourOfDay(2).appendLiteral(':')
                .appendMinuteOfHour(2).appendLiteral(':')
                .appendSecondOfMinute(2).toFormatter();
        startService(new Intent(this, BluetoothService.class));

        textView.setMovementMethod(new ScrollingMovementMethod());
        textView.setTypeface(Typeface.MONOSPACE);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_clear) {
            textView.setText("");
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onEventMainThread(Pair<Integer, String> pair) {
        DateTime dateTime = new DateTime();
        switch (pair.first) {
            case BluetoothService.MSG_CONNECTED:
                //textView.setText(dateTime.toString(formatter) + " - "+ pair.second + "\n" + textView.getText());
                textView.append(dateTime.toString(formatter) + " - "
                        + pair.second + "\n");
                break;
            case BluetoothService.MSG_READ_DATA:
                //textView.setText(dateTime.toString(formatter) + " - "+ pair.second + "\n" + textView.getText());
                textView.append(dateTime.toString(formatter) + " - "
                        + pair.second + "\n");
                break;

        }
    }
}

@@ -218,25 +218,130 @@
 oid.
-graphics.Typeface
+content.pm.PackageManager;%0Aimport android.content.pm.ResolveInfo;%0Aimport android.graphics.Typeface;%0Aimport android.net.Uri
 ;%0Aim
@@ -566,23 +566,135 @@
 oid.
-widget.TextView
+view.View;%0Aimport android.widget.ImageView;%0Aimport android.widget.TextView;%0Aimport android.widget.Toast;%0A%0Aimport java.util.List
 ;%0A%0Ai
@@ -900,16 +900,64 @@
 tView;%0A%0A
+    @InjectView(R.id.logo)%0A    ImageView logo;%0A%0A
     priv
@@ -1676,16 +1676,354 @@
 SPACE);%0A
+        logo.setOnClickListener(new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View view) %7B%0A                onEventMainThread(new Pair%3CInteger, String%3E(%0A                        BluetoothService.MSG_READ_DATA,%0A                        %22http://d.android.com%22%0A                ));%0A            %7D%0A        %7D);%0A
     %7D%0A%0A 
@@ -3620,23 +3620,720 @@
         
-break;%0A
+handleData(pair.second);%0A                break;%0A%0A        %7D%0A    %7D%0A%0A    private void handleData(String second) %7B%0A        Intent intent = new Intent(Intent.ACTION_VIEW);%0A        intent.setData(Uri.parse(second));%0A%0A        PackageManager manager = getApplicationContext().getPackageManager();%0A        List%3CResolveInfo%3E activities = manager.queryIntentActivities(intent, 0);%0A        if (activities.size() %3E 0) %7B%0A            //Then there is application can handle your intent%0A            startActivity(intent);%0A        %7D else %7B%0A            //No Application can handle your intent%0A            Toast.makeText(this, %22There is no application to handle this barcode%22,%0A                    Toast.LENGTH_SHORT).show();
 %0A       

Create intent from data