Compare commits
17 Commits
positions-
...
positions
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
17d2422690 | ||
|
|
55d72c3f82 | ||
|
|
74f685e07d | ||
|
|
e3826148fa | ||
|
|
bf52f86b3d | ||
|
|
566e84f53f | ||
| 2d3cee1121 | |||
|
|
2be6d5e122 | ||
| 6376ff4ccf | |||
|
|
dd20060754 | ||
|
|
298b337392 | ||
| 377ec4de09 | |||
|
|
033da4d544 | ||
|
|
1398ffc064 | ||
| 247f3f9b49 | |||
|
|
cec2e82550 | ||
|
|
ac5f425624 |
@@ -62,7 +62,7 @@ dependencies {
|
||||
// 应用介绍页类库
|
||||
api 'io.github.medyo:android-about-page:2.0.0'
|
||||
// 吐司类库
|
||||
api 'com.github.getActivity:ToastUtils:10.5'
|
||||
//api 'com.github.getActivity:ToastUtils:10.5'
|
||||
// 网络连接类库
|
||||
api 'com.squareup.okhttp3:okhttp:4.4.1'
|
||||
// AndroidX 类库
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#Created by .winboll/winboll_app_build.gradle
|
||||
#Sat Oct 25 18:29:48 HKT 2025
|
||||
stageCount=14
|
||||
#Mon Nov 10 03:35:32 GMT 2025
|
||||
stageCount=18
|
||||
libraryProject=
|
||||
baseVersion=15.0
|
||||
publishVersion=15.0.13
|
||||
buildCount=0
|
||||
baseBetaVersion=15.0.14
|
||||
publishVersion=15.0.17
|
||||
buildCount=71
|
||||
baseBetaVersion=15.0.18
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name">悟空笔记#</string>
|
||||
<string name="app_laojun_name">老君道說#</string>
|
||||
</resources>
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<string name="app_name">Positions +</string>
|
||||
<string name="app_name">Positions</string>
|
||||
|
||||
<string name="app_laojun_name">Positions +</string>
|
||||
|
||||
</resources>
|
||||
|
||||
34
positions/src/beta/res/xml/shortcuts.xml
Normal file
34
positions/src/beta/res/xml/shortcuts.xml
Normal file
@@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<!-- 切换启动入口的快捷菜单 -->
|
||||
<shortcut
|
||||
android:shortcutId="switch_launcher_main"
|
||||
android:enabled="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:shortcutShortLabel="@string/switch_to_main"
|
||||
android:shortcutLongLabel="@string/switch_to_main"
|
||||
android:shortcutDisabledMessage="@string/switch_disabled">
|
||||
<intent
|
||||
android:action="cc.winboll.studio.positions.MainActivity"
|
||||
android:targetPackage="cc.winboll.studio.positions.beta"
|
||||
android:targetClass="cc.winboll.studio.positions.MainActivity"
|
||||
android:data="switch_launcher_main" />
|
||||
|
||||
<categories android:name="android.shortcut.conversation" />
|
||||
</shortcut>
|
||||
<shortcut
|
||||
android:shortcutId="switch_launcher_laojun"
|
||||
android:enabled="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:shortcutShortLabel="@string/switch_to_laojun"
|
||||
android:shortcutLongLabel="@string/switch_to_laojun"
|
||||
android:shortcutDisabledMessage="@string/switch_disabled">
|
||||
<intent
|
||||
android:action="cc.winboll.studio.positions.MainActivityLaojun"
|
||||
android:targetPackage="cc.winboll.studio.positions.beta"
|
||||
android:targetClass="cc.winboll.studio.positions.MainActivity"
|
||||
android:data="switch_launcher_laojun" />
|
||||
|
||||
<categories android:name="android.shortcut.conversation" />
|
||||
</shortcut>
|
||||
</shortcuts>
|
||||
@@ -3,20 +3,14 @@
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="cc.winboll.studio.positions">
|
||||
|
||||
<!-- 前台服务权限(可选,提升后台定位稳定性,避免服务被回收) -->
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
|
||||
<!-- 只能在前台获取精确的位置信息 -->
|
||||
<!-- 权限配置不变 -->
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
|
||||
|
||||
<!-- 只有在前台运行时才能获取大致位置信息 -->
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
|
||||
|
||||
<!-- 拥有完全的网络访问权限 -->
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
|
||||
<!-- 在后台使用位置信息 -->
|
||||
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION"/>
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
|
||||
|
||||
<uses-feature
|
||||
android:name="android.hardware.location.gps"
|
||||
@@ -30,37 +24,82 @@
|
||||
android:resizeableActivity="true"
|
||||
android:name=".App">
|
||||
|
||||
<!-- 主Activity(非启动入口,无需LAUNCHER意图) -->
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:label="@string/app_name">
|
||||
|
||||
<intent-filter>
|
||||
|
||||
<action android:name="android.intent.action.MAIN"/>
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER"/>
|
||||
|
||||
</intent-filter>
|
||||
|
||||
</activity>
|
||||
|
||||
<!-- Wukong 别名入口(默认禁用,通过代码启用) -->
|
||||
<activity-alias
|
||||
android:name=".MainActivityWukong"
|
||||
android:targetActivity=".MainActivity"
|
||||
android:exported="true"
|
||||
android:label="@string/app_name"
|
||||
android:icon="@drawable/ic_launcher"
|
||||
android:enabled="true"> <!-- 默认禁用,避免桌面显示 -->
|
||||
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
|
||||
<!-- 长按图标快捷菜单 -->
|
||||
<meta-data
|
||||
android:name="android.app.shortcuts"
|
||||
android:resource="@xml/shortcuts" />
|
||||
</activity-alias>
|
||||
|
||||
<!-- Laojun 别名入口(默认禁用,通过代码启用) -->
|
||||
<activity-alias
|
||||
android:name=".MainActivityLaojun"
|
||||
android:targetActivity=".MainActivity"
|
||||
android:exported="true"
|
||||
android:label="@string/app_laojun_name"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:enabled="false"> <!-- 默认禁用,避免桌面显示 -->
|
||||
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
|
||||
<!-- 长按图标快捷菜单 -->
|
||||
<meta-data
|
||||
android:name="android.app.shortcuts"
|
||||
android:resource="@xml/shortcuts" />
|
||||
</activity-alias>
|
||||
|
||||
<!-- 其他配置不变 -->
|
||||
<meta-data
|
||||
android:name="android.max_aspect"
|
||||
android:value="4.0"/>
|
||||
|
||||
<activity android:name=".GlobalApplication$CrashActivity"/>
|
||||
|
||||
<activity android:name="cc.winboll.studio.positions.activities.LocationActivity"/>
|
||||
|
||||
<meta-data
|
||||
android:name="com.google.android.gms.version"
|
||||
android:value="@integer/google_play_services_version"/>
|
||||
|
||||
<service android:name=".services.MainService"/>
|
||||
<service
|
||||
android:name=".services.MainService"
|
||||
android:exported="false"/>
|
||||
<service
|
||||
android:name=".services.AssistantService"
|
||||
android:exported="false"/>
|
||||
<service
|
||||
android:name=".services.DistanceRefreshService"
|
||||
android:exported="false"/>
|
||||
|
||||
<service android:name=".services.AssistantService"/>
|
||||
<receiver android:name="cc.winboll.studio.positions.receivers.MotionStatusReceiver">
|
||||
<intent-filter>
|
||||
<action android:name="cc.winboll.studio.positions.receivers.MotionStatusReceiver"/>
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
<service android:name=".services.DistanceRefreshService"/>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@ import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.Gravity;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.ViewGroup;
|
||||
@@ -22,9 +21,11 @@ import android.widget.HorizontalScrollView;
|
||||
import android.widget.ScrollView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
import cc.winboll.studio.libaes.utils.WinBoLLActivityManager;
|
||||
import cc.winboll.studio.libappbase.GlobalApplication;
|
||||
import com.hjq.toast.ToastUtils;
|
||||
import com.hjq.toast.style.WhiteToastStyle;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.positions.activities.WinBoLLActivity;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.Closeable;
|
||||
@@ -41,15 +42,17 @@ import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import cc.winboll.studio.libaes.utils.WinBoLLActivityManager;
|
||||
|
||||
public class App extends GlobalApplication {
|
||||
|
||||
public static volatile AppLevel _mAppLevel = AppLevel.WUKONG;
|
||||
|
||||
private static Handler MAIN_HANDLER = new Handler(Looper.getMainLooper());
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
|
||||
setIsDebuging(BuildConfig.DEBUG);
|
||||
|
||||
WinBoLLActivityManager.init(this);
|
||||
@@ -58,13 +61,34 @@ public class App extends GlobalApplication {
|
||||
ToastUtils.init(this);
|
||||
// 设置 Toast 布局样式
|
||||
//ToastUtils.setView(R.layout.view_toast);
|
||||
ToastUtils.setStyle(new WhiteToastStyle());
|
||||
ToastUtils.setGravity(Gravity.BOTTOM, 0, 200);
|
||||
//ToastUtils.setStyle(new WhiteToastStyle());
|
||||
//ToastUtils.setGravity(Gravity.BOTTOM, 0, 200);
|
||||
|
||||
//CrashHandler.getInstance().registerGlobal(this);
|
||||
//CrashHandler.getInstance().registerPart(this);
|
||||
}
|
||||
|
||||
|
||||
public static void setAppLevel(WinBoLLActivity activity) {
|
||||
// 根据应用当前启动入口设定整体应用级别
|
||||
String launchComponent = activity.getComponentName().getClassName();
|
||||
boolean isAliasLaunch = launchComponent.endsWith("MainActivityLaojun");
|
||||
|
||||
if (isAliasLaunch) {
|
||||
// Alias入口启动逻辑(如切换应用级别、加载专属配置)
|
||||
LogUtils.d(TAG, "通过Alias入口启动,切换为LAOJUN级别");
|
||||
//ToastUtils.show("通过Alias入口启动,切换为LAOJUN级别");
|
||||
App._mAppLevel = AppLevel.LAOJUN; // 结合之前定义的枚举
|
||||
// 执行Alias专属初始化...
|
||||
} else {
|
||||
// 原入口启动逻辑
|
||||
LogUtils.d(TAG, "通过原入口启动,默认WUKONG级别");
|
||||
//ToastUtils.show("通过原入口启动,默认WUKONG级别");
|
||||
App._mAppLevel = AppLevel.WUKONG;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void write(InputStream input, OutputStream output) throws IOException {
|
||||
byte[] buf = new byte[1024 * 8];
|
||||
int len;
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package cc.winboll.studio.positions;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/11/10 07:23
|
||||
* @Describe 应用级别类型枚举
|
||||
*/
|
||||
public enum AppLevel {
|
||||
WUKONG("wukong", "悟空级别"),
|
||||
LAOJUN("laojun", "老君级别");
|
||||
|
||||
public static final String TAG = "AppLevel";
|
||||
|
||||
// 枚举属性
|
||||
private final String code; // 编码(如 "wukong")
|
||||
private final String desc; // 描述
|
||||
|
||||
// 构造方法(Java 7 需显式定义)
|
||||
AppLevel(String code, String desc) {
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
// Getter 方法(获取枚举属性)
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
|
||||
// 可选:根据 code 获取枚举项(便于业务使用)
|
||||
public static AppLevel getByCode(String code) {
|
||||
for (AppLevel level : values()) {
|
||||
if (level.code.equals(code)) {
|
||||
return level;
|
||||
}
|
||||
}
|
||||
return null; // 或抛出异常,根据业务需求调整
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,10 +17,12 @@ import androidx.core.content.ContextCompat;
|
||||
import cc.winboll.studio.libaes.interfaces.IWinBoLLActivity;
|
||||
import cc.winboll.studio.libaes.utils.WinBoLLActivityManager;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.positions.activities.LocationActivity;
|
||||
import cc.winboll.studio.positions.activities.WinBoLLActivity;
|
||||
import cc.winboll.studio.positions.services.MainService;
|
||||
import cc.winboll.studio.positions.utils.AppConfigsUtil;
|
||||
import cc.winboll.studio.positions.utils.ServiceUtil;
|
||||
import cc.winboll.studio.positions.utils.AppIconSwitcher;
|
||||
|
||||
/**
|
||||
* 主页面:仅负责
|
||||
@@ -81,6 +83,11 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main); // 关联主页面布局
|
||||
// 处理应用级别的切换请求
|
||||
handleSwitchRequest();
|
||||
|
||||
// 设置当前应用级别
|
||||
App.setAppLevel(this);
|
||||
|
||||
// 1. 初始化顶部 Toolbar(保留原逻辑,设置页面标题)
|
||||
initToolbar();
|
||||
@@ -94,6 +101,20 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
|
||||
//bindDistanceService();
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理快捷菜单的「切换入口」请求
|
||||
*/
|
||||
private void handleSwitchRequest() {
|
||||
Intent intent = getIntent();
|
||||
if (intent != null && "switch_launcher_main".equals(intent.getDataString())) {
|
||||
ToastUtils.show("应用级别切换到悟空笔记");
|
||||
AppIconSwitcher.switchToWukongIcon(this);
|
||||
} else if (intent != null && "switch_launcher_laojun".equals(intent.getDataString())) {
|
||||
ToastUtils.show("應用級別切換到老君道說");
|
||||
AppIconSwitcher.switchToLaojunIcon(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
@@ -113,9 +134,9 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
|
||||
mToolbar = (Toolbar) findViewById(R.id.toolbar); // Java 7 显式 findViewById + 强转
|
||||
setSupportActionBar(mToolbar);
|
||||
// 给ActionBar设置标题(先判断非空,避免空指针异常)
|
||||
if (getSupportActionBar() != null) {
|
||||
/*if (getSupportActionBar() != null) {
|
||||
getSupportActionBar().setTitle(getString(R.string.app_name));
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -141,18 +162,11 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
|
||||
// 权限就绪:执行服务启停逻辑
|
||||
if (isChecked) {
|
||||
LogUtils.d(TAG, "设置启动服务");
|
||||
AppConfigsUtil.getInstance(MainActivity.this).setIsEnableMainService(true);
|
||||
// 启动服务(startService确保服务独立运行,不受Activity绑定影响)
|
||||
startService(new Intent(MainActivity.this, MainService.class));
|
||||
ServiceUtil.startAutoService(MainActivity.this);
|
||||
} else {
|
||||
LogUtils.d(TAG, "设置关闭服务");
|
||||
AppConfigsUtil.getInstance(MainActivity.this).setIsEnableMainService(false);
|
||||
// 停止服务前先解绑,避免服务被Activity持有
|
||||
// if (isServiceBound) {
|
||||
// unbindService(mServiceConn);
|
||||
// isServiceBound = false;
|
||||
// }
|
||||
stopService(new Intent(MainActivity.this, MainService.class));
|
||||
|
||||
ServiceUtil.stopAutoService(MainActivity.this);
|
||||
}
|
||||
|
||||
mManagePositionsButton.setEnabled(isChecked);
|
||||
|
||||
@@ -0,0 +1,361 @@
|
||||
package cc.winboll.studio.positions.receivers;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/10/28 19:07
|
||||
* @Describe MotionStatusReceiver
|
||||
*/
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
import android.hardware.SensorManager;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.text.TextUtils;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.positions.services.MainService;
|
||||
import cc.winboll.studio.positions.utils.ServiceUtil;
|
||||
|
||||
/**
|
||||
* 运动状态监听Receiver
|
||||
* 功能:1.持续监听传感器(不关闭) 2.每5秒计算运动状态 3.按状态切换GPS模式(实时/30秒定时)
|
||||
*/
|
||||
public class MotionStatusReceiver extends BroadcastReceiver implements SensorEventListener {
|
||||
public static final String TAG = "MotionStatusReceiver";
|
||||
|
||||
// 广播Action
|
||||
public static final String ACTION_MOTION_STATUS_RECEIVER = "cc.winboll.studio.positions.receivers.MotionStatusReceiver";
|
||||
public static final String EXTRA_SENSORS_ENABLE = "EXTRA_SENSORS_ENABLE";
|
||||
// 传感器启动状态标志位
|
||||
boolean mIsSensorsEnable = false;
|
||||
// 运动状态常量
|
||||
private static final int MOTION_STATUS_STATIC = 0; // 静止/低运动
|
||||
private static final int MOTION_STATUS_WALKING = 1; // 行走/高速运动
|
||||
// 配置参数(按需求调整)
|
||||
private static final float ACCELEROMETER_THRESHOLD = 0.8f; // 加速度阈值
|
||||
private static final float GYROSCOPE_THRESHOLD = 0.5f; // 陀螺仪阈值
|
||||
private static final long STATUS_CALC_INTERVAL = 5000; // 运动状态计算间隔(5秒)
|
||||
private static final long GPS_STATIC_INTERVAL = 30; // 静止时GPS间隔(30秒)
|
||||
// 核心对象
|
||||
private volatile SensorManager mSensorManager;
|
||||
private Sensor mAccelerometer;
|
||||
private Sensor mGyroscope;
|
||||
private volatile boolean mIsSensorListening = false; // 传感器是否持续监听
|
||||
private int mCurrentMotionStatus = MOTION_STATUS_STATIC; // 当前运动状态
|
||||
private Handler mMainHandler; // 主线程Handler(用于定时计算)
|
||||
private Context mBroadcastContext; // 广播上下文
|
||||
// 传感器数据缓存(用于5秒内数据汇总,避免单次波动误判)
|
||||
private float mAccelMax = 0f; // 5秒内加速度最大值
|
||||
private float mGyroMax = 0f; // 5秒内陀螺仪最大值
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
|
||||
LogUtils.d(TAG, "===== 接收器启动:onReceive() 开始执行 =====");
|
||||
this.mBroadcastContext = context;
|
||||
mMainHandler = new Handler(Looper.getMainLooper());
|
||||
|
||||
if (TextUtils.equals(intent.getAction(), ACTION_MOTION_STATUS_RECEIVER)) {
|
||||
boolean isSettingEnable = intent.getBooleanExtra(EXTRA_SENSORS_ENABLE, false);
|
||||
if (mIsSensorsEnable == false && isSettingEnable == true) {
|
||||
mIsSensorsEnable = true;
|
||||
// 1. 初始化传感器(必执行)
|
||||
initSensors();
|
||||
|
||||
|
||||
if (mAccelerometer == null || mGyroscope == null) {
|
||||
LogUtils.e(TAG, "设备缺少加速度/陀螺仪,无法持续监听");
|
||||
cleanResources(false); // 传感器不可用才清理
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 校验参数
|
||||
if (context == null || intent == null) {
|
||||
LogUtils.d(TAG, "onReceive():无效参数,终止处理");
|
||||
cleanResources(false);
|
||||
return;
|
||||
}
|
||||
LogUtils.d(TAG, "onReceive():接收到广播Action=" + intent.getAction());
|
||||
|
||||
// 3. 启动持续传感器监听(核心:不关闭,重复调用无影响)
|
||||
startSensorListening();
|
||||
|
||||
// 4. 启动5秒定时计算运动状态(核心:持续触发状态判断)
|
||||
startStatusCalcTimer();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 5. 处理外部广播触发(可选,保留外部控制能力)
|
||||
// if (TextUtils.equals(intent.getAction(), ACTION_MOTION_STATUS_RECEIVER)) {
|
||||
// int motionStatus = intent.getIntExtra(EXTRA_MOTION_STATUS, MOTION_STATUS_STATIC);
|
||||
// String statusDesc = motionStatus == MOTION_STATUS_WALKING ? "高速运动" : "静止/低运动";
|
||||
// LogUtils.d(TAG, "外部广播触发,强制设置运动状态:" + statusDesc);
|
||||
// mCurrentMotionStatus = motionStatus;
|
||||
// handleMotionStatus(mCurrentMotionStatus); // 立即执行GPS切换
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化传感器(持续监听,复用实例)
|
||||
*/
|
||||
private void initSensors() {
|
||||
LogUtils.d(TAG, "initSensors():初始化传感器");
|
||||
if (mSensorManager != null || mBroadcastContext == null) return;
|
||||
|
||||
mSensorManager = (SensorManager) mBroadcastContext.getSystemService(Context.SENSOR_SERVICE);
|
||||
if (mSensorManager == null) {
|
||||
LogUtils.e(TAG, "设备不支持传感器服务");
|
||||
return;
|
||||
}
|
||||
// 获取传感器实例(持续复用,不销毁)
|
||||
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
|
||||
mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
|
||||
|
||||
LogUtils.d(TAG, "传感器初始化结果:加速度=" + (mAccelerometer != null) + ",陀螺仪=" + (mGyroscope != null));
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动传感器持续监听(核心:不关闭,注册一次一直生效)
|
||||
*/
|
||||
private void startSensorListening() {
|
||||
if (mSensorManager == null || mAccelerometer == null || mGyroscope == null) return;
|
||||
|
||||
if (!mIsSensorListening) {
|
||||
// 注册传感器监听(持续生效,直到服务销毁才注销)
|
||||
mSensorManager.registerListener(
|
||||
this,
|
||||
mAccelerometer,
|
||||
SensorManager.SENSOR_DELAY_NORMAL, // 正常延迟,平衡性能与精度
|
||||
mMainHandler
|
||||
);
|
||||
mSensorManager.registerListener(
|
||||
this,
|
||||
mGyroscope,
|
||||
SensorManager.SENSOR_DELAY_NORMAL,
|
||||
mMainHandler
|
||||
);
|
||||
mIsSensorListening = true;
|
||||
LogUtils.d(TAG, "startSensorListening():传感器持续监听已启动(不关闭)");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动5秒定时计算运动状态(核心:周期性汇总传感器数据)
|
||||
*/
|
||||
private void startStatusCalcTimer() {
|
||||
if (mMainHandler == null) return;
|
||||
|
||||
// 移除旧任务(避免重复注册)
|
||||
mMainHandler.removeCallbacks(mStatusCalcRunnable);
|
||||
// 启动定时任务(每5秒执行一次)
|
||||
mMainHandler.postDelayed(mStatusCalcRunnable, STATUS_CALC_INTERVAL);
|
||||
LogUtils.d(TAG, "startStatusCalcTimer():5秒运动状态计算定时器已启动");
|
||||
}
|
||||
|
||||
/**
|
||||
* 运动状态计算任务(5秒执行一次)
|
||||
*/
|
||||
private final Runnable mStatusCalcRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// 1. 基于5秒内缓存的最大传感器数据判断状态
|
||||
boolean isHighMotion = (mAccelMax > ACCELEROMETER_THRESHOLD) && (mGyroMax > GYROSCOPE_THRESHOLD);
|
||||
int newMotionStatus = isHighMotion ? MOTION_STATUS_WALKING : MOTION_STATUS_STATIC;
|
||||
|
||||
// 2. 状态变化时才处理(避免频繁切换GPS)
|
||||
if (newMotionStatus != mCurrentMotionStatus) {
|
||||
mCurrentMotionStatus = newMotionStatus;
|
||||
String statusDesc = isHighMotion ? "高速运动" : "静止/低运动";
|
||||
LogUtils.d(TAG, "运动状态更新(5秒计算):" + statusDesc
|
||||
+ "(加速度最大值=" + mAccelMax + ",陀螺仪最大值=" + mGyroMax + ")");
|
||||
handleMotionStatus(newMotionStatus); // 切换GPS模式
|
||||
} else {
|
||||
LogUtils.d(TAG, "运动状态无变化(5秒计算):" + (isHighMotion ? "高速运动" : "静止/低运动"));
|
||||
}
|
||||
|
||||
// 3. 重置传感器数据缓存,准备下一个5秒周期
|
||||
mAccelMax = 0f;
|
||||
mGyroMax = 0f;
|
||||
|
||||
// 4. 循环执行定时任务(核心:持续计算)
|
||||
mMainHandler.postDelayed(this, STATUS_CALC_INTERVAL);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 传感器数据变化回调(核心:实时缓存最大数据)
|
||||
*/
|
||||
@Override
|
||||
public void onSensorChanged(SensorEvent event) {
|
||||
if (event == null) return;
|
||||
|
||||
// 实时缓存5秒内的最大传感器数据(避免单次波动误判)
|
||||
switch (event.sensor.getType()) {
|
||||
case Sensor.TYPE_ACCELEROMETER:
|
||||
float accelTotal = Math.abs(event.values[0]) + Math.abs(event.values[1]) + Math.abs(event.values[2]);
|
||||
if (accelTotal > mAccelMax) mAccelMax = accelTotal; // 缓存最大值
|
||||
LogUtils.d(TAG, "加速度传感器实时数据:合值=" + accelTotal + "(当前5秒最大值=" + mAccelMax + ")");
|
||||
break;
|
||||
case Sensor.TYPE_GYROSCOPE:
|
||||
float gyroTotal = Math.abs(event.values[0]) + Math.abs(event.values[1]) + Math.abs(event.values[2]);
|
||||
if (gyroTotal > mGyroMax) mGyroMax = gyroTotal; // 缓存最大值
|
||||
LogUtils.d(TAG, "陀螺仪实时数据:合值=" + gyroTotal + "(当前5秒最大值=" + mGyroMax + ")");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理运动状态(核心:按状态切换GPS模式)
|
||||
*/
|
||||
private void handleMotionStatus(int motionStatus) {
|
||||
LogUtils.d(TAG, "handleMotionStatus():开始处理运动状态,切换GPS模式");
|
||||
if (mBroadcastContext == null) {
|
||||
LogUtils.w(TAG, "上下文为空,无法处理GPS");
|
||||
return;
|
||||
}
|
||||
|
||||
MainService mainService = getMainService();
|
||||
if (mainService == null) {
|
||||
LogUtils.e(TAG, "MainService未启动,GPS控制失败");
|
||||
return;
|
||||
}
|
||||
|
||||
if (motionStatus == MOTION_STATUS_WALKING) {
|
||||
// 高速运动:启动GPS实时更新(2秒/1米)
|
||||
handleHighMotionGPS(mainService);
|
||||
} else {
|
||||
// 静止/低运动:启动GPS30秒定时更新
|
||||
handleStaticGPS(mainService);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 高速运动GPS处理:实时更新
|
||||
*/
|
||||
private void handleHighMotionGPS(MainService mainService) {
|
||||
// 动态权限判断(Android 6.0+)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
|
||||
mBroadcastContext.checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION)
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
sendPermissionRequestBroadcast();
|
||||
return;
|
||||
}
|
||||
|
||||
// 启动实时GPS(已启动则不重复操作)
|
||||
if (!mainService.isGpsListening()) {
|
||||
mainService.startGpsLocation(); // 实时更新(2秒/1米)
|
||||
mainService.stopGpsStaticTimer(); // 停止定时GPS
|
||||
LogUtils.d(TAG, "高速运动:已启动GPS实时更新");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 静止/低运动GPS处理:30秒定时更新
|
||||
*/
|
||||
private void handleStaticGPS(MainService mainService) {
|
||||
// 停止实时GPS(已停止则不重复操作)
|
||||
if (mainService.isGpsListening()) {
|
||||
mainService.stopGpsLocation(); // 停止实时更新
|
||||
LogUtils.d(TAG, "静止/低运动:已停止GPS实时更新");
|
||||
}
|
||||
|
||||
// 启动30秒定时GPS(已启动则不重复操作)
|
||||
mainService.startGpsStaticTimer(GPS_STATIC_INTERVAL); // 30秒一次
|
||||
LogUtils.d(TAG, "静止/低运动:已启动GPS30秒定时更新");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取MainService实例(复用逻辑)
|
||||
*/
|
||||
private MainService getMainService() {
|
||||
if (mBroadcastContext == null) return null;
|
||||
|
||||
// 优先获取单例
|
||||
MainService singleton = MainService.getInstance(mBroadcastContext);
|
||||
if (singleton != null && singleton.isServiceRunning()) {
|
||||
return singleton;
|
||||
}
|
||||
|
||||
// 启动服务并重试
|
||||
if (!ServiceUtil.isServiceAlive(mBroadcastContext, MainService.class.getName())) {
|
||||
mBroadcastContext.startService(new Intent(mBroadcastContext, MainService.class));
|
||||
try {
|
||||
Thread.sleep(500); // 等待服务启动
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
return MainService.getInstance(mBroadcastContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送GPS权限申请广播(Receiver无法直接申请)
|
||||
*/
|
||||
private void sendPermissionRequestBroadcast() {
|
||||
Intent permissionIntent = new Intent("cc.winboll.studio.positions.ACTION_REQUEST_GPS_PERMISSION");
|
||||
permissionIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||
mBroadcastContext.sendBroadcast(permissionIntent);
|
||||
LogUtils.d(TAG, "GPS权限缺失,已发送申请广播");
|
||||
}
|
||||
|
||||
/**
|
||||
* 资源清理(核心:传感器不关闭,仅清理Handler和上下文)
|
||||
* @param isForceStopSensor 是否强制停止传感器(仅服务销毁时传true)
|
||||
*/
|
||||
private void cleanResources(boolean isForceStopSensor) {
|
||||
// 1. 停止定时计算任务
|
||||
if (mMainHandler != null) {
|
||||
mMainHandler.removeCallbacksAndMessages(null);
|
||||
mMainHandler = null;
|
||||
LogUtils.d(TAG, "cleanResources():已停止运动状态计算定时器");
|
||||
}
|
||||
|
||||
// 2. 强制停止传感器(仅当外部触发销毁时执行,正常情况不关闭)
|
||||
if (isForceStopSensor && mSensorManager != null && mIsSensorListening) {
|
||||
mSensorManager.unregisterListener(this);
|
||||
mIsSensorListening = false;
|
||||
LogUtils.d(TAG, "cleanResources():已强制停止传感器监听");
|
||||
}
|
||||
|
||||
// 3. 置空上下文(避免内存泄漏)
|
||||
mBroadcastContext = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 传感器精度变化回调(日志监控)
|
||||
*/
|
||||
@Override
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||
String sensorType = sensor.getType() == Sensor.TYPE_ACCELEROMETER ? "加速度" : "陀螺仪";
|
||||
String accuracyDesc = getAccuracyDesc(accuracy);
|
||||
LogUtils.d(TAG, sensorType + "传感器精度变化:" + accuracyDesc);
|
||||
}
|
||||
|
||||
/**
|
||||
* 传感器精度描述转换
|
||||
*/
|
||||
private String getAccuracyDesc(int accuracy) {
|
||||
switch (accuracy) {
|
||||
case SensorManager.SENSOR_STATUS_ACCURACY_HIGH: return "高";
|
||||
case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM: return "中";
|
||||
case SensorManager.SENSOR_STATUS_ACCURACY_LOW: return "低";
|
||||
case SensorManager.SENSOR_STATUS_UNRELIABLE: return "不可靠";
|
||||
default: return "未知";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 补充:Receiver销毁时强制清理(需在MainService注销时调用)
|
||||
*/
|
||||
public void forceCleanResources() {
|
||||
cleanResources(true); // 强制停止传感器
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ import cc.winboll.studio.positions.utils.ServiceUtil;
|
||||
public class AssistantService extends Service {
|
||||
|
||||
public final static String TAG = "AssistantService";
|
||||
public static final String EXTRA_IS_SETTING_TO_ENABLE = "EXTRA_IS_SETTING_TO_ENABLE";
|
||||
|
||||
MyServiceConnection mMyServiceConnection;
|
||||
volatile boolean mIsServiceRunning;
|
||||
@@ -37,13 +38,18 @@ public class AssistantService extends Service {
|
||||
}
|
||||
// 设置运行参数
|
||||
mIsServiceRunning = false;
|
||||
if (mAppConfigsUtil.isEnableMainService(true)) {
|
||||
run();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
if (mAppConfigsUtil.isEnableMainService(true)) {
|
||||
run();
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
return mAppConfigsUtil.isEnableMainService(true) ? Service.START_STICKY : super.onStartCommand(intent, flags, startId);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -41,11 +41,11 @@ public class AppConfigsUtil {
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
void loadConfigs() {
|
||||
public void loadConfigs() {
|
||||
mAppConfigsModel = AppConfigsModel.loadBean(mContext, AppConfigsModel.class);
|
||||
}
|
||||
|
||||
void saveConfigs() {
|
||||
public void saveConfigs() {
|
||||
AppConfigsModel.saveBean(mContext, mAppConfigsModel);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,222 @@
|
||||
package cc.winboll.studio.positions.utils;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/11/10 09:51
|
||||
* @Describe 应用图标切换工具类(启用组件时创建对应快捷方式)
|
||||
*/
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
import cc.winboll.studio.positions.R;
|
||||
|
||||
public class AppIconSwitcher {
|
||||
public static final String TAG = "AppIconSwitcher";
|
||||
|
||||
// 组件名常量(需与 AndroidManifest 一致)
|
||||
private static final String WUKONG_ACTIVITY = "cc.winboll.studio.positions.MainActivityWukong";
|
||||
private static final String LAOJUN_ACTIVITY = "cc.winboll.studio.positions.MainActivityLaojun";
|
||||
|
||||
// 快捷方式配置(名称+图标,需与实际资源匹配)
|
||||
private static final String WUKONG_SHORTCUT_NAME = "位置服务-Wukong";
|
||||
private static final int WUKONG_SHORTCUT_ICON = R.drawable.ic_launcher; // Wukong 图标资源
|
||||
private static final String LAOJUN_SHORTCUT_NAME = "位置服务-Laojun";
|
||||
private static final int LAOJUN_SHORTCUT_ICON = R.mipmap.ic_launcher; // Laojun 图标资源
|
||||
|
||||
/**
|
||||
* 切换到 Laojun 图标:禁用 Wukong,启用 Laojun + 创建 Laojun 快捷方式
|
||||
*/
|
||||
public static boolean switchToLaojunIcon(Context context) {
|
||||
if (context == null) {
|
||||
Log.e(TAG, "切换失败:上下文为空");
|
||||
Toast.makeText(context, "图标切换失败", Toast.LENGTH_SHORT).show();
|
||||
return false;
|
||||
}
|
||||
|
||||
PackageManager pm = context.getPackageManager();
|
||||
ComponentName wukongComponent = new ComponentName(context, WUKONG_ACTIVITY);
|
||||
ComponentName laojunComponent = new ComponentName(context, LAOJUN_ACTIVITY);
|
||||
|
||||
try {
|
||||
// 1. 禁用 Wukong 组件,启用 Laojun 组件
|
||||
disableComponent(pm, wukongComponent);
|
||||
enableComponent(pm, laojunComponent);
|
||||
|
||||
// 2. 创建 Laojun 组件对应的快捷方式(自动去重)
|
||||
boolean shortcutCreated = createComponentShortcut(context, laojunComponent, LAOJUN_SHORTCUT_NAME, LAOJUN_SHORTCUT_ICON);
|
||||
|
||||
// 3. 通知桌面刷新图标
|
||||
context.sendBroadcast(new Intent(Intent.ACTION_PACKAGE_CHANGED)
|
||||
.setData(android.net.Uri.parse("package:" + context.getPackageName())));
|
||||
|
||||
// 4. 反馈结果
|
||||
String logMsg = shortcutCreated ? "启用 Laojun + 快捷方式创建成功" : "启用 Laojun 成功,快捷方式创建失败";
|
||||
String toastMsg = shortcutCreated ? "图标切换为 Laojun,已创建快捷方式" : "图标切换为 Laojun,快捷方式创建失败";
|
||||
Log.d(TAG, logMsg);
|
||||
Toast.makeText(context, toastMsg, Toast.LENGTH_SHORT).show();
|
||||
|
||||
return true;
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Laojun 图标切换失败:" + e.getMessage());
|
||||
// 失败兜底:启用 Wukong 组件
|
||||
enableComponent(pm, wukongComponent);
|
||||
Toast.makeText(context, "图标切换失败", Toast.LENGTH_SHORT).show();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换到 Wukong 图标:启用 Wukong,禁用 Laojun + 创建 Wukong 快捷方式
|
||||
*/
|
||||
public static boolean switchToWukongIcon(Context context) {
|
||||
if (context == null) {
|
||||
Log.e(TAG, "切换失败:上下文为空");
|
||||
Toast.makeText(context, "图标切换失败", Toast.LENGTH_SHORT).show();
|
||||
return false;
|
||||
}
|
||||
|
||||
PackageManager pm = context.getPackageManager();
|
||||
ComponentName wukongComponent = new ComponentName(context, WUKONG_ACTIVITY);
|
||||
ComponentName laojunComponent = new ComponentName(context, LAOJUN_ACTIVITY);
|
||||
|
||||
try {
|
||||
// 1. 启用 Wukong 组件,禁用 Laojun 组件
|
||||
enableComponent(pm, wukongComponent);
|
||||
disableComponent(pm, laojunComponent);
|
||||
|
||||
// 2. 创建 Wukong 组件对应的快捷方式(自动去重)
|
||||
boolean shortcutCreated = createComponentShortcut(context, wukongComponent, WUKONG_SHORTCUT_NAME, WUKONG_SHORTCUT_ICON);
|
||||
|
||||
// 3. 通知桌面刷新图标
|
||||
context.sendBroadcast(new Intent(Intent.ACTION_PACKAGE_CHANGED)
|
||||
.setData(android.net.Uri.parse("package:" + context.getPackageName())));
|
||||
|
||||
// 4. 反馈结果
|
||||
String logMsg = shortcutCreated ? "启用 Wukong + 快捷方式创建成功" : "启用 Wukong 成功,快捷方式创建失败";
|
||||
String toastMsg = shortcutCreated ? "图标切换为 Wukong,已创建快捷方式" : "图标切换为 Wukong,快捷方式创建失败";
|
||||
Log.d(TAG, logMsg);
|
||||
Toast.makeText(context, toastMsg, Toast.LENGTH_SHORT).show();
|
||||
|
||||
return true;
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Wukong 图标切换失败:" + e.getMessage());
|
||||
// 失败兜底:启用 Wukong 组件
|
||||
enableComponent(pm, wukongComponent);
|
||||
Toast.makeText(context, "图标切换失败", Toast.LENGTH_SHORT).show();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建指定组件的桌面快捷方式(自动去重,兼容 Android 8.0+)
|
||||
* @param component 目标组件(如 LAOJUN_ACTIVITY)
|
||||
* @param name 快捷方式名称
|
||||
* @param iconRes 快捷方式图标资源ID
|
||||
* @return 是否创建成功
|
||||
*/
|
||||
private static boolean createComponentShortcut(Context context, ComponentName component, String name, int iconRes) {
|
||||
if (context == null || component == null || name == null || iconRes == 0) {
|
||||
Log.e(TAG, "快捷方式创建失败:参数为空");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Android 8.0+(API 26+):使用 ShortcutManager(系统推荐)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
try {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
android.content.pm.ShortcutManager shortcutManager = context.getSystemService(android.content.pm.ShortcutManager.class);
|
||||
if (shortcutManager == null || !shortcutManager.isRequestPinShortcutSupported()) {
|
||||
Log.e(TAG, "系统不支持创建快捷方式");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查是否已存在该组件的快捷方式(去重)
|
||||
for (android.content.pm.ShortcutInfo info : shortcutManager.getPinnedShortcuts()) {
|
||||
if (component.getClassName().equals(info.getIntent().getComponent().getClassName())) {
|
||||
Log.d(TAG, "快捷方式已存在:" + component.getClassName());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// 构建启动目标组件的意图
|
||||
Intent launchIntent = new Intent(Intent.ACTION_MAIN)
|
||||
.setComponent(component)
|
||||
.addCategory(Intent.CATEGORY_LAUNCHER)
|
||||
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
|
||||
// 构建快捷方式信息
|
||||
android.content.pm.ShortcutInfo shortcutInfo = new android.content.pm.ShortcutInfo.Builder(context, component.getClassName())
|
||||
.setShortLabel(name)
|
||||
.setLongLabel(name)
|
||||
.setIcon(android.graphics.drawable.Icon.createWithResource(context, iconRes))
|
||||
.setIntent(launchIntent)
|
||||
.build();
|
||||
|
||||
// 请求创建快捷方式(需用户确认)
|
||||
shortcutManager.requestPinShortcut(shortcutInfo, null);
|
||||
return true;
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Android O+ 快捷方式创建失败:" + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// Android 8.0 以下:使用广播(兼容旧机型)
|
||||
try {
|
||||
// 构建启动目标组件的意图
|
||||
Intent launchIntent = new Intent(Intent.ACTION_MAIN)
|
||||
.setComponent(component)
|
||||
.addCategory(Intent.CATEGORY_LAUNCHER)
|
||||
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
|
||||
// 构建创建快捷方式的广播意图
|
||||
Intent installIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
|
||||
installIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
|
||||
installIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
|
||||
installIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
|
||||
Intent.ShortcutIconResource.fromContext(context, iconRes));
|
||||
installIntent.putExtra("duplicate", false); // 禁止重复创建
|
||||
|
||||
context.sendBroadcast(installIntent);
|
||||
return true;
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Android O- 快捷方式创建失败:" + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启用组件(带状态检查,避免重复操作)
|
||||
*/
|
||||
private static void enableComponent(PackageManager pm, ComponentName component) {
|
||||
if (pm.getComponentEnabledSetting(component) != PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
|
||||
pm.setComponentEnabledSetting(
|
||||
component,
|
||||
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
|
||||
PackageManager.DONT_KILL_APP | PackageManager.SYNCHRONOUS
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 禁用组件(带状态检查,避免重复操作)
|
||||
*/
|
||||
private static void disableComponent(PackageManager pm, ComponentName component) {
|
||||
if (pm.getComponentEnabledSetting(component) != PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
|
||||
pm.setComponentEnabledSetting(
|
||||
component,
|
||||
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
||||
PackageManager.DONT_KILL_APP | PackageManager.SYNCHRONOUS
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,246 @@
|
||||
package cc.winboll.studio.positions.utils;
|
||||
import android.content.Context;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.text.TextUtils;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.positions.models.PositionModel;
|
||||
import cc.winboll.studio.positions.models.PositionTaskModel;
|
||||
import cc.winboll.studio.positions.services.MainService;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/10/27 18:40
|
||||
* @Describe 距离计算工具集(单例模式)
|
||||
*/
|
||||
public class DistanceCalculatorUtil {
|
||||
|
||||
public static final String TAG = "DistanceCalculatorUtil";
|
||||
|
||||
// 1. 私有静态 volatile 实例:保证多线程下实例可见性,避免指令重排序导致的空指针
|
||||
private static volatile DistanceCalculatorUtil sInstance;
|
||||
Context mContext;
|
||||
ArrayList<PositionModel> mPositionList; // 位置数据列表
|
||||
ArrayList<PositionTaskModel> mAllTasks;// 任务数据列表
|
||||
PositionModel mGpsPositionCalculated;
|
||||
long mLastCalculatedTime = 0;
|
||||
long mMinCalculatedTimeBettween = 30000; // GPS数据更新时,两次计算之间的最小时间间隔
|
||||
double mMinjumpDistance = 10.0f; // GPS数据更新时,能跳跃距离最小有效值,达到有效值时,两次计算之间的最小时间间隔阀值将被忽略。
|
||||
|
||||
// 2. 私有构造器:禁止外部通过 new 关键字创建实例,确保单例唯一性
|
||||
private DistanceCalculatorUtil(Context context) {
|
||||
// 可选:初始化工具类依赖的资源(如配置参数、缓存等)
|
||||
mContext = context;
|
||||
|
||||
LogUtils.d(TAG, "DistanceCalculatorUtil 单例实例初始化");
|
||||
}
|
||||
|
||||
// 3. 公开静态方法:双重校验锁获取单例,兼顾线程安全与性能
|
||||
public static DistanceCalculatorUtil getInstance(Context context) {
|
||||
// 第一重校验:避免已创建实例时的频繁加锁,提升性能
|
||||
if (sInstance == null) {
|
||||
// 加锁:确保多线程下仅一个线程进入实例创建逻辑
|
||||
synchronized (DistanceCalculatorUtil.class) {
|
||||
// 第二重校验:防止多线程并发时重复创建实例
|
||||
if (sInstance == null) {
|
||||
sInstance = new DistanceCalculatorUtil(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
// ---------------------- 以下可补充距离计算相关工具方法 ----------------------
|
||||
/**
|
||||
* 示例:Haversine 公式计算两点间距离(单位:米)
|
||||
* @param lat1 第一点纬度
|
||||
* @param lon1 第一点经度
|
||||
* @param lat2 第二点纬度
|
||||
* @param lon2 第二点经度
|
||||
* @return 两点间直线距离(米),计算失败返回 -1
|
||||
*/
|
||||
public static double calculateHaversineDistance(double lat1, double lon1, double lat2, double lon2) {
|
||||
try {
|
||||
final double EARTH_RADIUS = 6371000; // 地球半径(米)
|
||||
// 角度转弧度
|
||||
double latDiff = Math.toRadians(lat2 - lat1);
|
||||
double lonDiff = Math.toRadians(lon2 - lon1);
|
||||
|
||||
// Haversine 核心公式
|
||||
double a = Math.sin(latDiff / 2) * Math.sin(latDiff / 2)
|
||||
+ Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
|
||||
* Math.sin(lonDiff / 2) * Math.sin(lonDiff / 2);
|
||||
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||
|
||||
return EARTH_RADIUS * c; // 返回距离(米)
|
||||
} catch (Exception e) {
|
||||
LogUtils.d(TAG, "Haversine 距离计算失败:" + e.getMessage());
|
||||
return -1; // 标记计算失败
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 计算两点间距离(Haversine公式,纯Java 7 基础API,无数学工具类依赖)
|
||||
* @param gpsLat GPS纬度
|
||||
* @param gpsLon GPS经度
|
||||
* @param posLat 目标位置纬度
|
||||
* @param posLon 目标位置经度
|
||||
* @return 两点间距离(单位:米)
|
||||
*/
|
||||
/*private double calculateHaversineDistance(double gpsLat, double gpsLon, double posLat, double posLon) {
|
||||
final double EARTH_RADIUS = 6371000; // 地球半径(米)
|
||||
double latDiff = Math.toRadians(posLat - gpsLat);
|
||||
double lonDiff = Math.toRadians(posLon - gpsLon);
|
||||
// Haversine公式核心计算(Java 7 基础数学方法)
|
||||
double a = Math.sin(latDiff / 2) * Math.sin(latDiff / 2)
|
||||
+ Math.cos(Math.toRadians(gpsLat)) * Math.cos(Math.toRadians(posLat))
|
||||
* Math.sin(lonDiff / 2) * Math.sin(lonDiff / 2);
|
||||
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||
return EARTH_RADIUS * c;
|
||||
}*/
|
||||
|
||||
/**
|
||||
* 校验所有任务触发条件(距离达标则触发任务通知)
|
||||
*/
|
||||
public void checkAllTaskTriggerCondition(PositionModel currentGpsPosition) {
|
||||
if (currentGpsPosition == null) {
|
||||
LogUtils.d(TAG, "传入坐标参数为空,退出函数。");
|
||||
return;
|
||||
}
|
||||
|
||||
// 计算频率控制模块
|
||||
//
|
||||
// 计算与最近一次GPS计算的时间间隔
|
||||
long nCalculatedTimeBettween = System.currentTimeMillis() - mLastCalculatedTime;
|
||||
// 计算跳跃距离
|
||||
double gpsPositionCalculatedLatitude = mGpsPositionCalculated == null ?0.0f: mGpsPositionCalculated.getLatitude();
|
||||
double gpsPositionCalculatedLongitude = mGpsPositionCalculated == null ?0.0f: mGpsPositionCalculated.getLongitude();
|
||||
double jumpDistance = calculateHaversineDistance(gpsPositionCalculatedLatitude, gpsPositionCalculatedLongitude, currentGpsPosition.getLatitude(), currentGpsPosition.getLongitude());
|
||||
if (jumpDistance < mMinjumpDistance) {
|
||||
LogUtils.d(TAG, String.format("checkAllTaskTriggerCondition:跳跃距离%f,小于50米。", jumpDistance));
|
||||
// 跳跃距离小于最小有效跳跃值
|
||||
if (nCalculatedTimeBettween < mMinCalculatedTimeBettween) {
|
||||
//间隔小于最小时间间隔设定
|
||||
LogUtils.d(TAG, String.format("checkAllTaskTriggerCondition:与最近一次计算间隔时间%d,坐标变化忽略。", nCalculatedTimeBettween));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (mGpsPositionCalculated == null) {
|
||||
mGpsPositionCalculated = currentGpsPosition;
|
||||
LogUtils.d(TAG, "最后计算位置记录为空,现在使用新坐标为初始化。");
|
||||
}
|
||||
|
||||
LogUtils.d(TAG, String.format("checkAllTaskTriggerCondition:跳跃距离%f,与上次计算间隔%d,现在启动任务数据计算。", jumpDistance, nCalculatedTimeBettween));
|
||||
|
||||
// 获取位置任务基础数据
|
||||
MainService mainService = MainService.getInstance(mContext);
|
||||
mPositionList = mainService.getPositionList();
|
||||
mAllTasks = mainService.getAllTasks();
|
||||
|
||||
// 位置数据为空,跳过校验。
|
||||
if (mPositionList.isEmpty()) {
|
||||
LogUtils.d(TAG, "checkAllTaskTriggerCondition:位置数据为空,跳过距离计算。");
|
||||
return;
|
||||
}
|
||||
|
||||
// 更新所有位置点的位置距离数据
|
||||
refreshRealPositionDistance(currentGpsPosition);
|
||||
|
||||
// 任务数据为空,跳过校验。
|
||||
if (mAllTasks.isEmpty()) {
|
||||
LogUtils.d(TAG, "checkAllTaskTriggerCondition:任务数据为空,跳过任务提醒检查计算。");
|
||||
return;
|
||||
}
|
||||
|
||||
// 迭代器遍历任务(Java 7 安全遍历,避免并发修改异常)
|
||||
Iterator<PositionTaskModel> taskIter = mAllTasks.iterator();
|
||||
while (taskIter.hasNext()) {
|
||||
PositionTaskModel task = taskIter.next();
|
||||
// 仅校验“已启用”且“绑定有效位置”的任务
|
||||
if (!task.isEnable() || TextUtils.isEmpty(task.getPositionId())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 查找任务绑定的位置(Java 7 迭代器遍历位置列表)
|
||||
PositionModel bindPos = null;
|
||||
Iterator<PositionModel> posIter = mPositionList.iterator();
|
||||
while (posIter.hasNext()) {
|
||||
PositionModel pos = posIter.next();
|
||||
if (task.getPositionId().equals(pos.getPositionId())) {
|
||||
bindPos = pos;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (bindPos == null) {
|
||||
LogUtils.w(TAG, "任务ID=" + task.getTaskId() + ":绑定位置不存在,跳过");
|
||||
task.setIsBingo(false);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 校验任务开始时间
|
||||
if (task.getStartTime() > System.currentTimeMillis()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 校验距离条件(判断是否满足任务触发阈值)
|
||||
double currentDistance = bindPos.getRealPositionDistance();
|
||||
if (currentDistance < 0) {
|
||||
LogUtils.w(TAG, "任务ID=" + task.getTaskId() + ":距离计算失败,跳过");
|
||||
task.setIsBingo(false);
|
||||
continue;
|
||||
}
|
||||
|
||||
boolean isTriggered = false;
|
||||
int taskDistance = task.getDiscussDistance();
|
||||
// 任务触发条件:大于/小于指定距离(Java 7 基础判断,无三元运算符嵌套)
|
||||
if (task.isGreaterThan()) {
|
||||
isTriggered = currentDistance > taskDistance;
|
||||
} else if (task.isLessThan()) {
|
||||
isTriggered = currentDistance < taskDistance;
|
||||
}
|
||||
|
||||
// 更新任务触发状态+发送通知(状态变化时才处理)
|
||||
if (task.isBingo() != isTriggered) {
|
||||
task.setIsBingo(isTriggered);
|
||||
if (isTriggered) {
|
||||
MainService.getInstance(mContext).sendTaskTriggerNotification(task, bindPos, currentDistance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MainService.getInstance(mContext).saveAllTasks(); // 持久化更新后的任务状态
|
||||
// 记录最后坐标更新点
|
||||
mGpsPositionCalculated = currentGpsPosition;
|
||||
// 记录数据计算时间
|
||||
mLastCalculatedTime = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 强制刷新所有位置距离(GPS更新后调用,计算距离+校验任务触发条件)
|
||||
*/
|
||||
public void refreshRealPositionDistance(PositionModel currentGpsPosition) {
|
||||
// 遍历所有位置计算距离(Java 7 增强for循环,无Stream)
|
||||
for (PositionModel pos : mPositionList) {
|
||||
if (pos.isEnableRealPositionDistance()) {
|
||||
double distance = DistanceCalculatorUtil.calculateHaversineDistance(
|
||||
currentGpsPosition.getLatitude(),
|
||||
currentGpsPosition.getLongitude(),
|
||||
pos.getLatitude(),
|
||||
pos.getLongitude()
|
||||
);
|
||||
pos.setRealPositionDistance(distance);
|
||||
} else {
|
||||
pos.setRealPositionDistance(-1); // 未启用距离计算,标记为无效
|
||||
}
|
||||
}
|
||||
|
||||
// 距离刷新后通知GPS监听者
|
||||
MainService.getInstance(mContext).notifyAllGpsListeners(currentGpsPosition);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
package cc.winboll.studio.positions.utils;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/11/05 15:49
|
||||
* @Describe LocalMotionDetector
|
||||
*/
|
||||
import android.content.Context;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
import android.hardware.SensorManager;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.util.Log;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
|
||||
/**
|
||||
* 本机运动状态监测工具(无联网,纯传感器)
|
||||
*/
|
||||
public class LocalMotionDetector implements SensorEventListener {
|
||||
public static final String TAG = "LocalMotionDetector";
|
||||
|
||||
// 配置参数(重点修改:调高运动阈值,适配坐立持机场景)
|
||||
private static final float MOTION_THRESHOLD = 1.8f; // 从0.5f调高到1.8f(过滤坐立轻微晃动)
|
||||
private static final long STATUS_CHECK_INTERVAL = 3000; // 3秒判断一次状态
|
||||
private static final int STEP_CHANGE_THRESHOLD = 2; // 3秒≥2步判定行走
|
||||
|
||||
private SensorManager mSensorManager;
|
||||
private Sensor mAccelerometer;
|
||||
private Sensor mStepCounter;
|
||||
private Handler mMainHandler;
|
||||
private MotionStatusCallback mCallback;
|
||||
|
||||
private boolean mIsDetecting = false;
|
||||
private float mLastAccelMagnitude = 0f;
|
||||
private int mLastStepCount = 0;
|
||||
private int mCurrentStepCount = 0;
|
||||
private boolean mIsWalking = false;
|
||||
|
||||
// 单例模式
|
||||
private static LocalMotionDetector sInstance;
|
||||
public static LocalMotionDetector getInstance() {
|
||||
if (sInstance == null) {
|
||||
synchronized (LocalMotionDetector.class) {
|
||||
if (sInstance == null) {
|
||||
sInstance = new LocalMotionDetector();
|
||||
}
|
||||
}
|
||||
}
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
private LocalMotionDetector() {
|
||||
mMainHandler = new Handler(Looper.getMainLooper());
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始监测运动状态
|
||||
*/
|
||||
public void startDetection(Context context, MotionStatusCallback callback) {
|
||||
if (mIsDetecting) return;
|
||||
mCallback = callback;
|
||||
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
|
||||
|
||||
// 初始化传感器
|
||||
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
|
||||
mStepCounter = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
|
||||
|
||||
// 注册传感器监听
|
||||
if (mAccelerometer != null) {
|
||||
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL, mMainHandler);
|
||||
}
|
||||
if (mStepCounter != null) {
|
||||
mSensorManager.registerListener(this, mStepCounter, SensorManager.SENSOR_DELAY_NORMAL, mMainHandler);
|
||||
LogUtils.d(TAG, "计步传感器已启动");
|
||||
} else {
|
||||
LogUtils.d(TAG, "设备不支持计步传感器,仅用加速度判断");
|
||||
}
|
||||
|
||||
// 启动定时状态检测
|
||||
mMainHandler.postDelayed(mStatusCheckRunnable, STATUS_CHECK_INTERVAL);
|
||||
mIsDetecting = true;
|
||||
LogUtils.d(TAG, "运动状态监测已启动");
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止监测
|
||||
*/
|
||||
public void stopDetection() {
|
||||
if (!mIsDetecting) return;
|
||||
if (mSensorManager != null) {
|
||||
mSensorManager.unregisterListener(this);
|
||||
}
|
||||
mMainHandler.removeCallbacksAndMessages(null);
|
||||
mIsDetecting = false;
|
||||
mIsWalking = false;
|
||||
mCallback = null;
|
||||
LogUtils.d(TAG, "运动状态监测已停止");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSensorChanged(SensorEvent event) {
|
||||
if (!mIsDetecting) return;
|
||||
switch (event.sensor.getType()) {
|
||||
case Sensor.TYPE_ACCELEROMETER:
|
||||
// 计算加速度幅度(保留原逻辑,阈值已调高)
|
||||
float accelX = Math.abs(event.values[0]);
|
||||
float accelY = Math.abs(event.values[1]);
|
||||
float accelZ = Math.abs(event.values[2]);
|
||||
mLastAccelMagnitude = accelX + accelY + accelZ;
|
||||
break;
|
||||
case Sensor.TYPE_STEP_COUNTER:
|
||||
// 累计步数
|
||||
mCurrentStepCount = (int) event.values[0];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 定时判断运动状态(优化逻辑:计步为0时,即使有轻微加速度也判定为静止)
|
||||
*/
|
||||
private final Runnable mStatusCheckRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (!mIsDetecting || mCallback == null) return;
|
||||
|
||||
//LogUtils.d(TAG, "mStatusCheckRunnable run");
|
||||
|
||||
boolean newIsWalking = false;
|
||||
// 结合计步器+加速度判断(优化:优先计步,无步数时严格按高阈值判断)
|
||||
if (mStepCounter != null) {
|
||||
int stepChange = mCurrentStepCount - mLastStepCount;
|
||||
// 只有“步数达标” 或 “无步数但加速度远超坐立幅度”,才判定为行走
|
||||
newIsWalking = (stepChange >= STEP_CHANGE_THRESHOLD)
|
||||
&& (mLastAccelMagnitude >= MOTION_THRESHOLD); // 增加步数+加速度双重校验
|
||||
mLastStepCount = mCurrentStepCount;
|
||||
} else {
|
||||
// 无计步器时,仅用高阈值判断
|
||||
newIsWalking = mLastAccelMagnitude >= MOTION_THRESHOLD;
|
||||
}
|
||||
|
||||
// 状态变化时回调
|
||||
if (newIsWalking != mIsWalking) {
|
||||
mIsWalking = newIsWalking;
|
||||
String statusDesc = mIsWalking ? "行走状态" : "静止/低运动状态";
|
||||
LogUtils.d(TAG, "运动状态变化:" + statusDesc + " | 加速度幅度:" + mLastAccelMagnitude); // 增加日志便于调试
|
||||
mCallback.onMotionStatusChanged(mIsWalking, statusDesc);
|
||||
}
|
||||
|
||||
LogUtils.d(TAG, String.format("运动状态 newIsWalking %s", newIsWalking));
|
||||
|
||||
// 循环检测
|
||||
mMainHandler.postDelayed(this, STATUS_CHECK_INTERVAL);
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
|
||||
|
||||
/**
|
||||
* 运动状态回调接口
|
||||
*/
|
||||
public interface MotionStatusCallback {
|
||||
void onMotionStatusChanged(boolean isWalking, String statusDesc);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,10 @@ package cc.winboll.studio.positions.utils;
|
||||
*/
|
||||
import android.app.ActivityManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.positions.services.AssistantService;
|
||||
import cc.winboll.studio.positions.services.MainService;
|
||||
import java.util.List;
|
||||
|
||||
public class ServiceUtil {
|
||||
@@ -31,4 +35,35 @@ public class ServiceUtil {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void stopAutoService(Context context) {
|
||||
AppConfigsUtil appConfigsUtil = AppConfigsUtil.getInstance(context);
|
||||
appConfigsUtil.setIsEnableMainService(false);
|
||||
appConfigsUtil.saveConfigs();
|
||||
// 关闭并设置主服务
|
||||
Intent intent1 = new Intent(context, MainService.class);
|
||||
intent1.putExtra(MainService.EXTRA_IS_SETTING_TO_ENABLE, false);
|
||||
context.stopService(intent1); // 先停止旧服务
|
||||
context.startService(intent1); // 传入新的启动标志位,返回给系统
|
||||
// 关闭并设置主服务守护进程
|
||||
Intent intent2 = new Intent(context, AssistantService.class);
|
||||
intent2.putExtra(AssistantService.EXTRA_IS_SETTING_TO_ENABLE, false);
|
||||
context.stopService(intent2); // 先停止旧服务
|
||||
context.startService(intent2); // 传入新的启动标志位,返回给系统
|
||||
// 再次关闭所有服务
|
||||
context.stopService(intent1);
|
||||
context.stopService(intent2);
|
||||
|
||||
LogUtils.d(TAG, "stopAutoService");
|
||||
}
|
||||
|
||||
public static void startAutoService(Context context) {
|
||||
AppConfigsUtil appConfigsUtil = AppConfigsUtil.getInstance(context);
|
||||
appConfigsUtil.setIsEnableMainService(true);
|
||||
appConfigsUtil.saveConfigs();
|
||||
Intent intent = new Intent(context, MainService.class);
|
||||
intent.putExtra(MainService.EXTRA_IS_SETTING_TO_ENABLE, true);
|
||||
context.startService(intent);
|
||||
LogUtils.d(TAG, "startAutoService");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,282 @@
|
||||
package cc.winboll.studio.positions.views;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/11/10 08:29
|
||||
* @Describe 沙漏计时器控件
|
||||
*/
|
||||
import android.content.Context;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.ClipDrawable;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.graphics.drawable.LayerDrawable;
|
||||
import android.text.InputFilter;
|
||||
import android.text.InputType;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.Gravity;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.EditText;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.Switch;
|
||||
import android.widget.TextView;
|
||||
|
||||
/**
|
||||
* 沙漏视图类(Java 7语法,修复ProgressDrawable和setHeight问题)
|
||||
*/
|
||||
public class HourglassView extends LinearLayout {
|
||||
public static final String TAG = "HourglassView";
|
||||
// 数据模型
|
||||
private String hourglassId;
|
||||
private int hour; // 小时
|
||||
private int minute; // 分钟
|
||||
private boolean isEnabled; // 开关状态
|
||||
|
||||
// 控件引用
|
||||
private EditText etHour;
|
||||
private EditText etMinute;
|
||||
private ProgressBar progressBar;
|
||||
private Switch switchControl;
|
||||
|
||||
// 样式参数
|
||||
private int textSize = 16;
|
||||
private int padding = 8;
|
||||
private int progressColor = 0xFF2196F3; // 进度条颜色
|
||||
private int progressBgColor = 0xFFE0E0E0; // 进度条背景色
|
||||
private int textColor = 0xFF333333;
|
||||
private int editTextWidth = 40; // 输入框宽度(dp)
|
||||
private int progressHeight = 8; // 进度条高度(dp,新增参数)
|
||||
|
||||
public HourglassView(Context context) {
|
||||
super(context);
|
||||
initView();
|
||||
}
|
||||
|
||||
public HourglassView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
initView();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化视图布局
|
||||
*/
|
||||
private void initView() {
|
||||
setOrientation(HORIZONTAL);
|
||||
setGravity(Gravity.CENTER_VERTICAL);
|
||||
setPadding(dp2px(padding), dp2px(padding), dp2px(padding), dp2px(padding));
|
||||
|
||||
// 1. 左侧时间输入区域(水平布局)
|
||||
LinearLayout inputLayout = new LinearLayout(getContext());
|
||||
inputLayout.setOrientation(HORIZONTAL);
|
||||
inputLayout.setGravity(Gravity.CENTER_VERTICAL);
|
||||
LayoutParams inputParams = new LayoutParams(
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
);
|
||||
inputParams.setMargins(0, 0, dp2px(padding * 2), 0);
|
||||
addView(inputLayout, inputParams);
|
||||
|
||||
// 小时输入框
|
||||
etHour = createNumberEditText();
|
||||
etHour.setHint("时");
|
||||
etHour.setFilters(new InputFilter[]{new InputFilter.LengthFilter(2)});
|
||||
inputLayout.addView(etHour, getEditTextParams());
|
||||
|
||||
// 分隔符
|
||||
TextView divider = new TextView(getContext());
|
||||
divider.setText(":");
|
||||
divider.setTextSize(textSize);
|
||||
divider.setTextColor(textColor);
|
||||
LayoutParams dividerParams = new LayoutParams(
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
);
|
||||
dividerParams.setMargins(dp2px(padding / 2), 0, dp2px(padding / 2), 0);
|
||||
inputLayout.addView(divider, dividerParams);
|
||||
|
||||
// 分钟输入框
|
||||
etMinute = createNumberEditText();
|
||||
etMinute.setHint("分");
|
||||
etMinute.setFilters(new InputFilter[]{new InputFilter.LengthFilter(2)});
|
||||
inputLayout.addView(etMinute, getEditTextParams());
|
||||
|
||||
// 2. 中间进度条(修复:通过LayoutParams设置高度,替代setHeight)
|
||||
progressBar = new ProgressBar(getContext(), null, android.R.attr.progressBarStyleHorizontal);
|
||||
progressBar.setProgressDrawable(createProgressDrawable()); // 传入Drawable类型
|
||||
|
||||
// 修复核心:用LayoutParams设置进度条高度(兼容低版本)
|
||||
LayoutParams progressParams = new LayoutParams(
|
||||
0,
|
||||
dp2px(progressHeight), // 直接在布局参数中设置高度(dp转px)
|
||||
1.0f
|
||||
);
|
||||
progressParams.setMargins(0, 0, dp2px(padding * 2), 0);
|
||||
addView(progressBar, progressParams);
|
||||
|
||||
// 3. 右侧开关
|
||||
switchControl = new Switch(getContext());
|
||||
switchControl.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(android.widget.CompoundButton buttonView, boolean isChecked) {
|
||||
isEnabled = isChecked;
|
||||
// 开关状态控制输入框是否可编辑
|
||||
etHour.setEnabled(!isChecked);
|
||||
etMinute.setEnabled(!isChecked);
|
||||
// 更新进度条(仅在开关开启时生效)
|
||||
if (isChecked) {
|
||||
updateProgressBar();
|
||||
}
|
||||
}
|
||||
});
|
||||
addView(switchControl);
|
||||
|
||||
// 初始状态
|
||||
isEnabled = false;
|
||||
etHour.setEnabled(true);
|
||||
etMinute.setEnabled(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建数字输入框
|
||||
*/
|
||||
private EditText createNumberEditText() {
|
||||
EditText editText = new EditText(getContext());
|
||||
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
|
||||
editText.setTextSize(textSize);
|
||||
editText.setTextColor(textColor);
|
||||
editText.setGravity(Gravity.CENTER);
|
||||
editText.setSingleLine(true);
|
||||
editText.setBackgroundResource(android.R.drawable.edit_text); // 默认输入框背景
|
||||
return editText;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取输入框布局参数
|
||||
*/
|
||||
private LayoutParams getEditTextParams() {
|
||||
LayoutParams params = new LayoutParams(
|
||||
dp2px(editTextWidth),
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
);
|
||||
params.setMargins(0, 0, dp2px(padding), 0);
|
||||
return params;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修复核心:创建ProgressDrawable(返回Drawable类型,而非Paint)
|
||||
* 用LayerDrawable实现「背景+进度」的双层进度条
|
||||
*/
|
||||
private Drawable createProgressDrawable() {
|
||||
// 1. 进度条背景(灰色)
|
||||
ColorDrawable bgDrawable = new ColorDrawable(progressBgColor);
|
||||
// 2. 进度条前景(主题色)
|
||||
ColorDrawable progressDrawable = new ColorDrawable(progressColor);
|
||||
// 3. 用ClipDrawable包裹前景,实现进度裁剪
|
||||
ClipDrawable clipDrawable = new ClipDrawable(progressDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
|
||||
|
||||
// 4. 组合成LayerDrawable(顺序:背景在下,进度在上)
|
||||
Drawable[] layers = new Drawable[]{bgDrawable, clipDrawable};
|
||||
LayerDrawable layerDrawable = new LayerDrawable(layers);
|
||||
|
||||
// 5. 设置进度条的层级ID(必须与系统ProgressBar的ID匹配)
|
||||
layerDrawable.setId(0, android.R.id.background);
|
||||
layerDrawable.setId(1, android.R.id.progress);
|
||||
|
||||
return layerDrawable;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新进度条(总时间 = 小时*60 + 分钟,单位:分钟)
|
||||
*/
|
||||
private void updateProgressBar() {
|
||||
try {
|
||||
// 获取输入的时间(为空时默认0)
|
||||
int inputHour = TextUtils.isEmpty(etHour.getText().toString().trim())
|
||||
? 0 : Integer.parseInt(etHour.getText().toString().trim());
|
||||
int inputMinute = TextUtils.isEmpty(etMinute.getText().toString().trim())
|
||||
? 0 : Integer.parseInt(etMinute.getText().toString().trim());
|
||||
|
||||
// 校验时间合法性(小时0-99,分钟0-59)
|
||||
inputHour = Math.max(0, Math.min(99, inputHour));
|
||||
inputMinute = Math.max(0, Math.min(59, inputMinute));
|
||||
|
||||
// 计算总分钟数(进度条最大值)
|
||||
int totalMinutes = inputHour * 60 + inputMinute;
|
||||
totalMinutes = Math.max(1, totalMinutes); // 最小1分钟,避免进度条无长度
|
||||
|
||||
// 更新进度条
|
||||
progressBar.setMax(totalMinutes);
|
||||
progressBar.setProgress(totalMinutes); // 初始显示满进度,可根据实际需求修改
|
||||
|
||||
// 更新数据模型
|
||||
this.hour = inputHour;
|
||||
this.minute = inputMinute;
|
||||
|
||||
} catch (NumberFormatException e) {
|
||||
// 输入非法时重置进度条
|
||||
progressBar.setMax(0);
|
||||
progressBar.setProgress(0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* dp转px(适配不同设备)
|
||||
*/
|
||||
private int dp2px(int dp) {
|
||||
return (int) (dp * getContext().getResources().getDisplayMetrics().density + 0.5f);
|
||||
}
|
||||
|
||||
// ------------------- 数据模型 getter/setter -------------------
|
||||
public String getHourglassId() {
|
||||
return hourglassId;
|
||||
}
|
||||
|
||||
public void setHourglassId(String hourglassId) {
|
||||
this.hourglassId = hourglassId;
|
||||
}
|
||||
|
||||
public int getHour() {
|
||||
return hour;
|
||||
}
|
||||
|
||||
public void setHour(int hour) {
|
||||
this.hour = Math.max(0, Math.min(99, hour)); // 限制范围
|
||||
etHour.setText(String.valueOf(this.hour));
|
||||
}
|
||||
|
||||
public int getMinute() {
|
||||
return minute;
|
||||
}
|
||||
|
||||
public void setMinute(int minute) {
|
||||
this.minute = Math.max(0, Math.min(59, minute)); // 限制范围
|
||||
etMinute.setText(String.valueOf(this.minute));
|
||||
}
|
||||
|
||||
public boolean isEnabled() {
|
||||
return isEnabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
isEnabled = enabled;
|
||||
switchControl.setChecked(enabled);
|
||||
}
|
||||
|
||||
/**
|
||||
* 手动更新进度条(外部调用)
|
||||
*/
|
||||
public void refreshProgress() {
|
||||
if (isEnabled) {
|
||||
updateProgressBar();
|
||||
}
|
||||
}
|
||||
|
||||
// 工具类:判断字符串是否为空(Java7无TextUtils.isEmpty,手动实现)
|
||||
private static class TextUtils {
|
||||
public static boolean isEmpty(CharSequence str) {
|
||||
return str == null || str.length() == 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,8 @@ import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import cc.winboll.studio.positions.App;
|
||||
import cc.winboll.studio.positions.AppLevel;
|
||||
|
||||
public class PositionTaskListView extends LinearLayout {
|
||||
// 视图模式常量
|
||||
@@ -483,6 +485,16 @@ public class PositionTaskListView extends LinearLayout {
|
||||
final EditText etEditDistance = dialogView.findViewById(R.id.et_edit_distance);
|
||||
Button btnCancel = dialogView.findViewById(R.id.btn_dialog_cancel);
|
||||
Button btnSave = dialogView.findViewById(R.id.btn_dialog_save);
|
||||
HourglassView hourglassView = dialogView.findViewById(R.id.hourglassView);
|
||||
if (App._mAppLevel == AppLevel.WUKONG) {
|
||||
hourglassView.setVisibility(View.GONE);
|
||||
} else if (App._mAppLevel == AppLevel.WUKONG) {
|
||||
hourglassView.setHourglassId("hourglass_001");
|
||||
hourglassView.setHour(1);
|
||||
hourglassView.setMinute(30);
|
||||
hourglassView.setEnabled(false); // 开启开关
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 绑定外层对话框内的控件
|
||||
|
||||
@@ -65,6 +65,16 @@
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
<cc.winboll.studio.positions.views.HourglassView
|
||||
android:id="@+id/hourglassView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"/>
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
@@ -85,7 +95,6 @@
|
||||
android:text="Text"
|
||||
android:layout_weight="1.0"/>
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name">悟空笔记</string>
|
||||
<string name="app_laojun_name">老君道說</string>
|
||||
</resources>
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
<resources>
|
||||
<string name="app_name">Positions</string>
|
||||
<string name="app_laojun_name">Positions</string>
|
||||
<string name="switch_to_main">切换到悟空笔记</string>
|
||||
<string name="switch_to_laojun">切换到老君道說</string>
|
||||
<string name="switch_disabled">切换功能不可用</string>
|
||||
</resources>
|
||||
|
||||
34
positions/src/main/res/xml/shortcuts.xml
Normal file
34
positions/src/main/res/xml/shortcuts.xml
Normal file
@@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<!-- 切换启动入口的快捷菜单 -->
|
||||
<shortcut
|
||||
android:shortcutId="switch_launcher_main"
|
||||
android:enabled="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:shortcutShortLabel="@string/switch_to_main"
|
||||
android:shortcutLongLabel="@string/switch_to_main"
|
||||
android:shortcutDisabledMessage="@string/switch_disabled">
|
||||
<intent
|
||||
android:action="cc.winboll.studio.positions.MainActivity"
|
||||
android:targetPackage="cc.winboll.studio.positions"
|
||||
android:targetClass="cc.winboll.studio.positions.MainActivity"
|
||||
android:data="switch_launcher" />
|
||||
|
||||
<categories android:name="android.shortcut.conversation" />
|
||||
</shortcut>
|
||||
<shortcut
|
||||
android:shortcutId="switch_launcher_laojun"
|
||||
android:enabled="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:shortcutShortLabel="@string/switch_to_laojun"
|
||||
android:shortcutLongLabel="@string/switch_to_laojun"
|
||||
android:shortcutDisabledMessage="@string/switch_disabled">
|
||||
<intent
|
||||
android:action="cc.winboll.studio.positions.MainActivityLaojun"
|
||||
android:targetPackage="cc.winboll.studio.positions"
|
||||
android:targetClass="cc.winboll.studio.positions.MainActivity"
|
||||
android:data="switch_launcher" />
|
||||
|
||||
<categories android:name="android.shortcut.conversation" />
|
||||
</shortcut>
|
||||
</shortcuts>
|
||||
Reference in New Issue
Block a user