You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MiNotes/app/src/main/java/net/micode/notes/ui/AlarmAlertActivity.java

165 lines
6.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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;
@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 {
// 获取传递过来的NoteId和Snippet
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;// 如果文本过长则截取前60个字符加上一个显示“…”的标识
} catch (IllegalArgumentException e) {
e.printStackTrace();
return;
}
mPlayer = new MediaPlayer();
// 从数据源中查询记录是否存在如果存在则显示提醒菜单和播放提醒声音否则结束Activity
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
showActionDialog();// 显示提醒菜单
playAlarmSound();// 播放提醒声音
} else {
finish();// Note不存在结束Activity
}
}
private boolean isScreenOn() {// 检测屏幕是否点亮
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);// 获取PowerManager对象
return pm.isScreenOn(); // 返回屏幕是否点亮的状态
}
private void playAlarmSound() {
// 获取系统设置中的闹钟铃声
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 {
// 设置播放器的数据源
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) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 显示提醒菜单Dialog包括Note的文本信息、确认键和选择键(如果屏幕是点亮的)
private void showActionDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle(R.string.app_name); // 设置Dialog的标题为应用名称
dialog.setMessage(mSnippet); // 设置Dialog的信息为Note的文本信息
dialog.setPositiveButton(R.string.notealert_ok, this); // 设置确认键点击后执行onClick()方法
if (isScreenOn()) { // 如果屏幕是点亮的,设置选择键 “进入笔记”, 点击后执行onClick()方法
dialog.setNegativeButton(R.string.notealert_enter, this);
}
dialog.show().setOnDismissListener(this); // 显示Dialog并设置当Dialog消失时的监听器
}
// Dialog按键点击响应函数, which为点击的按键的序号
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); // 打开Note编辑界面
break;
default:
break;
}
}
// 监听Dialog消失的响应函数
public void onDismiss(DialogInterface dialog) {
stopAlarmSound(); // 停止播放闹钟声音
finish(); // 结束Activity
}
// 停止播放闹钟声音
private void stopAlarmSound() {
if (mPlayer != null) { // 如果播放器存在
mPlayer.stop(); // 停止播放
mPlayer.release(); // 释放播放器占用的资源
mPlayer = null; // 将播放器设为空
}
}