MTK Android Q上关机闹钟无效时检查
系统源码修改
文件
alps/vendor/mediatek/proprietary/frameworks/base/services/core/java/com/mediatek/server/MtkAlarmManagerService.java
增加
/**
*For PowerOffalarm feature, this function is used for AlarmManagerService
* to set the latest alarm registered
*/
private void resetPoweroffAlarm(Alarm alarm) {
String setPackageName = alarm.operation.getTargetPackage();
long latestTime = alarm.when;
// Only DeskClock is allowed to set this alarm
if (mNativeData != 0 && mNativeData != -1) {
if (setPackageName.equals("com.android.deskclock")) {
set(mNativeData, PRE_SCHEDULE_POWER_OFF_ALARM,
latestTime / 1000, (latestTime % 1000) * 1000 * 1000);
/// M: For Poweroff alarm support for Aging app
} else if(setPackageName.equals("com.mediatek.sqa8.aging")) {
set(mNativeData, 7, latestTime / 1000, (latestTime % 1000) * 1000 * 1000);
///@}
} else {
// 在这里添加自己的包名
/**
* 如
* if ("com.xxx.xxxx".equals(setPackageName)) {
* set(mNativeData, PRE_SCHEDULE_POWER_OFF_ALARM, latestTime / 1000, (latestTime % 1000) * 1000 * 1000);
* }
*/
Slog.w(TAG, "unknown package (" + setPackageName + ") to set power off alarm");
}
Slog.i(TAG, "reset power off alarm is " + setPackageName);
} else {
Slog.i(TAG, " do not set alarm to RTC when fd close ");
}
}
取消
/**
* For PowerOffalarm feature, this function is used for APP to
* cancelPoweroffAlarm
*/
@Override
public void cancelPoweroffAlarmImpl(String name) {
Slog.i(TAG, "remove power off alarm pacakge name " + name);
// not need synchronized
synchronized (mPowerOffAlarmLock) {
removePoweroffAlarmLocked(name);
// AlarmPair tempAlarmPair = mPoweroffAlarms.remove(name);
// it will always to cancel the alarm in alarm driver
if (mNativeData != 0 && mNativeData != -1) {
if (name.equals("com.android.deskclock")) {
set(mNativeData, PRE_SCHEDULE_POWER_OFF_ALARM, 0, 0);
}
// 在这里添加自己的包名
/**
* 如
* if ("com.xxx.xxxx".equals(name)) {
* set(mNativeData, PRE_SCHEDULE_POWER_OFF_ALARM, 0, 0);
* }
*/
}
if (mPoweroffAlarms.size() > 0) {
resetPoweroffAlarm(mPoweroffAlarms.get(0));
}
}
}
应用内修改
注册
仿照DeskClock源码
注册文件中增加action com.android.deskclock.START_ALARM,接收开机指令然后开机即可
开机参考
alps\vendor\mediatek\proprietary\packages\apps\DeskClock\src\com\android\deskclock\AlarmAlertWakeLock.java
<service android:name=".alarms.AlarmService"
android:exported="true"
android:directBootAware="true">
<!-- M: add intent action for power off alarm to start AlarmService @{ -->
<intent-filter>
<action android:name="com.android.deskclock.START_ALARM" />
</intent-filter>
</service>
设置关机闹钟
参考
alps\vendor\mediatek\proprietary\packages\apps\DeskClock\src\com\android\deskclock\alarms\AlarmStateManager.java
/// M: [ALPS03269543] [Power off Alarm] @ setPoweroffAlarm if needed {
public static void setPoweroffAlarm(Context context, AlarmInstance nextAlarm) {
if (nextAlarm != null && PowerOffAlarm.canEnablePowerOffAlarm()) {
long timeInMillis = nextAlarm.getAlarmTime().getTimeInMillis();
Intent stateChangeIntent = createStateChangeIntent(context, ALARM_MANAGER_TAG,
nextAlarm, AlarmInstance.FIRED_STATE);
// Power off alarm should use the different pendingIntent to normal alarm
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, nextAlarm.hashCode(),
stateChangeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
LogUtils.v("Set for PowerOffAlarm alarmType = 8, time = "
+ AlarmUtils.getFormattedTime(context, nextAlarm.getAlarmTime()));
am.setExact(POWER_OFF_WAKE_UP, timeInMillis, pendingIntent);
// Add for power_off_alarm to backup the external ringtone
}
}
/** @} */
关键是这一句
am.setExact(POWER_OFF_WAKE_UP, timeInMillis, pendingIntent);
其中 POWER_OFF_WAKE_UP = 7;
这个参数可以从系统的AlarmManager处查得
取消
AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
am.cancelPoweroffAlarm(context.getPackageName());