Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.Manifest;
- import android.annotation.SuppressLint;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.location.Location;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.os.Looper;
- import android.provider.Settings;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import androidx.annotation.NonNull;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.app.ActivityCompat;
- import java.io.File;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import com.google.android.gms.location.FusedLocationProviderClient;
- import com.google.android.gms.location.LocationCallback;
- import com.google.android.gms.location.LocationRequest;
- import com.google.android.gms.location.LocationResult;
- import com.google.android.gms.location.LocationServices;
- import com.google.android.gms.tasks.OnCompleteListener;
- import com.google.android.gms.tasks.Task;
- public class MainActivity extends AppCompatActivity {
- // File
- private boolean isFileSave = false;
- public static File myFile;
- private String fileName;
- private Date currentDate;
- public final Context context = this;
- // Button UI
- private Button btnSave;
- private Button btnEndSave;
- private Button btnSensorsActivity;
- private Button btnInactivity;
- private Button btnHorizontalWalk;
- private Button btnStairs;
- private Button btnEscalator;
- private Button btnElevator;
- private Button btnMoovingWalkway;
- // Behavior
- public static int behavior = 0;
- private final int selectedBehaviorColor = 0xFF00d7b3;
- private final int unSelectedBehaviorColor = 0xFF343A40;
- // Instance
- private static MainActivity instance;
- // GPS
- FusedLocationProviderClient mFusedLocationClient;
- int PERMISSION_ID = 44;
- public static MainActivity getInstance() {
- return instance;
- }
- @SuppressLint("MissingPermission")
- public void getLastLocation() {
- // check if permissions are given
- if (checkPermissions()) {
- // check if location is enabled
- if (isLocationEnabled()) {
- // getting last
- // location from
- // FusedLocationClient
- // object
- mFusedLocationClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
- @Override
- public void onComplete(@NonNull Task<Location> task) {
- Location location = task.getResult();
- if (location == null) {
- requestNewLocationData();
- } else {
- /*latitudeTextView.setText(location.getLatitude() + "");
- longitTextView.setText(location.getLongitude() + "");*/
- requestNewLocationData();
- }
- }
- });
- } else {
- Toast.makeText(this, "Please turn on" + " your location...", Toast.LENGTH_LONG).show();
- Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
- startActivity(intent);
- }
- } else {
- // if permissions aren't available,
- // request for permissions
- requestPermissions();
- }
- }
- @SuppressLint("MissingPermission")
- private void requestNewLocationData() {
- // Initializing LocationRequest
- // object with appropriate methods
- LocationRequest mLocationRequest = new LocationRequest();
- mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
- mLocationRequest.setInterval(5000);
- mLocationRequest.setFastestInterval(5000/2);
- // setting LocationRequest
- // on FusedLocationClient
- mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
- mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
- }
- private LocationCallback mLocationCallback = new LocationCallback() {
- @Override
- public void onLocationResult(LocationResult locationResult) {
- Location mLastLocation = locationResult.getLastLocation();
- /*latitudeTextView.setText("Latitude: " + mLastLocation.getLatitude() + "");
- longitTextView.setText("Longitude: " + mLastLocation.getLongitude() + "");*/
- SensorsListener.latitude = mLastLocation.getLatitude();
- SensorsListener.longitude = mLastLocation.getLongitude();
- }
- };
- // method to check for permissions
- private boolean checkPermissions() {
- return ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
- // If we want background location
- // on Android 10.0 and higher,
- // use:
- // ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED
- }
- // method to request for permissions
- private void requestPermissions() {
- ActivityCompat.requestPermissions(this, new String[]{
- Manifest.permission.ACCESS_COARSE_LOCATION,
- Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_ID);
- }
- // method to check
- // if location is enabled
- private boolean isLocationEnabled() {
- LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
- return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
- }
- // If everything is alright then
- @Override
- public void
- onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- if (requestCode == PERMISSION_ID) {
- if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- getLastLocation();
- }
- }
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- instance = this;
- mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
- // Init UI
- initializeUi();
- // Init Behavior
- initializeBehavior();
- // Save
- btnSave.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if (!isFileSave) {
- // Get dialog_input.xml view
- LayoutInflater li = LayoutInflater.from(context);
- View promptsView = li.inflate(R.layout.dialog_input, null);
- AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
- // Set dialog_input.xml to alertdialog builder
- alertDialogBuilder.setView(promptsView);
- final EditText userInput = (EditText) promptsView.findViewById(R.id.editTextDialogUserInput);
- // Set dialog message
- alertDialogBuilder.setCancelable(false).setPositiveButton("Validate", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- // Get date
- currentDate = new Date();
- SimpleDateFormat formatterDate = new SimpleDateFormat("yyyyMMdd"); // yy-mm-dd
- SimpleDateFormat formatterTime = new SimpleDateFormat("HHmmss");
- String dateNow = formatterDate.format(currentDate.getTime());
- String timeNow = formatterTime.format(currentDate.getTime());
- // Get user input and set it to filename
- File folder = getExternalFilesDir("Sensors"); // Folder Name
- fileName = userInput.getText().toString() + "_" + dateNow + "_" + timeNow + ".csv";
- myFile = new File(folder, fileName); // Filename
- Toast t = Toast.makeText(MainActivity.this, "Start of the backup.", Toast.LENGTH_SHORT);
- t.show();
- isFileSave = true;
- Intent sensorsService = new Intent(MainActivity.this, SensorsListener.class);
- startService(sensorsService);
- }
- }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- Toast t = Toast.makeText(MainActivity.this, "Backup canceled.", Toast.LENGTH_SHORT);
- t.show();
- dialog.cancel();
- }
- });
- // Create alert dialog
- AlertDialog alertDialog = alertDialogBuilder.create();
- // Show it
- alertDialog.show();
- } else {
- Toast t = Toast.makeText(MainActivity.this, "Backup already in progress, finish there.", Toast.LENGTH_SHORT);
- t.show();
- }
- }
- });
- // End Save
- btnEndSave.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if (isFileSave) {
- Toast t = Toast.makeText(MainActivity.this, "End of the backup.", Toast.LENGTH_SHORT);
- t.show();
- isFileSave = false;
- Intent sensorsService = new Intent(MainActivity.this, SensorsListener.class);
- stopService(sensorsService);
- } else {
- Toast t = Toast.makeText(MainActivity.this, "No backup in progress.", Toast.LENGTH_SHORT);
- t.show();
- }
- }
- });
- // Open Sensors Activity
- btnSensorsActivity.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent sensorsIntent = new Intent(MainActivity.this, SensorsActivity.class);
- startActivity(sensorsIntent);
- }
- });
- // Behavior : Inactivity
- btnInactivity.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- resetBehaviorButtonColor();
- btnInactivity.setBackgroundColor(selectedBehaviorColor);
- behavior = 0;
- }
- });
- // Behavior : Horizontal Walk
- btnHorizontalWalk.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- resetBehaviorButtonColor();
- btnHorizontalWalk.setBackgroundColor(selectedBehaviorColor);
- behavior = 1;
- }
- });
- // Behavior : Stairs
- btnStairs.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- resetBehaviorButtonColor();
- btnStairs.setBackgroundColor(selectedBehaviorColor);
- behavior = 2;
- }
- });
- // Behavior : Escalator
- btnEscalator.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- resetBehaviorButtonColor();
- btnEscalator.setBackgroundColor(selectedBehaviorColor);
- behavior = 4;
- }
- });
- // Behavior : Elevator
- btnElevator.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- resetBehaviorButtonColor();
- btnElevator.setBackgroundColor(selectedBehaviorColor);
- behavior = 5;
- }
- });
- // Behavior : Mooving Walkway
- btnMoovingWalkway.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- resetBehaviorButtonColor();
- btnMoovingWalkway.setBackgroundColor(selectedBehaviorColor);
- behavior = 6;
- }
- });
- }
- // Init UI
- private void initializeUi() {
- btnSave = findViewById(R.id.btn_save);
- btnEndSave = findViewById(R.id.btn_end_save);
- btnSensorsActivity = findViewById(R.id.btn_sensors_activity);
- btnInactivity = findViewById(R.id.btn_inactivity);
- btnHorizontalWalk = findViewById(R.id.btn_horizontal_walk);
- btnStairs = findViewById(R.id.btn_stairs);
- btnEscalator = findViewById(R.id.btn_escalator);
- btnElevator = findViewById(R.id.btn_elevator);
- btnMoovingWalkway = findViewById(R.id.btn_mooving_walkway);
- }
- // Init Behavior
- private void initializeBehavior() {
- btnInactivity.setBackgroundColor(selectedBehaviorColor);
- behavior = 0;
- }
- // Reset Behavior Button Color
- private void resetBehaviorButtonColor() {
- btnInactivity.setBackgroundColor(unSelectedBehaviorColor);
- btnHorizontalWalk.setBackgroundColor(unSelectedBehaviorColor);
- btnStairs.setBackgroundColor(unSelectedBehaviorColor);
- btnEscalator.setBackgroundColor(unSelectedBehaviorColor);
- btnElevator.setBackgroundColor(unSelectedBehaviorColor);
- btnMoovingWalkway.setBackgroundColor(unSelectedBehaviorColor);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement