How to make copy of android.media.Image object? - android

I need to make a copy of "android.media.Image" object for a solution, but since the class is defined abstract we can not make instances of it ourselves.
I am getting my instance of this object from ((ImageReader)reader).acquireLatestImage();
Is there any workaround that I can get two different clone instances of the object returned by this method?
Any suggestions are much appreciated.

Related

Kotlin Firebase Parsing Duplication

So I have a Game object that has an init block where I setup the object and upload to the Firebase Firestore. Then when I'm listening for changes in that object I have to convert the DocumentSnapshot to a Game object.
game = snapshot.toObject(Game::class.java)
Pretty simple. The problem is is that this calls the init block of my Game class and uploads another game object. Is there a way I can avoid calling the init block while doing this? Thanks!
When you use automatic field mapping like this, the convention is that you should use a class definition that contains only the fields you want to map, and nothing else. Objects that have only getters and setters for properties are called JavaBeans, and their sole purpose is to store data. These objects must define a default no-arg constructor.
If you have additional logic that works with your Game object, that should go in a different class. It's better design to keep your data separate from the logic that works with the data (as you have discovered).
Move your init code to a constructor, which you can call when you're creating an instance of your Game class and you want it to upload the game object.

Does retrieving a parcelable object through bundle always create new copy?

I'm passing a parcelable object to a fragment by adding into a bundle while creating the fragment. In onc instance modification to this parcelled object reflects modification in original object and in another case it is not. I'm a little baffled by this behaviour.
Till now I have assumed retrieving a parcelled objects through a bundle always create new object[no sure whether it's shallow copy or deep copy].
Someone please clarify parcelable behaviour.
I was struggling with a similar issue. At the first glance it seems that we always obtain a new deep copy from the parcelled objects. Moreover, there are even some StackOverflow answers which suggest to use Parcelable interface to clone objects. All this just increases confusion regarding the subject.
Here is what I've found after a lot of searching and googling:
Take a closer look at the official Parcel documentation. Here is the important quote:
An unusual feature of Parcel is the ability to read and write active
objects. For these objects the actual contents of the object is not
written, rather a special token referencing the object is written.
When reading the object back from the Parcel, you do not get a new
instance of the object, but rather a handle that operates on the
exact same object that was originally written.
Ok, as you can see, there are some special objects that are not being copyed during unparceling. But this is still a bit confusing. Does it mean we have another strong reference to the original object which prevents its garbage collection? And what are the use-cases for such objects?
To answer the aforementioned questions I decided to look through the Android source code. The methods I was looking for are readStrongBinder and writeStrongBinder which according to the docs do not cause a new object creation when the parcels are sent/received. And I think I found the desired answer in the ResultReceiver.java class. Here is the interesting line:
mReceiver = IResultReceiver.Stub.asInterface(in.readStrongBinder());
To understand what is this line actually doing we should go to the official AIDL documentation. Here are the most important parts:
The steps a calling class must take to call a remote interface defined
with AIDL:
...
5. In your implementation of onServiceConnected(), you will receive an
IBinder instance (called service). Call
YourInterfaceName.Stub.asInterface((IBinder)service) to cast the
returned parameter to YourInterface type.
A few comments on calling an IPC service:
Objects are reference counted across processes.
So let's put all things together:
The parcelled objects can be extracted without involving deep copy process.
If the parcelled objects are read using readStrongBinder method no new instances are being created. We just objtain a new reference to the original object and this reference can prevent its dealllocation.
To know whether our object will be deep copyed after the parcel has been received we should take a closer look at the concrete Parcelable interface implementation.
Android documentation can be really confusing and it may take a lot of time to understand it correctly.
Hope this info will help you.
If you want to read about a real-world example when the confusion regarding Parcelable objects can cause serious problems check out my blog post.

Inversion Of Control New Object Creation

I am getting into Inversion of Control, specifically using Guice and RoboGuice for Android and I have a question.
I have a method call that returns a Resource (which is essentially an XML or JSON String).
public Resource getResource(){
// Some implementation details that call a web service and throw the result in a string...
String resource = ........
}
The Resource class is really just a wrapped String, so I figured it made sense to pass it in in the constructor, since it is an essential part of a Resource object.
public class Resource{
Resource(String theXMLorJSON){
...
}
}
A couple of questions:
How do I construct a new Resource in the getResource call? I would think that I want to use IoC and not call new in the method.
If another class takes a Resource in the constructor, how can I use the Guice container to construct it when I need a dynamic String at construction time? I just asked a similar question and believe there may be a specific way to handle this using Guice.
Thanks so much!
I think you may be misunderstanding something about dependency injection. You don't need to try to avoid using new in all cases... you primarily want to avoid using new to create anything that you might want to be able to mock out for testing, and it's generally best to allow the container to wire up any class that depends on such an object.
Your Resource class, though, sounds like a simple value object that you can easily create manually in any testing you do. It also doesn't depend on any kind of services... it just contains a String. So there's no reason to try to have the container create it.
The class containing the getResource() method, on the other hand, you definitely want the container to create, because you'd like to be able to use something that depends on that class in testing without having to actually call a web service.
Note that if you have a class with a constructor that takes both dependencies you want injected by the container and parameters that are only known at runtime, you need to create an intermediate factory of some kind with a method that only takes the runtime parameters. With Guice you can automatically create such a factory from an interface using the Assisted Inject (not sure if that works with RoboGuice, but it's easy to create such a factory implementation manually too).

make singleton object using xml in android

Is their any way to make singleton object by using xml.As we know if we write className with package it call the constructor of the class but i want to use this xml in various other xml files using include tag.But it call all the time its consturctor and creating various object of that.
<jp.ne.biglobe.common.CustomSlidingDrawer>
something
</jp.ne.biglobe.common.CustomSlidingDrawer>
it call the constructor of the CustomSlidingDrawer class As i included it several other file.
Please Suggest me how to make it singleton object.
Maybe you could create a normal class, not singleton that you instanciate through xml. All instances would have a static method getView that returns the singleton.
But this would be awfull design. Just a syntactic workaround.
Regards,
Stéphane

When do I need a custom readObject/writeObject in a Serialised object?

I am trying to save an object via Serialisation. I get a NotSerializableException.
I notice that I only get it when I have instantiated another object within the object I am trying to save.
Am I mean to write my own versions of readObject/writeObject for this?
Any object within a serialised object also has to be serialised, if you see what I mean?
theblitz, the new object you put inside your object must implement Serializable too.

Categories

Resources