/*
 * 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.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 {

    private static final String [] PROJECTION = new String [] {
        NoteColumns.ID,
        NoteColumns.ALERTED_DATE
    };

    private static final int COLUMN_ID                = 0;
    private static final int COLUMN_ALERTED_DATE      = 1;

    @Override
    public void onReceive(Context context, Intent intent) { //设置接收装置
        //获取当前时间的常用方法。它返回自 1970 年 1 月 1 日 00:00:00.000 到当前时刻的时间距离
        long currentDate = System.currentTimeMillis();
        //查询未到响应时间且是有提醒类型的便签
        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);
        if (c != null) {
            if (c.moveToFirst()) {
                do {
                    long alertDate = c.getLong(COLUMN_ALERTED_DATE);
                    //显示intent格式，明确启动应用中的AlarmReceiver类
                    Intent sender = new Intent(context, AlarmReceiver.class);
                    //指定要访问的资源URI(这里的uir是把id和contentUri连接后成一个新的Uri)
                    sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
                    //(满足特定条件但这里是0即没有)向BroadcastReceiver发送执行sender的广播 
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
                    //获取 AlarmManager
                    AlarmManager alermManager = (AlarmManager) context
                            .getSystemService(Context.ALARM_SERVICE);
                    //set(int type，long startTime，PendingIntent pi) ,设置一次性定时器，到达时间执行完就没了
                    alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
                                    //闹钟在睡眠状态下会唤醒系统并执行提示功能
                } while (c.moveToNext());
            }
            c.close();
        }
    }
}
