|
|
|
@ -0,0 +1,187 @@
|
|
|
|
|
// 导入所需的包和类
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// 定义一个继承自Activity的类,并实现OnClickListener和OnDismissListener接口
|
|
|
|
|
public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener {
|
|
|
|
|
// 成员变量,用于存储笔记的ID和摘要
|
|
|
|
|
private long mNoteId;
|
|
|
|
|
private String mSnippet;
|
|
|
|
|
// 定义摘要预览的最大长度
|
|
|
|
|
private static final int SNIPPET_PREW_MAX_LEN = 60;
|
|
|
|
|
// 声明一个MediaPlayer对象,用于播放声音
|
|
|
|
|
MediaPlayer mPlayer;
|
|
|
|
|
|
|
|
|
|
// 当Activity创建时调用
|
|
|
|
|
@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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取启动该Activity的Intent
|
|
|
|
|
Intent intent = getIntent();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 从Intent中提取笔记ID
|
|
|
|
|
mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1));
|
|
|
|
|
// 获取笔记的摘要
|
|
|
|
|
mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId);
|
|
|
|
|
// 如果摘要长度超过最大长度,则进行截断,并添加省略号信息
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化MediaPlayer对象
|
|
|
|
|
mPlayer = new MediaPlayer();
|
|
|
|
|
// 检查笔记是否在数据库中且类型为笔记
|
|
|
|
|
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
|
|
|
|
|
// 显示操作对话框
|
|
|
|
|
showActionDialog();
|
|
|
|
|
// 播放闹钟声音
|
|
|
|
|
playAlarmSound();
|
|
|
|
|
} else {
|
|
|
|
|
// 如果笔记不存在,则结束Activity
|
|
|
|
|
finish();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查屏幕是否处于点亮状态
|
|
|
|
|
private boolean isScreenOn() {
|
|
|
|
|
// 获取PowerManager服务
|
|
|
|
|
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
|
|
|
|
// 返回屏幕是否点亮的状态
|
|
|
|
|
return pm.isScreenOn();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 播放闹钟声音的方法
|
|
|
|
|
private void playAlarmSound() {
|
|
|
|
|
// 获取默认的闹钟铃声Uri
|
|
|
|
|
Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM);
|
|
|
|
|
|
|
|
|
|
// 获取静音模式下受影响的音频流类型
|
|
|
|
|
int silentModeStreams = Settings.System.getInt(getContentResolver(),
|
|
|
|
|
Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
|
|
|
|
|
|
|
|
|
|
// 如果静音模式下闹钟声音受影响,则设置MediaPlayer的音频流类型为受影响的音频流
|
|
|
|
|
if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) {
|
|
|
|
|
mPlayer.setAudioStreamType(silentModeStreams);
|
|
|
|
|
} else {
|
|
|
|
|
// 否则,设置音频流类型为闹钟
|
|
|
|
|
mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
// 设置MediaPlayer的数据源并准备播放
|
|
|
|
|
mPlayer.setDataSource(this, url);
|
|
|
|
|
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对象
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 实现OnClickListener接口的onClick方法,处理对话框按钮的点击事件
|
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
|
switch (which) {
|
|
|
|
|
case DialogInterface.BUTTON_NEGATIVE:
|
|
|
|
|
// 如果点击的是取消按钮,则跳转到编辑笔记Activity
|
|
|
|
|
Intent intent = new Intent(this, NoteEditActivity.class);
|
|
|
|
|
intent.setAction(Intent.ACTION_VIEW);
|
|
|
|
|
intent.putExtra(Intent.EXTRA_UID, mNoteId);
|
|
|
|
|
startActivity(intent);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 实现OnDismissListener接口的onDismiss方法,处理对话框关闭事件
|
|
|
|
|
public void onDismiss(DialogInterface dialog) {
|
|
|
|
|
// 停止播放闹钟声音
|
|
|
|
|
stopAlarmSound();
|
|
|
|
|
// 结束Activity
|
|
|
|
|
finish();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 停止播放闹钟声音的方法
|
|
|
|
|
private void stopAlarmSound() {
|
|
|
|
|
if (mPlayer != null) {
|
|
|
|
|
// 停止播放并释放MediaPlayer资源
|
|
|
|
|
mPlayer.stop();
|
|
|
|
|
mPlayer.release();
|
|
|
|
|
mPlayer = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|