Android Code
Android Code
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bhushan.wsafety">
<uses-feature
android:name="android.hardware.sensor.accelerometer"
android:required="true" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Wsafety">
<activity android:name=".RegisterNumberActivity"></activity>
<activity android:name=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<service
android:name=".ServiceMine"
android:enabled="true"
android:foregroundServiceType="location" />
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context=".MainActivity">
<View
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:onClick="PopupMenu"
android:clickable="true"
android:focusable="true"
android:layout_margin="20dp"
android:background="@drawable/ic_baseline_more_vert_24"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center">
<TextView
android:id="@+id/textNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:fontFamily="@font/varela_round"
android:text="SOS Will Be Sent To\n"
android:textAlignment="center"
android:textSize="23sp"
android:textStyle="bold|italic" />
<com.google.android.material.button.MaterialButton
android:id="@+id/start"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_below="@+id/textNum"
android:layout_marginTop="13dp"
android:layout_marginBottom="10dp"
android:backgroundTint="#ffffff"
android:gravity="center"
android:onClick="startServiceV"
android:text="Start"
android:textColor="@color/black"
app:cornerRadius="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.button.MaterialButton
android:id="@+id/stop"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_below="@+id/start"
android:layout_marginTop="26dp"
android:backgroundTint="#ffffff"
android:gravity="center"
android:onClick="stopService"
android:text="stop"
android:textColor="@color/black"
app:cornerRadius="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/start" />
</RelativeLayout>
</RelativeLayout>
activity_register_number.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="@drawable/background"
android:layout_height="match_parent"
tools:context=".RegisterNumberActivity">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Enter Number To\nSend SMS\nin\nEMERGENCY!"
android:textSize="28sp"
android:textStyle="italic|bold"
android:fontFamily="@font/varela_round"
android:textColor="@color/black"
android:id="@+id/text1"
android:textAlignment="center"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:id="@+id/number"
android:layout_below="@id/text1"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:hint="Number"
android:inputType="numberDecimal"
android:id="@+id/numberEdit"
android:maxLength="10"
android:fontFamily="@font/varela_round"
android:textAlignment="center"
android:layout_height="match_parent"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_margin="10dp"
android:onClick="saveNumber"
android:textColor="@color/black"
app:cornerRadius="10dp"
android:layout_below="@id/number"
android:layout_centerHorizontal="true"
android:fontFamily="@font/varela_round"
android:backgroundTint="#ffffff"
android:text="Finish"/>
</RelativeLayout>
</RelativeLayout>
activity_splash_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://
schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SplashScreen">
<ImageView
android:id="@+id/girlVector"
android:layout_width="130dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:src="@drawable/girl_vector"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Feel Safe Everywhere"
android:fontFamily="@font/varela_round"
android:textSize="28sp"
android:layout_below="@id/girlVector"
app:layout_constraintTop_toBottomOf="@id/girlVector"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_centerHorizontal="true"/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.bhushan.wsafety;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.PopupMenu;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import com.google.android.material.snackbar.Snackbar;
import java.util.Map;
import java.util.function.BiConsumer;
@Override
protected void onResume() {
super.onResume();
SharedPreferencessharedPreferences =
getSharedPreferences("MySharedPref",MODE_PRIVATE);
String ENUM = sharedPreferences.getString("ENUM","NONE");
if(ENUM.equalsIgnoreCase("NONE")){
startActivity(new Intent(this,RegisterNumberActivity.class));
}else {
TextViewtextView = findViewById(R.id.textNum);
textView.setText("SOS Will Be Sent To\n"+ENUM);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.O) {
if (android.os.Build.VERSION.SDK_INT>= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("MYID",
"CHANNELFOREGROUND", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager m = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
m.createNotificationChannel(channel);
}
}
private ActivityResultLauncher<String[]>multiplePermissions=
registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new
ActivityResultCallback<Map<String, Boolean>>() {
@Override
public void onActivityResult(Map<String, Boolean> result) {
});
public void stopService(View view) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) ==
PackageManager.PERMISSION_GRANTED&&ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) ==
PackageManager.PERMISSION_GRANTED&&ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
Intent notificationIntent = new Intent(this,ServiceMine.class);
notificationIntent.setAction("Start");
if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.O) {
getApplicationContext().startForegroundService(notificationIntent);
Snackbar.make(findViewById(android.R.id.content),"Service Started!",
Snackbar.LENGTH_LONG).show();
}
}else{
multiplePermissions.launch(new String[]
{Manifest.permission.SEND_SMS,Manifest.permission.ACCESS_COARSE_LOCATION,Ma
nifest.permission.ACCESS_FINE_LOCATION});
}
package com.bhushan.wsafety;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.google.android.material.textfield.TextInputEditText;
TextInputEditTextnumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_number);
number = findViewById(R.id.numberEdit);
}
import android.Manifest;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.IBinder;
import android.telephony.SmsManager;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import com.github.tbouron.shakedetector.library.ShakeDetector;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;
public class ServiceMine extends Service {
booleanisRunning= false;
FusedLocationProviderClientfusedLocationClient;
@Nullable
@Override
public IBinderonBind(Intent intent) {
return null;
}
SmsManagermanager = SmsManager.getDefault();
String myLocation;
@Override
public void onCreate() {
super.onCreate();
fusedLocationClient= LocationServices.getFusedLocationProviderClient(this);
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED&&ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
fusedLocationClient.getLastLocation()
.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
// Logic to handle location object
location.getAltitude();
location.getLongitude();
myLocation= "http://maps.google.com/maps?q=loc:"+location.getLatitude()
+","+location.getLongitude();
}else {
myLocation= "Unable to Find Location :(";
}
}
});
ShakeDetector.create(this, () -> {
SharedPreferencessharedPreferences =
getSharedPreferences("MySharedPref",MODE_PRIVATE);
String ENUM = sharedPreferences.getString("ENUM","NONE");
if(!ENUM.equalsIgnoreCase("NONE")){
manager.sendTextMessage(ENUM,null,"Im in Trouble!\nSending My Location :\
n"+myLocation,null,null);
}
});
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equalsIgnoreCase("STOP")) {
if(isRunning) {
this.stopForeground(true);
this.stopSelf();
}
} else {
if (android.os.Build.VERSION.SDK_INT>=
android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("MYID",
"CHANNELFOREGROUND", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager m = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
m.createNotificationChannel(channel);
return super.onStartCommand(intent,flags,startId);
@Override
public void onDestroy() {
super.onDestroy();
}
}
SplashScreen.java
package com.bhushan.wsafety;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
new CountDownTimer(1000,500){
@Override
public void onTick(long millisUntilFinished) {
@Override
public void onFinish() {
startActivity(new Intent(SplashScreen.this,MainActivity.class));
SplashScreen.this.finish();
}
}.start();
}
}
background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
</shape>
Colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<colorname="purple_200">#FFBB86FC</color>
<colorname="purple_500">#FF6200EE</color>
<colorname="purple_700">#FF3700B3</color>
<colorname="teal_200">#FF03DAC5</color>
<colorname="teal_700">#FF018786</color>
<colorname="black">#FF000000</color>
<colorname="white">#FFFFFFFF</color>
</resources>