|
|
|
@ -39,21 +39,26 @@ import net.micode.notes.tool.DataUtils;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 闹钟提醒活动类,用于在便签提醒时间到达时显示提醒对话框并播放提醒音
|
|
|
|
|
*/
|
|
|
|
|
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,77 +66,109 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
|
|
|
|
|
| WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从Intent中获取便签ID和内容
|
|
|
|
|
Intent intent = getIntent();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 从URI路径中获取便签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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化媒体播放器
|
|
|
|
|
mPlayer = new 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.isInteractive();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 播放闹钟提醒音
|
|
|
|
|
*/
|
|
|
|
|
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) {
|
|
|
|
|
// 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.Builder dialog = new AlertDialog.Builder(this);
|
|
|
|
|
dialog.setTitle(R.string.app_name);
|
|
|
|
|
dialog.setMessage(mSnippet);
|
|
|
|
|
dialog.setPositiveButton(R.string.notealert_ok, 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);
|
|
|
|
@ -139,16 +176,25 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 对话框关闭事件处理
|
|
|
|
|
*/
|
|
|
|
|
public void onDismiss(DialogInterface dialog) {
|
|
|
|
|
// 停止播放提醒音并关闭活动
|
|
|
|
|
stopAlarmSound();
|
|
|
|
|
finish();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 停止播放提醒音并释放资源
|
|
|
|
|
*/
|
|
|
|
|
private void stopAlarmSound() {
|
|
|
|
|
if (mPlayer != null) {
|
|
|
|
|
// 停止播放
|
|
|
|
|
mPlayer.stop();
|
|
|
|
|
// 释放媒体播放器资源
|
|
|
|
|
mPlayer.release();
|
|
|
|
|
mPlayer = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|