@ -0,0 +1,197 @@
|
||||
/*
|
||||
* 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.Intent; // 创建启动新活动的意图
|
||||
import android.media.AudioManager; // 管理音频设置
|
||||
import android.media.MediaPlayer; // 播放音频文件
|
||||
import android.media.RingtoneManager; // 访问系统铃声
|
||||
import android.net.Uri; // 表示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; // 处理IO异常
|
||||
|
||||
|
||||
public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener {
|
||||
private long mNoteId;
|
||||
// 笔记的唯一标识
|
||||
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 {
|
||||
// 从意图中提取笔记ID
|
||||
mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1));
|
||||
// 根据笔记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; // 发生异常时返回
|
||||
}
|
||||
|
||||
mPlayer = new MediaPlayer(); // 初始化媒体播放器
|
||||
// 检查笔记是否在数据库中可见
|
||||
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
|
||||
showActionDialog(); // 显示操作对话框
|
||||
playAlarmSound(); // 播放警报声音
|
||||
} else {
|
||||
finish();
|
||||
// 如果不可见,则结束活动
|
||||
}
|
||||
}
|
||||
|
||||
// 检查屏幕是否开启的方法
|
||||
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) {
|
||||
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);
|
||||
// 传递笔记ID
|
||||
startActivity(intent);
|
||||
// 启动活动
|
||||
break;
|
||||
default:
|
||||
break; // 默认情况不处理
|
||||
}
|
||||
}
|
||||
|
||||
public void onDismiss(DialogInterface dialog) {
|
||||
stopAlarmSound();
|
||||
// 停止播放警报声音
|
||||
finish();
|
||||
// 结束当前活动
|
||||
}
|
||||
|
||||
private void stopAlarmSound() {
|
||||
if (mPlayer != null) {
|
||||
mPlayer.stop();
|
||||
// 停止播放
|
||||
mPlayer.release();
|
||||
// 释放媒体播放器资源
|
||||
mPlayer = null;
|
||||
// 清空播放器引用
|
||||
}
|
||||
}
|
@ -0,0 +1,118 @@
|
||||
/*
|
||||
* 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 java.util.Calendar; // 导入日历类,用于日期和时间的管理
|
||||
|
||||
import net.micode.notes.R; // 导入资源类,用于访问字符串资源等
|
||||
import net.micode.notes.ui.DateTimePicker; // 导入自定义的日期时间选择器类
|
||||
import net.micode.notes.ui.DateTimePicker.OnDateTimeChangedListener; // 导入日期时间变化监听器接口
|
||||
|
||||
import android.app.AlertDialog; // 导入对话框类,用于创建对话框
|
||||
import android.content.Context; // 导入上下文类,用于获取应用环境信息
|
||||
import android.content.DialogInterface; // 导入对话框接口类,用于处理对话框事件
|
||||
import android.content.DialogInterface.OnClickListener; // 导入对话框点击事件监听器接口
|
||||
import android.text.format.DateFormat; // 导入日期格式化类,用于格式化日期和时间
|
||||
import android.text.format.DateUtils; // 导入日期工具类,提供日期格式化和显示功能
|
||||
|
||||
// 自定义日期时间选择对话框类
|
||||
public class DateTimePickerDialog extends AlertDialog implements OnClickListener {
|
||||
|
||||
private Calendar mDate = Calendar.getInstance();
|
||||
// 用于存储当前选中的日期和时间
|
||||
private boolean mIs24HourView;
|
||||
// 标识是否使用24小时制
|
||||
private OnDateTimeSetListener mOnDateTimeSetListener;
|
||||
// 日期时间设置的监听器
|
||||
private DateTimePicker mDateTimePicker;
|
||||
// 日期时间选择器实例
|
||||
|
||||
// 日期时间设置监听器接口
|
||||
public interface OnDateTimeSetListener {
|
||||
void OnDateTimeSet(AlertDialog dialog, long date);
|
||||
// 设置日期时间的回调方法
|
||||
}
|
||||
|
||||
// 构造函数,初始化对话框并设置初始日期
|
||||
public DateTimePickerDialog(Context context, long date) {
|
||||
super(context); // 调用父类构造函数
|
||||
mDateTimePicker = new DateTimePicker(context);
|
||||
// 创建日期时间选择器
|
||||
setView(mDateTimePicker);
|
||||
// 将选择器设置为对话框的视图
|
||||
|
||||
// 设置日期时间变化的监听器
|
||||
mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() {
|
||||
public void onDateTimeChanged(DateTimePicker view, int year, int month,
|
||||
int dayOfMonth, int hourOfDay, int minute) {
|
||||
// 更新日历对象中的年、月、日、小时和分钟
|
||||
mDate.set(Calendar.YEAR, year);
|
||||
mDate.set(Calendar.MONTH, month);
|
||||
mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
|
||||
mDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
|
||||
mDate.set(Calendar.MINUTE, minute);
|
||||
updateTitle(mDate.getTimeInMillis());
|
||||
// 更新对话框标题
|
||||
}
|
||||
});
|
||||
|
||||
mDate.setTimeInMillis(date); // 设置初始日期时间
|
||||
mDate.set(Calendar.SECOND, 0); // 将秒数设为0
|
||||
mDateTimePicker.setCurrentDate(mDate.getTimeInMillis());
|
||||
// 设置选择器的当前日期时间
|
||||
setButton(context.getString(R.string.datetime_dialog_ok), this);
|
||||
// 设置“确定”按钮
|
||||
setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null);
|
||||
// 设置“取消”按钮
|
||||
set24HourView(DateFormat.is24HourFormat(this.getContext()));
|
||||
// 根据系统设置决定是否使用24小时制
|
||||
updateTitle(mDate.getTimeInMillis());
|
||||
// 初始化对话框标题
|
||||
}
|
||||
|
||||
// 设置是否使用24小时制
|
||||
public void set24HourView(boolean is24HourView) {
|
||||
mIs24HourView = is24HourView; // 更新24小时制标识
|
||||
}
|
||||
|
||||
// 设置日期时间设置的监听器
|
||||
public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {
|
||||
mOnDateTimeSetListener = callBack; // 保存监听器
|
||||
}
|
||||
|
||||
// 更新对话框的标题,显示当前选中的日期和时间
|
||||
private void updateTitle(long date) {
|
||||
// 设置显示格式,包括年份、日期和时间
|
||||
int flag =
|
||||
DateUtils.FORMAT_SHOW_YEAR | // 显示年份
|
||||
DateUtils.FORMAT_SHOW_DATE | // 显示日期
|
||||
DateUtils.FORMAT_SHOW_TIME; // 显示时间
|
||||
flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_12HOUR;
|
||||
// 根据设置选择小时制
|
||||
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));
|
||||
// 格式化日期时间并设置为对话框标题
|
||||
}
|
||||
|
||||
// 点击事件处理
|
||||
public void onClick(DialogInterface arg0, int arg1) {
|
||||
// 如果设置了监听器,则调用其方法并传递当前时间戳
|
||||
if (mOnDateTimeSetListener != null) {
|
||||
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue