package com.android.deskclock;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.database.Cursor;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CursorAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.Calendar;
/**
* AlarmClock application.
*/
public class AlarmClock extends Activity implements OnItemClickListener {
static final String PREFERENCES = "AlarmClock";
/** This must be false for production. If true, turns on logging,
test code, etc. */
static final boolean DEBUG = false;
private SharedPreferences mPrefs;
private LayoutInflater mFactory;
private ListView mAlarmsList;
private Cursor mCursor;
private void updateIndicatorAndAlarm(boolean enabled, ImageView bar,
Alarm alarm) {
bar.setImageResource(enabled ? R.drawable.ic_indicator_on
: R.drawable.ic_indicator_off);
Alarms.enableAlarm(this, alarm.id, enabled);
if (enabled) {
SetAlarm.popAlarmSetToast(this, alarm.hour, alarm.minutes,
alarm.daysOfWeek);
}
}
private class AlarmTimeAdapter extends CursorAdapter {
public AlarmTimeAdapter(Context context, Cursor cursor) {
super(context, cursor);
}
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View ret = mFactory.inflate(R.layout.alarm_time, parent, false);
DigitalClock digitalClock =
(DigitalClock) ret.findViewById(R.id.digitalClock);
digitalClock.setLive(false);
return ret;
}
public void bindView(View view, Context context, Cursor cursor) {
final Alarm alarm = new Alarm(cursor);
View indicator = view.findViewById(R.id.indicator);
// Set the initial resource for the bar image.
final ImageView barOnOff =
(ImageView) indicator.findViewById(R.id.bar_onoff);
barOnOff.setImageResource(alarm.enabled ?
R.drawable.ic_indicator_on : R.drawable.ic_indicator_off);
// Set the initial state of the clock "checkbox"
final CheckBox clockOnOff =
(CheckBox) indicator.findViewById(R.id.clock_onoff);
clockOnOff.setChecked(alarm.enabled);
// Clicking outside the "checkbox" should also change the state.
indicator.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
clockOnOff.toggle();
updateIndicatorAndAlarm(clockOnOff.isChecked(),
barOnOff, alarm);
}
});
DigitalClock digitalClock =
(DigitalClock) view.findViewById(R.id.digitalClock);
// set the alarm text
final Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, alarm.hour);
c.set(Calendar.MINUTE, alarm.minutes);
digitalClock.updateTime(c);
digitalClock.setTypeface(Typeface.DEFAULT);
// Set the repeat text or leave it blank if it does not repeat.
TextView daysOfWeekView =
(TextView) digitalClock.findViewById(R.id.daysOfWeek);
final String daysOfWeekStr =
alarm.daysOfWeek.toString(AlarmClock.this, false);
if (daysOfWeekStr != null && daysOfWeekStr.length() != 0) {
daysOfWeekView.setText(daysOfWeekStr);
daysOfWeekView.setVisibility(View.VISIBLE);
} else {
daysOfWeekView.setVisibility(View.GONE);
}
// Display the label
TextView labelView =
(TextView) view.findViewById(R.id.label);
if (alarm.label != null && alarm.label.length() != 0) {
labelView.setText(alarm.label);
labelView.setVisibility(View.VISIBLE);
} else {
labelView.setVisibility(View.GONE);
}
}
};
@Override
public boolean onContextItemSelected(final MenuItem item) {
final AdapterContextMenuInfo info =
(AdapterContextMenuInfo) item.getMenuInfo();
final int id = (int) info.id;
// Error check just in case.
if (id == -1) {
return super.onContextItemSelected(item);
}
switch (item.getItemId()) {
case R.id.delete_alarm:
// Confirm that the alarm will be deleted.
new AlertDialog.Builder(this)
.setTitle(getString(R.string.delete_alarm))
.setMessage(getString(R.string.delete_alarm_confirm))
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d,
int w) {
Alarms.deleteAlarm(AlarmClock.this, id);
}
})
.setNegativeButton(android.R.string.cancel, null)
.show();
return true;
case R.id.enable_alarm:
final Cursor c = (Cursor) mAlarmsList.getAdapter()
.getItem(info.position);
final Alarm alarm = new Alarm(c);
Alarms.enableAlarm(this, alarm.id, !alarm.enabled);
if (!alarm.enabled) {
SetAlarm.popAlarmSetToast(this, alarm.hour, alarm.minutes,
alarm.daysOfWeek);
}
return true;
case R.id.edit_alarm:
Intent intent = new Intent(this, SetAlarm.class);
intent.putExtra(Alarms.ALARM_ID, id);
startActivity(intent);
return true;
default:
break;
}
return super.onContextItemSelected(item);
}
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
mFactory = LayoutInflater.from(this);
mPrefs = getSharedPreferences(PREFERENCES, 0);
mCursor = Alarms.getAlarmsCursor(getContentResolver());
updateLayout();
}
private void updateLayout() {
setContentView(R.layout.alarm_clock);
mAlarmsList = (ListView) findViewById(R.id.alarms_list);
AlarmTimeAdapter adapter = new AlarmTimeAdapter(this, mCursor);
mAlarmsList.setAdapter(adapter);
mAlarmsList.setVerticalScrollBarEnabled(true);
mAlarmsList.setOnItemClickListener(this);
mAlarmsList.setOnCreateContextMenuListener(this);
View addAlarm = findViewById(R.id.add_alarm);
addAlarm.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addNewAlarm();
}
});
// Make the entire v
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
AlarmAlertWakeLock.rar (2个子文件)
AlarmAlertWakeLock.java 921B
AlarmClock.java 11KB
共 2 条
- 1
资源评论
我虽横行却不霸道
- 粉丝: 91
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功