lunedì 17 aprile 2017

Privacy Policy - Guida TV

This privacy policy governs your use of the software application Guida TV

Permissions granted by the app:
INTERNET
Permission usage: The app uses this permission to retrieve television listings displayed to users.

Automatic data collection
The app does not collect any personal information, it may sent events to our server in order to help us understand how the application is being used.

Changes
This Privacy Policy may be updated from time to time for any reason. Any changes to our Privacy Policy will be made in this document. You are advised to consult this Privacy Policy regularly for any changes.

domenica 12 febbraio 2017

Privacy Policy - My Wine Cellar

This privacy policy governs your use of the software application My Wine Cellar

Permissions granted by the app:
STORAGE
Permission usage: Allows import/export of the wine database backup to the internal storage of the device. This permission is also needed in order to attach a picture to a wine label.
CAMERA
Permission usage: The app uses this permission to take pictures and save them to the internal storage (path .../Pictures/MyWineCellar).

Automatic data collection
The app does not collect any personal information, it may sent events to third party analytics network (Google Analytics) to help us understand how the application is being used.
Changes
This Privacy Policy may be updated from time to time for any reason. Any changes to our Privacy Policy will be made in this document. You are advised to consult this Privacy Policy regularly for any changes.

sabato 20 febbraio 2016

How to remove Android apps that you can't uninstall

My Android app Turn off screen is used every day by thousands of people. The app allows the user to turn off the screen of the device and put it on standby, extending physical power button lifetime. It needs the Device Administrator API in order to work properly, so when a user uses my app for the first time he has to make turn off screen a device Administrator app. This operation is quite simple and fast, but it makes the app apparently unremovable. In fact when we try to delete a device Administrator app the remove button is disabled:




Fortunatly the solution is simple, we have to deactivate the device administrator right of the app. These are the steps we need: go to the settings, and navigate to security




Then select Device administrators




Tap on Turn off screen to deactivate the device administrator right




Finally tap on deactivate




At this point the uninstall button is enabled, so we can remove the app




In order to make this operation faster I put in the main activity of my app an uninstall option, which automatically deactivates the administrator device and shows the uninstalling activity.




domenica 14 febbraio 2016

Base conversion of unsigned floating point numbers

One of the most successful Android apps I published on Google Play is certainly Base Converter. It converts numbers from decimal numeral system to binary, hexadecimal, octal and vice versa. Some users have recently asked me an explanation for the algorithms used to perform the conversion, but it is not convenient to integrate an article within an app, so I wrote this paper to satisfy their request. For convenience I will not write all the algorithms used to perform the conversion, however the techniques shown below will make you able to convert between the four bases (you have to use two algorithms if necessary).


Converting floating point bin 1011011.11001 to dec

We start converting the integer part:

$$1011011_{2} = 1 \cdot 2^{6} + 0 \cdot 2^{5} + 1 \cdot 2^{4} + 1 \cdot 2^{3} + 0 \cdot 2^{2} + 1 \cdot 2^{1} + 1 \cdot 2^{0}$$
$$1011011_{2} = 64 + 16 + 8 + 2 + 1 = 91_{10}$$
Then we convert the fractional part:

$$0.11001_{2} = 1 \cdot 2^{-1} + 1 \cdot 2^{-2} + 0 \cdot 2^{-3} + 0 \cdot 2^{-4} + 1 \cdot 2^{-5}$$
$$0.11001_{2} = 1 \cdot \frac{1}{2^{1}} + 1 \cdot \frac{1}{2^{2}} + 0 \cdot \frac{1}{2^{3}} + 0 \cdot \frac{1}{2^{4}} + 1 \cdot \frac{1}{2^{5}}$$
$$0.11001_{2} = 0.5 + 0.25 + 0.03125 = 0.78125_{10}$$
So we obtain:

$$1011011.11001_{2} = 91.78125_{10}$$

Converting floating point dec 273.241 to bin

As above we start from the integer part. We divide the integer part by two, annotating the remainder of the division. Then we divide the result of the division by two, and we write again the remainder of the division (before the previous one). The procedure stops when the division result is zero.

$$\frac{273}{2} = 136, \, r = \textbf{1} \longrightarrow \textbf{1}$$
$$\frac{136}{2} = 68, \, r = \textbf{0} \longrightarrow \textbf{0}1$$
$$\frac{68}{2} = 34, \, r = \textbf{0} \longrightarrow \textbf{0}01$$
$$\frac{34}{2} = 17, \, r = \textbf{0} \longrightarrow \textbf{0}001$$
$$\frac{17}{2} = 8, \, r = \textbf{1} \longrightarrow \textbf{1}0001$$
$$\frac{8}{2} = 4, \, r = \textbf{0} \longrightarrow \textbf{0}10001$$
$$\frac{4}{2} = 2, \, r = \textbf{0} \longrightarrow \textbf{0}010001$$
$$\frac{2}{2} = 1, \, r = \textbf{0} \longrightarrow \textbf{0}0010001$$
$$\frac{1}{2} = 0, \, r = \textbf{1} \longrightarrow \textbf{1}00010001$$
Now we convert the fractional part. In this case the fractional part is multiplied by two, and we annote the integer part of the result. Then we multiply the result of the previous multiplication by two, and we write again the integer part of the result (after the previous one). The algorthm stops when the result of the multiplication is zero, or when you get sufficient significant digits.

$$0.241 \cdot 2 = \textbf{0}.482 \longrightarrow 0.\textbf{0}$$
$$0.482 \cdot 2 = \textbf{0}.964 \longrightarrow 0.0\textbf{0}$$
$$0.964 \cdot 2 = \textbf{1}.928 \longrightarrow 0.00\textbf{1}$$
$$0.928 \cdot 2 = \textbf{1}.856 \longrightarrow 0.001\textbf{1}$$
$$0.856 \cdot 2 = \textbf{1}.712 \longrightarrow 0.0011\textbf{1}$$
$$0.712 \cdot 2 = \textbf{1}.424 \longrightarrow 0.00111\textbf{1}$$
$$0.424 \cdot 2 = \textbf{0}.848 \longrightarrow 0.001111\textbf{0}$$
$$0.848 \cdot 2 = \textbf{1}.696 \longrightarrow 0.0011110\textbf{1}$$
So the final result is:
$$273.241_{10} = 100010001.00111101_{2}$$

Converting floating point dec 273.241 to hex

The procedure is similar to the previous one, we only change the target radix to 16:

$$\frac{273}{16} = 17, \, r = \textbf{1} \longrightarrow \textbf{1}$$
$$\frac{17}{16} = 1 , \, r = \textbf{1} \longrightarrow \textbf{1}1$$
$$\frac{68}{16} = 0, \, r = \textbf{1} \longrightarrow \textbf{1}11$$

About the fractional part we change the radix to 16 again, in addition we convert the result of the multiplication from decimal to hexadecimal (for this conversion you can refer to this table):

$$0.241 \cdot 16 = \textbf{3}.856 \longrightarrow 0.\textbf{3}$$
$$0.856 \cdot 16 = \textbf{13}.696 \longrightarrow 0.3\textbf{D}$$
$$0.696 \cdot 16 = \textbf{11}.136 \longrightarrow 0.3D\textbf{B}$$
$$0.136 \cdot 16 = \textbf{2}.176\longrightarrow 0.3DB\textbf{2}$$
$$0.176 \cdot 16 = \textbf{2}.816 \longrightarrow 0.3DB2\textbf{2}$$

So we obtain:
$$273.241_{10} = 111.3DB22_{16}$$

Converting floating point dec 273.241 to oct

We use the same algorithm of the two previous sections, obviously in this case the target radix is 8:

$$\frac{273}{8} = 34, \, r = \textbf{1} \longrightarrow \textbf{1}$$
$$\frac{34}{8} = 4 , \, r = \textbf{2} \longrightarrow \textbf{2}1$$

$$\frac{68}{8} = 0, \, r = \textbf{4} \longrightarrow \textbf{4}21$$
Now we convert the fractional part:

$$0.241 \cdot 8 = \textbf{1}.928 \longrightarrow 0.\textbf{1}$$
$$0.928 \cdot 8 = \textbf{7}.424 \longrightarrow 0.1\textbf{7}$$
$$0.424 \cdot 8 = \textbf{3}.392 \longrightarrow 0.17\textbf{3}$$
$$0.392 \cdot 8 = \textbf{3}.136 \longrightarrow 0.173\textbf{3}$$
$$0.136 \cdot 8 = \textbf{1}.088 \longrightarrow 0.1733\textbf{1}$$

So the result is:

$$273.241_{10} = 421.17331_{8}$$

Converting floating point bin 101001011.1010011011 to hex

This is the number we want to convert:

$$\texttt{101001011.1010011011}$$
First of all we split the digits in groups made of four numbers, possibly we add additional zero digits on the left in the integer part, and on the right of the fractional part to fill the first and the last group:

$${\color{red}{\texttt{1}}} {\color{black}{\texttt{0100}}}  {\color{green}{\texttt{1011}}} \texttt{.} {\color{brown}{\texttt{1010}}} {\color{blue}{\texttt{0110}}} {\color{purple}{\texttt{11}}}$$
$${\color{red}{\textbf{000}}} {\color{red}{\texttt{1}}} \quad {\color{black}{\texttt{0100}}} \quad {\color{green}{\texttt{1011}}} \quad \texttt{.} \quad {\color{brown}{\texttt{1010}}} \quad {\color{blue}{\texttt{0110}}} \quad {\color{purple}{\texttt{11}}} {\color{purple}{\textbf{00}}}$$
Then we convert the binary groups to the hexadecimal base (for this conversion you can refer to this table).

$${\color{red}{\texttt{1}}} \quad {\color{black}{\texttt{4}}} \quad {\color{green}{\texttt{B}}} \quad \texttt{.} \quad {\color{brown}{\texttt{A}}} \quad {\color{blue}{\texttt{6}}} \quad {\color{purple}{\texttt{C}}}$$
So the result is:
$$\texttt{101001011.1010011011}_{2} = \texttt{14B.A6C}_{10}$$
To perform the conversion from hexadecimal to decimal you have to do the same procedure shown above, by reversing the steps.


Converting floating point bin 101001011.1010011011 to oct

$$\texttt{101001011.1010011011}$$
We use the same procedure of the previous section, but the groups are now composed of three digits.
$${\color{red}{\texttt{101}}} {\color{black}{\texttt{001}}}  {\color{green}{\texttt{011}}} \texttt{.} {\color{brown}{\texttt{101}}} {\color{blue}{\texttt{001}}} {\color{purple}{\texttt{101}}} {\color{orange}{\texttt{1}}}$$
$${\color{red}{\texttt{101}}} \quad {\color{black}{\texttt{001}}} \quad {\color{green}{\texttt{011}}} \quad \texttt{.} \quad {\color{brown}{\texttt{101}}} \quad {\color{blue}{\texttt{001}}} \quad {\color{purple}{\texttt{101}}} \quad {\color{orange}{\texttt{1}}} {\color{orange}{\textbf{00}}}$$
$${\color{red}{\texttt{5}}} \quad {\color{black}{\texttt{1}}} \quad {\color{green}{\texttt{3}}} \quad \texttt{.} \quad {\color{brown}{\texttt{5}}} \quad {\color{blue}{\texttt{1}}} \quad {\color{purple}{\texttt{5}}} \quad {\color{orange}{\texttt{4}}}$$
$$\texttt{101001011.1010011011} = \texttt{513.5154}$$


Quick conversion table for binary and hexadecimal numbers

Binary Hexadecimal
$$\texttt{0000}$$ $$\texttt{0}$$
$$\texttt{0001}$$ $$\texttt{1}$$
$$\texttt{0010}$$ $$\texttt{2}$$
$$\texttt{0011}$$ $$\texttt{3}$$
$$\texttt{0100}$$ $$\texttt{4}$$
$$\texttt{0101}$$ $$\texttt{5}$$
$$\texttt{0110}$$ $$\texttt{6}$$
$$\texttt{0111}$$ $$\texttt{7}$$
$$\texttt{1000}$$ $$\texttt{8}$$
$$\texttt{1001}$$ $$\texttt{9}$$
$$\texttt{1010}$$ $$\texttt{A}$$
$$\texttt{1011}$$ $$\texttt{B}$$
$$\texttt{1100}$$ $$\texttt{C}$$
$$\texttt{1101}$$ $$\texttt{D}$$
$$\texttt{1110}$$ $$\texttt{E}$$
$$\texttt{1111}$$ $$\texttt{F}$$

martedì 29 settembre 2015

Creating Android TextView with custom font

This article demonstrates how to extend a TextView to use a custom font. We will add to the standard TextView an attribute font, that will be accessible either from xml or programmatically.

Create a new Android project and create in the values folder the attributes.xml file, in order to define the attribute font (this will be used in xml configuration).

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TextViewCustomFont">
        <attr name="font" format="string" />
    </declare-styleable>
</resources>

Then we need to extend the TextView class with our customization:

package com.devsourcenter;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.TextView;

import com.devsourcenter.textviewwithcustomfont.R;

public class TextViewCustomFont extends TextView {

   public TextViewCustomFont(Context context, AttributeSet attributes) {
      super(context, attributes);
      // attributes variable contains all the attributes defined in the xml file

      // typedArray contains only the attribute specified by the resource TextViewCustomFont
      TypedArray typedArray = context.obtainStyledAttributes(attributes,
            R.styleable.TextViewCustomFont);
      // get the font attribute from xml
      String fontPath = typedArray
            .getString(R.styleable.TextViewCustomFont_font);

      // if the attribute is present it is set as font
      if (fontPath != null) {
         AssetManager assetManager = context.getResources().getAssets();
         // this is the object related to the font
         Typeface typeface = Typeface
               .createFromAsset(assetManager, fontPath);
         // now the assign the font to the TextView
         setTypeface(typeface);
         typedArray.recycle();
      }
   }

   /**
    * Assign the font in fontPath to the TextView
    * 
    * @param fontPath
    *            Path of the font file in assets folder
    */
   public void setFont(@Nullable String fontPath) {
      AssetManager assetManager = getContext().getResources().getAssets();
      Typeface typeface = Typeface.createFromAsset(assetManager, fontPath);
      setTypeface(typeface);
   }
}

The setFont method is used to set the font programmatically, while the constructor checks if the font is specified in the xml file. Now we can include TextViewCustomFont in our activity layout (activity_main.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res/com.devsourcenter.textviewwithcustomfont"
    tools:context="com.devsourcenter.textviewwithcustomfont.MainActivity" >

    <!-- we set the font in xml with attribute font -->
    
    <com.devsourcenter.TextViewCustomFont
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:font="fonts/palatino.ttf"
        android:text="@string/palatino"
        android:textSize="40sp" />

    <!-- in this view we set the font programmatically -->
    
    <com.devsourcenter.TextViewCustomFont
        android:id="@+id/courier_new_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/courier_new" 
        android:textSize="40sp" />
        
</LinearLayout>

In the first TextView we set the font with the xml custom attribute font (we declared the namespace for the prefix app to use the attribute), otherwise the font of the second TextView is changed directly in the Activity:

package com.devsourcenter.textviewwithcustomfont;

import com.devsourcenter.TextViewCustomFont;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      TextViewCustomFont customTextView = 
            (TextViewCustomFont) findViewById(R.id.courier_new_text_view);
      // set font programmatically
      customTextView.setFont("fonts/courierNew.ttf");
   }

}

As shown above we put the fonts courierNew.ttf and palatino.ttf in the assets/fonts folder. The running activity looks like this:


domenica 27 settembre 2015

Create Spring MVC project with Eclipse and Maven

In this tutorial we create a simple Spring MVC application using Eclipse and Apache Maven. The application let us to calculate area and perimeter of a rectangle, passing and receiving the data through a JSON. We will use Jackson to convert data from/to JSON, and we will deploy the generated war archive using Apache Tomcat.

First of all create a new project, select Maven project and press next:



Select maven-archetype-webapp:


Configure the project:


Press finish to create the project. If you see error The superclass javax.servlet.http.HttpServlet was not found add Apache Tomcat to the runtime environments.


Create the source folder java:


You should add the dependencies in the pom.xml file, in order to load Spring framework and Jackson libraries:

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
   <modelversion>4.0.0</modelversion>
   <groupid>com.devsourcenter</groupid>
   <artifactid>SpringMVCExample</artifactid>
   <packaging>war</packaging>
   <version>0.0.1-SNAPSHOT</version>
   <name>SpringMVCExample Maven Webapp</name>
   <url>http://maven.apache.org</url>
   <dependencies>
      <dependency>
         <groupid>org.springframework</groupid>
         <artifactid>spring-webmvc</artifactid>
         <version>4.1.4.RELEASE</version>
      </dependency>

      <dependency>
         <groupid>com.fasterxml.jackson.core</groupid>
         <artifactid>jackson-core</artifactid>
         <version>2.6.1</version>
      </dependency>

      <dependency>
         <groupid>com.fasterxml.jackson.core</groupid>
         <artifactid>jackson-databind</artifactid>
         <version>2.6.1</version>
      </dependency>
   </dependencies>
   <build>
      <finalname>SpringMVCExample</finalname>
   </build>
</project>

Create the mvc-dispatcher.xml file in the application classpath, in order to load the xml containing the beans definition. The tag <mvc:annotation-driven /> allows us to use the Java annotations in our controller.

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:util="http://www.springframework.org/schema/util"
   xsi:schemaLocation="http://www.springframework.org/schema/beans     
      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
      http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util-4.0.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-4.0.xsd">

   <mvc:annotation-driven /> 
   <import resource="classpath:beans-context.xml" />
</beans>

Create the beans-context.xml file in the application classpath, to register our Controller.

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:cache="http://www.springframework.org/schema/cache" xmlns:util="http://www.springframework.org/schema/util"
   xmlns:infinispan="http://www.infinispan.org/schemas/spring" xmlns:p="http://www.springframework.org/schema/p"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans     
      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
      http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util-4.0.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-4.0.xsd">
        
   <bean id="rectangleController" class="com.devsourcenter.controller.RectangleController" />
 
</beans>

Configure the DispatcherServlet and integrate Spring in web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
       version="2.5">

   <display-name>SpringMVCExample</display-name>

   <servlet>
      <servlet-name>mvc-dispatcher</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/mvc-dispatcher.xml</param-value>
         </init-param>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>mvc-dispatcher</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>
</web-app>

Now Spring is configured, so we can start coding. The Rectangle class will wrap the input data, while the Result class will contain output information:

package com.devsourcenter.model;

public class Rectangle {
   private Double length;
   private Double width;
   
   public Double getLength() {
      return length;
   }
   public void setLength(Double length) {
      this.length = length;
   }
   public Double getWidth() {
      return width;
   }
   public void setWidth(Double width) {
      this.width = width;
   }   
}

package com.devsourcenter.model;

public class Result {
   private Double area;
   private Double perimeter;
   
   public Double getArea() {
      return area;
   }
   public void setArea(Double area) {
      this.area = area;
   }
   public Double getPerimeter() {
      return perimeter;
   }
   public void setPerimeter(Double perimeter) {
      this.perimeter = perimeter;
   }
}

The RectangleController will receive input data and will return the result with area and perimeter. When input data is invalid the controller returns error 400.

package com.devsourcenter.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.devsourcenter.model.Rectangle;
import com.devsourcenter.model.Result;

@RestController
public class RectangleController {

   @RequestMapping(value = "/calculate", method = RequestMethod.POST)
   public @ResponseBody ResponseEntity<?> calculate(
         @RequestBody Rectangle rectangle) {
      // check if input data is invalid
      if (rectangle.getLength() == null 
            || rectangle.getLength() <= 0
            || rectangle.getWidth() == null 
            || rectangle.getWidth() <= 0) {
         return new ResponseEntity<HttpStatus>(HttpStatus.BAD_REQUEST);
      } else {
         // if data is valid calculate area and perimeter
         Result res = new Result();
         res.setArea(rectangle.getLength() * rectangle.getWidth());
         res.setPerimeter(2 * (rectangle.getLength() 
               + rectangle.getWidth()));
         
         // finally we return the result with status code 200
         return new ResponseEntity<Result>(res, HttpStatus.OK);
      }
   }
}

At this point our Application is complete, this is the final project directory structure:


We can launch the Application by right-clicking on the project SpringMVCExample and selecting Run As -> Run on Server. After that can perform a test making a PUT call to http://localhost:8080/SpringMVCExample/calculate and passing a valid JSON:



sabato 26 settembre 2015

How to hide projects in Eclipse

If you have a lot of projects opened in Eclipse workspace, then you may want to hide some of then. For example you have the projects ProjA, ProjB, ProjC and you want to show only ProjB. Let's see how we can do this: first of all you have to close the projects you want to hide, right-clicking on them and selecting Close Project as shown below.


Then under the View Menu you should select Filters (or Customize View or Filter is not listed)...



And finally check Closed project:



To reopen them again uncheck Closed projects, right click on them and select open project.