Sunday, 15 November 2015

Change layout dynamically in Android applications. A case study.

Android applications are usually built to function in a variety of devices that have different screen dimensions and also they must be functional in any orientation of the screen and in any change of the orientation. Moreover the efficient use of space especially in devices with small screens is very important and gives great value to the application, so the GUI of an activity must be designed with special care to screen sizes and orientation changes.
The indicative way to design the GUI of an activity is to prepare a layout file for it. This is a more straight forward procedure than defining the graphical elements of your app programmatically and is easier to handle and test. All the default layout files are stored in /res/layout directory of the projects file structure. In case you want to prepare a layout file for an activity with landscape orientation, you have to create directory /res/layout-land and you place the layout file in there. When the size of the screen matters and you want different UI for different screen sizes then you must create a layout for each case. For instance, a layout designed for screens with smaller size larger than 600 dp must be placed in a directory named layout-sw600dp and if you want to combine screen sizes and orientation you must place the appropriate layout file in the directory named layout-600dp-land.
All the layouts that refer to the same activity must have the same name regardless of the directory they are stored. When an app is executed, Android will search for the most appropriate layout considering the screen size and orientation. In case there is not a special layout defined, Android will use the layout stored in the default directory.
Problems start when some of the elements of the screen change dynamically while the app is running and a layout has already been loaded. In most cases the change in the orientation of the screen causes troubles in the dynamically changed elements. When orientation changes, Android usually reloads the activity losing all the changes the user has caused. In order to avoid that you must edit the manifest file and in the activities that are declare some how like this

        <activity
            android:name=".app.MyActivity"
            android:label="@string/title_activity_myactivity" >
        </activity>


add one line of code changing them like this

        <activity
            android:name=".app.MyActivity"
            android:configChanges="keyboardHidden|screenSize|orientation"
            android:label="@string/title_activity_myactivity" >
        </activity>


This tells Android that you know what to do with your activity so it shouldn’t change anything on its own. Let’s see now what you should do when it is you that you want to cause layout changes when orientation changes. In order to present this more efficiently I will use the example of my app called Map This Way.
In an activity of this app called activity_line the user may record the GPS tracks along a route. In the layout there is some text that is updated with the current GPS coordinates and a map view that shows user’s current location. In big screens when the screen is in landscape orientation the map is better to be on the right of the screen and the text on the left and when the screen orientation is vertical the map view is better displayed under the text. In small screens the map view should always be under the text. In order to do this I prepared layout files for all cases. Then I had to arrange how the app will behave in screen orientation changes.


Orientation changes are detected and manipulated by the onConfigurationChanged method which must be overridden.

@Override
public void onConfigurationChanged(Configuration newConfig){
     super.onConfigurationChanged(newConfig);
     setContentView(R.layout.activity_line);
}


The above code reloads the UI of the activity using the most appropriate layout file for the new orientation of the screen. The advantage of using this technique instead of letting Android do the changes is that in this method you may add code to define how the activity elements will be displayed in the new screen. The disadvantage is that the elements are initialized and reloaded so you will have to add code so as to initialize them properly and update the values of the layout elements with the changes that have occurred so far. Also you have to restart procedures that may have stopped after orientation changed like advertisement display etc.
In the case of my app the map view has to be updated so the users will see their current location and if the mapping procedure has started the colors and functionality of layout elements has to be adjusted accordingly.
In general, a good way to deal with such situations is to initialize the layout elements in the onConfigurationChanged method as you did in the onCreate method. So for update of the map view of my app I use the method showmap(x, y) which updates the location of the map and it is executed in both methods.
Another problem occurs when you have to pass some value from a variable that affects layout elements to the newly initialized elements after orientation changes. For this, the use of some global variable is very helpful. In my app when the mapping procedure has started, a red button appears which may be pressed by the user and cause the stopping of the mapping procedure. The way I worked around this issue is the declaration of a global boolean variable called mapping which takes the value true when mapping has started and false otherwise also the declaration of a global Button instance. Then I used both in onConfigurationChanged method.
All the above were formed as follows.

@Override
public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.activity_line);
   showmap(x, y);
   if (mapping){
        button1=(Button) findViewById(R.id.button1);
        button1.setBackgroundColor(0xffff0000);
   }
}

Then activity_line is functional and ready to use.


Friday, 9 October 2015

Against the idea of a computable universe

The computable universe hypothesis, states that every operation and phenomenon in the universe is computable. So if you can describe all the phenomena in state s1 of some part of the universe (or of the whole universe), then you can compute the physical operations in any state si that is a successor of s1.
An equivalent hypothesis is that every set of physical operations can be simulated by a Turing machine or an automaton. This means that there is a Turing or some other computational machine that will accept as input the data that describe these operations and will output the result of them and then it will halt. The two hypotheses are equivalent as the simulation of every operation by a Turing machine implies the computability of the transition from state s1 to any state si.
These hypotheses also support the idea that the universe is deterministic. The transition from s1 to si is computable and as a result predictable which makes it deterministic.
There are many papers and textbooks that propose theories that are based on these hypotheses, there are also many that oppose such ideas. My opinion is against the idea of a computable universe, the reasons for this are: randomness, incompleteness and uncomputability.

Randomness


The existence of randomness in physical processes remains a debate among scientists. Einstein rejected it, as he believed that universe is deterministic and stating that "God does not play dishes"; a phrase that marked the debate. The truth is that there are many indications that some physical operations must be random.
Steven Hawking provides some very convincing arguments in one of his lectures. According to Hawking the way the universe evolves seems to be not deterministic, moreover the existence of extreme objects in it like the black holes and the Uncertainty Principle makes accurate computations and predictions impossible. Although in computer science we can only use pseudo-random operations in order to approach true randomness, in other fields true randomness seems to be present. Quantum mechanics build probabilistic models in order to study quantum phenomena that are believed to evolve truly randomly.
 

Incompleteness


Any not trivial system of logic is incomplete. This is a very simplified description of the incompleteness theorem stated and proven by Kurt Godel which is one the most important theorems in science.
Any system physical or not that operates under certain laws may be efficiently described by mathematics and more specifically by some system of mathematical logic using propositions and axioms. If the system of logic is not trivial (is not very simple) then there are true statements in it that may not be proven as true; the system is incomplete. This results that there are phenomena that may not be computed by mathematics.
This lecture of Hawking approaches incompleteness theorem from a physicist perspective reaching the same conclusions.
The issue that we face is that in order to compute some mathematical object O we need to use or to build some proper mathematical structure. But no matter how this structure will be it will always be incomplete, so a part of O will not be able to be proven using our mathematics.
 

Uncomputability


The theoretical background of uncomputability in Computer Science is related to Godel’s incompleteness theorem and states that there are instances of computational problems that may not be computed by a machine or a human. Computational procedures may be simulated by Turing Machines that perform them and then halt, so we usually state that a problem P is uncomputable if there is not a Turing Machine that may compute all the instances of P and then halt. Equivalently, if you input some data in a Turing Machine you may not know if it will halt or not.
The foundation for this was set by Alan Turing before the first general purpose computer was ever built.
Its affect in programming is something that we experience every day. No matter how hard have you worked on a piece of software you can never be sure that it will work as expected.
A parallelism in physics that I have thought is included in the question 
“How can you be sure that for operation H there is a Turing Machine that will halt on input of the description of H?”
The above arguments may seem to be weak but there is a stronger one for which I talked about in a previous post.
Causation (or else causality) refers to the cause-effect relation that may exist between two events or phenomena. The causal inference problem regards the determination of causation between events.
In my previous post I present a proof on which Causal Inference is logically and computationally undecidable. Relations among phenomena and physical processes are important factors in the structure of the universe. The Causal Inference undecidability results that there are physical phenomena for which we may not decide or compute if they are related to each other or not. This makes a part of the universe uncomputable and unexplained using logical arguments.

Thursday, 17 September 2015

How to make a hosted web app listed in app stores and installable for the end users

Building a hosted web app has many advantages. One advantage is that it is not necessary to create big software packages containing the whole app where the users will have to download and install. It is only necessary for them to open the main page of the app and work with it. Also the app may be updated whenever it is necessary and the end users receive the update in real time.
Hosted apps do not require to be placed in an app store and follow certain specifications on the way they are built. Just find a host which may be free, paid or the server of your company and put your creation in there.
There is a small down side in this practice. Apps that require installation create a stronger bind to the end users while hosted apps have a more loose attachment to their users. Also, apps that are listed in an app store receive some promotion just by being there; they may be searched and found easier.
The good news are that there are three app stores that list hosted web apps and two of them provide some methods of installing a link or a small app pointing to your hosted app in the end user's computer.
I had to discover all these things I write about in this post after I finished developing WMS Map Viewer on line. It is a web app and it was really easier to develop it than to find out what to do with it afterwards.
An easy choice for listing your app is the Opera Mobile Store. The store lists apps mainly for mobile devices (new, old and very old) but also accepts submissions for HTML5 web apps for desktop computers. The submission procedure is similar to any other app store. The submitted apps are being reviewed before publication (which I find a good idea) and usually it doesn’t take more than one or two days. When you click on a web app you view its store listing and there is a download button which simply opens the homepage of your app. See my listing for example.
The other two app stores are the Chrome Web Store and the Firefox Marketplace. Both stores address to certain browsers and they both share an interesting feature. When a user downloads an app from these stores the app (or at least a link) gets installed on the user’s computer.
In Chrome Web Store you have to pay a onetime fee (like in Play Store) this fee is currently 5$ and you may place up to 20 apps. Then you have to confirm that the web page you submit is yours by logging in to Google web masters and put a metadata file in the root directory of your app. You can do the submission procedure with any browser but the apps are available for download only in Chrome.
During submission it is only necessary for you to provide a .json type manifest file, a description of the app and some screenshots. But as the store accepts submissions of apps, themes and extensions for Chrome with the same procedure, you have to be very careful and submit the correct manifest. Otherwise the submission procedure might assume that you submit an extension and then your store listing will simply not work. The only way to solve this if it happens to you, is to make a new submission. The previous one is characterized as an extension and this cannot be changed and the submission cannot be deleted. Use this tutorial for instructions don’t forget the part

  "app": {
    "urls": [
      "http://mysubdomain.example.com/"
    ],
    "launch": {
      "web_url": "http://mysubdomain.example.com/"
    }


this makes all the difference between an app and an extension.
See the listing of my app as an example.
After listing your app, any Chrome user may download it. Chrome places downloaded apps in the app menu regardless if they are packed or hosted. So the apps are functional in the Chrome environment like any other app or extension.
In Firefox Marketplace you are also required to place a .json type manifest, but as it has to have the .webapp extension you have to declare in the metadata of your server that the manifest is of type
<mime-type>application/x-web-app-manifest+json</mime-type>
this is a platform dependent procedure and there is a lot of documentation about it in the help files of the Marketplace. The syntax of the manifest is quite trivial, unless you wish to put special installation parameters.
The interesting thing is that when a Firefox user downloads an app in Windows, the app is installed like a regular windows app that may be removed only from the Control Panel. In fact, Firefox installs a small app which is basically a browsers window named after the downloaded app with some cache memory. When a user runs this installation the hosted app opens in this window which has different functionality than Firefox normal windows. This feature is useful as your hosted app runs like a local app improving user experience.
The downside of Firefox Marketplace is the review process. It takes a lot of days, a week or more. And some of the reviewers must not be very experienced. The reviewer of my app didn’t know how to run it; despite the help files in it. So I had to send him detailed instructions and wait even more days for the approval of my submission.

Tuesday, 25 August 2015

An online map viewing application

WMS Map Viewer on line is a light-weighted client for web map services that use the WMS protocol. It is a web application compatible with all modern versions of WMS and also supports the projection of kml files as overlays in the chosen WMS connections.
It is a follow up of my Android application and I like especially the fact that you can simply open it in a browser and just work with it. Most of the WMS viewers are heavy applications that require to be downloaded installed etc.
Here are a few of the many sites that you  can find and provide WMS maps.

Europe
http://eusoils.jrc.ec.europa.eu/wms/wms.htm
http://ows.terrestris.de/dienste.html
CUZK Geoportal - Orthophoto

America
http://viewer.nationalmap.gov/exa…/services/serviceList.html
http://www.data.gov.bc.ca/dbc/geographic/connect/index.page?

Sunday, 19 July 2015

Proving uncountability without diagonalization



In this post, I propose a proof of the uncountability of the set of real numbers without using the famous diagonalization method of Georg Cantor. My proof is based on the Church-Turing thesis which states that any computable operation may be computed or simulated by a Turing Machine. So, if a set of mathematical objects is countable a counting operation may be performed and there is a Turing Machine that may compute it.

Definition. Let S be an ordered set of discrete objects. If S is countable then there is Turing Machine M which on input of the descriptions of objects a and b it outputs c which is a representation of the number of elements in S between objects a and b.

If we may count the elements between a and b (or else the distance of a and b in S) then we may count the whole set, as a and b may be any objects. This definition avoids a direct connection of the set N of natural numbers to S, so it differs from diagonalization, yet as I show below it reaches the same conclusions on natural, rational and real numbers. Output c has to be a number and not the symbol of infinity or anything similar, otherwise we cannot consider it as a counting result. First let’s test this definition on natural and rational numbers.

Theorem 1. Set N of natural numbers and set Q or rational numbers are countable.

Proof for N. Set N may be defined as an ordered set of linear structure. For every i we place in the i-th place of N number i. Let a and b be the representations of two natural numbers where a represents a smaller value than b and M a TM that computes their distance. In order to make the presentation more simple and intuitive and without loss of generality, let M be a 2-tape machine and a, b and c be represented in unary numeral system. Then we print a in tape-1 and b in tape-2. For any “1” printed in tape-1 M erases one “1” that is printed in tape-2. Finally a representation of c is left in tape-2 and this is the output.

For instance, on a = 8 and b = 13 we print
tape-1 : #11111111######
tape-2 : #1111111111111#
After running the configuration of M we get
tape-1 : #11111111######
tape-2 : #11111#########

Proof for Q. Let’s first define Q as an ordered set. It is known that Q may be defined as the Cartesian product of sets X and Y, where X and Y are equal to N / {0}. For every ordered pair (x, y) of the product there is corresponding number q which is in Q and stands that q = y / x. Using this definition we may create an ordered set of linear structure equivalent to Q.
Set Q may be structured as an ordered set of subsets. Initially we place numbers “0” and “1 /1”, then for every i > 2 we place in the i-th place of the set the subset of Q that for all its elements stands x+y=i. By placing in ascending order the elements within each subset we create an ordered set of linear structure with all the elements of Q. On this procedure we end up with a representation of Q that is commonly used in literature.
On input of the descriptions of a and b, TM M using the above procedure creates the ordered subset of Q that contains a and b and then computes their distance by simply counting the elements between them one by one.
The ordered set of linear structure that is subset of Q and contains a and b is computable as it may be constructed algorithmically as shown above and is finite as I will show next.
Let a=y1/x1 and b=y2/x2, then between a and b there is a finite number of subsets d= (x2+y2)-(x1+y1). For each subset stands that i=x+y and is finite as there is a finite number of combinations that may generate i given that i, x, y belong in N.

Cartesian product

1/1   1/2   1/3   1/4 ……
2/1   2/2   2/3   2/4 ……
3/1   3/2   3/3   3/4 ……
4/1   4/2   4/3   4/4 ……
………………………..

Order of elements

 1       2       4       7  ……
 3       5       8      11 ……
 6       9      12     14 ……
10     13     15     16 ……
………………………...


Q as set of subsets

(0), (1/1), (1/2, 2/1), (1/3, 2/2, 3/1), (1/4, 2/3, 3/2, 4/1), (… 2/4, 3/3, 4/2 …), (… 3/4, 4/3 …), (… 4/4 …)


Q as an ordered set of linear structure

0, 1/1, 1/2, 2/1, 1/3, 2/2, 3/1, 1/4, 2/3, 3/2, 4/1, …, 2/4, 3/3, 4/2, …, 3/4, 4/3, …, 4/4 …


Theorem 2. The set R of real numbers is uncountable.

Proof. It is known that there is no algorithmic procedure which may produce the numbers in R. That is because most of numbers in R are uncomputable. 
Let's consider a description d for the numbers in R so that they may be represented in TM M. If there is procedure implemented in M that structures R as an ordered set then M computes uncomputable numbers which is a contradiction. As a result R may not be structured as an ordered set and is not countable by M.