Added 'lock' option
This commit is contained in:
parent
314908a083
commit
27f3d165ca
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<bytecodeTargetLevel target="11" />
|
||||
<bytecodeTargetLevel target="1.8" />
|
||||
</component>
|
||||
</project>
|
|
@ -12,6 +12,6 @@
|
|||
</deviceKey>
|
||||
</Target>
|
||||
</targetSelectedWithDropDown>
|
||||
<timeTargetWasSelectedWithDropDown value="2022-09-27T08:04:37.916029Z" />
|
||||
<timeTargetWasSelectedWithDropDown value="2022-09-29T21:08:01.717115Z" />
|
||||
</component>
|
||||
</project>
|
|
@ -7,6 +7,7 @@
|
|||
<option name="testRunner" value="GRADLE" />
|
||||
<option name="distributionType" value="DEFAULT_WRAPPED" />
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="gradleJvm" value="11" />
|
||||
<option name="modules">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
|
|
|
@ -36,6 +36,19 @@
|
|||
</intent-filter>
|
||||
</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
|
||||
android:name=".MainActivity"
|
||||
android:exported="true">
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
package com.xfarrow.locatemydevice;
|
||||
|
||||
import android.app.admin.DeviceAdminReceiver;
|
||||
|
||||
public class AdminReceiver extends DeviceAdminReceiver {
|
||||
}
|
|
@ -6,6 +6,8 @@ import androidx.appcompat.app.AppCompatActivity;
|
|||
import androidx.core.app.ActivityCompat;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.admin.DevicePolicyManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
|
@ -51,9 +53,18 @@ public class MainActivity extends AppCompatActivity {
|
|||
Utils.PERMISSION_MULTIPLE
|
||||
);
|
||||
}
|
||||
|
||||
// Request display overlay
|
||||
if(!android.provider.Settings.canDrawOverlays(this)) {
|
||||
displayDrawOverlayPermissionDialog();
|
||||
}
|
||||
|
||||
// Admin permission
|
||||
ComponentName cn = new ComponentName(this, AdminReceiver.class);
|
||||
DevicePolicyManager mgr = (DevicePolicyManager)getSystemService(DEVICE_POLICY_SERVICE);
|
||||
if(!mgr.isAdminActive(cn)){
|
||||
requestAdminPermission(cn);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -110,4 +121,12 @@ public class MainActivity extends AppCompatActivity {
|
|||
});
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -1,8 +1,11 @@
|
|||
package com.xfarrow.locatemydevice;
|
||||
|
||||
import static android.content.Context.DEVICE_POLICY_SERVICE;
|
||||
|
||||
import android.Manifest;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.admin.DevicePolicyManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
|
@ -58,7 +61,9 @@ public class SmsHandler {
|
|||
+ "|"
|
||||
+ 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);
|
||||
Matcher matcher = pattern.matcher(message);
|
||||
|
@ -298,5 +303,20 @@ public class SmsHandler {
|
|||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,8 @@ import android.widget.Toast;
|
|||
|
||||
public class SmsReceiver extends BroadcastReceiver {
|
||||
// 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
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
|
||||
|
|
|
@ -17,6 +17,7 @@ public class Utils {
|
|||
public static final String BATTERY_OPTION = "battery";
|
||||
public static final String CALL_ME_OPTION = "callme";
|
||||
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_DISABLE_SUBOPTION = "-disable";
|
||||
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<uses-policies>
|
||||
<force-lock/>
|
||||
</uses-policies>
|
||||
|
||||
</device-admin>
|
Loading…
Reference in New Issue