
/*
 * 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;//这个类包含在 net.micode.notes.ui包里
//导入各种类
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;//监听开机广播
import android.content.ContentUris;//内容链接
import android.content.Context;//文本
import android.content.Intent;
import android.database.Cursor;//目的和光标

import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns;//便签栏


public class AlarmInitReceiver extends BroadcastReceiver {//该类继承BroadcastReceiver（广播接收器）类，实现闹钟响应的接收器

    private static final String [] PROJECTION = new String [] {//声明PROJECTION，每个元素包括id和提醒日期
        NoteColumns.ID,
        NoteColumns.ALERTED_DATE
    };
//设定ID和闹钟信息的初始值
    private static final int COLUMN_ID                = 0;
    private static final int COLUMN_ALERTED_DATE      = 1;

    @Override
    public void onReceive(Context context, Intent intent) {//接收广播后的处理过程
        long currentDate = System.currentTimeMillis();//通过系统函数获了当前时间，存入currentDate变量。
        Cursor c = context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
                PROJECTION,
                NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE,
                new String[] { String.valueOf(currentDate) },
                null);//光标c遍历数据库，寻找出所有提醒时间大于现在时间的便签

        if (c != null) {//当c != null时候，然后执行相关对信息的读取工作，直至读取结束，然后关闭cursor
            if (c.moveToFirst()) {//将cursor移动到开始处
                do {
                    long alertDate = c.getLong(COLUMN_ALERTED_DATE);//获取要提醒的日期
                    Intent sender = new Intent(context, AlarmReceiver.class);//通过定义一个Intent方法，与AlarmReceiver类建立连接
                    sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));//改变数据为 新的uri 和id号
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);//创建一个广播组件，可以在手机广播栏显示相应内容
                    AlarmManager alermManager = (AlarmManager) context
                            .getSystemService(Context.ALARM_SERVICE);//新建闹钟管理者，使用系统的闹钟服务
                    alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);//设置一系列需要传输的数据
                } while (c.moveToNext());//游标移动到下一位置,不空则继续循环
            }
            c.close();
        }
    }
}
