|
|
|
@ -1,19 +1,3 @@
|
|
|
|
|
/*
|
|
|
|
|
* 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;
|
|
|
|
@ -39,21 +23,29 @@ import net.micode.notes.tool.DataUtils;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 闹钟提醒活动类,负责在闹钟时间到达时显示提醒对话框并播放提醒声音
|
|
|
|
|
*/
|
|
|
|
|
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 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 +53,15 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
|
|
|
|
|
| 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;
|
|
|
|
@ -74,35 +70,53 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建媒体播放器实例
|
|
|
|
|
mPlayer = new MediaPlayer();
|
|
|
|
|
// 检查笔记是否存在且有效
|
|
|
|
|
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
|
|
|
|
|
// 显示闹钟提醒对话框
|
|
|
|
|
showActionDialog();
|
|
|
|
|
// 播放闹钟声音
|
|
|
|
|
playAlarmSound();
|
|
|
|
|
} else {
|
|
|
|
|
// 笔记不存在则关闭Activity
|
|
|
|
|
finish();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查屏幕是否开启
|
|
|
|
|
* @return 屏幕开启状态
|
|
|
|
|
*/
|
|
|
|
|
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) {
|
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
@ -119,20 +133,33 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 显示闹钟提醒操作对话框
|
|
|
|
|
*/
|
|
|
|
|
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:
|
|
|
|
|
// 启动笔记编辑Activity并传递笔记ID
|
|
|
|
|
Intent intent = new Intent(this, NoteEditActivity.class);
|
|
|
|
|
intent.setAction(Intent.ACTION_VIEW);
|
|
|
|
|
intent.putExtra(Intent.EXTRA_UID, mNoteId);
|
|
|
|
@ -143,11 +170,19 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理对话框关闭事件
|
|
|
|
|
*/
|
|
|
|
|
public void onDismiss(DialogInterface dialog) {
|
|
|
|
|
// 停止闹钟声音
|
|
|
|
|
stopAlarmSound();
|
|
|
|
|
// 关闭Activity
|
|
|
|
|
finish();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 停止闹钟声音并释放资源
|
|
|
|
|
*/
|
|
|
|
|
private void stopAlarmSound() {
|
|
|
|
|
if (mPlayer != null) {
|
|
|
|
|
mPlayer.stop();
|
|
|
|
|