You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
xiaomi_note/ui/AlarmAlertActivity.java

187 lines
7.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.micode.notes.ui;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings;
import android.view.Window;
import android.view.WindowManager;
import net.micode.notes.R;
import net.micode.notes.data.Notes;
import net.micode.notes.tool.DataUtils;
import java.io.IOException;
/**
* 这是AlarmAlertActivity类继承了Activity并实现了OnClickListener和OnDismissListener接口。
* 该活动的目的是在触发时显示闹钟警报并播放闹钟声音。
*/
public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener {
private long mNoteId; // 存储与闹钟关联的笔记ID
private String mSnippet; // 存储笔记内容的摘要
private static final int SNIPPET_PREW_MAX_LEN = 60; // 笔记摘要预览的最大长度
MediaPlayer mPlayer; // 用于播放闹钟声音的MediaPlayer对象
/**
* 当活动创建时调用。负责设置窗口标志、从意图中获取笔记ID和摘要、初始化MediaPlayer
* 如果笔记在数据库中可见,则显示操作对话框并播放闹钟声音。
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
final Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
if (!isScreenOn()) {
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
| WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
}
Intent intent = getIntent();
try {
mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1)); // 从意图中获取笔记ID
mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId); // 根据笔记ID获取摘要
mSnippet = mSnippet.length() > SNIPPET_PREW_MAX_LEN ? mSnippet.substring(0,
SNIPPET_PREW_MAX_LEN) + getResources().getString(R.string.notelist_string_info)
: mSnippet;
} catch (IllegalArgumentException e) { // 捕获异常
e.printStackTrace();
return;
}
mPlayer = new MediaPlayer(); // 初始化MediaPlayer对象
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) { // 检查笔记是否在数据库中可见
showActionDialog(); // 显示操作对话框
playAlarmSound(); // 播放闹钟声音
} else {
finish(); // 结束当前活动
}
}
/**
* 检查设备屏幕是否亮着。
*
* @return 如果屏幕亮着则返回true否则返回false
*/
private boolean isScreenOn() {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
return pm.isScreenOn(); // 返回屏幕是否亮着的状态
}
/**
* 使用MediaPlayer播放闹钟声音。
* 根据静音模式设置音频流类型。
*/
private void playAlarmSound() {
Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM); // 获取闹钟声音的URI
int silentModeStreams = Settings.System.getInt(getContentResolver(),
Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0); // 获取静音模式下受影响的音频流类型
if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) { // 判断闹钟声音是否受静音模式影响
mPlayer.setAudioStreamType(silentModeStreams); // 设置音频流类型
} else {
mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
}
try {
mPlayer.setDataSource(this, url); // 设置数据源为闹钟声音的URI
mPlayer.prepare(); // 准备播放器
mPlayer.setLooping(true); // 设置循环播放
mPlayer.start(); // 开始播放
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 显示操作对话框,包括闹钟摘要和两个按钮:确定和进入。
* 如果屏幕亮着,则还有一个取消按钮。
*/
private void showActionDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle(R.string.app_name); // 设置对话框标题为应用名称
dialog.setMessage(mSnippet); // 设置对话框消息为闹钟摘要
dialog.setPositiveButton(R.string.notealert_ok, this); // 设置确定按钮,并设置点击监听器为当前活动
if (isScreenOn()) { // 如果屏幕亮着
dialog.setNegativeButton(R.string.notealert_enter, this); // 设置取消按钮,并设置点击监听器为当前活动
}
dialog.show().setOnDismissListener(this); // 显示对话框,并设置对话框消失监听器为当前活动
}
/**
* 处理对话框按钮的点击事件。
* 根据点击的按钮执行相应的操作。
*/
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_NEGATIVE: // 点击了取消按钮
Intent intent = new Intent(this, NoteEditActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra(Intent.EXTRA_UID, mNoteId);
startActivity(intent); // 启动笔记编辑活动,进入该笔记的编辑页面
break;
default:
break;
}
}
/**
* 处理对话框消失事件。
* 停止闹钟声音并结束当前活动。
*/
public void onDismiss(DialogInterface dialog) {
stopAlarmSound(); // 停止闹钟声音
finish(); // 结束当前活动
}
/**
* 停止闹钟声音。
* 如果MediaPlayer对象存在则停止播放、释放资源并置为null。
*/
private void stopAlarmSound() {
if (mPlayer != null) {
mPlayer.stop(); // 停止播放
mPlayer.release(); // 释放资源
mPlayer = null; // 将MediaPlayer对象置为null
}
}
}