package com.ohtu.wearable.wearabledataservice.fragments;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.ohtu.wearable.wearabledataservice.R;

public class FragmentThree extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        View view = inflater.inflate(R.layout.button_layout,container,false);

        final LinearLayout layout = (LinearLayout) view.findViewById(R.id.root_layout);
        final Button bn1 = (Button) view.findViewById(R.id.btnaddnewtext1);
        final Button bn2 = (Button) view.findViewById(R.id.btnaddnewtext2);

        bn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO: Code for Activities what will appears when user click button


                Toast.makeText(v.getContext(), "Server is on", Toast.LENGTH_SHORT).show();
                //TextView tv1 = new TextView(v.getContext());
                //tv1.setText("...");
                //layout.addView(tv1);

            }

        });

        bn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO: Code for Activities what will appears when user click button
                //Bn2 Have to show Battery Level in Tv2(TextArea) or in Notification

                //TextView tv2 = new TextView(v.getContext());
                Toast.makeText(v.getContext(), "Battery Level: " + batteryLevel(v.getContext()), Toast.LENGTH_LONG).show();
                //tv2.setText("Battery Level: " + batteryLevel(v.getContext()));
                //layout.addView(tv2);

            }
        });

        return view;
    }

    //Method Shows Battery Level
    private String batteryLevel(Context context) {

        Intent intent;
        intent = context.registerReceiver(null, new
                IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        int    level   = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int    scale   = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
        int    percent = (level*100)/scale;
        return String.valueOf(percent) + "%";
    }

    //TODO: Start and Stop server if needed

}
@@ -487,16 +487,82 @@
 rvice.R;
+%0Aimport com.ohtu.wearable.wearabledataservice.SensorServerService;
 %0A%0Apublic
@@ -1324,16 +1324,216 @@
 button%0A%0A
+                SensorServerService sensorServerService = new SensorServerService();%0A                sensorServerService.stopServer();%0A                //pit%C3%A4%C3%A4 muuttaa niin, ett%C3%A4 teksti tulee metodista
 %0A       
@@ -1584,13 +1584,15 @@
 ver 
-is on
+stopped
 %22, T
@@ -1758,16 +1758,400 @@
 w(tv1);%0A
+                %0A                //toistaiseksi jos yritt%C3%A4%C3%A4 kutsua metodia startServer - kaatuu.%0A                /*sensorServerService.startServer();%0A                //tai %0A                /*MainActivity  mainActivity= new MainActivity();%0A                mainActivity.startServerService();%0A                Toast.makeText(v.getContext(), %22Server started%22, Toast.LENGTH_SHORT).show();*/
 %0A       
@@ -3348,8 +3348,9 @@
 eeded%0A%0A%7D
+%0A

Update FragmentThree.java