|
|
/*
|
|
|
* 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.
|
|
|
*/
|
|
|
|
|
|
// AlarmAlertActivity 类是一个 Activity,它用于处理笔记的提醒警报。
|
|
|
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; // 用于播放音频的 MediaPlayer 对象。
|
|
|
|
|
|
// 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);
|
|
|
}
|
|
|
|
|
|
// 获取意图中的数据。
|
|
|
Intent intent = getIntent();
|
|
|
|
|
|
// 尝试获取笔记 ID 和摘要。
|
|
|
try {
|
|
|
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;
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
e.printStackTrace();
|
|
|
return; // 如果出错,结束活动。
|
|
|
}
|
|
|
|
|
|
mPlayer = new MediaPlayer(); // 创建新的 MediaPlayer 对象。
|
|
|
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
|
|
|
showActionDialog(); // 显示操作对话框。
|
|
|
playAlarmSound(); // 播放警报声。
|
|
|
} else {
|
|
|
finish(); // 如果笔记不存在,结束活动。
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// isScreenOn 方法检查屏幕是否开启。
|
|
|
private boolean isScreenOn() {
|
|
|
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
|
|
return pm.isScreenOn();
|
|
|
}
|
|
|
|
|
|
// playAlarmSound 方法播放警报声。
|
|
|
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) {
|
|
|
e.printStackTrace();
|
|
|
} catch (SecurityException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (IllegalStateException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// showActionDialog 方法显示一个操作对话框。
|
|
|
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); // 显示对话框并设置关闭监听器。
|
|
|
}
|
|
|
|
|
|
// onClick 方法是 OnClickListener 接口的实现,用于处理确定按钮的点击事件。
|
|
|
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); // 启动 NoteEditActivity。
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// onDismiss 方法是 OnDismissListener 接口的实现,用于处理对话框关闭的事件。
|
|
|
public void onDismiss(DialogInterface dialog) {
|
|
|
stopAlarmSound(); // 停止警报声。
|
|
|
finish(); // 结束活动。
|
|
|
}
|
|
|
|
|
|
// stopAlarmSound 方法停止警报声的播放。
|
|
|
private void stopAlarmSound() {
|
|
|
if (mPlayer != null) {
|
|
|
mPlayer.stop();
|
|
|
mPlayer.release();
|
|
|
mPlayer = null; // 释放 MediaPlayer 对象。
|
|
|
}
|
|
|
}
|
|
|
} |