|
|
// 声明版权信息,表明该文件由MiCode开源社区在2010-2011年间编写,并遵循Apache License 2.0许可协议
|
|
|
/*
|
|
|
* 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; // 导入Activity类,用于创建活动界面
|
|
|
import android.app.AlertDialog; // 导入AlertDialog类,用于显示对话框
|
|
|
import android.content.Context; // 导入Context类,用于获取应用程序上下文
|
|
|
import android.content.DialogInterface; // 导入DialogInterface类,用于对话框事件监听
|
|
|
import android.content.DialogInterface.OnClickListener; // 导入OnClickListener接口,用于监听对话框按钮点击事件
|
|
|
import android.content.DialogInterface.OnDismissListener; // 导入OnDismissListener接口,用于监听对话框消失事件
|
|
|
import android.content.Intent; // 导入Intent类,用于在组件之间传递消息
|
|
|
import android.media.AudioManager; // 导入AudioManager类,用于管理音频设置
|
|
|
import android.media.MediaPlayer; // 导入MediaPlayer类,用于播放媒体文件
|
|
|
import android.media.RingtoneManager; // 导入RingtoneManager类,用于管理铃声
|
|
|
import android.net.Uri; // 导入Uri类,用于表示统一资源标识符
|
|
|
import android.os.Bundle; // 导入Bundle类,用于传递数据
|
|
|
import android.os.PowerManager; // 导入PowerManager类,用于管理电源设置
|
|
|
import android.provider.Settings; // 导入Settings类,用于访问系统设置
|
|
|
import android.view.Window; // 导入Window类,用于管理窗口
|
|
|
import android.view.WindowManager; // 导入WindowManager类,用于管理窗口属性
|
|
|
|
|
|
import net.micode.notes.R; // 导入应用程序的资源文件
|
|
|
import net.micode.notes.data.Notes; // 导入Notes类,用于处理笔记数据
|
|
|
import net.micode.notes.tool.DataUtils; // 导入DataUtils类,用于数据处理工具
|
|
|
|
|
|
import java.io.IOException; // 导入IOException类,用于处理输入/输出异常
|
|
|
public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener {
|
|
|
private long mNoteId; // 声明一个长整型变量mNoteId,用于存储笔记ID
|
|
|
private String mSnippet; // 声明一个字符串变量mSnippet,用于存储笔记摘要
|
|
|
private static final int SNIPPET_PREW_MAX_LEN = 60; // 定义常量SNIPPET_PREW_MAX_LEN,表示笔记摘要的最大长度
|
|
|
MediaPlayer mPlayer; // 声明一个MediaPlayer对象mPlayer,用于播放提醒音
|
|
|
|
|
|
// 重写Activity类的onCreate方法,用于初始化活动界面
|
|
|
@Override
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
super.onCreate(savedInstanceState); // 调用父类的onCreate方法
|
|
|
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(); // 获取启动该活动的Intent对象
|
|
|
|
|
|
try {
|
|
|
// 从Intent中获取笔记ID,并根据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(); // 创建MediaPlayer对象
|
|
|
// 如果笔记类型为普通笔记,则显示操作对话框并播放提醒音
|
|
|
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
|
|
|
showActionDialog();
|
|
|
playAlarmSound();
|
|
|
} else {
|
|
|
finish(); // 如果笔记类型不为普通笔记,则结束活动
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 定义isScreenOn方法,用于判断屏幕是否开启
|
|
|
private boolean isScreenOn() {
|
|
|
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); // 获取电源管理服务
|
|
|
return pm.isScreenOn(); // 返回屏幕是否开启的状态
|
|
|
}
|
|
|
|
|
|
// 定义playAlarmSound方法,用于播放提醒音
|
|
|
private void playAlarmSound() {
|
|
|
Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM); // 获取默认提醒音的URI
|
|
|
|
|
|
// 获取静音模式下影响的音频流类型
|
|
|
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); // 设置MediaPlayer的数据源
|
|
|
mPlayer.prepare(); // 准备MediaPlayer
|
|
|
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(); // 打印异常信息
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 定义showActionDialog方法,用于显示操作对话框
|
|
|
private void showActionDialog() {
|
|
|
AlertDialog.Builder dialog = new AlertDialog.Builder(this); // 创建AlertDialog.Builder对象
|
|
|
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); // 显示对话框并设置对话框消失监听器
|
|
|
}
|
|
|
|
|
|
// 实现OnClickListener接口的onClick方法,用于处理对话框按钮点击事件
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
switch (which) {
|
|
|
case DialogInterface.BUTTON_NEGATIVE:
|
|
|
// 如果点击“进入”按钮,则启动NoteEditActivity并传递笔记ID
|
|
|
Intent intent = new Intent(this, NoteEditActivity.class);
|
|
|
intent.setAction(Intent.ACTION_VIEW);
|
|
|
intent.putExtra(Intent.EXTRA_UID, mNoteId);
|
|
|
startActivity(intent);
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 实现OnDismissListener接口的onDismiss方法,用于处理对话框消失事件
|
|
|
public void onDismiss(DialogInterface dialog) {
|
|
|
stopAlarmSound(); // 停止提醒音
|
|
|
finish(); // 结束活动
|
|
|
}
|
|
|
|
|
|
// 定义stopAlarmSound方法,用于停止提醒音
|
|
|
private void stopAlarmSound() {
|
|
|
if (mPlayer != null) {
|
|
|
mPlayer.stop(); // 停止MediaPlayer
|
|
|
mPlayer.release(); // 释放MediaPlayer资源
|
|
|
mPlayer = null; // 将MediaPlayer对象置为空
|
|
|
}
|
|
|
}
|
|
|
} |