Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.35 KB | None | 0 0
  1. package com.example.nihal.myapplication;
  2.  
  3. import android.app.ActivityManager;
  4. import android.content.BroadcastReceiver;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.IntentFilter;
  8. import android.content.SharedPreferences;
  9. import android.graphics.Color;
  10. import android.graphics.Paint;
  11. import android.os.Bundle;
  12. import android.support.v7.app.AppCompatActivity;
  13. import android.util.Log;
  14. import android.view.View;
  15. import android.widget.Button;
  16. import android.widget.TextView;
  17.  
  18. import com.github.mikephil.charting.charts.BarChart;
  19. import com.github.mikephil.charting.components.AxisBase;
  20. import com.github.mikephil.charting.components.Description;
  21. import com.github.mikephil.charting.components.XAxis;
  22. import com.github.mikephil.charting.components.YAxis;
  23. import com.github.mikephil.charting.data.BarData;
  24. import com.github.mikephil.charting.data.BarDataSet;
  25. import com.github.mikephil.charting.data.BarEntry;
  26. import com.github.mikephil.charting.formatter.IAxisValueFormatter;
  27.  
  28. import java.text.SimpleDateFormat;
  29. import java.util.ArrayList;
  30. import java.util.Calendar;
  31. import java.util.List;
  32.  
  33. import static com.example.nihal.myapplication.SettingsActivity.PREFS_NAME;
  34.  
  35. /**
  36. * Created by Boiijek on 21/10/2017.
  37. */
  38.  
  39. public class MainActivity extends AppCompatActivity {
  40. final Context context = this;
  41. String setno, checkinglux, checkingtemp;
  42. Button testbutton;
  43.  
  44. TextView textalertsends, tempalertsends, luxalertsends,emailalertsends, luxupdate, tempupdate;
  45. BroadcastReceiver updateUIReceiver;
  46.  
  47.  
  48. public static final String EXTRA_TEMP = "temp_extra";
  49. public static final String EXTRA_LUX = "lux_extra";
  50.  
  51. private List<BarEntry> entries = new ArrayList<>();
  52.  
  53. private List<String> times = new ArrayList<>();
  54.  
  55. BarChart mChart;
  56.  
  57.  
  58. @Override
  59. protected void onCreate(Bundle savedInstanceState) {
  60. super.onCreate(savedInstanceState);
  61.  
  62.  
  63. setContentView(R.layout.activity_profile);
  64. mChart = findViewById(R.id.chart);
  65.  
  66. // Chart Properties
  67. Description description = new Description();
  68. description.setTextColor(Color.WHITE);
  69. description.setText("Live Temperature Data"); // Set description for Bar Chart
  70. mChart.setDescription(description);
  71. mChart.setDrawGridBackground(false); // Remove Grid
  72. mChart.getAxisLeft().setDrawGridLines(false);
  73. mChart.getXAxis().setDrawGridLines(false);
  74. mChart.getLegend().setTextColor(Color.YELLOW); // Set Legend Text to Yellow
  75. mChart.setTouchEnabled(true); // Interactive options
  76. mChart.setDragEnabled(true); // Interactive options
  77. mChart.setPinchZoom(true); // Interactive options
  78. mChart.getRendererXAxis().getPaintAxisLabels().setTextAlign(Paint.Align.LEFT); // Align Label with Values
  79.  
  80. // X-AXIS
  81. XAxis xAxis = mChart.getXAxis();
  82. xAxis.setValueFormatter(createDateFormatter());
  83. xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
  84. xAxis.setLabelCount(5);
  85. xAxis.setTextColor(Color.WHITE);
  86.  
  87.  
  88. // Y-AXIS
  89. mChart.getAxisRight().setEnabled(false);
  90. YAxis yAxis = mChart.getAxisLeft();
  91. yAxis.setDrawGridLines(false); // Remove Grid
  92. yAxis = mChart.getAxisRight();
  93. yAxis.setDrawGridLines(false); // Remove Grid
  94. yAxis = mChart.getAxisLeft();
  95. yAxis.setTextColor(Color.WHITE); // Set Colour of Y-Axis's Text White
  96.  
  97.  
  98. // Set Data
  99. setYAxisValues();
  100. setXAxisValues();
  101. setData(entries);
  102.  
  103.  
  104.  
  105. SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
  106. setno = settings.getString("finalno", "NULL");
  107. checkingtemp = settings.getString("checkingtemp", "99999");
  108. checkinglux = settings.getString("checkinglux", "0");
  109. String sendingmail = (settings.getString("sendingmail", "fypspzcb2018@gmail.com"));
  110. textalertsends = (TextView) findViewById(R.id.textalertsends);
  111. tempalertsends = (TextView) findViewById(R.id.tempalertsends);
  112. emailalertsends = (TextView) findViewById(R.id.emailalertsends);
  113. luxalertsends = (TextView) findViewById(R.id.luxalertsends);
  114.  
  115.  
  116. textalertsends.setText("SMS Alerts sent to " + setno);
  117. tempalertsends.setText("Alerted when Temp. is over " + checkingtemp + "°C");
  118. luxalertsends.setText("Alerted when Lux is below " + checkinglux + " lux");
  119. emailalertsends.setText("Email Alerts sent to " + sendingmail);
  120.  
  121. testbutton = (Button) findViewById(R.id.button55);
  122.  
  123.  
  124. {
  125. if (isMyServiceRunning() == false) {
  126. testbutton.setBackgroundColor(Color.GREEN);
  127. testbutton.setTextColor(Color.BLACK);
  128. testbutton.setText("Start Background Monitoring");
  129. } else {
  130. testbutton.setBackgroundColor(Color.RED);
  131. testbutton.setText("Stop Background Monitoring");
  132.  
  133. }
  134. }
  135.  
  136.  
  137. testbutton.setOnClickListener(new View.OnClickListener() {
  138. @Override
  139. public void onClick(View v) {
  140.  
  141.  
  142. if (isMyServiceRunning() == true) {
  143. Intent intent = new Intent(MainActivity.this, TimeService.class);
  144. stopService(intent);
  145. testbutton.setBackgroundColor(Color.GREEN);
  146. testbutton.setTextColor(Color.BLACK);
  147. testbutton.setText("Start Background Monitoring");
  148. } else {
  149. Intent intent = new Intent(MainActivity.this, TimeService.class);
  150. startService(intent);
  151. testbutton.setBackgroundColor(Color.RED);
  152. testbutton.setText("Stop Background Monitoring");
  153.  
  154.  
  155. }
  156. }
  157.  
  158. });
  159.  
  160.  
  161.  
  162. IntentFilter filter = new IntentFilter();
  163. filter.addAction("com.example.nihal.myapplication.UPDATE_DATA");
  164.  
  165.  
  166. updateUIReceiver = new BroadcastReceiver() {
  167.  
  168. @Override
  169. public void onReceive(Context context, Intent intent) {
  170.  
  171.  
  172. luxupdate = (TextView) findViewById(R.id.luxview);
  173. tempupdate = (TextView) findViewById(R.id.tempview);
  174.  
  175.  
  176. Calendar c = Calendar.getInstance();
  177.  
  178. SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
  179. String formattedDate = df.format(c.getTime());
  180.  
  181. times.remove(0);
  182. times.add(4, formattedDate);
  183.  
  184. int temp = intent.getIntExtra(EXTRA_TEMP, 0);
  185. int lux = intent.getIntExtra(EXTRA_LUX, 0);
  186. luxupdate.setText(lux + " lux");
  187. tempupdate.setText(temp + "°C");
  188. entries.remove(0);
  189. entries.add(4,new BarEntry(0f, temp));
  190.  
  191. List<BarEntry> newEntries = new ArrayList<>();
  192. List<Integer> colors = new ArrayList<>();
  193. int green = Color.rgb(51, 204, 51);
  194. int red = Color.rgb(211, 87, 44);
  195. for(int i=0; i < 5; i++)
  196. {
  197. newEntries.add(new BarEntry((float)i,entries.get(i).getY()));
  198. Log.d("check", String.valueOf(entries.get(i).getY()));
  199. Log.d("check", checkingtemp);
  200.  
  201.  
  202. if(entries.get(i).getY() > Integer.parseInt(checkingtemp))
  203. {
  204. colors.add(red);
  205.  
  206. } else
  207. {
  208. colors.add(green);
  209. }
  210. }
  211. setData(newEntries);
  212. Log.i("MainActivity", "UI is being updated " + newEntries.get(3).getY());
  213.  
  214.  
  215.  
  216. }
  217. };
  218. registerReceiver(updateUIReceiver, filter);
  219.  
  220. }
  221.  
  222. IAxisValueFormatter createDateFormatter() {
  223. IAxisValueFormatter formatter = new IAxisValueFormatter() {
  224. @Override
  225. public String getFormattedValue(float value, AxisBase axis) {
  226. axis.setTextColor(Color.WHITE);
  227. return times.get((int)value);
  228.  
  229. }
  230.  
  231. // we don't draw numbers, so no decimal digits needed
  232. public int getDecimalDigits() {
  233. return 0;
  234. }
  235.  
  236. };
  237.  
  238. return formatter;
  239. }
  240.  
  241.  
  242. private void setYAxisValues() {
  243. entries.add(new BarEntry(0f, 0f));
  244. entries.add(new BarEntry(1f, 0f));
  245. entries.add(new BarEntry(2f, 0f));
  246. entries.add(new BarEntry(3f, 0f));
  247. entries.add(new BarEntry(4f, 0f));
  248.  
  249. }
  250.  
  251. private void setXAxisValues() {
  252. times.add("00:00:00");
  253. times.add("00:00:00");
  254. times.add("00:00:00");
  255. times.add("00:00:00");
  256. times.add("00:00:00");
  257. }
  258.  
  259. private void setData(List<BarEntry> barEntries) {
  260.  
  261. BarDataSet set = new BarDataSet(barEntries, "X Axis - Time // Y Axis - Temp");
  262. BarData data = new BarData(set);
  263. data.setBarWidth(0.5f); // set custom bar width
  264. mChart.setData(data);
  265. mChart.setFitBars(true); // make the x-axis fit exactly all bars
  266. mChart.invalidate();
  267. //mChart.animateY(1000);
  268. set.setColors(Color.MAGENTA); // SETS Bar Colour
  269. set.setColor(colors); // THISS LINEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
  270. set.setValueTextColor(Color.WHITE); // SETS Bar's Value's Colour
  271. }
  272.  
  273.  
  274.  
  275. public void startService(View view) {
  276. Intent intent = new Intent(this, TimeService.class);
  277. startService(intent);
  278. }
  279.  
  280. public void stopService(View view) {
  281. Intent intent = new Intent(this, TimeService.class);
  282. stopService(intent);
  283.  
  284. }
  285.  
  286. private boolean isMyServiceRunning() {
  287. ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  288. for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
  289. if (TimeService.class.getName().equals(service.service.getClassName())) {
  290. return true;
  291. }
  292. }
  293. return false;
  294. }
  295.  
  296. @Override
  297. protected void onDestroy() {
  298. super.onDestroy();
  299. unregisterReceiver(updateUIReceiver);
  300. }
  301.  
  302. @Override
  303. public void onBackPressed() {
  304. finish();
  305. super.onBackPressed();
  306. }
  307.  
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement