Tooth uncle tutorial is easy to understand
Results show
function
Block touch events in designated app interfaces to make it impossible for others to control your phone
The principle of
Add a timer and check if it is the specified app. If it is, use the suspension window to block it. If it is not, do not block it
The environment
Autojs version: 9.0.5
Android version: 10.0.0
MIUI version: 12.5.1
Here’s what you’ll learn
- Stop other scripts that are not themselves
- Custom check box
- Automatic saving of UI configuration
- Restore the last configuration when the script is opened
- Get the list of apps on your phone
- Check whether the app exists
- Application and judgment of suspension window permission
- View usage permission application and judgment
- Allows background popup application
- Turn off battery optimization application and judgment
- Allow self-initiated applications
- Hide and display desktop ICONS
- Recent tasks hidden and displayed
Classification of permissions
- Hidden classes
- Hide desktop ICONS
- Hide from recent tasks
- The hidden class
- Suspension window permission
- View usage permissions
- Allows background pop-ups
- Off battery optimization
- Allow self-start
- There are two types of unhidden classes, whether or not Android provides an API to determine whether or not the user has granted permissions
- You can judge: suspension window permission, view usage permission, turn off battery optimization
- Do not judge: The background interface is allowed to pop up and the startup is allowed
Permissions function
-
Hover window permission: used to shield users from touch events
-
View usage permissions: for example, view the current app, how long an app has been used and so on
-
Allow background pop-ups: Without this permission, the app can’t do anything in the background
-
Turn off battery optimization: When the battery is low, there is a high probability that the system will not kill you first
-
Allow auto start: used for timing startup, or auto start after startup
-
Hide desktop ICONS
-
Hide from recent tasks: App is not visible in recent tasks
The code on
1. Suspension window permission
Hover window permission: {apply: function () {
app.startActivity({
packageName: "com.android.settings".className: "com.android.settings.Settings$AppDrawOverlaySettingsActivity".data: "package:" + context.packageName.toString(),
});
},
check: function () {
returnfloaty.checkPermission(); }},Copy the code
2. Customize the control
(function () {
util.extend(CustomView, ui.Widget);
function CustomView() {
ui.Widget.call(this);
let scope = this;
let paint = new Paint(Paint.ANTI_ALIAS_FLAG);
let rect = new android.graphics.RectF();
paint.setAntiAlias(true);
paint.setAlpha(255); // Transparency 0-255 0 completely transparent 255 completely opaque
paint.setStyle(android.graphics.Paint.Style.FILL);
let deviation = 12;
let checkedColor = "#27ae60";
let unCheckedColor = "#bdc3c7";
this.checked = false;
this.defineAttr("checked".(view, attr, value, defineSetter) = > {
if (value === "false") {
this.checked = false;
} else if (value === "true") {
this.checked = true;
}
this.updateColor();
});
this.defineAttr("checkedColor".(view, attr, value, defineSetter) = > {
checkedColor = value;
});
this.defineAttr("unCheckedColor".(view, attr, value, defineSetter) = > {
unCheckedColor = value;
});
this.setChecked = function (value) {
this.checked = value;
this.updateColor();
};
this.getChecked = function () {
return this.checked;
};
this.updateColor = function () {
if (this.checked) {
setBackground(scope.view, checkedColor);
} else{ setBackground(scope.view, unCheckedColor); }};this.onFinishInflation = function (view) {
view.post(function () { scope.updateColor(); }); }; }... CustomView.prototype.render =function () {
return <View w="30dp" h="30dp" margin="6 6 6"></View>;
};
ui.registerWidget("custom-checkbox", CustomView);
returnCustomView; }) ();Copy the code
3. Get the list of apps
function getAppList() {
let appList = $app.getInstalledApps({
get: ["meta_data"].match: ["factory_only"]});return appList;
}
Copy the code
4. Check whether the APP exists
function appExists(appName, appList) {
var len = appList.length;
for (var i = 0; i < len; i++) {
let item = appList[i];
if (item.label === appName) {
return true; }}}Copy the code
5. The UI interface
ui.layout(
<scroll>
<vertical padding="16dp">
<text text="Application lock" margin="0 0 0 10" textColor="#d9000000" textSize="35sp" w="*" gravity="center"></text>
<text
text="-- Uncle Teeth tutorial easy to understand"
margin="0 0 0 10"
textColor="# 80000000"
textSize="25sp"
w="*"
gravity="center"
></text>
<card cardCornerRadius="5dp" w="match_parent" h="wrap_content">
<list id="list" padding="10" bg="#7a57d1">
<horizontal w="match_parent" margin="0 0 0 6">
<View w="10dp" h="match_parent" bg="{{this.guideColor}}"></View>
<vertical layout_weight="1" marginLeft="10">
<text id="permissionName" textSize="24sp" textColor="#d9ffffff" text="{{this.permissionName}}"></text>
<text
id="permissionDescription"
textSize="16sp"
textColor="#80ffffff"
text="{{this.permissionDescription}}"
></text>
</vertical>
<custom-checkbox
id="permissionStatus"
checkedColor="#30e6db"
unCheckedColor="#F5F7FA"
checked="{{this.permissionStatus}}"
></custom-checkbox>
</horizontal>
</list>
</card>
<horizontal margin="0 10 0 0">
<text id="label" text="App name to lock:" textSize="24sp" textColor="#d9000000"></text>
<input id="appName" w="*" singleLine="true"></input>
</horizontal>
<button id="lock" bg="#7a57d1" textColor="#d9ffffff" textSize="24sp" text="Lock"></button>
<text id="desc" textSize="18sp" textColor="# 80000000"></text>
</vertical>
</scroll>
);
Copy the code
6. The way of soft keyboard presentation: the layout height is unchanged, and the layout is moved up to make enough space for the soft keyboard
activity.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Copy the code
7. Save the configuration at any time
ui.emitter.on("pause".() = > {
// Record the status of check boxes that cannot properly determine permissions
var len = items.length;
for (var i = 0; i < len; i++) {
let item = items[i];
if(! ~unHiddenPermission.indexOf(item.permissionName)) { storage.put(item.permissionName, item.permissionStatus); }else {
if(! ~canCheckPermission.indexOf(item.permissionName)) { storage.put(item.permissionName, item.permissionStatus); } } } storage.put("appName", ui.appName.text());
});
Copy the code
8. Suspension window interface
window = floaty.rawWindow(
<frame gravity="center" bg="#00ff0000">
<text id="content" textColor="#0076ff03" textSize="66sp" w="*" h="*" gravity="center"></text>
</frame>
);
window.content.setText("Application of lock n Teeth tutorial");
window.setSize(-1, -1);
window.setTouchable(true);
Copy the code
9. The timer locks the specified app
setInterval(function () {
var currentPackageName = currentPackage();
if (packageName === currentPackageName) {
window.setPosition(0.0);
} else {
window.setPosition(6000.6000); }},200);
Copy the code
Quotes.
Thinking is the most important, other Baidu, Bing, StackOverflow, Android documents, autoJS documents, and finally in the group to ask — fang Shu tutorial
The statement
This tutorial is intended for learning purposes only and is not intended for any other use