/*
 * 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;


/**
 * @projectName（项目名称）: xiaomi label
 * @package（包）: ui
 * @className（类名称）: AlarmInitReceiver
 * @description（类描述）: 该类继承于BroadcastReceiver，实现了对闹钟信息的接受
 * @author（创建人）: zhangchaoqun
 * @createDate（创建时间）: datetime
 * @updateUser（修改人）: user
 * @updateDate（修改时间）: datetime
 * @updateRemark（修改备注）: 说明本次修改内容
 * @version（版本）: v1.0
 */
public class AlarmInitReceiver extends BroadcastReceiver {

    //声明PROJECTION，每个元素包括id和提醒日期
    private static final String [] PROJECTION = new String [] {
        NoteColumns.ID,
        NoteColumns.ALERTED_DATE
    };

    //定义ID和时间初始值
    private static final int COLUMN_ID                = 0;
    private static final int COLUMN_ALERTED_DATE      = 1;

    @Override//表示重写
    /**
     * @description 描述:通过context并且监听intent来对接受到的广播信息的处理
     * @param 参数1:context
     * @param 参数2:intent
     * @param 参数3:
     * @return 返回值:void
     * @author zhangchaoqun
     */
    public void onReceive(Context context, Intent intent) {
        //获取当前日期
        long currentDate = System.currentTimeMillis();
        //Cursor在这里的作用是通过查找数据库中的标签内容，找到和当前系统时间相等的标签
        Cursor c = context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
                PROJECTION,
                NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE,
                //将long类型的currentDate转变为String类
                new String[] { String.valueOf(currentDate) },
                null);

        /*
         *当c != null时，将cursor移动到开始处，执行相关读取工作然后关闭cursor
         */
        if (c != null) {
            //移动到开始位置
            if (c.moveToFirst()) {
                do {
                    //获取提醒日期
                    long alertDate = c.getLong(COLUMN_ALERTED_DATE);
                    //通过定义一个intent方法与AlarmReceiver类建立连接
                    Intent sender = new Intent(context, AlarmReceiver.class);
                    sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
                    //新建闹钟管理者，使用系统的闹钟服务
                    AlarmManager alermManager = (AlarmManager) context
                            .getSystemService(Context.ALARM_SERVICE);
                    //调用安卓的AlarmManager设置一个系统提醒事项设置提醒时间为便签中所存的alertDate
                    alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
                } while (c.moveToNext());
            }
            //关闭游标
            c.close();
        }
    }
}
