/* * 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. */ // 包声明,表明该类所在的包名为net.micode.notes.ui 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; // AlarmAlertActivity类继承自Activity,实现了OnClickListener和OnDismissListener接口,用于处理闹钟提醒相关的界面展示、声音播放以及用户交互等功能 public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener { // 用于存储关联笔记的ID,可能是触发闹钟提醒的那个笔记对应的唯一标识符 private long mNoteId; // 用于存储笔记的内容摘要,会进行一定长度限制处理后展示给用户 private String mSnippet; // 定义内容摘要的最大预览长度为60个字符 private static final int SNIPPET_PREW_MAX_LEN = 60; // MediaPlayer对象,用于播放闹钟提醒的声音 MediaPlayer mPlayer; // Activity创建时调用的方法,进行一系列初始化操作 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置Activity无标题栏,去除默认的标题显示 requestWindowFeature(Window.FEATURE_NO_TITLE); final Window win = getWindow(); // 设置窗口属性,使得在屏幕锁定时也能显示该Activity,方便用户看到提醒信息 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); } // 获取启动该Activity的意图(Intent),从中获取相关数据 Intent intent = getIntent(); try { // 从意图的数据部分解析出笔记的ID,假设数据路径中第二个元素是笔记ID(具体格式由传入意图决定) mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1)); // 通过DataUtils工具类根据笔记ID获取对应的内容摘要 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; } // 创建MediaPlayer对象,用于后续播放声音 mPlayer = new MediaPlayer(); // 检查该笔记是否在笔记数据库中可见(通过DataUtils工具类的方法判断),如果可见则展示操作对话框并播放闹钟声音,否则直接结束该Activity if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) { showActionDialog(); playAlarmSound(); } else { finish(); } } // 判断屏幕是否处于开启状态的方法,通过获取PowerManager服务并调用其isScreenOn方法来判断 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); // 根据静音模式设置情况来设置MediaPlayer的音频流类型,如果静音模式影响到闹钟音频流,则使用相应设置,否则使用默认的闹钟音频流类型 if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM))!= 0) { mPlayer.setAudioStreamType(silentModeStreams); } else { mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); } try { // 设置MediaPlayer的数据源为获取到的铃声Uri mPlayer.setDataSource(this, url); // 准备MediaPlayer,使其处于可播放状态(可能涉及加载音频资源等操作) mPlayer.prepare(); // 设置循环播放,让闹钟声音一直响,直到被停止 mPlayer.setLooping(true); // 启动MediaPlayer,开始播放闹钟声音 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); // 设置对话框的“确定”按钮文本及点击监听器(实现了OnClickListener接口) dialog.setPositiveButton(R.string.notealert_ok, this); // 如果屏幕处于开启状态,设置“进入”按钮文本及点击监听器,方便用户进一步操作(比如进入笔记编辑界面等) if (isScreenOn()) { dialog.setNegativeButton(R.string.notealert_enter, this); } // 显示对话框,并设置对话框关闭时的监听器(实现了OnDismissListener接口) dialog.show().setOnDismissListener(this); } // 实现OnClickListener接口的方法,处理对话框按钮点击事件 public void onClick(DialogInterface dialog, int which) { switch (which) { // 如果点击的是“进入”按钮(一般对应负向按钮,具体由设置顺序决定) case DialogInterface.BUTTON_NEGATIVE: // 创建一个启动NoteEditActivity的意图,设置动作为查看(ACTION_VIEW),并传入笔记的ID作为额外数据 Intent intent = new Intent(this, NoteEditActivity.class); intent.setAction(Intent.ACTION_VIEW); intent.putExtra(Intent.EXTRA_UID, mNoteId); // 启动NoteEditActivity,让用户可以查看或编辑对应的笔记内容 startActivity(intent); break; default: break; } } // 实现OnDismissListener接口的方法,当对话框关闭时调用 public void onDismiss(DialogInterface dialog) { // 停止播放闹钟声音,并释放MediaPlayer相关资源 stopAlarmSound(); // 结束当前的AlarmAlertActivity,完成整个闹钟提醒相关的操作流程 finish(); } // 用于停止播放闹钟声音并释放MediaPlayer资源的方法 private void stopAlarmSound() { if (mPlayer!= null) { // 停止MediaPlayer播放 mPlayer.stop(); // 释放MediaPlayer占用的资源 mPlayer.release(); // 将MediaPlayer对象置为null,避免后续误操作 mPlayer = null; } } }