|
|
|
@ -0,0 +1,199 @@
|
|
|
|
|
/*
|
|
|
|
|
* 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;
|
|
|
|
|
// 定义摘要信息的最大展示长度,超过这个长度会进行截断等处理,此处设置为60个字符
|
|
|
|
|
private static final int SNIPPET_PREW_MAX_LEN = 60;
|
|
|
|
|
// 用于播放音频的MediaPlayer对象,比如播放闹钟提醒的声音
|
|
|
|
|
MediaPlayer mPlayer;
|
|
|
|
|
|
|
|
|
|
// 重写Activity的onCreate方法,该方法在Activity创建时被调用,用于进行初始化相关的操作
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
// 调用父类(Activity)的onCreate方法,确保Activity的基本初始化流程正常进行
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
// 请求去除Activity的标题栏,让界面更加简洁,专注于展示提醒相关的内容
|
|
|
|
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
|
|
|
|
|
|
|
|
|
// 获取当前Activity的窗口对象,后续用于设置窗口的相关属性
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取启动当前Activity的Intent对象,Intent中包含了传递过来的相关数据和意图信息
|
|
|
|
|
Intent intent = getIntent();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 从Intent中获取传递过来的数据(此处假设数据的格式是Uri路径相关的形式),
|
|
|
|
|
// 提取路径中的第二个片段作为笔记的ID,具体的格式约定取决于使用方传入的情况
|
|
|
|
|
mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1));
|
|
|
|
|
// 根据获取到的笔记ID,通过DataUtils工具类从内容解析器(通常用于和数据库等数据源交互)中获取笔记的摘要信息
|
|
|
|
|
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工具类判断该笔记ID对应的笔记是否在笔记数据库中可见(可能涉及权限、有效性等判断),
|
|
|
|
|
// 如果可见则展示操作对话框并播放闹钟声音,否则直接结束当前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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 定义一个方法用于播放闹钟声音,包括获取铃声的Uri、设置音频流类型、加载音频资源并开始播放等操作
|
|
|
|
|
private void playAlarmSound() {
|
|
|
|
|
// 通过RingtoneManager获取系统默认的闹钟铃声对应的Uri,以便后续MediaPlayer根据此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);
|
|
|
|
|
mPlayer.prepare();
|
|
|
|
|
// 设置音频循环播放,保证闹钟声音持续响起,直到用户操作停止
|
|
|
|
|
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) {
|
|
|
|
|
// 如果在MediaPlayer处于非法状态下进行操作(比如未准备好就开始播放等情况),打印异常堆栈信息,进行异常处理
|
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
// 如果在读取音频数据源等输入输出操作过程中出现异常,打印异常堆栈信息,进行异常处理
|
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 定义一个方法用于展示操作对话框,创建AlertDialog.Builder对象,设置对话框的标题、内容、按钮以及按钮点击的监听器等属性,并显示对话框
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 实现OnClickListener接口的onClick方法,用于处理对话框按钮点击后的逻辑,
|
|
|
|
|
// 根据点击的按钮不同(通过which参数判断)执行不同的操作,比如启动编辑笔记的Activity等
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 实现OnDismissListener接口的onDismiss方法,用于处理对话框关闭后的逻辑,
|
|
|
|
|
// 在此处主要是停止正在播放的闹钟声音,并结束当前Activity
|
|
|
|
|
public void onDismiss(DialogInterface dialog) {
|
|
|
|
|
stopAlarmSound();
|
|
|
|
|
finish();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 定义一个方法用于停止播放闹钟声音,先判断MediaPlayer对象是否为空,
|
|
|
|
|
// 如果不为空则停止播放、释放资源,并将MediaPlayer对象置为null,释放相关内存等资源
|
|
|
|
|
private void stopAlarmSound() {
|
|
|
|
|
if (mPlayer!= null) {
|
|
|
|
|
mPlayer.stop();
|
|
|
|
|
mPlayer.release();
|
|
|
|
|
mPlayer = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|