|
|
/*
|
|
|
* 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;
|
|
|
|
|
|
// 导入Android框架中用于对话框、音频、媒体播放等相关功能的类
|
|
|
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;
|
|
|
|
|
|
// 导入Java的IO功能,用于处理可能的输入输出异常
|
|
|
import java.io.IOException;
|
|
|
|
|
|
// 定义AlarmAlertActivity类,它是一个Activity,用于显示闹钟提醒
|
|
|
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对象,用于播放闹钟声音
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
// 获取启动Activity的Intent
|
|
|
Intent intent = getIntent();
|
|
|
|
|
|
try {
|
|
|
// 从Intent中获取笔记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;
|
|
|
}
|
|
|
|
|
|
// 创建MediaPlayer对象
|
|
|
mPlayer = new MediaPlayer();
|
|
|
// 如果笔记存在于数据库中,则显示操作对话框并播放闹钟声音
|
|
|
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
|
|
|
showActionDialog();
|
|
|
playAlarmSound();
|
|
|
} else {
|
|
|
// 如果笔记不存在,则结束Activity
|
|
|
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);
|
|
|
|
|
|
// 如果闹钟铃声被静音模式影响,则设置MediaPlayer的音频流类型
|
|
|
if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) {
|
|
|
mPlayer.setAudioStreamType(silentModeStreams);
|
|
|
} else {
|
|
|
mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
|
|
|
}
|
|
|
try {
|
|
|
// 设置MediaPlayer的数据源为闹钟铃声,并准备播放
|
|
|
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对象
|
|
|
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:
|
|
|
// 如果用户点击否定按钮,则启动NoteEditActivity
|
|
|
Intent intent = new Intent(this, NoteEditActivity.class);
|
|
|
intent.setAction(Intent.ACTION_VIEW);
|
|
|
intent.putExtra(Intent.EXTRA_UID, mNoteId);
|
|
|
startActivity(intent);
|
|
|
break;
|
|
|
default:
|
|
|
// 默认不做任何操作
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 对话框消失事件处理方法
|
|
|
public void onDismiss(DialogInterface dialog) {
|
|
|
// 停止闹钟声音,并结束Activity
|
|
|
stopAlarmSound();
|
|
|
finish();
|
|
|
}
|
|
|
|
|
|
// 停止闹钟声音
|
|
|
private void stopAlarmSound() {
|
|
|
// 如果MediaPlayer对象不为空,则停止播放并释放资源
|
|
|
if (mPlayer != null) {
|
|
|
mPlayer.stop();
|
|
|
mPlayer.release();
|
|
|
mPlayer = null;
|
|
|
}
|
|
|
}
|
|
|
} |