Android Open Source - android-tools C F Z Service






From Project

Back to project page android-tools.

License

The source code is released under:

MIT License

If you think the Android project android-tools 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 net.comfreeze.lib.service;
/*w  w  w  .  j  a  v a2  s  .  c o m*/
import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.*;
import android.util.Log;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentLinkedQueue;

abstract public class CFZService extends Service {
    private static final String TAG = CFZService.class.getSimpleName();
    public static boolean silent = false;
    public boolean persistent = false;
    protected ConcurrentLinkedQueue<CFZWorkerItem> workQueue;
    protected CFZWorkerThread workerThread;
    ArrayList<Messenger> clients = new ArrayList<Messenger>();

    abstract protected int getNotificationId();

    abstract protected void handleIntent(Intent intent);

    abstract protected String getThreadTag();

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //Log.d(TAG, "Received start ID: " + startId + ": " + intent);
        //handleIntent(intent);
        android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
        if (persistent) {
            // Run until explicitly stopped
            return START_STICKY;
        } else {
            // Default - die when no more clients
            return super.onStartCommand(intent, flags, startId);
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initializeWorkQueue();
        Log.w(TAG, "Well, hello there... you come here often?");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.w(TAG, "I'M MELtInG... mel..tin...g...");
    }

    protected void initializeWorkQueue() {
        workQueue = new ConcurrentLinkedQueue<CFZWorkerItem>();
        workerThread = new CFZWorkerThread(workQueue, getThreadTag());
        workerThread.start();
    }

    protected void addRunnable(CFZWorkerItem work) {
        // cancelPreviousWorkItem(work);
        synchronized (workQueue) {
            workQueue.add(work);
            workQueue.notifyAll();
        }
    }

    protected void scheduleRunnable(CFZWorkerItem work) {
        // cancelPreviousWorkItem(work);
        synchronized (workQueue) {
            if (!workQueue.contains(work) && !work.equals(workerThread.getActive())) {
                workQueue.add(work);
                workQueue.notifyAll();
            }
        }
    }

    protected void schedulePriorityRunnable(CFZWorkerItem work) {
        synchronized (workQueue) {
            cancelActiveWorkItem(work);
            workQueue.add(work);
            workQueue.notifyAll();
        }
    }

    protected void replaceRunnable(CFZWorkerItem work) {
        synchronized (workQueue) {
            cancelPreviousWorkItem(work);
            workQueue.add(work);
            workQueue.notifyAll();
        }
    }

    protected void cancelPreviousWorkItem(CFZWorkerItem work) {
        synchronized (workQueue) {
            if (workQueue.contains(work))
                workQueue.remove(work);
            workQueue.notifyAll();
        }
    }

    protected void cancelActiveWorkItem(CFZWorkerItem work) {
        cancelPreviousWorkItem(work);
        if (work.equals(workerThread.active))
            workerThread.interrupt();
    }

    protected void showNotification(CharSequence message) {
        Log.d(TAG, "Showing notification");
    }

    protected void registerClient(Message message) {
        Log.d(TAG, "Registering client");
        if (null != message.replyTo)
            clients.add(message.replyTo);
        // sendMessage(message.replyTo, Message.obtain(null,
        // ResponseType.REGISTERED.ordinal()));
    }

    protected void unregisterClient(Message message) {
        Log.d(TAG, "Unregistering client");
        if (null != message.replyTo)
            clients.remove(message.replyTo);
    }

    protected void sendMessage(Messenger client, Message msg) {
        Log.d(TAG, "Sending mesage");
        try {
            client.send(msg);
        } catch (RemoteException e) {
            Log.e(TAG, "Exception", e);
            clients.remove(client);
        }
    }

    protected void sendMessage(Message msg) {
        for (int i = clients.size() - 1; i >= 0; i--) {
            sendMessage(clients.get(i), msg);
        }
    }

    protected void sendMessage(int code) {
        sendMessage(Message.obtain(null, code));
    }

    public static final class RESULT {
        public static final int OK = Activity.RESULT_OK;
        public static final int CANCELED = Activity.RESULT_CANCELED;
        public static final int FIRST_USER = Activity.RESULT_FIRST_USER;
        public static final int ERROR = 400;
    }

    public static class CFZWorkerThread extends Thread {
        private final ConcurrentLinkedQueue<CFZWorkerItem> workQueue;
        public CFZWorkerItem active;

        public CFZWorkerThread(ConcurrentLinkedQueue<CFZWorkerItem> workQueue, String name) {
            super(name);
            this.workQueue = workQueue;
        }

        private CFZWorkerItem getWork() {
            synchronized (workQueue) {
                try {
                    while (workQueue.isEmpty())
                        workQueue.wait();
                    return workQueue.remove();
                } catch (InterruptedException ie) {
                    throw new AssertionError(ie);
                }
            }
        }

        public Runnable getActive() {
            return this.active;
        }

        public void run() {
            for (; ; ) {
                active = getWork();
                active.run();
            }
        }
    }

    abstract public static class CFZWorkerItem implements Runnable {
        private final Intent intent;
        private final int what;

        public CFZWorkerItem(Intent intent, int what) {
            this.intent = intent;
            this.what = what;
        }

        public void run() {
            processItem(intent, what);
        }

        public boolean equals(CFZWorkerItem item) {
            if (null != item && null != item.intent
        /* */ && (null != item.intent.getAction()
      /* */ && null != intent
      /* */ && item.intent.getAction().equals(intent.getAction()))
      /* */ && item.what == what)
                return true;
            return false;
        }

        abstract public void processItem(Intent intent, int what);
    }
}




Java Source Code List

net.comfreeze.lib.BundleBuilder.java
net.comfreeze.lib.CFZApplication.java
net.comfreeze.lib.ContentValueBuilder.java
net.comfreeze.lib.FragmentMap.java
net.comfreeze.lib.adapter.HeaderListAdapter.java
net.comfreeze.lib.adapter.IHeaderListAdapter.java
net.comfreeze.lib.adapter.SeparatedListAdapter.java
net.comfreeze.lib.api.BaseAPI.java
net.comfreeze.lib.api.RestAPI.java
net.comfreeze.lib.api.XMLAPI.java
net.comfreeze.lib.api.helper.CursorHelper.java
net.comfreeze.lib.api.helper.JSONHelper.java
net.comfreeze.lib.api.helper.ModelHelper.java
net.comfreeze.lib.api.xml.WordpressAPI.java
net.comfreeze.lib.audio.SoundManager.java
net.comfreeze.lib.db.DatabaseHelper.java
net.comfreeze.lib.db.DatabaseTable.java
net.comfreeze.lib.db.helper.HelperCursor.java
net.comfreeze.lib.db.model.CFZModel.java
net.comfreeze.lib.db.model.FieldColumnMap.java
net.comfreeze.lib.fragments.CFZListFragment.java
net.comfreeze.lib.provider.CFZSimpleProvider.java
net.comfreeze.lib.service.CFZService.java
net.comfreeze.lib.ui.SupportFragmentActivity.java
net.comfreeze.lib.ui.dialog.CFZDialogProgress.java
net.comfreeze.lib.ui.fragment.CFZFragmentBase.java
net.comfreeze.lib.views.BiScrollView.java
net.comfreeze.lib.views.CFZViewHelper.java
net.comfreeze.lib.views.FlowLayout.java
net.comfreeze.lib.views.GestureHelper.java
net.comfreeze.lib.views.HeaderListView.java
net.comfreeze.lib.views.ResizingView.java
net.comfreeze.lib.views.ViewCollection.java
net.comfreeze.lib.views.ViewUtils.java
net.comfreeze.lib.xml.XMLParser.java
net.comfreeze.lib.xml.wordpress.FeedXmlParser.java