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.
git-xiaomibianqian/other/代码标注/ui/210340167肖凯文代码标注AlarmAlertA...

160 lines
7.7 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;
public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener {
private long mNoteId;//mNoteId可能是笔记的唯一标识符
private String mSnippet;//mSnippet可能是笔记的摘要或预览
private static final int SNIPPET_PREW_MAX_LEN = 60;//SNIPPET_PREW_MAX_LEN可能是规定的摘要或预览的最大长度初值为60
MediaPlayer mPlayer;//定义了一个名为mPlayer的MediaPlayer对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);//调用父类的onCreate
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 {
mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1));
mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId);//获取传入的意图并从中获取笔记ID然后使用该ID从内容提供程序中获取笔记的摘录
mSnippet = mSnippet.length() > SNIPPET_PREW_MAX_LEN ? mSnippet.substring(0,
SNIPPET_PREW_MAX_LEN) + getResources().getString(R.string.notelist_string_info)
: mSnippet;//如果摘录超过了预定义的最大长度则将其截断并附加一些字符串该代码将截断后的摘录分配给变量mSnippet
} catch (IllegalArgumentException e) {
e.printStackTrace();
return;
}//从Intent中获取传递的数据如果数据无效则抛出IllegalArgumentException异常并打印堆栈跟踪信息并返回
mPlayer = new MediaPlayer();//创建一个MediaPlayer对象
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
showActionDialog();
playAlarmSound();
}//检查当前笔记是否可见于笔记数据库,如果是,则显示操作对话框并播放警报声音
else {
finish();
}//如果不是,结束当前活动
}
private boolean isScreenOn() {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);//获取 PowerManager 对象,然后调用其 isScreenOn()
return pm.isScreenOn();//调用其 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) {
// 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();
}
}
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.setAction(Intent.ACTION_VIEW); // 设置意图的操作为ACTION_VIEW
intent.putExtra(Intent.EXTRA_UID, mNoteId);// 将笔记的ID作为额外的信息传递给NoteEditActivity类
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;// 将音频播放器置为空
}
}
}