Skip to content

Commit

Permalink
feat: 增加系统时间和网络时间误差判断。增加快捷打卡支持。
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKayJr committed Sep 12, 2024
1 parent 40f27f1 commit 4f8873f
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId = "cn.martinkay.autocheckinplugin"
minSdk = 24
targetSdk = 34
versionCode = 2
versionName = "2.0"
versionCode = 22
versionName = "2.2"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
61 changes: 59 additions & 2 deletions app/src/main/java/cn/martinkay/autocheckinplugin/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.app.TimePickerDialog
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
Expand Down Expand Up @@ -43,9 +42,13 @@ import com.alibaba.fastjson.JSON
import com.haibin.calendarview.Calendar
import com.haibin.calendarview.CalendarView
import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import rikka.shizuku.Shizuku
import rikka.shizuku.Shizuku.OnRequestPermissionResultListener
import java.net.URL

@SuppressLint("SetTextI18n")
class MainActivity : AppCompatActivity(R.layout.activity_main) {
Expand Down Expand Up @@ -111,14 +114,16 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val enableSmartRecognitionJump: Boolean =
SharePrefHelper.getBoolean(ENABLE_SMART_RECOGNITION_JUMP, false)
val enableStartQuickSign: Boolean =
SharePrefHelper.getBoolean(ENABLE_START_QUICK_SIGN, false)

when (item.itemId) {
R.id.action_settings -> {
val inflate = layoutInflater.inflate(R.layout.setting_dialog, null as ViewGroup?)
val builder = AlertDialog.Builder(this)
builder.setTitle("设置")

// 显示Toast
// 开启智能识别跳转
val enableSmartRecognitionJumpSwitch =
inflate.findViewById<Switch>(R.id.smart_recognition_jump)
enableSmartRecognitionJumpSwitch.isChecked = enableSmartRecognitionJump
Expand All @@ -133,6 +138,22 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
Toast.LENGTH_SHORT
).show()
}

val enableStartQuickSignSwitch =
inflate.findViewById<Switch>(R.id.start_quick_sign)
enableStartQuickSignSwitch.isChecked = enableStartQuickSign
enableStartQuickSignSwitch.setOnClickListener {
SharePrefHelper.putBoolean(
ENABLE_START_QUICK_SIGN,
enableStartQuickSignSwitch.isChecked
)
Toast.makeText(
this,
"${if (enableStartQuickSignSwitch.isChecked) "开启" else "关闭"}兼容快捷打卡",
Toast.LENGTH_SHORT
).show()
}

val saveBtn = inflate.findViewById<Button>(R.id.setting_save)
saveBtn.setOnClickListener {

Expand All @@ -142,6 +163,7 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
.setPositiveButton("完成", null as DialogInterface.OnClickListener?).create()
.show()
}

R.id.compatible_config -> {
}
}
Expand Down Expand Up @@ -490,6 +512,41 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {

// 开启自动签到
R.id.enable_auto_sign -> {

// 检测系统时间和网络时间是否误差过大
// NetworkOnMainThreadException

val url = URL("https://www.baidu.com")

GlobalScope.launch(Dispatchers.IO) {
val uc = url.openConnection()
uc.connect()
val ld = uc.date
val systemTime = System.currentTimeMillis()
val timeJitter = ld - systemTime
// 这里应该取绝对值
val abs = Math.abs(timeJitter)
if (abs > 1000 * 60 * 5) {
withContext(Dispatchers.Main) {
Toast.makeText(
this@MainActivity,
"系统时间和网络时间误差过大,请校准时间",
Toast.LENGTH_SHORT
).show()
binding.enableAutoSign.isChecked = false
autoSignConfig.isEnableAutoSign = false
}
} else {
withContext(Dispatchers.Main) {
Toast.makeText(
this@MainActivity,
"系统时间和网络时间误差正常",
Toast.LENGTH_SHORT
).show()
}
}
}

autoSignConfig.isEnableAutoSign = isChecked;
SharePrefHelper.putBoolean(IS_ENABLE_AUTO_SIGN, isChecked)
if (isChecked) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const val VERSION_UPDATE_FLAG = "version_update_flag"

const val ENABLE_SMART_RECOGNITION_JUMP = "enable_smart_recognition_jump"

const val ENABLE_START_QUICK_SIGN = "enable_start_quick_sign"

object SharePrefHelper {

private var mShare: SharedPreferences? = null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,100 @@
package cn.martinkay.autocheckinplugin.handler.pageprocessor.weixin;

import static cn.martinkay.autocheckinplugin.SharePrefHelperKt.ENABLE_START_QUICK_SIGN;

import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.martinkay.autocheckinplugin.SharePrefHelper;
import cn.martinkay.autocheckinplugin.handler.pageprocessor.BasePageProcessor;
import cn.martinkay.autocheckinplugin.service.MyAccessibilityService;
import cn.martinkay.autocheckinplugin.util.AccessibilityHelper;
import cn.martinkay.autocheckinplugin.utils.AutoSignPermissionUtils;

public class MessagePageProcessor extends BasePageProcessor {
@Override
public void processPage(AccessibilityEvent event, MyAccessibilityService myAccessibilityService) {
try {
AccessibilityHelper.clickButtonByNode(myAccessibilityService, findNodesByText(myAccessibilityService, "工作台"));
boolean enableStartQuickSign = SharePrefHelper.INSTANCE.getBoolean(ENABLE_START_QUICK_SIGN, false);
if (enableStartQuickSign) {
// 如果开启了兼容启动快捷打卡,则查找 打卡这个应用的消息
/**
* 需要根据时间的上下5分钟的范围来确定是否是最近的一次打卡消息,否则可能会误判
*/
// 获取当前时间的上下5分钟的范围
long fiveMinutes = 5 * 60 * 1000;
// 获取消息列表
AccessibilityNodeInfo offWork = AccessibilityHelper.getNodeByText(myAccessibilityService, "下班自动打卡", 0);
// xx:xx下班自动打卡 取xx:xx
if (offWork != null) {
String offWorkText = offWork.getText().toString();
String time = offWorkText.substring(0, offWorkText.indexOf("下班自动打卡"));
// time是xx:xx,转换为时间戳
// 定义时间格式
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
// 将时间字符串解析为Date对象
Date date = sdf.parse(time);

// 获取当前时间
Date currentTime = new Date();

// 将当前时间转换为时分格式
String currentTimeString = sdf.format(currentTime);
Date currentDate = sdf.parse(currentTimeString);

// 计算时间差值
long diff = Math.abs(date.getTime() - currentDate.getTime());

// 如果时间差值在5分钟内,则认为是最近的一次打卡消息
if (diff <= fiveMinutes) {
Log.w("CompleteProcessor", "打卡成功 关闭");
AutoSignPermissionUtils.INSTANCE.increaseTodayAutoSignCount();
myAccessibilityService.clickHomeKey();
myAccessibilityService.closeApp("com.tencent.wework");
myAccessibilityService.autoLock();
return;
}
} else {
AccessibilityNodeInfo work = AccessibilityHelper.getNodeByText(myAccessibilityService, "上班自动打卡", 0);
if (work != null) {

String workText = work.getText().toString();
String time = workText.substring(0, workText.indexOf("上班自动打卡"));
// time是xx:xx,转换为时间戳
// 定义时间格式
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
// 将时间字符串解析为Date对象
Date date = sdf.parse(time);

// 获取当前时间
Date currentTime = new Date();

// 将当前时间转换为时分格式
String currentTimeString = sdf.format(currentTime);
Date currentDate = sdf.parse(currentTimeString);

// 计算时间差值
long diff = Math.abs(date.getTime() - currentDate.getTime());

// 如果时间差值在5分钟内,则认为是最近的一次打卡消息
if (diff <= fiveMinutes) {
Log.w("CompleteProcessor", "打卡成功 关闭");
AutoSignPermissionUtils.INSTANCE.increaseTodayAutoSignCount();
myAccessibilityService.clickHomeKey();
myAccessibilityService.closeApp("com.tencent.wework");
myAccessibilityService.autoLock();
return;
}
}
}
AccessibilityHelper.clickButtonByNode(myAccessibilityService, findNodesByText(myAccessibilityService, "工作台"));
} else {
AccessibilityHelper.clickButtonByNode(myAccessibilityService, findNodesByText(myAccessibilityService, "工作台"));
}
} catch (Exception unused) {
Log.e("Weixin", "首页处理失败");
}
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@
android:layout_marginBottom="8dp"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Shizuku权限检测"
android:textColor="@android:color/background_dark"
android:textSize="20sp" />

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="@android:color/holo_red_light" />


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/layout/setting_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
android:text="@string/smart_recognition_jump"
tools:ignore="UseSwitchCompatOrMaterialXml" />

<Switch
android:id="@+id/start_quick_sign"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/start_quick_sign"
tools:ignore="UseSwitchCompatOrMaterialXml" />

<Button
android:id="@+id/setting_save"
android:layout_width="match_parent"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
<string name="compatible_config">兼容配置</string>
<string name="smart_recognition_jump">智能识别跳转(用于判断当前跳转是人为打开还是自动打开,来决定是否要自动操作,防止与人抢屏幕)</string>
<string name="settings_save">保存</string>
<string name="start_quick_sign">兼容快捷打卡(启动企业微信就自动打卡-此功能需要企业管理员开启)</string>
</resources>

0 comments on commit 4f8873f

Please sign in to comment.