|
|
|
@ -0,0 +1,313 @@
|
|
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 导入所需的Android和Java类
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
|
|
// 声明成员变量
|
|
|
|
|
|
|
|
|
|
private long mNoteId; // 笔记ID
|
|
|
|
|
|
|
|
|
|
private String mSnippet; // 笔记摘要
|
|
|
|
|
|
|
|
|
|
private static final int SNIPPET_PREW_MAX_LEN = 60; // 摘要最大长度
|
|
|
|
|
|
|
|
|
|
MediaPlayer mPlayer; // 媒体播放器,用于播放闹钟声音
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// onCreate方法,Activity创建时调用
|
|
|
|
|
|
|
|
|
|
@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(); // 获取启动此Activity的Intent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
// 从Intent中提取笔记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(); // 初始化媒体播放器
|
|
|
|
|
|
|
|
|
|
// 检查笔记是否在数据库中且类型为普通笔记
|
|
|
|
|
|
|
|
|
|
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
|
|
|
|
|
|
|
|
|
|
showActionDialog(); // 显示操作对话框
|
|
|
|
|
|
|
|
|
|
playAlarmSound(); // 播放闹钟声音
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
finish(); // 如果笔记不存在,则结束Activity
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查屏幕是否点亮
|
|
|
|
|
|
|
|
|
|
private boolean isScreenOn() {
|
|
|
|
|
|
|
|
|
|
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
|
|
|
|
|
|
|
|
|
return pm.isScreenOn();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 播放闹钟声音
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
mPlayer.prepare();
|
|
|
|
|
|
|
|
|
|
mPlayer.setLooping(true);
|
|
|
|
|
|
|
|
|
|
mPlayer.start();
|
|
|
|
|
|
|
|
|
|
} catch (IllegalArgumentException | SecurityException | IllegalStateException | 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); // 创建跳转到NoteEditActivity的Intent
|
|
|
|
|
|
|
|
|
|
intent.setAction(Intent.ACTION_VIEW); // 设置Intent动作为查看
|
|
|
|
|
|
|
|
|
|
intent.putExtra(Intent.EXTRA_UID, mNoteId); // 将笔记ID作为额外数据添加到Intent中
|
|
|
|
|
|
|
|
|
|
startActivity(intent); // 启动NoteEditActivity
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 对话框关闭时调用
|
|
|
|
|
|
|
|
|
|
public void onDismiss(DialogInterface dialog) {
|
|
|
|
|
|
|
|
|
|
stopAlarmSound(); // 停止播放闹钟声音
|
|
|
|
|
|
|
|
|
|
finish(); // 结束Activity
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 停止播放闹钟声音
|
|
|
|
|
|
|
|
|
|
private void stopAlarmSound() {
|
|
|
|
|
|
|
|
|
|
if (mPlayer != null) {
|
|
|
|
|
|
|
|
|
|
mPlayer.stop(); // 停止媒体播放器
|
|
|
|
|
|
|
|
|
|
mPlayer.release(); // 释放媒体播放器资源
|
|
|
|
|
|
|
|
|
|
mPlayer = null; // 将媒体播放器引用置为空
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|