Added 'lock' option

This commit is contained in:
Alessandro Ferro 2022-09-30 18:06:19 +02:00
parent 314908a083
commit 27f3d165ca
10 changed files with 72 additions and 4 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="CompilerConfiguration"> <component name="CompilerConfiguration">
<bytecodeTargetLevel target="11" /> <bytecodeTargetLevel target="1.8" />
</component> </component>
</project> </project>

View File

@ -12,6 +12,6 @@
</deviceKey> </deviceKey>
</Target> </Target>
</targetSelectedWithDropDown> </targetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2022-09-27T08:04:37.916029Z" /> <timeTargetWasSelectedWithDropDown value="2022-09-29T21:08:01.717115Z" />
</component> </component>
</project> </project>

View File

@ -7,6 +7,7 @@
<option name="testRunner" value="GRADLE" /> <option name="testRunner" value="GRADLE" />
<option name="distributionType" value="DEFAULT_WRAPPED" /> <option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="11" />
<option name="modules"> <option name="modules">
<set> <set>
<option value="$PROJECT_DIR$" /> <option value="$PROJECT_DIR$" />

View File

@ -36,6 +36,19 @@
</intent-filter> </intent-filter>
</receiver> </receiver>
<receiver
android:name="AdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN"
android:exported="true">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin"/>
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
</intent-filter>
</receiver>
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"
android:exported="true"> android:exported="true">

View File

@ -0,0 +1,6 @@
package com.xfarrow.locatemydevice;
import android.app.admin.DeviceAdminReceiver;
public class AdminReceiver extends DeviceAdminReceiver {
}

View File

@ -6,6 +6,8 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import android.Manifest; import android.Manifest;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
@ -51,9 +53,18 @@ public class MainActivity extends AppCompatActivity {
Utils.PERMISSION_MULTIPLE Utils.PERMISSION_MULTIPLE
); );
} }
// Request display overlay
if(!android.provider.Settings.canDrawOverlays(this)) { if(!android.provider.Settings.canDrawOverlays(this)) {
displayDrawOverlayPermissionDialog(); displayDrawOverlayPermissionDialog();
} }
// Admin permission
ComponentName cn = new ComponentName(this, AdminReceiver.class);
DevicePolicyManager mgr = (DevicePolicyManager)getSystemService(DEVICE_POLICY_SERVICE);
if(!mgr.isAdminActive(cn)){
requestAdminPermission(cn);
}
} }
@Override @Override
@ -110,4 +121,12 @@ public class MainActivity extends AppCompatActivity {
}); });
alert.show(); alert.show();
} }
public void requestAdminPermission(ComponentName cn){
Intent intent= new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cn);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "The app requires this" +
" permission to provide the 'lock' feature.");
startActivity(intent);
}
} }

View File

@ -1,8 +1,11 @@
package com.xfarrow.locatemydevice; package com.xfarrow.locatemydevice;
import static android.content.Context.DEVICE_POLICY_SERVICE;
import android.Manifest; import android.Manifest;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.admin.DevicePolicyManager; import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
@ -58,7 +61,9 @@ public class SmsHandler {
+ "|" + "|"
+ Utils.CALL_ME_OPTION + Utils.CALL_ME_OPTION
+ "|" + "|"
+ Utils.WIFI_OPTION + "((" + Utils.WIFI_ENABLE_SUBOPTION + ")|(" + Utils.WIFI_DISABLE_SUBOPTION + "))?"; + Utils.WIFI_OPTION + "((" + Utils.WIFI_ENABLE_SUBOPTION + ")|(" + Utils.WIFI_DISABLE_SUBOPTION + "))?"
+ "|"
+ Utils.LOCK_OPTION;
Pattern pattern = Pattern.compile(regexToMatch); Pattern pattern = Pattern.compile(regexToMatch);
Matcher matcher = pattern.matcher(message); Matcher matcher = pattern.matcher(message);
@ -298,5 +303,20 @@ public class SmsHandler {
Utils.sendSms(smsManager, responseSms.toString(), sender); Utils.sendSms(smsManager, responseSms.toString(), sender);
} }
// lock
else if(providedOption.equals(Utils.LOCK_OPTION)){
StringBuilder responseSms = new StringBuilder();
DevicePolicyManager mgr = (DevicePolicyManager)context.getSystemService(DEVICE_POLICY_SERVICE);
if(!mgr.isAdminActive(new ComponentName(context, AdminReceiver.class))){
responseSms.append("No admin permission. Aborted");
Utils.sendSms(smsManager, responseSms.toString(), sender);
return;
}
mgr.lockNow();
responseSms.append("Locked");
Utils.sendSms(smsManager, responseSms.toString(), sender);
}
} }
} }

View File

@ -11,7 +11,8 @@ import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver { public class SmsReceiver extends BroadcastReceiver {
// https://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html // https://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html
// This method gets fired as soon as it receives an SMS // This method gets fired as soon as it receives an SMS.
// Consider using a JobService: https://developer.android.com/topic/performance/vitals/anr#slow_broadcast_receivers
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {

View File

@ -17,6 +17,7 @@ public class Utils {
public static final String BATTERY_OPTION = "battery"; public static final String BATTERY_OPTION = "battery";
public static final String CALL_ME_OPTION = "callme"; public static final String CALL_ME_OPTION = "callme";
public static final String WIFI_OPTION = "wifi"; public static final String WIFI_OPTION = "wifi";
public static final String LOCK_OPTION = "lock";
public static final String WIFI_ENABLE_SUBOPTION = "-enable"; public static final String WIFI_ENABLE_SUBOPTION = "-enable";
public static final String WIFI_DISABLE_SUBOPTION = "-disable"; public static final String WIFI_DISABLE_SUBOPTION = "-disable";

View File

@ -0,0 +1,7 @@
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<force-lock/>
</uses-policies>
</device-admin>