|
|
|
@ -39,21 +39,27 @@ import net.micode.notes.tool.DataUtils;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 闹钟提醒Activity类
|
|
|
|
|
* 功能:当便签提醒时间到达时显示提醒对话框并播放闹铃
|
|
|
|
|
*/
|
|
|
|
|
public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener {
|
|
|
|
|
private long mNoteId;
|
|
|
|
|
private String mSnippet;
|
|
|
|
|
private static final int SNIPPET_PREW_MAX_LEN = 60;
|
|
|
|
|
MediaPlayer mPlayer;
|
|
|
|
|
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
|
|
|
|
@ -61,11 +67,15 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
|
|
|
|
|
| WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取启动此Activity的Intent
|
|
|
|
|
Intent intent = getIntent();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 从Intent URI中解析出便签ID
|
|
|
|
|
mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1));
|
|
|
|
|
// 根据ID从数据库获取便签内容摘要
|
|
|
|
|
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;
|
|
|
|
@ -74,65 +84,89 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化媒体播放器
|
|
|
|
|
mPlayer = new MediaPlayer();
|
|
|
|
|
// 检查便签是否仍然存在于数据库中
|
|
|
|
|
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
|
|
|
|
|
showActionDialog();
|
|
|
|
|
playAlarmSound();
|
|
|
|
|
showActionDialog(); // 显示提醒对话框
|
|
|
|
|
playAlarmSound(); // 播放闹铃声音
|
|
|
|
|
} else {
|
|
|
|
|
finish();
|
|
|
|
|
finish(); // 如果便签已被删除则直接结束
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查屏幕是否处于点亮状态
|
|
|
|
|
* @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();
|
|
|
|
|
mPlayer.setLooping(true); // 设置循环播放
|
|
|
|
|
mPlayer.start(); // 开始播放
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (SecurityException e) {
|
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (IllegalStateException e) {
|
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 显示提醒操作对话框
|
|
|
|
|
*/
|
|
|
|
|
private void showActionDialog() {
|
|
|
|
|
// 创建AlertDialog构建器
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 对话框按钮点击事件处理
|
|
|
|
|
* @param dialog 对话框实例
|
|
|
|
|
* @param which 被点击的按钮标识
|
|
|
|
|
*/
|
|
|
|
|
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);
|
|
|
|
@ -143,16 +177,23 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 对话框关闭事件处理
|
|
|
|
|
* @param dialog 对话框实例
|
|
|
|
|
*/
|
|
|
|
|
public void onDismiss(DialogInterface dialog) {
|
|
|
|
|
stopAlarmSound();
|
|
|
|
|
finish();
|
|
|
|
|
stopAlarmSound(); // 停止闹铃声音
|
|
|
|
|
finish(); // 结束当前Activity
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 停止闹铃声音方法
|
|
|
|
|
*/
|
|
|
|
|
private void stopAlarmSound() {
|
|
|
|
|
if (mPlayer != null) {
|
|
|
|
|
mPlayer.stop();
|
|
|
|
|
mPlayer.release();
|
|
|
|
|
mPlayer = null;
|
|
|
|
|
mPlayer.stop(); // 停止播放
|
|
|
|
|
mPlayer.release(); // 释放资源
|
|
|
|
|
mPlayer = null; // 置空引用
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|