/*
 * ClockReminder: To remind you in every clock on Android Wear.
 * Copyright (C) 2016 Yan QiDong
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package io.github.yanqd0.clockreminder;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

/**
 * To take some actions in the background.
 *
 * @author yanqd0
 */
public final class ActionService extends IntentService {
    public static final String ACTION_START = "io.github.yanqd0.clockreminder.action.ACTION_START";
    public static final String ACTION_STOP = "io.github.yanqd0.clockreminder.action.ACTION_STOP";

    public ActionService() {
        super("ActionService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent == null) return;

        String action = intent.getAction();
        if (action == null) return;

        switch (action) {
            case ACTION_START:
                handleActionStart();
                break;
            case ACTION_STOP:
                handleActionStop();
                break;
            default:
                break;
        }
    }

    private void handleActionStart() {
        Log.d(Option.TAG, "handleActionStart()");
    }

    private void handleActionStop() {
        Log.d(Option.TAG, "handleActionStop()");
    }
}

@@ -828,29 +828,96 @@
 oid.app.
-IntentService
+AlarmManager;%0Aimport android.app.IntentService;%0Aimport android.app.PendingIntent
 ;%0Aimport
@@ -967,16 +967,44 @@
 l.Log;%0A%0A
+import java.util.Calendar;%0A%0A
 /**%0A * T
@@ -1124,31 +1124,24 @@
 rvice %7B%0A    
-public 
 static final
@@ -1221,23 +1221,16 @@
 T%22;%0A    
-public 
 static f
@@ -1307,16 +1307,117 @@
 N_STOP%22;
+%0A    private static final String ACTION_CHIME = %22io.github.yanqd0.clockreminder.action.ACTION_CHIME%22;
 %0A%0A    pu
@@ -1683,24 +1683,92 @@
  (action) %7B%0A
+            case ACTION_CHIME:%0A                handleActionChime();%0A
             
@@ -2029,159 +2029,1266 @@
 tion
-Start() %7B%0A        Log.d(Option.TAG, %22handleActionStart()%22);%0A    %7D%0A%0A    private void handleActionStop() %7B%0A        Log.d(Option.TAG, %22handleActionStop()%22
+Chime() %7B%0A        Log.d(Option.TAG, %22handleActionChime()%22);%0A        startChimeActivity();%0A        scheduleNextChime();%0A    %7D%0A%0A    private void startChimeActivity() %7B%0A        Log.v(Option.TAG, %22startChimeActivity()%22);%0A        // TODO: A chime interface should be created.%0A    %7D%0A%0A    private void handleActionStart() %7B%0A        Log.d(Option.TAG, %22handleActionStart()%22);%0A        scheduleNextChime();%0A    %7D%0A%0A    private void scheduleNextChime() %7B%0A        Calendar cal = Calendar.getInstance();%0A        cal.set(Calendar.MINUTE, 0);%0A        cal.set(Calendar.SECOND, 0);%0A%0A        long nextTime = cal.getTimeInMillis();%0A        nextTime += AlarmManager.INTERVAL_HOUR;%0A%0A        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);%0A        am.setExact(AlarmManager.RTC_WAKEUP, nextTime, getClockIntent());%0A    %7D%0A%0A    private PendingIntent getClockIntent() %7B%0A        Intent intent = new Intent(this, ActionService.class);%0A        intent.setAction(ACTION_CHIME);%0A        return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);%0A    %7D%0A%0A    private void handleActionStop() %7B%0A        Log.d(Option.TAG, %22handleActionStop()%22);%0A        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);%0A        am.cancel(getClockIntent()
 );%0A 

Add operations of AlarmManager. #9