From 195c29b7b9cd4675c831b07bd753fe0e45e364cf Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Mon, 10 Oct 2022 11:21:04 +0200 Subject: [PATCH] Added option "ring" --- src/app/src/main/AndroidManifest.xml | 11 ++- .../locatemydevice/RingerActivity.java | 70 +++++++++++++++++++ .../locatemydevice/ShowMessageActivity.java | 2 + .../xfarrow/locatemydevice/SmsHandler.java | 17 ++++- .../com/xfarrow/locatemydevice/Utils.java | 1 + .../src/main/res/layout/activity_ringer.xml | 19 +++++ 6 files changed, 115 insertions(+), 5 deletions(-) create mode 100644 src/app/src/main/java/com/xfarrow/locatemydevice/RingerActivity.java create mode 100644 src/app/src/main/res/layout/activity_ringer.xml diff --git a/src/app/src/main/AndroidManifest.xml b/src/app/src/main/AndroidManifest.xml index 147c5e9..17e5caf 100644 --- a/src/app/src/main/AndroidManifest.xml +++ b/src/app/src/main/AndroidManifest.xml @@ -23,7 +23,13 @@ android:supportsRtl="true" android:theme="@style/Theme.LocateMyDevice" tools:targetApi="30"> - + + + - - + = Build.VERSION_CODES.O_MR1) + { + setShowWhenLocked(true); + setTurnScreenOn(true); + } + else + { + getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | + WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); + } + } + + private void setViews(){ + Button stopButton = findViewById(R.id.stopButton); + + stopButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stopRinging(); + } + }); + } + + private void startRinging(){ + Uri ringtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); + ringtoneManager = RingtoneManager.getRingtone(this, ringtone); + ringtoneManager.setVolume(1f); + ringtoneManager.play(); + } + + private void stopRinging(){ + ringtoneManager.stop(); + finishAffinity(); + } + + @Override + public void onBackPressed(){ + stopRinging(); + } +} \ No newline at end of file diff --git a/src/app/src/main/java/com/xfarrow/locatemydevice/ShowMessageActivity.java b/src/app/src/main/java/com/xfarrow/locatemydevice/ShowMessageActivity.java index 7eee732..080bf6d 100644 --- a/src/app/src/main/java/com/xfarrow/locatemydevice/ShowMessageActivity.java +++ b/src/app/src/main/java/com/xfarrow/locatemydevice/ShowMessageActivity.java @@ -1,5 +1,6 @@ package com.xfarrow.locatemydevice; +import android.annotation.SuppressLint; import android.os.Build; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; @@ -21,6 +22,7 @@ public class ShowMessageActivity extends AppCompatActivity { } // https://stackoverflow.com/questions/35356848/android-how-to-launch-activity-over-lock-screen + @SuppressLint("ObsoleteSdkInt") private void showOnLockscreen(){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { diff --git a/src/app/src/main/java/com/xfarrow/locatemydevice/SmsHandler.java b/src/app/src/main/java/com/xfarrow/locatemydevice/SmsHandler.java index f5924df..051b114 100644 --- a/src/app/src/main/java/com/xfarrow/locatemydevice/SmsHandler.java +++ b/src/app/src/main/java/com/xfarrow/locatemydevice/SmsHandler.java @@ -14,6 +14,8 @@ import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; +import android.media.Ringtone; +import android.media.RingtoneManager; import android.net.Uri; import android.net.wifi.ScanResult; import android.net.wifi.WifiInfo; @@ -65,7 +67,9 @@ public class SmsHandler { + "|" + Utils.LOCK_OPTION + "|" - + Utils.SHOW_MESSAGE_OPTION + "\\s+\"[\\w\\W]*[\"]$"; + + Utils.SHOW_MESSAGE_OPTION + "\\s+\"[\\w\\W]*[\"]$" + + "|" + + Utils.RING_OPTION; Pattern pattern = Pattern.compile(regexToMatch); Matcher matcher = pattern.matcher(message); @@ -320,7 +324,7 @@ public class SmsHandler { Utils.sendSms(smsManager, responseSms.toString(), sender); } - //show + // show else if(providedOption.contains(Utils.SHOW_MESSAGE_OPTION)){ String messageToDisplay = message.substring(message.indexOf("\"") + 1, message.lastIndexOf("\"")); @@ -332,5 +336,14 @@ public class SmsHandler { Utils.sendSms(smsManager, "Displayed on screen", sender); } + // ring + else if(providedOption.equals(Utils.RING_OPTION)){ + + Intent ringerActivityIntent = new Intent(context, RingerActivity.class); + ringerActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(ringerActivityIntent); + + Utils.sendSms(smsManager, "Ringing", sender); + } } } diff --git a/src/app/src/main/java/com/xfarrow/locatemydevice/Utils.java b/src/app/src/main/java/com/xfarrow/locatemydevice/Utils.java index 877412e..bdb88b8 100644 --- a/src/app/src/main/java/com/xfarrow/locatemydevice/Utils.java +++ b/src/app/src/main/java/com/xfarrow/locatemydevice/Utils.java @@ -19,6 +19,7 @@ public class Utils { public static final String WIFI_OPTION = "wifi"; public static final String LOCK_OPTION = "lock"; public static final String SHOW_MESSAGE_OPTION = "show"; + public static final String RING_OPTION = "ring"; public static final String WIFI_ENABLE_SUBOPTION = "-enable"; public static final String WIFI_DISABLE_SUBOPTION = "-disable"; diff --git a/src/app/src/main/res/layout/activity_ringer.xml b/src/app/src/main/res/layout/activity_ringer.xml new file mode 100644 index 0000000..ee94d6f --- /dev/null +++ b/src/app/src/main/res/layout/activity_ringer.xml @@ -0,0 +1,19 @@ + + + +