Android Open Source - rcdroid Car Service






From Project

Back to project page rcdroid.

License

The source code is released under:

Copyright (c) 2013, Jamie Furness (jamie@jamierf.co.uk) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following...

If you think the Android project rcdroid 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 com.jamierf.rcdroid;
// w  w  w  .  j  a  v  a 2  s.c  o  m
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import com.jamierf.maestro.MaestroServoController;
import com.jamierf.maestro.api.Product;
import com.jamierf.maestro.binding.AndroidDriverBinding;
import com.jamierf.maestro.binding.AsyncBindingListener;
import com.jamierf.maestro.settings.Settings;
import com.jamierf.rcdroid.config.CarConfig;
import com.jamierf.rcdroid.http.WebController;
import com.jamierf.rcdroid.input.SensorController;
import com.jamierf.rcdroid.logic.CarEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CarService extends Service {

    private static final Logger LOG = LoggerFactory.getLogger(CarService.class);
    private static final int NOTIFICATION_ID = R.string.app_name;

    private NotificationManager notificationManager;
    private CarEngine engine;

    @Override
    public Binder onBind(Intent intent) {
        return new Binder();
    }

    @Override
    public void onCreate() {
        LOG.info("Created car service");

        notificationManager = (NotificationManager) this.getSystemService(Service.NOTIFICATION_SERVICE);

        // TODO: config
        final CarConfig config = new CarConfig();

        final Settings settings = Settings.builder()
                .setNeverSuspend(true)
                .build();

        // Connect to the servo controller
        final Product product = config.getServos().getProduct();
        AndroidDriverBinding.bindToDevice(this, product, new AsyncBindingListener<AndroidDriverBinding>() {
            @Override
            public void onBind(int vendorId, int productId, AndroidDriverBinding driver) {
                try {
                    final SensorController sensors = new SensorController(CarService.this);
                    final MaestroServoController servos = new MaestroServoController(driver, settings);
                    final WebController web = new WebController(getAssets(), config.getWeb());

                    engine = new CarEngine(CarService.this, sensors, servos, web, config);
                } catch (Exception e) {
                    this.onException(e);
                }
            }

            @Override
            public void onException(Throwable throwable) {
                LOG.error("Unknown error occurred", throwable);
            }
        });
    }

    public void setNotification(int iconId, int textId) {
        final CharSequence text = this.getText(textId);

        // Create a notification with icon and text
        final Notification notification = new Notification(iconId, text, System.currentTimeMillis());

        // Set what to do when it's clicked
        final PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, CarActivity.class), 0);
        notification.setLatestEventInfo(this, text, text, intent);

        // Send the notification
        notificationManager.notify(NOTIFICATION_ID, notification);
    }

    public void removeNotification() {
        notificationManager.cancel(NOTIFICATION_ID);
    }

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

    @Override
    public void onDestroy() {
        // Stop the engine
        if (engine != null) {
            engine.stop();
            engine = null;
        }
    }
}




Java Source Code List

com.jamierf.rcdroid.CarActivity.java
com.jamierf.rcdroid.CarService.java
com.jamierf.rcdroid.config.CarConfig.java
com.jamierf.rcdroid.config.ServoConfig.java
com.jamierf.rcdroid.config.WebUIConfig.java
com.jamierf.rcdroid.http.Packet.java
com.jamierf.rcdroid.http.WebController.java
com.jamierf.rcdroid.http.handler.AssetResourceHandler.java
com.jamierf.rcdroid.http.handler.ControlProtocolHandler.java
com.jamierf.rcdroid.http.handler.JsonPOJOHandler.java
com.jamierf.rcdroid.http.listener.ClientListener.java
com.jamierf.rcdroid.http.listener.PacketListener.java
com.jamierf.rcdroid.input.SensorController.java
com.jamierf.rcdroid.input.api.BatteryStatus.java
com.jamierf.rcdroid.input.api.Coordinate.java
com.jamierf.rcdroid.input.sensor.AbstractSensor.java
com.jamierf.rcdroid.input.sensor.AccelerationSensor.java
com.jamierf.rcdroid.input.sensor.BatterySensor.java
com.jamierf.rcdroid.input.sensor.LocationSensor.java
com.jamierf.rcdroid.input.sensor.RotationSensor.java
com.jamierf.rcdroid.input.sensor.listener.SensorListener.java
com.jamierf.rcdroid.logic.CarEngine.java
com.jamierf.rcdroid.logic.packets.AbstractServoHandler.java
com.jamierf.rcdroid.logic.packets.PlayAlarmRequestHandler.java
com.jamierf.rcdroid.logic.packets.SetRotationRequestHandler.java
com.jamierf.rcdroid.logic.packets.SetSpeedRequestHandler.java
com.jamierf.rcdroid.logic.sensors.AccelerationSensorHandler.java
com.jamierf.rcdroid.logic.sensors.BatterySensorHandler.java
com.jamierf.rcdroid.logic.sensors.LocationSensorHandler.java
com.jamierf.rcdroid.logic.sensors.RotationSensorHandler.java