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.
yzy_502/src/ui/AlarmAlertActivity.java

190 lines
7.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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
| WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
}
Intent intent = getIntent();
try {
mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1)); // 从Intent中获取便签ID
mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId); // 根据便签ID获取便签摘要
// 如果摘要长度超过限制,截取部分内容并添加提示信息
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
}
}
// 检查屏幕是否点亮
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 {
// 设置音频源为警告铃声URI
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) {
// I/O异常打印堆栈轨迹
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) {
// 按钮点击事件处理
switch (which) {
case DialogInterface.BUTTON_NEGATIVE:
// 如果是消极按钮
Intent intent = new Intent(this, NoteEditActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra(Intent.EXTRA_UID, mNoteId);
// 启动编辑笔记的活动
startActivity(intent);
break;
default:
break;
}
}
public void onDismiss(DialogInterface dialog) {
// 对话框关闭事件处理
stopAlarmSound(); // 停止警报音
finish(); // 结束当前活动
}
// 停止警报音
private void stopAlarmSound() {
if (mPlayer != null) {
mPlayer.stop(); // 停止音频播放
mPlayer.release(); // 释放资源
mPlayer = null; // 将播放器置空
}
}
}