Monday, March 21, 2016

Android permissions

Permissions

To use a permission, use <uses-permission> tag inside manifest tag but outside application tag.
To define your own permission, use <permission> tag outside application.

Permission issue

If there are two applications 'a' and 'b', a uses a permission from b and a is installed before b, I think (not sure) that no permission will be requested to the user when a uses b.

Friday, January 30, 2015

About abstractions

Motivation

I have written this post because many folks don't have an idea of what an abstraction or generalization is. At least they don't know explicitly. I think it is very important when designing and writing software as well as in understanding mathematics.

Let's take first a concrete example. Let's define the following two ideas
A vehicle is something or somebody that can carry out something.
A car is a vehicle that has 4 wheels and can move autonomously.
As we can see, a car is a special type of vehicle. So, what I say about a vehicle is true for a car. But the other case is not true, what I can say about a car is not necessary true for a vehicle.

I can say once that vehicles can carry out a bicicle and I automatically know that every vehicle can do this (including a car) without saying it again for the car. You could think that if there are potentially infinite types of special vehicles, it can be very tyring to say that for all of them (and nasty).

But, the counterpart is that I don't know exactly what a vehicle is. I can be a vehicle given our definition. So, if we suppose that people cannot walk through the road, I cannot say that a vehicle can go through the road. But I can say that for cars. I can tell less things about a vehicle. That is because we haven't said how the road and the vehicle relates each other. But we can say that for the car.

Let's see another example. Money representes a generic (abstract) debt. Someone owes you something. You can ask someone to pay that money to you, for example by an electronic payment or sending money by ordinary mail. So we can say things about money, it can be transferred through different channels because we already have the infraestructure. But someone can also owe you a concrete debt like a cow. But, sending a cow electronically is impossible and sending by email a little messy. But, that cow could be converted into abstract debt (the money) and ask the money to be sent. The money is logically (in your mind) attached (related) to the cow, so, in some sense, the money will arrived to you and you will say: "oh, the money from the cow that guy owed me."

What is abstraction 

Let's try to define it. Abstraction is the process of thinking about ideas, about concepts. It is the process of thinking about the properties of something or the properties that several objects have in common and create a new conceptual object (that only exists in our minds) with that properties. In that way, you can study the object in isolation.

In the first example, the properties were the vehicle definition. In the second example, was the idea of debt, regardless of the concrete debt.

We can talk about the idea all we want and as long as the concrete objects have the properties of the idea, all we say about the idea or all relationships we do with the idea and other objects, will be still correct for the concrete objects.

We can say things about a vehicle and they will be correct for a car. We can convert the cow to money and send the abstract equivalence of the cow through mail.

But, because we are talking about a small subset of properties we cannot say anything that we cannot derive from that properties. This is one of the items when we have to consider to stop the abstraction process. You don't want to abstract until you cannot say anything.

Note also another thing. We give name to the concepts we create. We say: "These are the properties of the idea and its name is given by this set of symbols so you don't have to repeat the properties again and again". So the symbols become a representation (one of many possible) of the idea.

In the first example, we give the name vehicle, In the second example, we call debt to "something that someone owe you, whatever it is".

Abstraction in mathematics and software

Before to applying abstraction to software I would like to apply to mathematics. Mathematics is the very abstraction itself. We all know the set of possitive numbers. We know that they have

  1. An operation called +.
  2. For every "a" and "b" belonging to possitives "a" + "b" is also possitive.
  3. For every "a", "b" and "c" belonging to possitives "a" + ("b" + "c") = ("a" + "b") + "c".
  4. The operation + has a special object called 0 that fulfils that for every "a" belonging to the possitive numbers "a" + 0 = 0 + "a" = a.
  5. It fulfils also that there is a number called "-a" such as "a" + "-a" = "-a" + "a" = 0.

A set that fulfils those 5 properties is called a group. We can study groups in isolation because we have defined them and we can develop algorithms for them. Then we can apply all those algorithms to the possitive integers.

Is that familiar to you as a software developer? That is the concept of interface. But an interface is less powerful because you can only define what method's name and number, types and order of the attributes that the interface has. You cannot say, hmmm, this interface has a special object that fulfils these properties. Also, in static typed languages, you must declare that an object implements an interface. This is not necessary in mathematics. Why? Because it is logically compelled. Once I have defined the idea (the group) you cannot arbritrarily decide whether you match the definition or not. You match or not whatever you want, you cannot decide.

That issue introduce major problems when programming like the following. You define an interface and program against the interface (what we said about designing an algorithm for an idea, say once and apply infinite times). But, when you are going to use a concrete class what happens is that the class doesn't declare to implement your interface but it implements it by the definition of the interface (it has the methods with the same signature). The result is that you cannot use unless you can change it. In some languages you can use the "auto" keyword to solve this problem (https://msdn.microsoft.com/en-us/library/dd293667.aspx)

Abstraction in software

I think that a good example in software is a String. A String in an array of characters. You can define algorithms that deal with Strings like printing a String to the screen, the printer or whatever. Or saving a String in a file or sending it through the network.

If you convert concrete things to the String abstraction, you can do all of this stuff without implementing it by yourself. If you have a cow object and you convert it to string, you can send it through the network or to a file (like the cow could be converted into money).

Abstraction and representations

One thing I want talk about is the relationship about the ideas and their representation. An idea is something that belong to the mind but to transmit it, it we must be converted to something concrete.

To talk about the idea of five (five elements of whatever kind) I can use the following symbols: five (English), 5 (arabic), V (roman), cinco (Spanish), ||||| (child style). A lot of representations for the same idea. Some representations are better than others for certain tasks. The arabic is very expressive
(tell the idea in a very compact way) and also is easy to work with (it is specially true for bigger numbers). Roman is understood by less people than the other ones. The child style is very bad to work with. Do you imagine to work with 25 in this way? Note that we must know the association between the representation and the idea it represents. Otherwise there is nothing to do.

Another point to make is that the representation allows us to talk about all the possible ideas we need.

The same thing happens in software. You can have an idea and different representations. And one of these representations is the standard one. The one all of use so we can understand each other.

Let's take an example. What does the ASCII characters mean?
non printable characters: http://en.wikipedia.org/wiki/ASCII#ASCII_control_code_chart
printable characters: http://en.wikipedia.org/wiki/ASCII#ASCII_printable_code_chart

Someone can say that represent the alphabet but that is not true because, as you can see, there are non-printable-characters.

So it seems (to me) that a character, the concept, is a way to transmit a collection of ideas related with

  •    The idea of the alphabet (for example, the idea of a "a").
  •    The idea of text layout (tabulation, line feed).
  •    The idea of a transmission control.
  •    ¿The idea of simple audio output?

There are several representations of these ideas. One of them is to use the visual representation "a" to represent the idea of "a". We use the visual representation to describe it because we are used to it.

There is also the standard representation. A example of the standard representation is that "a" is represented by 110 0001.

There are also better representations than others. As well as ||||||||||| is very tedious and I can prefer to work with 10, the same can happen here. Suppose that you have a document with only two ideas, the idea of "a" and the idea of "b". The representation will be 110 0001 and 110 0010. But we don't need to talk about the whole ideas present in the ASCII table. We only need to talk about "a" and "b". So, a tool can decide to represent them as "0" and "1" and remember the association between the representation and the idea (something like the ASCII table).

I think that the words said by Richard Feynman worth a viewing. I hope you relate them to the previous content :)

Abstraction and how to design and understand software

Another point I would like to make is that developing software is hard. There are a lot of objects involved and a lot of details for each object. It is harder to do if you aproximate to a problem without any previous knowledge.

Wouldn't be easier if those objects with all of its details follow a certain abstraction (you can call it idea or pattern)? Yes, it would. Because instead of thinking about infinite different objects you can think about a few abstractions and what these abstractions provide to the overall architecture.

Let's do a little exercise about it. Let's study a process, a kernel process. We are going to study what a process really is.

The kernel is responsible for managing the resources of the machine and exposing an interface to the developer of applications to use those resources. But we don't know all concrete applications that can ever be made.

1. So, we need a mechanism such as new applications can be developed independently from the kernel and that we don't need to compile the kernel again to execute those applications. Doing this, we can test the kernel and the application in isolation.

2. The kernel then does not know about the application but must study something. I mean, which is its object of study. I see two. The machine and something generic that will represent for the kernel a generic application. I will called it a process. A particular process can be thought as something that will pick resources exposed by the kernel and combine them in a certain way.

To do that, I must provide an API to access the machine.

3. I can say that a process has a lifecycle that I control. For example, I can say that a process can be waiting, blocking... I can develop a lot of algorithms for a process, like suspending the whole set of processes and resume them later in the same state they were.

4. I can say that a process must offer something to the kernel. For example, a set of units that can be executed in a certain points of the lifecycle or other things I define (signals and the main entry point for example).

5. I can say that a process can have a symbol that identifies it, for example an integer.

What can we conclude? I will offer a conclusion for each of the previous points.

1. The property of extensibility and not modification of the kernel for an extension. Open for extension, closed for modification. What was correct before the process executing in the kernel is correct now because it wasn't modified. The process can be loaded and unloaded ondemand.

2. The kernel offers resources to the process. It offers a context to access to the elements the kernel wants to offer. For example, it can be storage access, memory access, file access... And it can provide security in the access to the resources by means of that context.

Those resources provided by the kernel can be shared or private to the process.

Algorithms that apply to a process, like a filter architecture. For example I can store a process into disk.

3. Knowing the state of the process I can introduce it in different lifecycles.

4. The process offers an interface to the kernel. The interface is explicit so the user application can redefine it and augment the functionality of the process. The application can create his own content and give the name of the symbol we will use to locate the object.

5. It has a name to register with the kernel. That allows addressing, from the kernel and from other processes. You can identify one process uniquely.

All conclusions suggest that this architecture is very similar to a plugin architecture. A process is a plugin for the kernel.

And the point is that it can worth the effort to study how to build a plugin architecture. In that way, when you have to build something similar you can take it and test whether it fits for your necessity without rethinking the whole thing again.

I suggest that you think in the same terms I have done about the following:

  • What a Java Object is?
  • What an Android Activity is?
  • What a filesystem type is?
  • What a driver is?
  • What a function is?
  • What a class in a programming language is?

They are all plugins :).

Conclusion

Abstraction is a very powerful technique. Allows us to isolate certain properties and to study them in isolation, without all the details of the concrete objects so they are easier to work with. They allow us to say something once and apply infinite times as long as the properties remain in concrete objects.

An abstraction allows us to go in the opposite way. Instead of going from the abstraction to something concrete, we can deattach all the details from the concrete thing and identify the abstraction behind that. Once you have the big picture, the details are easier to put in the framework of understanding.

The benefits from the previous two paragraphs and we said in the section "Abstraction and how to design and understand software" is that abstractions are tool and platform independent. As an example, you don't loose it every time you change of programming language.

Another thing to take into account is that the abstraction can have different representations. All can be different and some of them can make certain operations on the abstraction easier to make. But all of them refer to the same abstraction. You must understand the representation to know what idea is behind that.

The counterpart that we can reach is that we abstract or generalize so much that we don't know what we are talking about. In consequence, we cannot say anything useful.

Tuesday, December 2, 2014

First cup

I have finally decided to write into a blog. In spite of the url of the blog, it is not only about coding but also about computer science, software engineering, maybe some math and whatever related with this stuff.
With this blog I want to achieve several goals:

  • I want to publish and talk about my projects in public so people can benefit of what I am doing. This includes talking about tools I used that can be useful for others. I think that it will useful to explain the reason of doing the things that way.
  • I would like to improve my English. My native tongue is Spanish and I think that writing and working in English will give me more fluency. 
  • Another reason is to improve my writing skills. Writing a blog compels you to structure the content in a way that a reader that cannot know about the subject can understand it. This also compels me to understand it well before telling to others.
  • The last but not least is that I want to let breadcrumbs in the road so you can come back (or going back if you never were). I think that this is my main goal. With all the frameworks we have at out disposal maybe we don't know what really happens, what concepts are involved and how they interrelate.

I am sure that I will learn a lot of things because telling to other people what you do gives extra motivation. And you can also learn from the visitors too!!!.
I hope you enjoy lots of cups :).