Android Open Source - Android-NetPowerctrl-Shared App2 Plugin Service






From Project

Back to project page Android-NetPowerctrl-Shared.

License

The source code is released under:

Copyright (c) 2014, David Gr?ff All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: *...

If you think the Android project Android-NetPowerctrl-Shared 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 oly.netpowerctrl.plugin;
/*w w w  . j a  v  a  2 s . com*/
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import oly.netpowerctrl.device_base.device.Device;
import oly.netpowerctrl.device_base.device.DeviceConnection;
import oly.netpowerctrl.device_base.device.DeviceConnectionAPI;
import oly.netpowerctrl.device_base.device.DevicePort;

/**
 * This service (or an inherited class) will be started if the netpowerctrl app
 * starts to find plugins. Remember to register this class or your inherited class
 * in the AndroidManifest file.
 */
public abstract class App2PluginService extends Service implements App2PluginCommunication.ComCallbacks, onDeviceStateChange {
    public DeviceConnectionStateInterface states;
    protected App2PluginCommunication mRemoteBinder;
    protected Device device;
    protected boolean device_complete_transfer = true;

    public App2PluginService() {
    }

    public void setStates(DeviceConnectionStateInterface states) {
        this.states = states;
    }

    /**
     * Instead of sending the complete device description if a reachability of one of your connections
     * changes, only the device connection will be send.
     *
     * @param partialUpdates If true: Send only changed device connection otherwise send always the complete device.
     */
    public void setSendPartialUpdates(boolean partialUpdates) {
        device_complete_transfer = !partialUpdates;
    }

    public void updateDevice(Device device) {
        mRemoteBinder.deviceChanged(device);
    }

    public void updateDevicePort(DevicePort devicePort) {
        if (device_complete_transfer)
            mRemoteBinder.deviceChanged(devicePort.device);
        else
            mRemoteBinder.updateDevicePort(devicePort);
        device_complete_transfer = false;
    }

    public void updateDeviceConnection(Device device, int device_connection_id) {
        DeviceConnection deviceConnection = device.getConnectionByID(device_connection_id);

        if (deviceConnection.getNotReachableReason() != null)
            Log.w("STATUS", deviceConnection.getNotReachableReason());
        else
            Log.w("STATUS", "Reachable");

        if (device_complete_transfer)
            mRemoteBinder.deviceChanged(device);
        else
            mRemoteBinder.updateDeviceConnection(device, device_connection_id);
    }

    @Override
    public void setNewState(Device device, int device_connection_id, int state_index) {
        DeviceConnection deviceConnection = device.getConnectionByID(device_connection_id);
        if (states.isReachableState(state_index)) {
            deviceConnection.device.connectionUsed(deviceConnection);
        } else
            deviceConnection.device.setStatusMessage(deviceConnection,
                    states.getTranslation(this, state_index), true);

        if (mRemoteBinder.AppInterface != null)
            updateDeviceConnection(device, device_connection_id);
    }

    /**
     * Create a convenience device with a DeviceConnection of type DeviceConnectionAPI,
     * accessible via the member variable "device".
     *
     * @param pluginID   A unique id among all plugins. For example use your applications android package name.
     * @param deviceName A device name shown in the user interface of the host app.
     */
    public void createOneDevice(String pluginID, String deviceName) {
        device = new Device(true);
        device.setPluginInterface(device); // Fake plugin interface, otherwise reachableState is always false
        device.pluginID = pluginID;
        device.setDeviceName(deviceName);
        device.setUniqueDeviceID(device.pluginID);
        DeviceConnectionAPI deviceConnectionAPI = new DeviceConnectionAPI(device);
        deviceConnectionAPI.mHostName = device.getDeviceName();
        deviceConnectionAPI.setIncludeUseCounter(true);
        device.addConnection(deviceConnectionAPI);
    }

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

        mRemoteBinder = new App2PluginCommunication(this);
    }

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




Java Source Code List

oly.netpowerctrl.device_base.ApplicationTest.java
oly.netpowerctrl.device_base.data.JSONHelper.java
oly.netpowerctrl.device_base.data.StorableInterface.java
oly.netpowerctrl.device_base.device.DeviceConnectionAPI.java
oly.netpowerctrl.device_base.device.DeviceConnectionFabric.java
oly.netpowerctrl.device_base.device.DeviceConnectionHTTP.java
oly.netpowerctrl.device_base.device.DeviceConnectionUDP.java
oly.netpowerctrl.device_base.device.DeviceConnection.java
oly.netpowerctrl.device_base.device.DeviceFeatureFabric.java
oly.netpowerctrl.device_base.device.DeviceFeatureInterface.java
oly.netpowerctrl.device_base.device.DeviceFeatureTemperature.java
oly.netpowerctrl.device_base.device.DevicePort.java
oly.netpowerctrl.device_base.device.Device.java
oly.netpowerctrl.device_base.executables.ExecutableReachability.java
oly.netpowerctrl.device_base.executables.ExecutableType.java
oly.netpowerctrl.device_base.executables.Executable.java
oly.netpowerctrl.plugin.App2PluginCommunication.java
oly.netpowerctrl.plugin.App2PluginService.java
oly.netpowerctrl.plugin.DeviceConnectionStateInterface.java
oly.netpowerctrl.plugin.onDeviceStateChange.java
oly.netpowerctrl.plugin.onServiceStateChange.java