Android Open Source - berlinclock-android-widget Update Service






From Project

Back to project page berlinclock-android-widget.

License

The source code is released under:

MIT License

If you think the Android project berlinclock-android-widget listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ch.crisi.berlinclock_widget;
//from w w w.  j  av a2s. c o  m
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.widget.RemoteViews;
import ch.crisi.tdd.berlinclock.BerlinClock;
import ch.crisi.tdd.berlinclock.Color;
import ch.crisi.tdd.berlinclock.Lamp;

import java.util.Calendar;
import java.util.List;

public class UpdateService extends Service {

    private IntentFilter intentFilter;
    private BroadcastReceiver broadcastReceiver;
    private static final String[] fiveHourViews = new String[]{
            "a1", "a2", "a3", "a4"
    };
    private static final String[] oneHourViews = new String[]{
            "b1", "b2", "b3", "b4"
    };
    private static final String[] fiveMinuteViews = new String[]{
            "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "c11"
    };
    private static final String[] oneMinuteViews = new String[]{
            "d1", "d2", "d3", "d4"
    };

    private BerlinClock berlinClock;

    @Override
    public void onCreate() {
        super.onCreate();

        berlinClock = new BerlinClock();
        broadcastReceiver = new TimeActionsReceiver(this);
        intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
        intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        intentFilter.addAction(Intent.ACTION_TIME_TICK);

        registerReceiver(broadcastReceiver, intentFilter);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        update();

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        unregisterReceiver(broadcastReceiver);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public void update() {
        final RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget);

        final Calendar cal = Calendar.getInstance();
        final int hour = cal.get(Calendar.HOUR_OF_DAY);
        final int minute = cal.get(Calendar.MINUTE);

        berlinClock.setHours(hour).setMinutes(minute);

        updateViews(remoteViews, berlinClock.getFiveHoursRow().getLamps(), fiveHourViews);
        updateViews(remoteViews, berlinClock.getOneHourRow().getLamps(), oneHourViews);
        updateViews(remoteViews, berlinClock.getFiveMinutesRow().getLamps(), fiveMinuteViews);
        updateViews(remoteViews, berlinClock.getOneMinuteRow().getLamps(), oneMinuteViews);

        final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
        final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(this, BerlinClockWidget.class));
        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
    }

    private void updateViews(RemoteViews remoteViews, List<Lamp> lamps, String[] viewNames) {
        for (int i = 0; i < viewNames.length; i++) {
            int id = getResources().getIdentifier(viewNames[i], "id", getPackageName());
            final Lamp lamp = lamps.get(i);

            if (lamp.isOn()) {
                remoteViews.setInt(id, "setBackgroundColor", getLitColor(lamp));
            } else {
                remoteViews.setInt(id, "setBackgroundResource", getOffColor(lamp));
            }
        }
    }

    private int getLitColor(Lamp lamp) {
        if (lamp.getColor() == Color.RED) {
            return getResources().getColor(R.color.red);
        } else {
            return getResources().getColor(R.color.yellow);
        }
    }

    private int getOffColor(Lamp lamp) {
        if (lamp.getColor() == Color.RED) {
            return R.drawable.red_border;
        } else {
            return R.drawable.yellow_border;
        }
    }

}




Java Source Code List

ch.crisi.berlinclock_widget.ApplicationTest.java
ch.crisi.berlinclock_widget.BerlinClockWidget.java
ch.crisi.berlinclock_widget.TimeActionsReceiver.java
ch.crisi.berlinclock_widget.UpdateService.java