|
|
/*
|
|
|
* 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;//便签内容在数据库存储的ID
|
|
|
private String mSnippet;//闹钟出现的内容
|
|
|
private static final int SNIPPET_PREW_MAX_LEN = 60;
|
|
|
MediaPlayer mPlayer;
|
|
|
|
|
|
@Override
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
/**
|
|
|
* Bundle在Android开发中非常常见,它的作用主要时用于传递数据。
|
|
|
* Bundle传递的数据包括:string、int、boolean、byte、float、long、double等基本类型或它们对应的数组,也可以是对象或对象数组。
|
|
|
* 当Bundle传递的是对象或对象数组时,必须实现Serialiable或Parcelable接口。
|
|
|
*/
|
|
|
|
|
|
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 {
|
|
|
mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1));
|
|
|
mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId);//根据ID从数据库中获取标签的内容
|
|
|
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();//闹钟完成
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private boolean isScreenOn() {//判断屏幕是否锁屏,调用系统函数判断,最后返回值是boolean值(Ture或False)
|
|
|
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
|
|
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();//开始播放
|
|
|
/**
|
|
|
* MediaPlayer 类是媒体框架最重要的组成部分之一。
|
|
|
* 此类的对象能够获取、解码以及播放音频和视频,而且只需极少量设置。它支持多种不同的媒体源
|
|
|
* 比如本地资源、内部 URI,例如您可能从内容解析器那获取的 URI、外部网址(流式传输)
|
|
|
*/
|
|
|
} 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);
|
|
|
}
|
|
|
/**
|
|
|
* AlertDialog可以在当前的界面上显示一个对话框,这个对话框是置顶于所有界面元素之上的,能够屏蔽掉其他控件的交互能力,因此AlertDialog一般是用于提示一些非常重要的内容或者警告信息。
|
|
|
* 1.创建构造器AlertDialog.Builder的对象;
|
|
|
* 2.通过构造器对象调用setTitle、setMessage、setIcon等方法构造对话框的标题、信息和图标等内容;
|
|
|
* 3.根据需要调用setPositive/Negative/NeutralButton()方法设置正面按钮、负面按钮和中立按钮;
|
|
|
* 4.调用构造器对象的create方法创建AlertDialog对象;
|
|
|
* 5.AlertDialog对象调用show方法,让对话框在界面上显示。
|
|
|
*/
|
|
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
switch (which) {//选择click后下一步的操作
|
|
|
case DialogInterface.BUTTON_NEGATIVE://取消
|
|
|
Intent intent = new Intent(this, NoteEditActivity.class);//两个类间的数据传输
|
|
|
intent.setAction(Intent.ACTION_VIEW);//动作属性
|
|
|
intent.putExtra(Intent.EXTRA_UID, mNoteId);//实现key-value对,EXTRA_UID为key,mNoteId为键
|
|
|
startActivity(intent);//开始
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void onDismiss(DialogInterface dialog) {
|
|
|
stopAlarmSound();//暂停闹钟声音
|
|
|
finish();
|
|
|
}
|
|
|
|
|
|
private void stopAlarmSound() {
|
|
|
if (mPlayer != null) {
|
|
|
mPlayer.stop();//停止播放
|
|
|
mPlayer.release();//释放MediaPlayer对象相关的资源
|
|
|
mPlayer = null;
|
|
|
}
|
|
|
}
|
|
|
}
|