/* * 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 { // 便签ID private long mNoteId; // 便签摘要 private String mSnippet; // 摘要预览最大长度 private static final int SNIPPET_PREW_MAX_LEN = 60; // 媒体播放器,用于播放提醒声音 MediaPlayer mPlayer; /** * 活动创建时调用 * 初始化界面,设置窗口属性,播放提醒声音 * @param savedInstanceState 保存的实例状态 */ @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); } // 从意图中获取便签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) + 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(); } } /** * 检查屏幕是否开启 * @return 屏幕开启返回true,否则返回false */ 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) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException 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); } /** * 处理对话框按钮点击事件 * @param dialog 对话框 * @param which 点击的按钮 */ 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; } } /** * 处理对话框关闭事件 * 停止播放声音并结束活动 * @param dialog 对话框 */ public void onDismiss(DialogInterface dialog) { stopAlarmSound(); finish(); } /** * 停止播放闹钟声音 * 释放媒体播放器资源 */ private void stopAlarmSound() { if (mPlayer != null) { mPlayer.stop(); mPlayer.release(); mPlayer = null; } } }