DataProxy
Library that provide easy way make a comunication between remote application (such wear and phone application, processes, etc) using a common interface. This project transforms a interface method call into a byte array to be sent to anyware and when injected back to the library this method called will be invoked in all registered objects thats implements this same interface.
Usage
Create a common interface with void method.
public interface TestInterface {
void onTestNoParameters();
void onTestPrimitive(String s, int i, long l, float f, double d, short st, char c, boolean b, byte bt);
void onTestSerializable(File file);
void onTestParcelable(Intent intent);
...
}
Implements a Sender that is used to send a data stream to your receiver target, in this case we are sending through a broadcast.
public class BroadcastSender implements Sender {
private Context mContext;
public BroadcastSender(final Context context) {
mContext = context;
}
@Override
public void onInvokeStream(final byte[] data) {
Intent intent = new Intent("broadcast");
intent.putExtra("data", data);
mContext.sendBroadcast(intent);
}
}
Get mocked interface by passing the interface class and a sender, then call any method in this interface.
void callNoParams() {
final TestInterface test = DataProxy.get().build(TestInterface.class, new BroadcastSender(this))
test.onTestNoParameters();
}
In your receiver you must inject the data stream into the library.
public class DataReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
DataProxy.get().inject(intent.getByteArrayExtra("data"));
}
}
The callback onTestNoParameters()
of any registered object that implements this interface will be called.
@Override
public void onTestNoParameters() {
Log.d(TAG, "Called");
}
Supportable data
- Primitives
- Serializables
- Parcelables
- Primitives array
- Serializables array
- Parcelable array
Compatibility
Android API 1 or above.
License
Copyright 2015, fabriciovergal Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.