Thursday 15 November 2012

Posting blog from Mobile

This day has come, the day when we will write our blog from mobile phone. Who ever have thought those bulky black n white screen mobile phones would replace computers.

So called smart phones IOS or army of androids finally have take over to coputer world. Still we are using computers but imagine 10years from now will totally different. We will work on mobiles. Almost everything.. Use cloud servers for more complicated works. And computers will be kept for servers only everything else will be on mobile. People will use wireless mouse n keyboards for input to mobile. And TVs will replace computer screens.

pictures can also be uploaded from mobile see below picture.

Android IPC Binder Interface, Binder Proxy and Binder Native Simplified.


 The IInterface, BpInterface, and BnInterface classes are provided by the Android framework.
We start by defining an interface (think AIDL) that will be shared between the service and the client:

class IDemo : public IInterface {
    public:
        enum {
            ALERT = IBinder::FIRST_CALL_TRANSACTION,
            PUSH,
            ADD
        };
        // Sends a user-provided value to the service
        virtual void        push(int32_t data)          = 0;
        // Sends a fixed alert string to the service
        virtual void        alert()                     = 0;
        // Requests the service to perform an addition and return the result
        virtual int32_t     add(int32_t v1, int32_t v2) = 0;
        DECLARE_META_INTERFACE(Demo);
};
// This implementation macro would normally go in a cpp file
IMPLEMENT_META_INTERFACE(Demo, "Demo");

Next we define the server end, which is made up of 2 classes: BnDemo, and its derived class, Demo. BnDemo extracts the arguments from the data Parcel sent by the client, calls the appropriate virtual function (implemented in the Demo class) to do the heavy-lifting, and packs the returned values (if any) into a reply Parcel to be sent back to the client.
class BnDemo : public BnInterface {
    virtual status_t onTransact(uint32_t code, const Parcel& data,
                                Parcel* reply, uint32_t flags = 0);
};
status_t BnDemo::onTransact(uint32_t code, const Parcel& data,
                            Parcel* reply, uint32_t flags) {
    data.checkInterface(this);
    switch(code) {
        case ALERT: {
            alert();
            return NO_ERROR;
        } break;
        case PUSH: {
            int32_t inData = data.readInt32();
            push(inData);
            return NO_ERROR;
        } break;
        case ADD: {
            int32_t inV1 = data.readInt32();
            int32_t inV2 = data.readInt32();
            int32_t sum = add(inV1, inV2);
            reply->writeInt32(sum);
            return NO_ERROR;
        } break;
        default:
            return BBinder::onTransact(code, data, reply, flags);
    }
}
This is the Demo class, which would normally do the real work on the service side of the binder.
class Demo : public BnDemo {
    virtual void push(int32_t data) {
        // Do something with the data the client pushed
    }
    virtual void alert() {
        // Handle the alert
    }
    virtual int32_t add(int32_t v1, int32_t v2) {
        return v1 + v2;
    }
};

Now we define a service proxy, to be used on the client side. Notice again that any data the client needs to send to the service is packed in a Parcel and results (if any) are also returned in a Parcel.
class BpDemo : public BpInterface {
    public:
        BpDemo(const sp& impl) : BpInterface(impl) { }
        virtual void push(int32_t push_data) {
            Parcel data, reply;
            data.writeInterfaceToken(IDemo::getInterfaceDescriptor());
            data.writeInt32(push_data);
            remote()->transact(PUSH, data, &reply);
        }
        virtual void alert() {
            Parcel data, reply;
            data.writeInterfaceToken(IDemo::getInterfaceDescriptor());
            remote()->transact(ALERT, data, &reply, IBinder::FLAG_ONEWAY);
        }
        virtual int32_t add(int32_t v1, int32_t v2) {
            Parcel data, reply;
            data.writeInterfaceToken(IDemo::getInterfaceDescriptor());
            data.writeInt32(v1);
            data.writeInt32(v2);
            remote()->transact(ADD, data, &reply);
            int32_t res;
            status_t status = reply.readInt32(&res);
            return res;
        }
};

Finally, we start the service as follows:
defaultServiceManager()->addService(String16("Demo"), new Demo());
android::ProcessState::self()->startThreadPool();

And the client can now connect to the service and call some of the provided functions:
sp sm = defaultServiceManager();
sp binder = sm->getService(String16("Demo"));
sp demo = interface_cast(binder);
demo->alert();
demo->push(65);
int32_t sum = demo->add(453, 827);

Author:Gabriel Burca
Source: http://ebixio.com/blog/2012/07/07/using-android-ipc-binders-from-native-code/