|
|
/*
|
|
|
* 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 {
|
|
|
private long mNoteId;
|
|
|
private String mSnippet;
|
|
|
private static final int SNIPPET_PREW_MAX_LEN = 60;
|
|
|
MediaPlayer mPlayer;
|
|
|
|
|
|
@Override
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
// 调用父类的onCreate方法
|
|
|
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();
|
|
|
|
|
|
// 尝试从Intent中提取便签ID,并获取便签的预览内容
|
|
|
try {
|
|
|
mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1));
|
|
|
mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId);
|
|
|
// 如果便签预览内容过长,则截取前SNIPPET_PREW_MAX_LEN个字符,并添加提示信息
|
|
|
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();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 播放闹钟声音的方法
|
|
|
* 该方法首先根据设备的默认设置获取闹钟铃声的URI,
|
|
|
* 然后根据设备的静音模式设置调整音频流类型,
|
|
|
* 最后使用MediaPlayer播放闹钟声音,并设置循环播放
|
|
|
*/
|
|
|
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);
|
|
|
// 准备MediaPlayer播放器
|
|
|
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) {
|
|
|
// TODO Auto-generated catch block
|
|
|
e.printStackTrace();
|
|
|
} catch (IOException e) {
|
|
|
// TODO Auto-generated catch block
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 显示操作对话框
|
|
|
* 这个方法创建并显示一个对话框,对话框的标题是应用名称,内容来自mSnippet变量
|
|
|
* 如果屏幕处于点亮状态,对话框还会显示一个负向按钮
|
|
|
* 对话框的正向按钮和负向按钮都使用了当前对象作为点击事件监听器
|
|
|
* 当对话框被关闭时,会调用OnDismissListener接口,该接口也由当前对象实现
|
|
|
*/
|
|
|
private void showActionDialog() {
|
|
|
// 创建一个对话框构建器
|
|
|
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
|
|
|
// 设置对话框标题为应用名称
|
|
|
dialog.setTitle(R.string.app_name);
|
|
|
// 设置对话框内容为mSnippet变量的值
|
|
|
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:
|
|
|
// 创建一个指向NoteEditActivity的意图
|
|
|
Intent intent = new Intent(this, NoteEditActivity.class);
|
|
|
// 设置意图动作为查看,表示即将查看或编辑一个便签
|
|
|
intent.setAction(Intent.ACTION_VIEW);
|
|
|
// 将便签的唯一标识符添加到意图中,以便NoteEditActivity可以识别要编辑的便签
|
|
|
intent.putExtra(Intent.EXTRA_UID, mNoteId);
|
|
|
// 启动NoteEditActivity
|
|
|
startActivity(intent);
|
|
|
break;
|
|
|
default:
|
|
|
// 对于其他按钮,默认情况下不执行任何操作
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void onDismiss(DialogInterface dialog) {
|
|
|
stopAlarmSound();
|
|
|
finish();
|
|
|
}
|
|
|
|
|
|
private void stopAlarmSound() {
|
|
|
if (mPlayer != null) {
|
|
|
mPlayer.stop();
|
|
|
mPlayer.release();
|
|
|
mPlayer = null;
|
|
|
}
|
|
|
}
|
|
|
}
|