You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
5.7 KiB
153 lines
5.7 KiB
/*
|
|
* 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;
|
|
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;
|
|
|
|
|
|
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);
|
|
}
|
|
|
|
// 从Intent获取便签ID和内容摘要
|
|
Intent intent = getIntent();
|
|
try {
|
|
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) + "..." : mSnippet;
|
|
} catch (IllegalArgumentException e) {
|
|
e.printStackTrace();
|
|
return;
|
|
}
|
|
|
|
// 初始化媒体播放器
|
|
mPlayer = new MediaPlayer();
|
|
// 检查便签是否有效
|
|
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
|
|
showActionDialog(); // 显示提醒对话框
|
|
playAlarmSound(); // 播放警报音
|
|
} else {
|
|
finish(); // 无效便签则结束活动
|
|
}
|
|
}
|
|
|
|
/** 检查屏幕是否亮起 */
|
|
private boolean isScreenOn() {
|
|
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
|
return pm.isScreenOn();
|
|
}
|
|
|
|
/** 播放系统默认闹铃音 */
|
|
private void playAlarmSound() {
|
|
Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM);
|
|
|
|
// 根据系统设置调整音频流类型
|
|
int silentModeStreams = Settings.System.getInt(getContentResolver(),
|
|
Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
|
|
mPlayer.setAudioStreamType((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0 ?
|
|
silentModeStreams : AudioManager.STREAM_ALARM);
|
|
|
|
try {
|
|
mPlayer.setDataSource(this, url);
|
|
mPlayer.prepare();
|
|
mPlayer.setLooping(true); // 循环播放
|
|
mPlayer.start();
|
|
} catch (Exception 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);
|
|
startActivity(intent);
|
|
}
|
|
}
|
|
|
|
/** 对话框关闭时停止铃声并结束活动 */
|
|
public void onDismiss(DialogInterface dialog) {
|
|
stopAlarmSound();
|
|
finish();
|
|
}
|
|
|
|
/** 停止播放警报音并释放资源 */
|
|
private void stopAlarmSound() {
|
|
if (mPlayer != null) {
|
|
mPlayer.stop();
|
|
mPlayer.release();
|
|
mPlayer = null;
|
|
}
|
|
}
|
|
}
|