Android Open Source - verfluchter-android Work Time Notifier Service






From Project

Back to project page verfluchter-android.

License

The source code is released under:

--2010-08-23 23:59:46-- http://www.gnu.org/licenses/gpl-3.0.txt Translacja www.gnu.org... 199.232.41.10 ???czenie si? z www.gnu.org|199.232.41.10|:80... po??czono. ??danie HTTP wys?ano, oczeki...

If you think the Android project verfluchter-android 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

/*
 * This file is part of verfluchter-android.
 *//from   w  ww  .  j av a 2  s  . c o  m
 * verfluchter-android is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * verfluchter-android 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */

package pl.xsolve.verfluchter.services;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import pl.xsolve.verfluchter.R;
import pl.xsolve.verfluchter.activities.VerfluchterActivity;
import pl.xsolve.verfluchter.tools.Constants;
import pl.xsolve.verfluchter.tools.SoulTools;
import pl.xsolve.verfluchter.tools.WorkStatus;

import java.util.GregorianCalendar;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Konrad Ktoso Malawski
 */
public class WorkTimeNotifierService extends Service {

    // logger tag
    private static final String TAG = WorkTimeNotifierService.class.getSimpleName();

    // time checking interval
    private static final long INTERVAL = 10 * Constants.MINUTE;

    private Timer timer = new Timer();
    public static final String INTENT_HEY_STOP_WORKING = "HEY_STOP_WORKING";

    NotificationManager notificationManager;

    /**
     * Called on service creation, will start the timer
     */
    @Override
    public void onCreate() {
        super.onCreate();

        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        start();

        Log.v(TAG, "Service started");
    }

    /**
     * Called on service destruction, will stop the timer
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        stop();

        Log.v(TAG, "Service stopped");
    }

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

    private void start() {
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                process();
            }
        }, Constants.MINUTE, INTERVAL);
    }

    private void process() {
        Log.v(TAG, "WorkTimeNotifierService is processing.");

        GregorianCalendar now = new GregorianCalendar();

        if (SoulTools.itsWeekend(now)) {
            return;
        }

        boolean workTimeHasBegun = SoulTools.workTimeHasBegun(now);
        boolean workTimeIsOver = SoulTools.workTimeIsOver(now);

        if (isWorking()) {
            if (workTimeIsOver) {
                notifyUser(WorkStatus.YOU_CAN_STOP_WORKING);
            }
        } else {// not working, but should I...?
            if (workTimeHasBegun && !workTimeIsOver) {
                notifyUser(WorkStatus.YOU_SHOULD_START_WORKING);
            }
        }
    }

    private boolean isWorking() {
        return VerfluchterActivity.isCurrentlyWorking();
    }

    private void notifyUser(WorkStatus workStatus) {
        Log.d(TAG, "Displaying notification for working status: " + workStatus);
        long when = System.currentTimeMillis();
        int icon = R.drawable.icon;
        CharSequence titleText;

        Context context = getApplicationContext();
        CharSequence contentTitle;
        CharSequence contentText;

        // setup strings etc
        titleText = getString(workStatus.contextTitle);
        contentTitle = getString(workStatus.contextTitle);
        contentText = getString(workStatus.contentText);

        // build the notification/intent
        Notification notification = new Notification(icon, titleText, when);
        Intent notificationIntent = new Intent(this, VerfluchterActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        // this Notification will be canceled after the user clicks it
        notification.defaults |= Notification.FLAG_AUTO_CANCEL;

//        if(isTrue(autoSettings.getSetting(AutoSettings.USE_SOUND_B, Boolean.class))){
//            notification.defaults |= Notification.DEFAULT_SOUND;
//        or even better: notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
//        http://developer.android.com/guide/topics/ui/notifiers/notifications.html
//        }

        // pass to the notification manager to display the notification
        notificationManager.notify(workStatus.ordinal(), notification);
    }

    private void stop() {
        notificationManager.cancelAll();

        if (timer != null) {
            timer.cancel();
        }
    }
}




Java Source Code List

pl.xsolve.verfluchter.activities.AboutViewActivity.java
pl.xsolve.verfluchter.activities.CommonViewActivity.java
pl.xsolve.verfluchter.activities.SettingsActivity.java
pl.xsolve.verfluchter.activities.VerfluchterActivity.java
pl.xsolve.verfluchter.exceptions.RestResponseException.java
pl.xsolve.verfluchter.guice.MyApplication.java
pl.xsolve.verfluchter.guice.MyModule.java
pl.xsolve.verfluchter.guice.Test.java
pl.xsolve.verfluchter.rest.EasySSLSocketFactory.java
pl.xsolve.verfluchter.rest.EasyX509TrustManager.java
pl.xsolve.verfluchter.rest.RequestMethod.java
pl.xsolve.verfluchter.rest.RestClient.java
pl.xsolve.verfluchter.rest.RestResponse.java
pl.xsolve.verfluchter.services.BootReceiver.java
pl.xsolve.verfluchter.services.RefreshService.java
pl.xsolve.verfluchter.services.WorkTimeNotifierService.java
pl.xsolve.verfluchter.tasks.ChangeWorkingStatusAsyncTask.java
pl.xsolve.verfluchter.tasks.ChangeWorkingStatusListener.java
pl.xsolve.verfluchter.tasks.RefreshDataAsyncTask.java
pl.xsolve.verfluchter.tasks.RefreshDataListener.java
pl.xsolve.verfluchter.tasks.general.CanDisplayErrorMessages.java
pl.xsolve.verfluchter.tasks.general.CanDisplayProgress.java
pl.xsolve.verfluchter.tasks.general.RestAsyncTask.java
pl.xsolve.verfluchter.tasks.general.SampleAsyncTask.java
pl.xsolve.verfluchter.tools.AutoSettingsImpl.java
pl.xsolve.verfluchter.tools.AutoSettings.java
pl.xsolve.verfluchter.tools.Constants.java
pl.xsolve.verfluchter.tools.HourMin.java
pl.xsolve.verfluchter.tools.PasswdUtil.java
pl.xsolve.verfluchter.tools.SoulTools.java
pl.xsolve.verfluchter.tools.UberSimplePasswdUtil.java
pl.xsolve.verfluchter.tools.WorkStatus.java