Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.nihal.myapplication;
- import android.app.ActivityManager;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.content.SharedPreferences;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import com.github.mikephil.charting.charts.BarChart;
- import com.github.mikephil.charting.components.AxisBase;
- import com.github.mikephil.charting.components.Description;
- import com.github.mikephil.charting.components.XAxis;
- import com.github.mikephil.charting.components.YAxis;
- import com.github.mikephil.charting.data.BarData;
- import com.github.mikephil.charting.data.BarDataSet;
- import com.github.mikephil.charting.data.BarEntry;
- import com.github.mikephil.charting.formatter.IAxisValueFormatter;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.List;
- import static com.example.nihal.myapplication.SettingsActivity.PREFS_NAME;
- /**
- * Created by Boiijek on 21/10/2017.
- */
- public class MainActivity extends AppCompatActivity {
- final Context context = this;
- String setno, checkinglux, checkingtemp;
- Button testbutton;
- TextView textalertsends, tempalertsends, luxalertsends,emailalertsends, luxupdate, tempupdate;
- BroadcastReceiver updateUIReceiver;
- public static final String EXTRA_TEMP = "temp_extra";
- public static final String EXTRA_LUX = "lux_extra";
- private List<BarEntry> entries = new ArrayList<>();
- private List<String> times = new ArrayList<>();
- BarChart mChart;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_profile);
- mChart = findViewById(R.id.chart);
- // Chart Properties
- Description description = new Description();
- description.setTextColor(Color.WHITE);
- description.setText("Live Temperature Data"); // Set description for Bar Chart
- mChart.setDescription(description);
- mChart.setDrawGridBackground(false); // Remove Grid
- mChart.getAxisLeft().setDrawGridLines(false);
- mChart.getXAxis().setDrawGridLines(false);
- mChart.getLegend().setTextColor(Color.YELLOW); // Set Legend Text to Yellow
- mChart.setTouchEnabled(true); // Interactive options
- mChart.setDragEnabled(true); // Interactive options
- mChart.setPinchZoom(true); // Interactive options
- mChart.getRendererXAxis().getPaintAxisLabels().setTextAlign(Paint.Align.LEFT); // Align Label with Values
- // X-AXIS
- XAxis xAxis = mChart.getXAxis();
- xAxis.setValueFormatter(createDateFormatter());
- xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
- xAxis.setLabelCount(5);
- xAxis.setTextColor(Color.WHITE);
- // Y-AXIS
- mChart.getAxisRight().setEnabled(false);
- YAxis yAxis = mChart.getAxisLeft();
- yAxis.setDrawGridLines(false); // Remove Grid
- yAxis = mChart.getAxisRight();
- yAxis.setDrawGridLines(false); // Remove Grid
- yAxis = mChart.getAxisLeft();
- yAxis.setTextColor(Color.WHITE); // Set Colour of Y-Axis's Text White
- // Set Data
- setYAxisValues();
- setXAxisValues();
- setData(entries);
- SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
- setno = settings.getString("finalno", "NULL");
- checkingtemp = settings.getString("checkingtemp", "99999");
- checkinglux = settings.getString("checkinglux", "0");
- String sendingmail = (settings.getString("sendingmail", "fypspzcb2018@gmail.com"));
- textalertsends = (TextView) findViewById(R.id.textalertsends);
- tempalertsends = (TextView) findViewById(R.id.tempalertsends);
- emailalertsends = (TextView) findViewById(R.id.emailalertsends);
- luxalertsends = (TextView) findViewById(R.id.luxalertsends);
- textalertsends.setText("SMS Alerts sent to " + setno);
- tempalertsends.setText("Alerted when Temp. is over " + checkingtemp + "°C");
- luxalertsends.setText("Alerted when Lux is below " + checkinglux + " lux");
- emailalertsends.setText("Email Alerts sent to " + sendingmail);
- testbutton = (Button) findViewById(R.id.button55);
- {
- if (isMyServiceRunning() == false) {
- testbutton.setBackgroundColor(Color.GREEN);
- testbutton.setTextColor(Color.BLACK);
- testbutton.setText("Start Background Monitoring");
- } else {
- testbutton.setBackgroundColor(Color.RED);
- testbutton.setText("Stop Background Monitoring");
- }
- }
- testbutton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (isMyServiceRunning() == true) {
- Intent intent = new Intent(MainActivity.this, TimeService.class);
- stopService(intent);
- testbutton.setBackgroundColor(Color.GREEN);
- testbutton.setTextColor(Color.BLACK);
- testbutton.setText("Start Background Monitoring");
- } else {
- Intent intent = new Intent(MainActivity.this, TimeService.class);
- startService(intent);
- testbutton.setBackgroundColor(Color.RED);
- testbutton.setText("Stop Background Monitoring");
- }
- }
- });
- IntentFilter filter = new IntentFilter();
- filter.addAction("com.example.nihal.myapplication.UPDATE_DATA");
- updateUIReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- luxupdate = (TextView) findViewById(R.id.luxview);
- tempupdate = (TextView) findViewById(R.id.tempview);
- Calendar c = Calendar.getInstance();
- SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
- String formattedDate = df.format(c.getTime());
- times.remove(0);
- times.add(4, formattedDate);
- int temp = intent.getIntExtra(EXTRA_TEMP, 0);
- int lux = intent.getIntExtra(EXTRA_LUX, 0);
- luxupdate.setText(lux + " lux");
- tempupdate.setText(temp + "°C");
- entries.remove(0);
- entries.add(4,new BarEntry(0f, temp));
- List<BarEntry> newEntries = new ArrayList<>();
- List<Integer> colors = new ArrayList<>();
- int green = Color.rgb(51, 204, 51);
- int red = Color.rgb(211, 87, 44);
- for(int i=0; i < 5; i++)
- {
- newEntries.add(new BarEntry((float)i,entries.get(i).getY()));
- Log.d("check", String.valueOf(entries.get(i).getY()));
- Log.d("check", checkingtemp);
- if(entries.get(i).getY() > Integer.parseInt(checkingtemp))
- {
- colors.add(red);
- } else
- {
- colors.add(green);
- }
- }
- setData(newEntries);
- Log.i("MainActivity", "UI is being updated " + newEntries.get(3).getY());
- }
- };
- registerReceiver(updateUIReceiver, filter);
- }
- IAxisValueFormatter createDateFormatter() {
- IAxisValueFormatter formatter = new IAxisValueFormatter() {
- @Override
- public String getFormattedValue(float value, AxisBase axis) {
- axis.setTextColor(Color.WHITE);
- return times.get((int)value);
- }
- // we don't draw numbers, so no decimal digits needed
- public int getDecimalDigits() {
- return 0;
- }
- };
- return formatter;
- }
- private void setYAxisValues() {
- entries.add(new BarEntry(0f, 0f));
- entries.add(new BarEntry(1f, 0f));
- entries.add(new BarEntry(2f, 0f));
- entries.add(new BarEntry(3f, 0f));
- entries.add(new BarEntry(4f, 0f));
- }
- private void setXAxisValues() {
- times.add("00:00:00");
- times.add("00:00:00");
- times.add("00:00:00");
- times.add("00:00:00");
- times.add("00:00:00");
- }
- private void setData(List<BarEntry> barEntries) {
- BarDataSet set = new BarDataSet(barEntries, "X Axis - Time // Y Axis - Temp");
- BarData data = new BarData(set);
- data.setBarWidth(0.5f); // set custom bar width
- mChart.setData(data);
- mChart.setFitBars(true); // make the x-axis fit exactly all bars
- mChart.invalidate();
- //mChart.animateY(1000);
- set.setColors(Color.MAGENTA); // SETS Bar Colour
- set.setColor(colors); // THISS LINEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
- set.setValueTextColor(Color.WHITE); // SETS Bar's Value's Colour
- }
- public void startService(View view) {
- Intent intent = new Intent(this, TimeService.class);
- startService(intent);
- }
- public void stopService(View view) {
- Intent intent = new Intent(this, TimeService.class);
- stopService(intent);
- }
- private boolean isMyServiceRunning() {
- ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
- for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
- if (TimeService.class.getName().equals(service.service.getClassName())) {
- return true;
- }
- }
- return false;
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- unregisterReceiver(updateUIReceiver);
- }
- @Override
- public void onBackPressed() {
- finish();
- super.onBackPressed();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement