|
|
/*
|
|
|
* 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net)
|
|
|
* 根据 Apache 许可证 2.0 版本("许可证")授权;
|
|
|
* 除非符合许可证的规定,否则不得使用本文件。
|
|
|
* 您可以从以下网址获取许可证副本:
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
* 除非适用法律要求或书面同意,本软件按"原样"分发,
|
|
|
* 没有任何明示或暗示的保证或条件。
|
|
|
* 详见许可证中规定的权限和限制。
|
|
|
* (注:这是一份标准的Apache许可证2.0版本的开源声明)
|
|
|
*/
|
|
|
// 定义包路径
|
|
|
package net.micode.notes.ui;
|
|
|
|
|
|
// 导入Android相关类库
|
|
|
import android.app.Activity; // 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; // URI处理
|
|
|
import android.os.Bundle; // 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; // 数据工具类
|
|
|
|
|
|
// IO异常处理
|
|
|
import java.io.IOException;
|
|
|
|
|
|
/**
|
|
|
* 闹钟提醒Activity
|
|
|
* 处理笔记提醒通知,包括显示对话框和播放提醒音效
|
|
|
*/
|
|
|
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; // 媒体播放器实例
|
|
|
|
|
|
@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 URI中解析出笔记ID(格式:content://xxx/notes/1)
|
|
|
mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1));
|
|
|
// 通过ContentResolver获取笔记摘要
|
|
|
mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId);
|
|
|
// 截取摘要前60个字符,超出部分添加省略提示
|
|
|
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();
|
|
|
// 检查笔记是否可见(未被删除)
|
|
|
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
|
|
|
showActionDialog(); // 显示操作对话框
|
|
|
playAlarmSound(); // 播放提醒音效
|
|
|
} else {
|
|
|
finish(); // 笔记不可见则直接结束Activity
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 检查屏幕是否点亮
|
|
|
* @return boolean 屏幕状态
|
|
|
*/
|
|
|
private boolean isScreenOn() {
|
|
|
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);
|
|
|
|
|
|
// 设置音频流类型
|
|
|
if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) {
|
|
|
mPlayer.setAudioStreamType(silentModeStreams);
|
|
|
} else {
|
|
|
mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
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 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) {
|
|
|
if (which == DialogInterface.BUTTON_NEGATIVE) { // "进入"按钮
|
|
|
Intent intent = new Intent(this, NoteEditActivity.class);
|
|
|
intent.setAction(Intent.ACTION_VIEW); // 设置动作为查看
|
|
|
intent.putExtra(Intent.EXTRA_UID, mNoteId); // 传递笔记ID
|
|
|
startActivity(intent); // 启动笔记编辑Activity
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 对话框关闭事件处理
|
|
|
*/
|
|
|
public void onDismiss(DialogInterface dialog) {
|
|
|
stopAlarmSound(); // 停止音效
|
|
|
finish(); // 结束当前Activity
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 停止播放提醒音效
|
|
|
*/
|
|
|
private void stopAlarmSound() {
|
|
|
if (mPlayer != null) {
|
|
|
mPlayer.stop(); // 停止播放
|
|
|
mPlayer.release(); // 释放资源
|
|
|
mPlayer = null; // 置空引用
|
|
|
}
|
|
|
}
|
|
|
} |