Android Open Source - BLEMeshChat Chat Service






From Project

Back to project page BLEMeshChat.

License

The source code is released under:

GNU General Public License

If you think the Android project BLEMeshChat 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 pro.dbro.ble;
//from  w ww. j  a v  a  2 s  . c om
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

/**
 * Created by davidbrodsky on 11/4/14.
 */
public class ChatService extends Service implements ActivityRecevingMessagesIndicator {
    public static final String TAG = "ChatService";

    private ChatApp mApp;
    private boolean mActivityRecevingMessages;

    private ChatServiceBinder mBinder;

    private Looper mBackgroundLooper;
    private BackgroundThreadHandler mBackgroundHandler;
    private Handler mForegroundHandler;

    /** Handler Messages */
    public static final int CONNECT       = 0;
    public static final int SEND_MESSAGEE = 1;
    public static final int SHUTDOWN      = 2;

    @Override
    public void onCreate() {
        Log.i(TAG, "onCreate");
        mApp = new ChatApp(getApplicationContext());
        mApp.setActivityBoundIndicator(this);
        // Immediately make us available if an identity is available
        if (mApp.getPrimaryIdentity() != null) mApp.makeAvailable();

        // Start up the thread running the service.  Note that we create a
        // separate thread because the service normally runs in the process's
        // main thread, which we don't want to block.  We also make it
        // background priority so CPU-intensive work will not disrupt our UI.
        HandlerThread thread = new HandlerThread(TAG, android.os.Process.THREAD_PRIORITY_BACKGROUND);
        thread.start();

        // Get the HandlerThread's Looper and use it for our Handler
        mBackgroundLooper = thread.getLooper();
        mBackgroundHandler = new BackgroundThreadHandler(mBackgroundLooper);
        mForegroundHandler = new Handler(Looper.getMainLooper());
    }

    @Override
    public IBinder onBind(Intent intent) {
        if (mBinder == null) mBinder = new ChatServiceBinder();
        Log.i(TAG, "Bind service");
        return mBinder;
    }

    @Override
    public int onStartCommand (Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    /** ActivityRecevingMessagesIndicator */
    @Override
    public boolean isActivityReceivingMessages() {
        return mActivityRecevingMessages;
    }

    /** Binder through which Activities can interact with this Service */
    public class ChatServiceBinder extends Binder {

//        public ChatService getService() {
//            // Return this instance of LocalService so clients can call public methods
//            return ChatService.this;
//        }

        public void connect() {
            mBackgroundHandler.sendMessage(mBackgroundHandler.obtainMessage(CONNECT));
        }

        public void sendPublicMessageFromPrimaryIdentity(String message) {
            mBackgroundHandler.sendMessage(mBackgroundHandler.obtainMessage(SEND_MESSAGEE, message));
        }

        public ChatApp getChatApp() {
            return mApp;
        }

        public void shutdown() {
            mBackgroundHandler.sendMessage(mBackgroundHandler.obtainMessage(SHUTDOWN));
        }

        /**
         * Set by Activity bound to this Service. If isActive is false, this Service
         * should post incoming messages as Notifications.
         */
        public void setActivityReceivingMessages(boolean receivingMessages) {
            mActivityRecevingMessages = receivingMessages;
            Log.i(TAG, "setActivityRecevingMessages " + receivingMessages);
        }
    }

    /** Handler that processes Messages on a background thread */
    private final class BackgroundThreadHandler extends Handler {
        public BackgroundThreadHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case CONNECT:
                    Log.i(TAG, "handling connect");
                    mApp.makeAvailable();
                    break;
                case SEND_MESSAGEE:
                    mApp.sendPublicMessageFromPrimaryIdentity((String) msg.obj);
                    break;
                case SHUTDOWN:
                    Log.i(TAG, "handling shutdown");
                    mApp.makeUnavailable();

                    // Stop the service using the startId, so that we don't stop
                    // the service in the middle of handling another job
                    stopSelf(msg.arg1);
                    break;
            }
        }
    }
}




Java Source Code List

im.delight.android.identicons.AsymmetricIdenticon.java
im.delight.android.identicons.Identicon.java
im.delight.android.identicons.SymmetricIdenticon.java
pro.dbro.ble.ActivityRecevingMessagesIndicator.java
pro.dbro.ble.ChatAppTest.java
pro.dbro.ble.ChatApp.java
pro.dbro.ble.ChatService.java
pro.dbro.ble.crypto.KeyPair.java
pro.dbro.ble.crypto.SodiumShaker.java
pro.dbro.ble.data.ContentProviderStore.java
pro.dbro.ble.data.DataStore.java
pro.dbro.ble.data.model.ChatContentProvider.java
pro.dbro.ble.data.model.ChatDatabase.java
pro.dbro.ble.data.model.CursorModel.java
pro.dbro.ble.data.model.DataUtil.java
pro.dbro.ble.data.model.IdentityDeliveryTable.java
pro.dbro.ble.data.model.MessageCollection.java
pro.dbro.ble.data.model.MessageDeliveryTable.java
pro.dbro.ble.data.model.MessageTable.java
pro.dbro.ble.data.model.Message.java
pro.dbro.ble.data.model.PeerTable.java
pro.dbro.ble.data.model.Peer.java
pro.dbro.ble.protocol.BLEProtocol.java
pro.dbro.ble.protocol.IdentityPacket.java
pro.dbro.ble.protocol.MessagePacket.java
pro.dbro.ble.protocol.OwnedIdentityPacket.java
pro.dbro.ble.protocol.Protocol.java
pro.dbro.ble.transport.ConnectionGovernor.java
pro.dbro.ble.transport.ConnectionListener.java
pro.dbro.ble.transport.Transport.java
pro.dbro.ble.transport.ble.BLECentralConnection.java
pro.dbro.ble.transport.ble.BLECentralRequest.java
pro.dbro.ble.transport.ble.BLECentral.java
pro.dbro.ble.transport.ble.BLEPeripheralResponse.java
pro.dbro.ble.transport.ble.BLEPeripheral.java
pro.dbro.ble.transport.ble.BLETransport.java
pro.dbro.ble.transport.ble.BLEUtil.java
pro.dbro.ble.transport.ble.GATT.java
pro.dbro.ble.ui.Notification.java
pro.dbro.ble.ui.activities.LogConsumer.java
pro.dbro.ble.ui.activities.MainActivity.java
pro.dbro.ble.ui.activities.Util.java
pro.dbro.ble.ui.adapter.CursorFilter.java
pro.dbro.ble.ui.adapter.MessageAdapter.java
pro.dbro.ble.ui.adapter.PeerAdapter.java
pro.dbro.ble.ui.adapter.RecyclerViewCursorAdapter.java
pro.dbro.ble.ui.fragment.MessageListFragment.java
pro.dbro.ble.util.RandomString.java