view location

This commit is contained in:
Page Asgardius 2022-10-29 13:06:26 -07:00
parent 09a570db4a
commit f332c0d011
5 changed files with 333 additions and 113 deletions

View file

@ -6,6 +6,8 @@ import static android.Manifest.permission.READ_PHONE_NUMBERS;
import static android.Manifest.permission.READ_PHONE_STATE; import static android.Manifest.permission.READ_PHONE_STATE;
import static android.Manifest.permission.READ_SMS; import static android.Manifest.permission.READ_SMS;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
@ -13,6 +15,7 @@ import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
import android.Manifest;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.ContentResolver; import android.content.ContentResolver;
@ -20,10 +23,14 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.AudioAttributes; import android.media.AudioAttributes;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.SystemClock;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
@ -36,6 +43,8 @@ import org.json.JSONObject;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.text.MessageFormat;
import java.util.Locale;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
@ -51,26 +60,42 @@ public class MainScreen extends AppCompatActivity {
SQLiteDatabase db; SQLiteDatabase db;
MyDbHelper dbHelper; MyDbHelper dbHelper;
TextView id ,email, nacionalidad, nacimiento; TextView id ,email, nacionalidad, nacimiento;
Button logout, notification, edit, delete; Button logout, notification, edit, delete, viewlocation;
LinearLayout adminactions; LinearLayout adminactions;
Uri crashsound; Uri crashsound;
TextView textView; TextView textView;
TelephonyManager telephonyManager; TelephonyManager telephonyManager;
private Location lastLocation;
private LocationManager locManager;
private final LocationListener locListener = new LocationListener() {
public void onLocationChanged(Location loc) {
updateLocation(loc);
}
public void onProviderEnabled(String provider) {
updateLocation();
}
public void onProviderDisabled(String provider) {
updateLocation();
}
};
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen); setContentView(R.layout.activity_main_screen);
textView = findViewById(R.id.location); textView = findViewById(R.id.location);
telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); /*telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
if (ActivityCompat.checkSelfPermission(this, READ_SMS) != PackageManager.PERMISSION_GRANTED && if (ActivityCompat.checkSelfPermission(this, READ_SMS) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, READ_PHONE_NUMBERS) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, READ_PHONE_NUMBERS) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.checkSelfPermission(this, READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{READ_SMS, READ_PHONE_NUMBERS, READ_PHONE_STATE,ACCESS_FINE_LOCATION}, 100); ActivityCompat.requestPermissions(this, new String[]{READ_SMS, READ_PHONE_NUMBERS, READ_PHONE_STATE,ACCESS_FINE_LOCATION}, 100);
} else { } else {
textView.setText(""+telephonyManager.getCellLocation()); textView.setText(""+telephonyManager.getCellLocation());
} }*/
username = getIntent().getStringExtra("username"); username = getIntent().getStringExtra("username");
id = (TextView) findViewById(R.id.username); id = (TextView) findViewById(R.id.username);
email = (TextView) findViewById(R.id.email); email = (TextView) findViewById(R.id.email);
@ -81,7 +106,10 @@ public class MainScreen extends AppCompatActivity {
adminactions = (LinearLayout) findViewById(R.id.linearLayoutAdmin); adminactions = (LinearLayout) findViewById(R.id.linearLayoutAdmin);
edit = (Button)findViewById(R.id.editaccount); edit = (Button)findViewById(R.id.editaccount);
delete = (Button)findViewById(R.id.deleteaccount); delete = (Button)findViewById(R.id.deleteaccount);
viewlocation = (Button)findViewById(R.id.view_location);
dbHelper = new MyDbHelper(this); dbHelper = new MyDbHelper(this);
locManager = (LocationManager) getSystemService(LOCATION_SERVICE);
startRequestingLocation();
crashsound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + this.getPackageName() + "/" + R.raw.crash); crashsound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + this.getPackageName() + "/" + R.raw.crash);
id.setText(username); id.setText(username);
Thread login = new Thread(new Runnable() { Thread login = new Thread(new Runnable() {
@ -237,6 +265,7 @@ public class MainScreen extends AppCompatActivity {
accountDelete(); accountDelete();
} }
}); });
viewlocation.setOnClickListener(this::viewLocation);
} }
private void mainMenu() { private void mainMenu() {
@ -268,6 +297,50 @@ public class MainScreen extends AppCompatActivity {
a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(a); startActivity(a);
} }
private final ActivityResultLauncher<String> requestPermissionLauncher =
registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
if (isGranted) {
startRequestingLocation();
} else {
Toast.makeText(this, R.string.permission_denied, Toast.LENGTH_SHORT).show();
finish();
}
});
private void startRequestingLocation() {
if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
return;
}
final String permission = Manifest.permission.ACCESS_FINE_LOCATION;
if (ActivityCompat.checkSelfPermission(this, permission)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissionLauncher.launch(permission);
return;
}
// GPS enabled and have permission - start requesting location updates
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
private void updateLocation() {
// Trigger a UI update without changing the location
updateLocation(lastLocation);
}
private void updateLocation(Location location) {
boolean locationEnabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean waitingForLocation = locationEnabled && !validLocation(location);
boolean haveLocation = locationEnabled && !waitingForLocation;
if (haveLocation) {
String newline = System.getProperty("line.separator");
viewlocation.setEnabled(true);
textView.setText("Ubicación obtenida");
lastLocation = location;
}
}
@RequiresApi(api = Build.VERSION_CODES.P) @RequiresApi(api = Build.VERSION_CODES.P)
@Override @Override
@ -281,8 +354,80 @@ public class MainScreen extends AppCompatActivity {
ActivityCompat.checkSelfPermission(this, READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.checkSelfPermission(this, READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
return; return;
} else { } else {
try {
textView.setText(""+telephonyManager.getCellLocation()); textView.setText(""+telephonyManager.getCellLocation());
} catch (Exception e) {
textView.setText("Ubicación desactivada");
} }
} }
} }
}
private boolean validLocation(Location location) {
if (location == null) {
return false;
}
// Location must be from less than 30 seconds ago to be considered valid
if (Build.VERSION.SDK_INT < 17) {
return System.currentTimeMillis() - location.getTime() < 30e3;
} else {
return SystemClock.elapsedRealtimeNanos() - location.getElapsedRealtimeNanos() < 30e9;
}
}
private String getAccuracy(Location location) {
float accuracy = location.getAccuracy();
if (accuracy < 0.01) {
return "?";
} else if (accuracy > 99) {
return "99+";
} else {
return String.format(Locale.US, "%2.0fm", accuracy);
}
}
private String getLatitude(Location location) {
return String.format(Locale.US, "%2.5f", location.getLatitude());
}
private String getDMSLatitude(Location location) {
double val = location.getLatitude();
return String.format(Locale.US, "%.0f° %2.0f %2.3f″ %s",
Math.floor(Math.abs(val)),
Math.floor(Math.abs(val * 60) % 60),
(Math.abs(val) * 3600) % 60,
val > 0 ? "N" : "S"
);
}
private String getDMSLongitude(Location location) {
double val = location.getLongitude();
return String.format(Locale.US, "%.0f° %2.0f %2.3f″ %s",
Math.floor(Math.abs(val)),
Math.floor(Math.abs(val * 60) % 60),
(Math.abs(val) * 3600) % 60,
val > 0 ? "E" : "W"
);
}
private String getLongitude(Location location) {
return String.format(Locale.US, "%3.5f", location.getLongitude());
}
private String formatLocation(Location location, String format) {
return MessageFormat.format(format,
getLatitude(location), getLongitude(location));
}
public void viewLocation(View view) {
if (!validLocation(lastLocation)) {
return;
}
String uri = formatLocation(lastLocation, "geo:{0},{1}?q={0},{1}");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(Intent.createChooser(intent, getString(R.string.view_location_via)));
}
} }

View file

@ -9,10 +9,72 @@
tools:showIn="@layout/activity_sign_up"> tools:showIn="@layout/activity_sign_up">
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="200dp" android:text="Nombre de usuario"
android:layout_marginTop="48dp" android:textAlignment="center"
android:layout_marginEnd="200dp" android:textColor="?attr/colorOnSecondary"
android:text="@string/large_text" /> android:textSize="25sp" />
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="48dp"
android:autofillHints="username"
android:focusable="true"
android:hint="Nombre de usuario"
android:inputType="textNoSuggestions"
android:textColorHint="?attr/colorOnSecondary"
tools:ignore="MissingConstraints" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Contraseña"
android:textAlignment="center"
android:textColor="?attr/colorOnSecondary"
android:textSize="25sp" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="48dp"
android:autofillHints="username"
android:focusable="true"
android:hint="Contraseña"
android:inputType="textPassword"
android:textColorHint="?attr/colorOnSecondary"
tools:ignore="MissingConstraints" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_margin="20dp"
android:orientation="horizontal"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="20dp">
<Button
android:id="@+id/login"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:backgroundTint="?attr/colorOnSecondary"
android:text="Iniciar Sesión"
android:textColor="?attr/colorOnPrimary"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/delete"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:backgroundTint="?attr/colorOnSecondary"
android:text="Registrarse"
android:textColor="?attr/colorOnPrimary"
tools:ignore="MissingConstraints" />
</LinearLayout>
</androidx.core.widget.NestedScrollView> </androidx.core.widget.NestedScrollView>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -8,17 +8,73 @@
tools:context=".SignUp" tools:context=".SignUp"
tools:showIn="@layout/activity_sign_up"> tools:showIn="@layout/activity_sign_up">
<androidx.core.widget.NestedScrollView <TextView
android:layout_width="840dp" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent" android:text="Nombre de usuario"
app:layout_constraintStart_toStartOf="parent" android:textAlignment="center"
app:layout_constraintTop_toTopOf="parent"> android:textColor="?attr/colorOnSecondary"
android:textSize="25sp" />
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="48dp"
android:autofillHints="username"
android:focusable="true"
android:hint="Nombre de usuario"
android:inputType="textNoSuggestions"
android:textColorHint="?attr/colorOnSecondary"
tools:ignore="MissingConstraints" />
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="48dp" android:text="Contraseña"
android:text="@string/large_text" /> android:textAlignment="center"
</androidx.core.widget.NestedScrollView> android:textColor="?attr/colorOnSecondary"
</androidx.constraintlayout.widget.ConstraintLayout> android:textSize="25sp" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="48dp"
android:autofillHints="username"
android:focusable="true"
android:hint="Contraseña"
android:inputType="textPassword"
android:textColorHint="?attr/colorOnSecondary"
tools:ignore="MissingConstraints" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_margin="20dp"
android:orientation="horizontal"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="20dp">
<Button
android:id="@+id/login"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:backgroundTint="?attr/colorOnSecondary"
android:text="Iniciar Sesión"
android:textColor="?attr/colorOnPrimary"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/delete"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:backgroundTint="?attr/colorOnSecondary"
android:text="Registrarse"
android:textColor="?attr/colorOnPrimary"
tools:ignore="MissingConstraints" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>

View file

@ -110,5 +110,25 @@
android:textAlignment="center" android:textAlignment="center"
android:textColor="@color/white" android:textColor="@color/white"
android:textSize="25sp" /> android:textSize="25sp" />
<LinearLayout
android:id="@+id/linearLayoutLocation"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_margin="0dp"
android:orientation="horizontal"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="20dp">
<Button
android:id="@+id/view_location"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:backgroundTint="@color/black"
android:enabled="false"
android:text="Ver ubicación"
android:textColor="@color/white"
tools:ignore="MissingConstraints" />
</LinearLayout>
</LinearLayout> </LinearLayout>

View file

@ -1,94 +1,31 @@
<resources> <resources>
<string name="app_name">R3 Forum Test</string> <string name="app_name">R3 Forum Test</string>
<string name="title_activity_sign_up">SignUp</string> <string name="title_activity_sign_up">SignUp</string>
<string name="large_text">
"Material is the metaphor.\n\n"
"A material metaphor is the unifying theory of a rationalized space and a system of motion."
"The material is grounded in tactile reality, inspired by the study of paper and ink, yet "
"technologically advanced and open to imagination and magic.\n"
"Surfaces and edges of the material provide visual cues that are grounded in reality. The "
"use of familiar tactile attributes helps users quickly understand affordances. Yet the "
"flexibility of the material creates new affordances that supercede those in the physical "
"world, without breaking the rules of physics.\n"
"The fundamentals of light, surface, and movement are key to conveying how objects move, "
"interact, and exist in space and in relation to each other. Realistic lighting shows "
"seams, divides space, and indicates moving parts.\n\n"
"Bold, graphic, intentional.\n\n"
"The foundational elements of print based design typography, grids, space, scale, color, "
"and use of imagery guide visual treatments. These elements do far more than please the "
"eye. They create hierarchy, meaning, and focus. Deliberate color choices, edge to edge "
"imagery, large scale typography, and intentional white space create a bold and graphic "
"interface that immerse the user in the experience.\n"
"An emphasis on user actions makes core functionality immediately apparent and provides "
"waypoints for the user.\n\n"
"Motion provides meaning.\n\n"
"Motion respects and reinforces the user as the prime mover. Primary user actions are "
"inflection points that initiate motion, transforming the whole design.\n"
"All action takes place in a single environment. Objects are presented to the user without "
"breaking the continuity of experience even as they transform and reorganize.\n"
"Motion is meaningful and appropriate, serving to focus attention and maintain continuity. "
"Feedback is subtle yet clear. Transitions are efficient yet coherent.\n\n"
"3D world.\n\n"
"The material environment is a 3D space, which means all objects have x, y, and z "
"dimensions. The z-axis is perpendicularly aligned to the plane of the display, with the "
"positive z-axis extending towards the viewer. Every sheet of material occupies a single "
"position along the z-axis and has a standard 1dp thickness.\n"
"On the web, the z-axis is used for layering and not for perspective. The 3D world is "
"emulated by manipulating the y-axis.\n\n"
"Light and shadow.\n\n"
"Within the material environment, virtual lights illuminate the scene. Key lights create "
"directional shadows, while ambient light creates soft shadows from all angles.\n"
"Shadows in the material environment are cast by these two light sources. In Android "
"development, shadows occur when light sources are blocked by sheets of material at "
"various positions along the z-axis. On the web, shadows are depicted by manipulating the "
"y-axis only. The following example shows the card with a height of 6dp.\n\n"
"Resting elevation.\n\n"
"All material objects, regardless of size, have a resting elevation, or default elevation "
"that does not change. If an object changes elevation, it should return to its resting "
"elevation as soon as possible.\n\n"
"Component elevations.\n\n"
"The resting elevation for a component type is consistent across apps (e.g., FAB elevation "
"does not vary from 6dp in one app to 16dp in another app).\n"
"Components may have different resting elevations across platforms, depending on the depth "
"of the environment (e.g., TV has a greater depth than mobile or desktop).\n\n"
"Responsive elevation and dynamic elevation offsets.\n\n"
"Some component types have responsive elevation, meaning they change elevation in response "
"to user input (e.g., normal, focused, and pressed) or system events. These elevation "
"changes are consistently implemented using dynamic elevation offsets.\n"
"Dynamic elevation offsets are the goal elevation that a component moves towards, relative "
"to the components resting state. They ensure that elevation changes are consistent "
"across actions and component types. For example, all components that lift on press have "
"the same elevation change relative to their resting elevation.\n"
"Once the input event is completed or cancelled, the component will return to its resting "
"elevation.\n\n"
"Avoiding elevation interference.\n\n"
"Components with responsive elevations may encounter other components as they move between "
"their resting elevations and dynamic elevation offsets. Because material cannot pass "
"through other material, components avoid interfering with one another any number of ways, "
"whether on a per component basis or using the entire app layout.\n"
"On a component level, components can move or be removed before they cause interference. "
"For example, a floating action button (FAB) can disappear or move off screen before a "
"user picks up a card, or it can move if a snackbar appears.\n"
"On the layout level, design your app layout to minimize opportunities for interference. "
"For example, position the FAB to one side of stream of a cards so the FAB wont interfere "
"when a user tries to pick up one of cards.\n\n"
</string>
<string name="action_settings">Settings</string> <string name="action_settings">Settings</string>
<string name="share_location">Share</string>
<string name="copy_location">Copy</string>
<string name="view_location">View</string>
<string name="copied">Copied to clipboard</string>
<string name="clipboard_error">Failed to copy text to clipboard</string>
<string name="getting_location">Getting location…</string>
<string name="share_location_via">Share location via</string>
<string name="view_location_via">View location via</string>
<string name="turn_on_GPS">Turn on GPS</string>
<string name="permission_denied">GPS permission denied</string>
<string name="accuracy">Accuracy</string>
<string name="latitude">Latitude</string>
<string name="longitude">Longitude</string>
<string name="choose_link">Choose a link type</string>
<string name="always_ask">Ask me every time</string>
<string name="googlemaps" translatable="false">Google Maps</string>
<string name="openstreetmap" translatable="false">OpenStreetMap</string>
<string name="geouri" translatable="false">GeoURI</string>
<string name="decimal">Decimal</string>
<string name="settings">Settings</string>
<string name="link_type">Link type</string>
</resources> </resources>