Advertisement
Guest User

Untitled

a guest
Mar 16th, 2021
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.33 KB | None | 0 0
  1.  
  2. import android.Manifest;
  3. import android.annotation.SuppressLint;
  4. import android.app.AlertDialog;
  5. import android.content.Context;
  6. import android.content.DialogInterface;
  7. import android.content.Intent;
  8. import android.content.pm.PackageManager;
  9. import android.location.Location;
  10. import android.location.LocationManager;
  11. import android.os.Bundle;
  12. import android.os.Looper;
  13. import android.provider.Settings;
  14. import android.view.LayoutInflater;
  15. import android.view.View;
  16. import android.widget.Button;
  17. import android.widget.EditText;
  18. import android.widget.Toast;
  19.  
  20. import androidx.annotation.NonNull;
  21. import androidx.appcompat.app.AppCompatActivity;
  22. import androidx.core.app.ActivityCompat;
  23.  
  24. import java.io.File;
  25. import java.text.SimpleDateFormat;
  26. import java.util.Date;
  27.  
  28. import com.google.android.gms.location.FusedLocationProviderClient;
  29. import com.google.android.gms.location.LocationCallback;
  30. import com.google.android.gms.location.LocationRequest;
  31. import com.google.android.gms.location.LocationResult;
  32. import com.google.android.gms.location.LocationServices;
  33. import com.google.android.gms.tasks.OnCompleteListener;
  34. import com.google.android.gms.tasks.Task;
  35.  
  36. public class MainActivity extends AppCompatActivity {
  37. // File
  38. private boolean isFileSave = false;
  39. public static File myFile;
  40. private String fileName;
  41. private Date currentDate;
  42.  
  43. public final Context context = this;
  44.  
  45. // Button UI
  46. private Button btnSave;
  47. private Button btnEndSave;
  48. private Button btnSensorsActivity;
  49. private Button btnInactivity;
  50. private Button btnHorizontalWalk;
  51. private Button btnStairs;
  52. private Button btnEscalator;
  53. private Button btnElevator;
  54. private Button btnMoovingWalkway;
  55.  
  56. // Behavior
  57. public static int behavior = 0;
  58. private final int selectedBehaviorColor = 0xFF00d7b3;
  59. private final int unSelectedBehaviorColor = 0xFF343A40;
  60.  
  61. // Instance
  62. private static MainActivity instance;
  63.  
  64. // GPS
  65. FusedLocationProviderClient mFusedLocationClient;
  66. int PERMISSION_ID = 44;
  67.  
  68. public static MainActivity getInstance() {
  69. return instance;
  70. }
  71.  
  72. @SuppressLint("MissingPermission")
  73. public void getLastLocation() {
  74. // check if permissions are given
  75. if (checkPermissions()) {
  76.  
  77. // check if location is enabled
  78. if (isLocationEnabled()) {
  79.  
  80. // getting last
  81. // location from
  82. // FusedLocationClient
  83. // object
  84. mFusedLocationClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
  85. @Override
  86. public void onComplete(@NonNull Task<Location> task) {
  87. Location location = task.getResult();
  88. if (location == null) {
  89. requestNewLocationData();
  90. } else {
  91. /*latitudeTextView.setText(location.getLatitude() + "");
  92. longitTextView.setText(location.getLongitude() + "");*/
  93. requestNewLocationData();
  94. }
  95. }
  96. });
  97. } else {
  98. Toast.makeText(this, "Please turn on" + " your location...", Toast.LENGTH_LONG).show();
  99. Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  100. startActivity(intent);
  101. }
  102. } else {
  103. // if permissions aren't available,
  104. // request for permissions
  105. requestPermissions();
  106. }
  107. }
  108.  
  109. @SuppressLint("MissingPermission")
  110. private void requestNewLocationData() {
  111. // Initializing LocationRequest
  112. // object with appropriate methods
  113. LocationRequest mLocationRequest = new LocationRequest();
  114. mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
  115. mLocationRequest.setInterval(5000);
  116. mLocationRequest.setFastestInterval(5000/2);
  117.  
  118. // setting LocationRequest
  119. // on FusedLocationClient
  120. mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
  121. mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
  122. }
  123.  
  124. private LocationCallback mLocationCallback = new LocationCallback() {
  125. @Override
  126. public void onLocationResult(LocationResult locationResult) {
  127. Location mLastLocation = locationResult.getLastLocation();
  128. /*latitudeTextView.setText("Latitude: " + mLastLocation.getLatitude() + "");
  129. longitTextView.setText("Longitude: " + mLastLocation.getLongitude() + "");*/
  130. SensorsListener.latitude = mLastLocation.getLatitude();
  131. SensorsListener.longitude = mLastLocation.getLongitude();
  132. }
  133. };
  134.  
  135. // method to check for permissions
  136. private boolean checkPermissions() {
  137. return ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
  138.  
  139. // If we want background location
  140. // on Android 10.0 and higher,
  141. // use:
  142. // ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED
  143. }
  144.  
  145. // method to request for permissions
  146. private void requestPermissions() {
  147. ActivityCompat.requestPermissions(this, new String[]{
  148. Manifest.permission.ACCESS_COARSE_LOCATION,
  149. Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_ID);
  150. }
  151.  
  152. // method to check
  153. // if location is enabled
  154. private boolean isLocationEnabled() {
  155. LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  156. return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  157. }
  158.  
  159. // If everything is alright then
  160. @Override
  161. public void
  162. onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  163. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  164. if (requestCode == PERMISSION_ID) {
  165. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  166. getLastLocation();
  167. }
  168. }
  169. }
  170.  
  171. @Override
  172. protected void onCreate(Bundle savedInstanceState) {
  173. super.onCreate(savedInstanceState);
  174. setContentView(R.layout.activity_main);
  175. instance = this;
  176. mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
  177.  
  178. // Init UI
  179. initializeUi();
  180.  
  181. // Init Behavior
  182. initializeBehavior();
  183.  
  184. // Save
  185. btnSave.setOnClickListener(new View.OnClickListener() {
  186. @Override
  187. public void onClick(View view) {
  188. if (!isFileSave) {
  189. // Get dialog_input.xml view
  190. LayoutInflater li = LayoutInflater.from(context);
  191. View promptsView = li.inflate(R.layout.dialog_input, null);
  192.  
  193. AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
  194.  
  195. // Set dialog_input.xml to alertdialog builder
  196. alertDialogBuilder.setView(promptsView);
  197.  
  198. final EditText userInput = (EditText) promptsView.findViewById(R.id.editTextDialogUserInput);
  199.  
  200. // Set dialog message
  201. alertDialogBuilder.setCancelable(false).setPositiveButton("Validate", new DialogInterface.OnClickListener() {
  202. public void onClick(DialogInterface dialog, int id) {
  203. // Get date
  204. currentDate = new Date();
  205. SimpleDateFormat formatterDate = new SimpleDateFormat("yyyyMMdd"); // yy-mm-dd
  206. SimpleDateFormat formatterTime = new SimpleDateFormat("HHmmss");
  207. String dateNow = formatterDate.format(currentDate.getTime());
  208. String timeNow = formatterTime.format(currentDate.getTime());
  209.  
  210. // Get user input and set it to filename
  211. File folder = getExternalFilesDir("Sensors"); // Folder Name
  212. fileName = userInput.getText().toString() + "_" + dateNow + "_" + timeNow + ".csv";
  213. myFile = new File(folder, fileName); // Filename
  214. Toast t = Toast.makeText(MainActivity.this, "Start of the backup.", Toast.LENGTH_SHORT);
  215. t.show();
  216. isFileSave = true;
  217. Intent sensorsService = new Intent(MainActivity.this, SensorsListener.class);
  218. startService(sensorsService);
  219. }
  220. }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  221. public void onClick(DialogInterface dialog, int id) {
  222. Toast t = Toast.makeText(MainActivity.this, "Backup canceled.", Toast.LENGTH_SHORT);
  223. t.show();
  224. dialog.cancel();
  225. }
  226. });
  227. // Create alert dialog
  228. AlertDialog alertDialog = alertDialogBuilder.create();
  229. // Show it
  230. alertDialog.show();
  231. } else {
  232. Toast t = Toast.makeText(MainActivity.this, "Backup already in progress, finish there.", Toast.LENGTH_SHORT);
  233. t.show();
  234. }
  235. }
  236. });
  237.  
  238. // End Save
  239. btnEndSave.setOnClickListener(new View.OnClickListener() {
  240. @Override
  241. public void onClick(View view) {
  242. if (isFileSave) {
  243. Toast t = Toast.makeText(MainActivity.this, "End of the backup.", Toast.LENGTH_SHORT);
  244. t.show();
  245. isFileSave = false;
  246. Intent sensorsService = new Intent(MainActivity.this, SensorsListener.class);
  247. stopService(sensorsService);
  248. } else {
  249. Toast t = Toast.makeText(MainActivity.this, "No backup in progress.", Toast.LENGTH_SHORT);
  250. t.show();
  251. }
  252. }
  253. });
  254.  
  255. // Open Sensors Activity
  256. btnSensorsActivity.setOnClickListener(new View.OnClickListener() {
  257. @Override
  258. public void onClick(View view) {
  259. Intent sensorsIntent = new Intent(MainActivity.this, SensorsActivity.class);
  260. startActivity(sensorsIntent);
  261. }
  262. });
  263.  
  264. // Behavior : Inactivity
  265. btnInactivity.setOnClickListener(new View.OnClickListener() {
  266. @Override
  267. public void onClick(View view) {
  268. resetBehaviorButtonColor();
  269. btnInactivity.setBackgroundColor(selectedBehaviorColor);
  270. behavior = 0;
  271. }
  272. });
  273.  
  274. // Behavior : Horizontal Walk
  275. btnHorizontalWalk.setOnClickListener(new View.OnClickListener() {
  276. @Override
  277. public void onClick(View view) {
  278. resetBehaviorButtonColor();
  279. btnHorizontalWalk.setBackgroundColor(selectedBehaviorColor);
  280. behavior = 1;
  281. }
  282. });
  283.  
  284. // Behavior : Stairs
  285. btnStairs.setOnClickListener(new View.OnClickListener() {
  286. @Override
  287. public void onClick(View view) {
  288. resetBehaviorButtonColor();
  289. btnStairs.setBackgroundColor(selectedBehaviorColor);
  290. behavior = 2;
  291. }
  292. });
  293.  
  294. // Behavior : Escalator
  295. btnEscalator.setOnClickListener(new View.OnClickListener() {
  296. @Override
  297. public void onClick(View view) {
  298. resetBehaviorButtonColor();
  299. btnEscalator.setBackgroundColor(selectedBehaviorColor);
  300. behavior = 4;
  301. }
  302. });
  303.  
  304. // Behavior : Elevator
  305. btnElevator.setOnClickListener(new View.OnClickListener() {
  306. @Override
  307. public void onClick(View view) {
  308. resetBehaviorButtonColor();
  309. btnElevator.setBackgroundColor(selectedBehaviorColor);
  310. behavior = 5;
  311. }
  312. });
  313.  
  314. // Behavior : Mooving Walkway
  315. btnMoovingWalkway.setOnClickListener(new View.OnClickListener() {
  316. @Override
  317. public void onClick(View view) {
  318. resetBehaviorButtonColor();
  319. btnMoovingWalkway.setBackgroundColor(selectedBehaviorColor);
  320. behavior = 6;
  321. }
  322. });
  323. }
  324.  
  325. // Init UI
  326. private void initializeUi() {
  327. btnSave = findViewById(R.id.btn_save);
  328. btnEndSave = findViewById(R.id.btn_end_save);
  329. btnSensorsActivity = findViewById(R.id.btn_sensors_activity);
  330. btnInactivity = findViewById(R.id.btn_inactivity);
  331. btnHorizontalWalk = findViewById(R.id.btn_horizontal_walk);
  332. btnStairs = findViewById(R.id.btn_stairs);
  333. btnEscalator = findViewById(R.id.btn_escalator);
  334. btnElevator = findViewById(R.id.btn_elevator);
  335. btnMoovingWalkway = findViewById(R.id.btn_mooving_walkway);
  336. }
  337.  
  338. // Init Behavior
  339. private void initializeBehavior() {
  340. btnInactivity.setBackgroundColor(selectedBehaviorColor);
  341. behavior = 0;
  342. }
  343.  
  344. // Reset Behavior Button Color
  345. private void resetBehaviorButtonColor() {
  346. btnInactivity.setBackgroundColor(unSelectedBehaviorColor);
  347. btnHorizontalWalk.setBackgroundColor(unSelectedBehaviorColor);
  348. btnStairs.setBackgroundColor(unSelectedBehaviorColor);
  349. btnEscalator.setBackgroundColor(unSelectedBehaviorColor);
  350. btnElevator.setBackgroundColor(unSelectedBehaviorColor);
  351. btnMoovingWalkway.setBackgroundColor(unSelectedBehaviorColor);
  352. }
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement