Pages

Tuesday, February 2, 2016

Evil Traits or Poor Programming Skills

So I came across this nice little article about how evil Traits are, and I thought to my self, here is yet another developer who thinks we should all work in Assembly. Are traits evil? Of cause not. Can they be used in a wrong way? Sure they can. So can functions, classes, interfaces and so on. But you cannot blame traits for peoples bad choices. Now there is a lot of reference in this article to people with C++ background saying how wrong traits are, as these references should some how prove a point? I am positive that I have seen the same points being made against OOP by people with C background.

Let's take an example of trait usage. Let's say that we want to build an OOP database abstraction layer so that we can easily adopt code for other databases. Here is what we need for basic features.
  • Select Query class
  • Delete Query class
  • Insert Query class
  • Update Query class
Also we want some shared tools such as 'compile', so let's make an abstract class that the above mentioned can extend from, let's call this 'QueryBuilder'.

abstract class QueryBuilder {}

class SelectQuery extends QueryBuilder {}
class UpdateQuery extends QueryBuilder {}
class InsertQuery extends QueryBuilder {}
class DeleteQuery extends QueryBuilder {}

What does all 4 classes needs for starters, well the options of choosing one or more tables. No mater the query type, you will need to define at least one table. So this one is best put in 'QuryBuilder'. In the above mentioned article he wrote that one problem with traits was that you could change the access level of methods when adding them to a class. Let's get an example of why this is useful. Most databases, if not all, does not really need multiple tables for an insert operation. If we add table features to the 'QueryBuilder', then this would be possible. Let's go another way. Let's create a trait instead and see where this takes us. We will call this `QueryBuilder_Table` where we use underscore as naming convention since it's a cut of part of 'QueryBuilder'.

trait QueryBuilder_Table {
    protected function table(string $name, string $alias=null) {
        // ...
    }
}

Now that we have the table feature in a trait we can add it in different ways to each class. All 4 classes excepts a table name and an alias in their constructor. But since we have made the 'table' method protected in the trait, we can extend the features in some of the classes by simply changing the access level.

class SelectQuery extends QueryBuilder {
    use QueryBuilder_Table { table as public; }

    public function __construct(string $table, string $alias) {
        $this->table($table, $alias);
    }
}

class UpdateQuery extends QueryBuilder {
    use QueryBuilder_Table { table as public; }

    public function __construct(string $table, string $alias) {
        $this->table($table, $alias);
    }
}

class DeleteQuery extends QueryBuilder {
    use QueryBuilder_Table { table as public; }

    public function __construct(string $table, string $alias) {
        $this->table($table, $alias);
    }
}

class InsertQuery extends QueryBuilder {
    use QueryBuilder_Table;

    public function __construct(string $table, string $alias) {
        $this->table($table, $alias);
    }
}

Now your able to define multiple tables in classes such as 'Select', but not in 'Insert' while using the same trait. Could this be done with normal inheritance, sure it could, but we are not done yet. Also I might add that extending a class with normal inheritance also allows you to change the access level. So if this is something that makes traits evil, then maybe someone should read a little more about the options with class inheritance.

Fields is another great thing to add to a query, but we have two different scenarios. 'Select' for example defines fields/columns that should be returned while 'Insert' and 'Update' defines fields to insert/update with a value. So for this we make two traits 'QueryBuilder_Field_Selectable' and 'QueryBuilder_Field_Updatable'.

trait QueryBuilder_Field_Selectable {
    public function field(string $name, string $alias=null) {
        // ...
    }

    public function fields(string ...$name) {
        // ...
    }
}

trait QueryBuilder_Field_Updatable {
    public function field(string $name, $value) {
        // ...
    }
}

Of cause we exclude 'Delete' as it does not need either of these options. Let's move on to conditions, something needed for 'Select', 'Update' and 'Delete'. Again we create a trait 'QueryBuilder_Conditional'.

trait QueryBuilder_Conditional {
    public function condition(string $field, $value, string $operator="=") {
        // ...
    }
}

We could go on from here and add more, like join features etc. But let's move along to something else. We have 3 classes that share one thing, 'Delete', 'Update' and 'Insert' does not need to return a result set. Instead they would be better of returning a number of affected rows. This is a feature that is actually worth checking for, so it would not be well placed in a trait. Instead we create another abstract class named `QueryExecuter` that extends from `QueryBuilder` with a method called `execute()`. At the same time we add a similar `enquire()` method to 'Select' that returns a result set. This leaves us with 5 actual data types 'QueryBuilder', 'QueryExecuter', 'Delete', 'Insert', 'Update' and 'Select'.

abstract class QueryExecuter extends QueryBuilder {
    public function execute(): int {
        // ...
    }
}

The great thing about using traits in this way, is that we get a proper separation of the different functionality. Sure 'Update' and 'Select' could have a shared class with conditional options like a `QueryConditional` for example. But that would actually be wrong and provide a very poor design. You would be better of implementing the features separately in each class. Even though they share some similar methods, the functionality they use those methods to provide is completely different. One updates a set of rows/columns while the other fetches and returns a set of rows. So when would it ever make sense to check for 'QueryConditional'. Using traits they can share these tools without having any relation with one another. Sharing 'QueryExecuter' does make sense, because it provides the exact same functionality. It executes the query and return the number of rows affected by it.

In the above mentioned article, one of the claims for traits being so evil was that classes does not inherit the data type of a trait. Well thats sort of the point, and when used correctly it's actually a good thing. You are not meant to check for options provided by traits. You are meant to check for options provided by a specific class. How that class obtains those options is beside the point.

Let's take a last look at the first example. We could have added the 'table()' feature to 'QueryBuilder' as with a protected access level and then overwrite 'table' as public and call `parent`. But there are two things that traits does better in this scenario. Firstly you don't need to overwrite it and make an additional call to 'parent' in order to change the access level. Secondly what if you want to use 'QueryBuilder' for something else that should not have 'table' options. Maybe create a 'Condition' class that can be used to add multi-level conditions.

In this case you could extend your 'QueryBuilder_Conditional' to except 'QueryBuilder' instances, and you could use your 'QueryBuilder_Conditional' trait within your 'Condition' class to gain the conditional features. Remember that 'QueryBuilder' is used to provide the 'compile()' feature, which is not exclusive to 'QueryExecuter' or the other classes that we have made so far.

trait QueryBuilder_Conditional {
    public function condQuery(QueryBuilder query) {
        // ...
    }

    ...
}

class ConditionQuery extends QueryBuilder {
    use QueryBuilder_Conditional
}

In conclusion Traits are in no way evil. They are a brilliant addition to PHP and will save people a lot of time. Sure they can be used wrong, but as mentioned above so can everything else.

Monday, June 15, 2015

Facebook remains in the dark ages

What is HTML5? Most people would be able to answer this question, whether they are highly educated IT professionals or a hobby programmer, but at Facebook it seams as if they are still struggling with this strange concept.

The thing that I love the most about HTML5, is the fact that we no longer need any plug-ins to be installed in browsers. It's canvas and socket features replaces the old and terrible Java Applets. Don't get me wrong, I love Java. It's a nice programming language and it's great when you want to easily port programs across multiple architectures. But the Java Applets is one of the worst thing ever created, next to Microsoft's ActiveX (Along with their Browser). It will not be missed. The HTML5 Video and EME features replaces both Flash and Silverlight. Both of which brings nothing but instability and security vulnerabilities.

For a long time now, I have not had any plug-ins installed in my browser and I have not had any reason to. It seams that the vast majority of websites has either switched to full HTML5 or at least provide this as a second option. The few sites that don't, are not very important. At least that has been my observation until today.

My brother wanted to show me a video on my laptop. He navigated to Facebook and logged into he's account. He then searched for a specific video and tried to play it, but it wouldn't start because Flash is not installed in my browser. I don't use Facebook myself, so I have never noticed this. But Facebook, one of the worlds largest websites, do not have any support for HTML5 playback. Personally I have never liked Facebook, I cannot say why as I don't really know it myself. Nevertheless I have always pictured them as technologically advanced. Properly because I could not imagine a website being able to reach this magnitude of popularity while falling behind in this area. They proved me wrong.

Why am I writing about this? I have no idea. Maybe it's just the chock getting to me. Next to Google/Youtube, this was the last place that I would ever expect to be so far behind in technology. I mean this is freaking Facebook that we are talking about, and their only video solution is Flash? A technology that should have been extinct by now. And this is just the observation I made in the 2 minutes that my brother was trying to get this video to play. This is something that I would have expected from a small personal site where the owner had not yet had the time or resources to make the switch. But even those sites are up-to-date. The only one missing is the one that should have plenty of resources and highly skilled personal to do it, although I am starting to doubt the later.

Wednesday, May 27, 2015

The world of Root and Xposed Bridge

People have a tendency to install and enable things that they do not fully understand. In this article I will try to explain just what Root and Xposed Bridge is and how dangerous it can be. Don't get me wrong, I love both of these things, but it is important to be careful and not grant just any application these types of rights.

Let's picture a small village. In the middle of this village is a large bank. It is surrounded with a lot of smaller buildings. The bank represents the core Android system while the buildings are all of the Applications. All the people within each building can communicate with one another since they are all within the same building. If a building needs to communicate with another building, it has to send someone across town to the other building. It is then up to the people in that building to decide whether or not they want to let that person inside. While inside, the person can make a request or a delivery. Maybe he wants to borrow some sugar. He then delivers the response (in this case the sugar) back to his building. The same applies for the bank which is surrounded by guards. To make a request for a specific item in the bank, a building will need a permission slip for that item in order to bypass the guard watching it.

One building however has an Xposed Module implemented. In this case it is a specific type of person, I spy if you will. The building can send this person over to the bank and make him act as a guard. The bank and it's other guards will not not sense that anything is wrong. This spy can now move around in any section of the bank without any permission slip. He can steel items, place new items or make changes to the existing once without anyone asking questions. He can also disguise himself as a member of other buildings and walk around those without an invite. This spy is essentially a god amongst men.

In Android it allows applications to provide features not normally available, like changing theme and colors in any part of the system, create security modules that can restrict other applications from gathering specific information and much more. But it also allows application to do things that you might not want it to, like gathering information and uploading it to a server. Since the module can do whatever it wants, there is no way to restrict it.

Root is similar. It is the main built-in Administrator in the Linux kernel (Which Android is built on top of). In this case it acts as the emperor of the village. It is the main authority in the system and no one would dare to tell it no. It can move, do and behave just as it feels like without no one trying to stop it. The most important thing to note here is that Xposed Modules is able to acts as root, even if the device is not rooted. It is also important to note that gaining root via Xposed Bridge will not trigger your normal Root Popup window on rooted devices. So you will not even know that this has happened.

There is no doubt that devices with Xposed Bridge and Root enabled are much more fun. This article is not meant to scare anyone from rooting their devices or install Xposed Bridge on them. It is meant to inform people about the danger of doing so to make them more aware next time they enable an Xposed module or grant root to an application asking for it. Make sure that the application in question can be trusted, which most importantly mean that you should not allow this for Closed Source applications. If the source codes are close, there is no telling what has been implemented into the application.

So the next time you think about enabling an application in Xposed Bridge or grant root to an application, do some research first. Make sure that you can find a link to the source codes, make sure that the developer is contactable, do some searches to make sure that others have not warned about this application.

In any case, do not just blindly enable whatever the application asks for.

Tuesday, April 7, 2015

Check if current user is owner

One would think that this is a simple task in Android, especially since one would expect Google to have added some kind of tool for it, like something to return the current user id. Well they have, but they also decided to hide it. Android actually has a very pore collection of tools when it comes to working with multi-users. Like many things in the framework, Google did not think that apps had any reason to access to any information regarding the users. Android's sources might be fully open, but the framework is more closed than boot loaders on HTC devices.

The user id is normally not a very important information since it is nothing more than a number from 10 and up, except for the owner which have 00. The only thing that this number can tell you, is which user was created first. But checking for the 00 id to identify the owner can be very helpful. You may be working on an app that should have certain restrictions when not used by the device owner. Maybe your app should not be used by other users at all. In any case identifying the owner is useful enough that Google should have added something like isOwner() to the UserManager service.

Searching the web, it seams like people are using a lot of reflection to access some of the hidden classes in order to identify the device owner. However reflection is not really needed, and it should be avoided if possible, simply because hidden classes are not official and might change over time. If this happens, apps need to be updated or they will be broken on future versions of Android.

Even though Android did not directly provide any access to the current user id, they did indirectly do this in one way, namely the app data folder. In Android 4.1 and below, all app data was placed in /data/data, but in newer versions they are placed in /data/user/[userid] and all apps have access to their own data folder. So to get the current user id from within an app, you only need to get the name of the parent folder to your apps data location. This is very simple.

MainActivity.java:
public class Utils {
 public static boolean isOwner(Context context) {
  /* 
   * Get the parent location. 
   * This can either be /data/user/[userid] or /data/data depending on Android version.
   */
  File file = new File(context.getApplicationInfo().dataDir).getParentFile();
  
  /*
   * Get the name of the folder in the parent location. 
   * This can either be [userid] or data
   */
  String user = file.getName();
  
  try {
   /*
    * Returns TRUE if this user has the id 0 (device owner)
    */
   return Integer.valueOf(user) == 0;
   
  } catch (NumberFormatException e) {
   /*
    * The user variable contained "data". 
    * This is not an multi-user environment, which means that we only have the device owner available.
    */
   return true;
  }
 }
}

The above code is even backward compatible with Android version without multi-user support, without any need to check the current API level. And we did not use any reflection or unsupported classes/methods.

Friday, March 20, 2015

JNI / Android NDK

If you visit the Android developer page and read about Android's NDK, it will seam as if Google is trying to scare people from using it and just stick with Java. In most cases this will properly be the best idea. Java is a great language and does a good job for most tasks. However JNI is not as dangerous as Google is trying to make it. Most of Android is build on JNI and IPC. Information is being parsed in and out of JVM and between processes constantly, so why should applications not take advantage of this as well?

One thing to remember about JNI is that it creates a bit of overload to parse in and out of JVM. But depending on the task, this is not necessarily enough downside to keep away from it. The best way to figure out whether or not to use C/C++ or Java, is to test your task in both and then compare the result.

One good example of a task better suited for JNI could be some extensive file operations. For this example we will collect the content of the stat file for all currently running processes on a device. On my device running Android 5, we are talking about rounded 300 files. We add this to a loop which will run 100 times which in turn will create 3000 file read operations.

The first thing we need, is a basic activity to run our examples in.

MainActivity.java:
public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  
  setContentView(R.layout.activity_main);
  
  StringBuilder builder = new StringBuilder();
  
  for (int i=0; i < 2; i++) {
   double start = System.nanoTime();
   int files = 0;
   
   for (int x=0; x < 100; x++) {
    String[] list = null;
    
    switch (i) {
     case 0: list = JavaCollector.collect(); break;
     case 1: list = NativeCollector.collect();
    }
    
    files += list.length;
   }
   
   double end = System.nanoTime();
   
   builder.append(i == 0 ? "JavaCollector" : "NativeCollector\n");
   builder.append("Files = " + files + "\n");
   builder.append("Time = " + ((end - start) * Math.pow(10, -9)) + " seconds\n");
   builder.append("\n");
  }
  
  TextView view = (TextView) findViewById(R.id.textview);
  view.setText(builder.toString());
 }
}

We also need a Java method to do the collection work. We will separate the native method and the Java method into two files in this example. It makes it easier to keep an overview.

JavaCollector.java:
public class JavaCollector {
 
 /*
  * Re-defined regexp to match directories in /proc with process id's
  */
 protected static final Pattern REFEXP_PID = Pattern.compile("^[0-9]+$");
 
 /*
  * The method that collects all the file data
  */
 public static String[] collect() {
  /*
   * Get a listing from the /proc directory
   */
  String[] procListing = new File("/proc").list();
  
  /*
   * Define our input stream
   */
  BufferedReader in = null;
  
  /*
   * Create a temp container for the file output
   */
  ArrayList<String> lines = new ArrayList<String>();
  
  /*
   * Handle each entity in /proc
   */
  for (String procEntity : procListing) {
   /*
    * We only want the pid directories
    */
   if (REFEXP_PID.matcher(procEntity).matches()) {
    try {
     /*
      * Open the sub directory stat file
      */
     in = new BufferedReader(new FileReader("/proc/" + procEntity + "/stat"));
     
     /*
      * Get the content of the stat file
      */
     lines.add(in.readLine());
     
    } catch (IOException e) {} finally {
     if (in != null) {
      try {
       /*
        * Close the file
        */
       in.close();
       in = null;
       
      } catch (IOException e) {}
     }
    }
   }
  }
  
  /*
   * Create and return a string array
   */
  return lines.toArray( new String[ lines.size() ] );
 }
}

And we cannot compare the above Java class to JNI without a native example as well.

NativeCollector.java:
public class NativeCollector {
 
 /*
  * Load our collector library
  */
 static {
  System.loadLibrary("collector");
 }
 
 /*
  * This is really a call to Java_com_example_NativeCollector_collect() in collector.cpp
  */
 public static native String[] collect();
}


collector.cpp:
JNIEXPORT jobjectArray JNICALL Java_com_example_NativeCollector_collect(JNIEnv *env, jobject thisObj) {
 /*
  * Open /proc
  */
 DIR* procDirectory = opendir("/proc");

 if (procDirectory != NULL) {
  /*
   * Create a temp container with minimum 100 indexes pre-allocated
   */
  std::vector<std::string> lines(100);

  /*
   * Create an input stream
   */
  std::ifstream in;

  /*
   * Create a variable for the proc entities
   */
  struct dirent* procEntity;

  /*
   * Handle each entity in /proc
   */
  while ((procEntity = readdir(procDirectory)) != NULL) {
   /*
    * We only want the pid directories
    */
   if (std::regex_match (procEntity->d_name, std::regex("^[0-9]+$") )) {
    /*
     * Open the sub directory stat file
     */
    std::string path = std::string("/proc/") + procEntity->d_name + "/stat";
    in.open( path.c_str() );

    if (in && in.good()) {
     /*
      * Get the content of the stat file
      */
     std::string line;
     std::getline(in, line);

     lines.push_back(line);
    }

    /*
     * Close the file
     */
    if (in) {
     in.close();
    }
   }
  }

  /*
   * Create Java return data
   */
  if (lines.size() > 0) {
   /*
    * Create a Java array
    */
   jobjectArray ret = env->NewObjectArray(lines.size(), env->FindClass("java/lang/String"), NULL);

   for (int i=0; i < lines.size(); i++) {
    /*
     * Place the line in a Java String
     */
    jstring stringObject = env->NewStringUTF( lines[i].c_str() );

    /*
     * Add the Java String to the Java Array
     */
    env->SetObjectArrayElement(ret, i, stringObject);

    /*
     * Release the Java String reference.
     *
     * Note: This is important. We can only have a limited ammount of Java objects
     * at a time and they are not auto released until we return to the JVM. And since we
     * are looping an unknown, but large, number of files, we could end up with a memory overflow.
     */
    env->DeleteLocalRef(stringObject);
   }

   /*
    * Return the array to JVM
    */
   return ret;
  }
 }

 /*
  * Return an empty array
  */
 return env->NewObjectArray(0, env->FindClass("java/lang/String"), NULL);;
}

If we compile and run this application, we will get the following result:
  • JavaCollector$collect: 21.2 seconds
  • NativeCollector$collect: 2.7 seconds

    This is not just slightly faster than Java, this is 87% faster. 
This is only a simple example. If it should be used in a real application, then depending on the amount of files and amount of data, it might be a good idea to make some sort of iteration mechanism to avoid memory overflow. But as the result shows, it is sometimes a good idea to check whether or not a JNI solution might be better. Java is not always the correct solution. 

So get started with:

Saturday, January 17, 2015

Communication across the UI

One of the greater things in newer Android versions is the Fragments. It allows you to split the UI into smaller pieces that can be put together in different ways, depending on different circumstances like screen size and such. However some times you might need to adapt things in one Fragment based on some conditions in another. Talking to an Activity from within a Fragment is easy enough, but Fragments to Fragments is another thing. Especially since it is unadvised to have one specific Fragment talk to another specific one.

A better solution is to have a small message delivery system that works similar to Android's broadcast system. That way you can parse information between Fragments, but without targeting specific ones. For this we need an extended Activity that has these capabilities.

One issue with Java (or not, depending on who you ask) is that Java does not support multi-inheritance. I for one is quite fine with this, but I would like to see something like traits be introduced into the language. In this the problem lies with the many different Activity and Fragment classes that Android has available. We don't want to copy paste everything into each Activity and Fragment sub-class that we want to use.

With the lack of traits or similar, we will be using the famous logic trick instead where we place all the logic into one class, and then only create redirection in each of the Activity and Fragment classes. Not only will this limit the size of each Activity and Fragment class, but it will also make sure that we only have to edit one class if we want to change something.

ActivityLogic.java:
final public class ActivityLogic {
    public static interface IActivityLogic {
        public void onReceiveMessage(String message, Object data, Boolean sticky);
        public void onFragmentAttachment(IActivityLogicFragment fragment);
        public void onFragmentDetachment(IActivityLogicFragment fragment);
        public void sendMessage(String message, Object data);
        public void sendMessage(String message, Object data, Boolean sticky);
    }
    
    public static interface IActivityLogicFragment {
        public void onReceiveMessage(String message, Object data, Boolean sticky);
    }
    
    private final static class ActivityLogic_MessageHandler extends AbstractHandler<ActivityLogic> {
        public ActivityLogic_MessageHandler(ActivityLogic reference) {
            super(reference);
        }

        @Override
        public void handleMessage(Message msg) {
            ActivityLogic logic = getReference();
            
            if (logic != null) {
                IActivityLogic activity = logic.ActivityLogic_mActivity.get();
                
                if (activity != null) {
                    Set<IActivityLogicFragment> fragments = new HashSet<IActivityLogicFragment>(logic.ActivityLogic_mFragments);
                    Object[] input = (Object[]) msg.obj;
                    String message = (String) input[0];
                    Object data = input[1];
                    
                    activity.onReceiveMessage(message, data, false);
                    
                    for (IActivityLogicFragment fragment : fragments) {
                        fragment.onReceiveMessage(message, data, false);
                    }
                }
            }
        }
    }
    
    private Set<IActivityLogicFragment> ActivityLogic_mFragments = Collections.newSetFromMap(new WeakHashMap<IActivityLogicFragment, Boolean>());
    private Map<String, Object> ActivityLogic_mStickyMessages = new HashMap<String, Object>();
    
    private ActivityLogic_MessageHandler ActivityLogic_mMessageHandler;
    private WeakReference<IActivityLogic> ActivityLogic_mActivity;
    
    public ActivityLogic(IActivityLogic activity) {
        ActivityLogic_mActivity = new WeakReference<IActivityLogic>(activity);
        ActivityLogic_mMessageHandler = new ActivityLogic_MessageHandler(this);
    }
    
    public void onFragmentAttachment(IActivityLogicFragment fragment) {
        synchronized (ActivityLogic_mFragments) {
            ActivityLogic_mFragments.add(fragment);
            
            for (String message : ActivityLogic_mStickyMessages.keySet()) {
                fragment.onReceiveMessage(message, ActivityLogic_mStickyMessages.get(message), true);
            }
        }
    }
    
    public void onFragmentDetachment(IActivityLogicFragment fragment) {
        synchronized (ActivityLogic_mFragments) {
            ActivityLogic_mFragments.remove(fragment);
        }
    }
    
    public void sendMessage(String message, Object data) {
        sendMessage(message, data, false);
    }
    
    public void sendMessage(String message, Object data, Boolean sticky) {
        synchronized(ActivityLogic_mFragments) {
            if (sticky) {
                ActivityLogic_mStickyMessages.put(message, data);
            }

            ActivityLogic_mMessageHandler.obtainMessage(0, new Object[]{message, data}).sendToTarget();
        }
    }
}

The above Activity Logic class is only the first step towards getting our message delivery system working. Since this is meant to be used to communicate between Fragments, we still need a Fragment Logic class as well.

FragmentLogic.java:
final public class FragmentLogic {
    
    public static interface IFragmentLogic extends IActivityLogicFragment {
        public IActivityLogic getParent();
        public void sendMessage(String message, Object data);
        public void sendMessage(String message, Object data, Boolean sticky);
    }
    
    private WeakReference<IActivityLogic> FragmentLogic_mActivity;
    private WeakReference<IFragmentLogic> FragmentLogic_mFragment;
    
    public FragmentLogic(IFragmentLogic fragment) {
        FragmentLogic_mFragment = new WeakReference<IFragmentLogic>(fragment);
    }
    
    public void onAttach(IActivityLogic activity) {
        FragmentLogic_mActivity = new WeakReference<IActivityLogic>(activity);

        IFragmentLogic fragment = FragmentLogic_mFragment.get();
        if (fragment != null) {
            activity.onFragmentAttachment(fragment);
        }
    }
    
    public void onDetach() {
        IFragmentLogic fragment = FragmentLogic_mFragment.get();
        IActivityLogic activity = FragmentLogic_mActivity.get();
        
        if (fragment != null && activity != null) {
            activity.onFragmentDetachment(fragment);
        }
        
        FragmentLogic_mActivity.clear();
    }
    
    public IActivityLogic getParent() {        
        return FragmentLogic_mActivity.get();
    }
    
    public void sendMessage(String message, Object data) {
        sendMessage(message, data, false);
    }
    
    public void sendMessage(String message, Object data, Boolean sticky) {
        IActivityLogic activity = getParent();
        
        if (activity != null) {
            activity.sendMessage(message, data, sticky);
        }
    }
}

Now that we have all of the logic behind this delivery system, we need to extend some classes that will be using these classes. Let's start with the Activity class.

AbstractActivity.java:
public abstract class AbstractActivity extends Activity implements IActivityLogic {
    
    private ActivityLogic mLogic;
    
    public AbstractActivity() {
        mLogic = new ActivityLogic(this);
    }
    
    @Override
    public void onFragmentAttachment(IActivityLogicFragment fragment) {
        mLogic.onFragmentAttachment(fragment);
    }
    
    @Override
    public void onFragmentDetachment(IActivityLogicFragment fragment) {
        mLogic.onFragmentDetachment(fragment);
    }
    
    @Override
    public void onReceiveMessage(String message, Object data, Boolean sticky) {}
    
    @Override
    public final void sendMessage(String message, Object data) {
        mLogic.sendMessage(message, data);
    }
    
    @Override
    public final void sendMessage(String message, Object data, Boolean sticky) {
        mLogic.sendMessage(message, data, sticky);
    }
}

And we will of course also need a Fragment class.

AbstractFragment.java:
public abstract class AbstractFragment extends Fragment implements IFragmentLogic {
    
    private FragmentLogic mLogic;
    
    public AbstractFragment() {
        mLogic = new FragmentLogic(this);
    }

    @Override
    public final IActivityLogic getParent() {        
        return mLogic.getParent();
    }
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        
        mLogic.onAttach((IActivityLogic) activity);
    }
    
    @Override
    public void onDetach() {
        super.onDetach();
        
        mLogic.onDetach();
    }
    
    public void onReceiveMessage(String message, Object data, Boolean sticky) {}
    
    public final void sendMessage(String message, Object data) {
        mLogic.sendMessage(message, data);
    }
    
    public final void sendMessage(String message, Object data, Boolean sticky) {
        mLogic.sendMessage(message, data, sticky);
    }
}

And that's it. We now have one normal Activity and Fragment class that we can use. Note that these are abstract, this is because we use these to extend our actual ones.

Let's take a small example of how to use these.

SomeFragment.java:
public class SomeFragment extends AbstractFragment {
    @Override
    public void onResume() {
        // Send a message to other Fragments or the Activity
        if (someVariable == "someCondition") {
            sendMessage("tellSomeone", "someCondition");
        }
    }

    @Override
    public void onReceiveMessage(String message, Object data, Boolean sticky) {
        // Receive message from other Fragments or the Activity
        if (message.equals("someOtherCondition")) {
            // Do something
        }
    }
}

If we need other types as well, like DialogFragment or ActionBarActivity, we can re-use our logic classes to easily create them.

Friday, October 31, 2014

How does Hacklang work

I must admit that I am not a fan of Facebook. Unlike 90% of the world, I do not, and have no intention of, ever owning an account. The list of reasons are long, and some of the things on that list have more to do with the people using it, rather than the service itself. Something just happens to normal people when entering the website, it's like watching one of the many talk-shows and reality programs on TV.

But although this article is about Facebook, it is not about the service that they are most famous for. This article is about a small extension to the PHP programming language, created by Facebook, called Hack, and the related Virtual Machine called HHVM. If you are a PHP programmer and either do not know about this or if you have not yet tried it, then I'll suggest you have a look at it. This extension provides features that PHP should have had ages ago. This article will not get into what Hack and HHVM is, I'll suggest that you visit the website http://hhvm.com if you are not already familiar with it.

What this article will focus on, is how HHVM/Hack acts. PHP is normally a dynamic typed language, but Hack introduces static typed features, and at the same time keeps compatibility with regular PHP. It's like mixing oil and water, and actually making them mix. The way it works, is that the VM mostly upholds the rules of PHP while the type checker upholds the rules of Hack. In other words, you can without any issues execute code that is not acceptable by the rules of static typing, but you will get errors when parsing it through the optional type checker. The errors from the type checker can also be removed by changing how strict it should be, which can be set pr. file. This allows you to mix dynamic typing and static typing, and still be able to use the type checker on the strict files.

class MyClass {
    public function test1(): void {
        $this->internal( Map{"key"=>1} );
    }

    public function test2(): void {
        $this->internal( Vector{true} );
    }

    private function internal(Map<string, bool> $param): void {
        // Code ...
    }
}

If we execute test1() from a browser, HHVM would not generate any errors. It would simply allow it. If we execute test2(), HHVM would break the request with an error. The reason for this is that HHVM follows PHP rules, which does not provide any generics. PHP however does allow one to define parameter type, which is the only thing that is being monitored for. The type checker on the other hand, would print a lot of errors here, first complaining about the wrong generics types in test1() and then complain about the wrong data type in test2().

class MyClass {
    public function test(): bool {
        return 1;
    }
}

The same applies for the return type. The above example will not conflict with HHVM. The only way to catch this problem, is by the type checker.

So the way that Hack is able to work along side PHP, is that the VM does not enforce any Hack rules. If you want to check your code for type safety, you will have to check it using the type checker. For the most part this will suffice. But the type checker is still a work in progress, and it does contain a few bugs.

class MyClass {
    public function test1(): void {
        $this->internal(Map{"key"=>new Map(array(
            "key"=>1
        ))});
    }

    public function test2(): void {
        $this->internal(Map{"key"=>Map{
            "key"=>1
        }});
    }

    private function internal(Map<string, Map<string, string>> $param): void {
        // Code ...
    }
}

Both test1() and test2() will parse the exact same value to internal(). However the type checker will only report an error on test2(). The wrong generics type in test1() will not be caught.

Thursday, October 30, 2014

PHP Traits extends OOP posibilities

OOP if a wonderful concept in programming. People have different opinions about it, but I for one love it. First of all you have all of your work divided into groups (Classes), and adding other concepts like namespaces or packages, your classes becomes sub-groups in even larger groups. It makes it easy to keep track of your work in large code bases, and it reduces the chance of things colliding, especially in environments where 3'rd party code can be added dynamically (E.g. CMS modules for an example). It also allows classes to share code with one another. If you want to create a class that adds functionally to an existing class, you can extend from that class, and adopt the already existing code into your new class. Then all you have to do, is change and/or add whatever you had in mind, without having to copy/paste the content of one class into another. At the same time, adding to and/or fixing something the first class, will automatically be appended to the second one.

But OOP does have it's limitations. For one, you can only extend from one class. So if you want to adopt code from two classes, you are out of luck. You can of course implement as many interfaces as you'd like, but interfaces only defines the structure of a class, and does not provide any pre-done work. This has now changed in PHP with the introduction of Traits since version 5.4. A Trait is something like an abstract class, the difference being that you do not extend from it, instead you sort of includes it's content within your class. It can somewhat be compared to extending objects in JavaScript via the prototype property, and you can include as many Traits as you'd like.

trait Snipped1 {
    public static function printMessage() {
        echo "Prints a text!";
    }
}

trait Snipped2 {}

class Example {
    use Snipped1, Snipped2;
}

Example::printMessage();

The class above will contain all the content from both Traits. Also unlike when extending two classes, all static content in Traits, like static properties, will have their own instance pr. class that uses them. So changing the value of an static Trait property from one class, will not change the value in other class, using the same Trait.

One thing that Traits does not do, is parse their type to classes using them. Content that you include from a Trait, will be treated as if it belongs to the class itself. This means that you can't use something like instanceof to check whether or not a class uses a Trait. There are ways to check, but these ways are slow and not very handy. And there is nothing wrong with this, it is meant to work this way, because classes does not implement or inherit from Traits, they simply copy/paste their content.

Personally I did not find Traits very useful at the beginning, mostly because if they are used wrong, they can create a lot of chaos in your code. Much more than chaotic inheritance. But after playing around with them, I found that they can be handy if you include interfaces. By using an interface to define a class structure and then add a Trait with some partial code, you will have what can be considered an abstract class divided into two parts. One containing the partial code and one containing the details about how a class should look.

interface ISnipped {
    public function printMessage();
    public function secondMethod();
}

trait Snipped {
    public function printMessage() {
        echo "Prints a text!";
    }
}

class Example implements ISnipped {
    use Snipped;

    public function secondMethod() {
        // Do something
    }
}

$example = new Example();

if ($example instanceof ISnipped) {
    $example->printMessage();
}

The above example can be compared to the example below
public abstract class Snipped {
    public function printMessage() {
        echo "Prints a text!";
    }

    public abstract function secondMethod();
}

class Example extends Snipped {
    public function secondMethod() {
        // Do something
    }
}

$example = new Example();

if ($example instanceof Snipped) {
    $example->printMessage();
}

Both examples provide much the same structure, but using Traits we can adopt from more than one. All we have to do, is implement more interfaces and add more traits.

Monday, October 27, 2014

Wordpress

A while ago, I was hired to extend a web site for a small company. The web site had been build by one of their graphics designers, who did not normally work with programming. As a result, this site had been built using Wordpress, because this was the easiest system for this non-programmer to start with, while he at the same time had to learn the basics of HTML, CSS and PHP. The custom work that he had done was not the best, but still quite impressive considering the time frame and the fact that he did not know anything about coding when he started. But this article is not about designers turning programmers. It is a small overview of Wordpress, and about some of the reasons why I would never build anything with in, unless I am forced to.

Starting on this site, I did not know anything about Wordpress. All that I knew was that it used to be a small blog system, which apparently had grown larger over time. I started reading some of the documentation, and it did seam like something worth looking into. I especially liked the idea of their action and filter callbacks. However, once I got started, I became less impressed.

First of all Wordpress is missing a lot of rules. Sure they have their Plugin.php file, but there is no rules as to what this file should contain. If you compare it to Drupal, which have a module file containing hooks with strict naming schemes, Wordpress introduces the same policy as PHP, where no standard for anything exist. Now I do like PHP, but it would be a lot better, if they implemented parts of Hack into it. Static Typing for one.

Their plugin system is also very minimal. If you need to add some additional tings to the existing blog system, then this is fine. But if you need to add some extensive work into Wordpress, you will need to integrate this directly into the "Theme" files. Also while talking about the Theme files, I think that the people behind Wordpress has misunderstood the word "Theme". These files actually makes up most of the system. They work as bootstrap of some sort, they contain a lot of the site logic as well as the Themes. All in all, they act much like MVC wrapped into one single "Theme" file.

The worst part of Wordpress, some of which was mentioned above, is their action and filers system. The idea is great, but it is extremely pure designed. This is the part that would have benefited the most by some stricter rules. One of the things that one should avoid when working with PHP, is their dynamic caller functions "call_user_func" and "call_user_func_array". Requests that normally takes nanoseconds to load, will take milliseconds and sometimes seconds, when introduced to the above functions. And this is only by a few calls pr. request. Wordpress has integrated this into the entire core of the system. Even before the initialization is done, these functions has been called numerous times. After that, even more calls is done by more of Wordpress's functions, by themes and by plugins. Depending on what you have installed and enabled, you can end up in 3 digit calls to these extremely slow functions. This is the worst design that I have ever seen in any framework for any language, and I cannot stop thinking about those poor CPU's that has to process all of this unnecessary code.

A better design would have been to implement either a strict procedural design, allowing only functions to be parsed, which can be executed without any additional resources. Or implement a strict OOP design by using interfaces, much like how callbacks works in Android.

If you just need a small blog site with few visitors and if you hate your server, then by all means, use Wordpress. But if you want a serious site with many visitors, if you love your server and if you'd like to have some nice coding standards, I would suggest using another CMS or build one using one of the many frameworks available. Wordpress is a mess, there is no standards of any kind, most of your work will have to be implemented using different hacks to work properly and it will rape your server daily.

Thursday, October 9, 2014

Microsoft Windows? Why?

I have not been a Windows user since XP was the new big thing, and even before that time, I was not a big fan of Windows. Why? Well Windows have always had a thing where it was trying to push every single one of my buttons, which often resulted in hardware pieces (Mouse, Keyboard, Monitors, etc.) being thrown across a room. This ended up becoming to expensive, and I decided to give Linux a try instead, which have saved me a lot on hardware purchases.

But living in a Microsoft world while being the guy known to have IT knowledge, it is not so easy to get rid of Windows 100%, although you might not use it at home. Once in awhile, people you know will come to you with their Windows problems. Most of the time I tell them to use a system that I want to support, if they require me to do so. I did not switch away from my Windows problems just to take on every one else's.

But for some reason I did not say no when I friend of mine came to me to get help formatting he's laptop. I thought that since Windows is now at version 8, a lot must have changed since the old XP. A quick format and a few driver installs would properly not take up to much of my time, especially since all the drivers was stored on a USB pen drive. But just as I remember it, things are never that easy with Windows. The installation went fine, the newly installed Windows booted without issues, only a few driver installs remained. I plugged-in the USB pen drive and opened 'My Computer', just like I would do on my Linux machines, which takes about half a second to load USB devices. But Windows did not load it, instead it pops up with a small yellow driver install notification in the button right corner? Well okay. But then it fails to locate a driver? A simple UNIVERSAL Mass Storage device with a Microsoft msdos partition table containing a Microsoft Fat32 file system, fails to load on a brand new Microsoft Windows 8 operating system?

I plugged-in the USB pen drive into my own laptop, running on Fedora 20, and my file manager opened right away, displaying the content of the drive. Now which of these operating system is known for being so user friendly?

I cannot specify how I managed to get this drive working. I spent about an hour playing around in the control panel, unloading/loading drivers, plug/unplugging the USB pen drive, trouble shooting and much more. Finally at some point, for some reason, it started working. But I would really not call this user friendly, especially since this is just one small example of the many issues one will stumble upon, when working with Windows. This operating system takes to much of once time on ridicules tasks like this and it increases your blood pressure to a very dangerous level. Why people keep insisting on using it, I really don't know. I find *nix systems like Linux, OSX and such, much more friendly to work with. Use those and you might even live longer.

Wednesday, February 12, 2014

Android Resources

This is not a guide or an article. It is mainly an observation that I can no longer ignorer. The issue is that most Android developers have a habit of working out custom solutions using the few tools that they know, rather than looking to see if Android has a pee-built tool for that specific task.

In this case, the issue is regarding Android's Resources class. Let's say that we want to create a dynamic string using placeholders. If you try to google this, you will mostly find the same solution on each page that you visit. I have written this solution below.

<resources>
    <string name="dynamic_string">Let\'s insert the number %1$d</string>
</resources>

Integer numberToInsert = 1;
String dynamicString = String.format( getResources().getString(R.string.welcome_messages), numberToInsert );

The problem here is that there is no point in including String.format into this, because Resources.getString is already able to do this for you.

Integer numberToInsert = 1;
String dynamicString = getResources().getString(R.string.welcome_messages, numberToInsert );

If you took a look at the documentation for this method, you would see that it does not only take [String] as an argument. It actually takes [String, Object...], where each Object parsed will be used to replace the placeholders of the string.

Please have a look at the Resources Documentation. This class contains a lot of useful tools for when working with the resources in Android. Many of which is not seen being used very much, if even at all.

Friday, November 8, 2013

Custom System Service using XposedBridge

For those of you who don't know XposedBridge, you should really have a look at it. By enabling you to hook any Class and Method within Android, system classes as well as normal application classes, this is just as powerful a tool as a root shell session. This however operates within the Java VM instead of hacking your way in from a shell.

After working with XposedBridge for awhile, I started stumbling across a few limitations. The Xposed Framework might be able to hook any method in any class, but it can of cause only add a hook before and after execution of the original method. This means that you cannot change anything within a method, but instead either replace it, change the arguments being parsed to it or change the result from it. In most cases this will be enough, however since Android parses a lot of things between multiple processes in JVM and Native code (which you cannot hook), there are circumstances where this will not be enough. For an example, if you decide to add a hook to PhoneWindowManager's interceptKeyBeforeQueueing or interceptKeyBeforeDispatching methods to change the incoming key code or some policy flags, that hook will not be enough. The two original methods will be executed with these new values, but after they have executed, the Native Event Handler will parse the original values to the application dispatchers, and your hook changes will not affect that part. This means that you would also have to add a hook to the KeyEvent's dispatch method. But, since that one will run in different processes than your first hook(s), you cannot share data between them, not even if you add all hooks to one single class instance or use a static class.

In some cases, you can use Broadcasts to send data from one process to another. But the problem with Broadcasts is that you cannot be sure about when the receiver will be invoked or when it is done processing your data. Also, sending a Broadcast or adding a Receiver requires access to a Context, and when working with Xposed you will not always have one of those. The best option in these cases is adding a custom System Service that you can use to share data between processes. The positive thing about System Services, unlike normal Application Services, is that they are available always, from the beginning of System Boot and until the System is shut down. Also they are much easier to connect to. Normally you would not be able to create such a service, but since we are working with XposedBridge, you can actually add whatever you'd like since you can operate as being part of Android.

The first thing that we will need, is an AIDL interface file. You cannot have a System Service without this. In your project, create a new package named android.os and in that package create a new file named ICustomService.aidl that looks like the below example.

android.os.ICustomService.aidl:
package android.os;
/** {@hide} */
interface IXAService {

}

We will also need a class in our regular project package that will be used to hook the service into Android. Create a class named CustomServiceHook.java somewhere in your project package.

CustomServiceHook.java:
public final class CustomServiceHook implements IXposedHookZygoteInit {
 @Override
 public void initZygote(IXposedHookZygoteInit.StartupParam startupParam) throws Throwable {
  CustomService.inject();
 }
}

Since you should already be familiar with XposedBridge, you should already know how to add this hook to be invoked by Xposed during boot.

The CustomService class that we call in our hook above is our System Service Class. The inject() method is a static method from where we will inject the Service into Android so that it can be used by our module. Create a new package named com.android.server and create a new class in that package named CustomService.java.

com.android.server.CustomService.java:
public class CustomService extends ICustomService.Stub {
 public static void inject() {

 }
}

This is our basic classes. Now it's time to extend CustomService.java to actually make it work. All of Android's original services are created and registered from com.android.server.SystemServer.java. However, all of the creation and registration is done from within a Thread. The problem here is that this Thread keeps the System Context as a normal variable, so we have no way to access it outside the Run() method. And since we cannot hook our way into the method but only add a hook before or after, this method and class is of no use to us.

However, the first thing that this method does, is invoke the main() method of com.android.server.am.ActivityManagerService in order to get the System Context. So if we add a hook after that method, the hook will be invoked in the beginning of the SystemServer thread, and we will also have access to the System Context which will be in the result from the main() method that we added our hook to. Let's change our CustomService.java file and add this hook to it.

com.android.server.CustomService.java:
public class CustomService extends ICustomService.Stub {

 private Context mContext;

 private static CustomService oInstance;

 public static void inject() {
  final Class ActivityManagerServiceClazz = XposedHelpers.findClass("com.android.server.am.ActivityManagerService", null);
  
  XposedBridge.hookAllMethods(
   ActivityManagerServiceClazz, 
   "main", 
   new XC_MethodHook() {
    @Override
    protected final void afterHookedMethod(final MethodHookParam param) {
     Context context = (Context) param.getResult();
     
     oInstance = new CustomService(context);
     
     XposedHelpers.callMethod(
       XposedTools.findClass("android.os.ServiceManager"), 
       "addService", 
       new Class[]{String.class, IBinder.class}, 
       "custom.service", 
       oInstance
     );
    }
   }
  );
 }

 public XAService(Context context) {
  mContext = context;
 }
}

The service will now be created and registered with Android during boot, but we are not quite done yet. At the time this service is created, no other services are available. So if you want to do some initializing, the constructor is not the place to do it. What we need is yet another hook that will be invoked once the system is ready. The ActivityManagerService class also has a good place for this. It has a systemReady() method that is invoked once all of the services has been created and is up and running. Let's add this to our CustomService class.

com.android.server.CustomService.java:
public class CustomService extends ICustomService.Stub {

 private Context mContext;

 private static CustomService oInstance;

 public static void inject() {
  final Class ActivityManagerServiceClazz = XposedHelpers.findClass("com.android.server.am.ActivityManagerService", null);
  
  XposedBridge.hookAllMethods(
   ActivityManagerServiceClazz, 
   "main", 
   new XC_MethodHook() {
    @Override
    protected final void afterHookedMethod(final MethodHookParam param) {
     Context context = (Context) param.getResult();
     
     oInstance = new CustomService(context);
     
     XposedHelpers.callMethod(
       XposedTools.findClass("android.os.ServiceManager"), 
       "addService", 
       new Class[]{String.class, IBinder.class}, 
       "custom.service", 
       oInstance
     );
    }
   }
  );

  XposedBridge.hookAllMethods(
   ActivityManagerServiceClazz, 
   "systemReady", 
   new XC_MethodHook() {
    @Override
    protected final void afterHookedMethod(final MethodHookParam param) {
     oInstance.systemReady();
    }
   }
  );
 }

 public XAService(Context context) {
  mContext = context;
 }

 private void systemReady() {
  // Make your initialization here
 }
}

Now you are done. Your new service will now be loaded into Android's ServiceManager during boot via our first hook to the ActivityServiceManager.main() method and it will be initialized once all services is ready via our second hook to ActivityServiceManager.systemReady().

After the initialization, you can access the binder using ServiceManager.getService( name ) just like with any other System Service provided by Android.

All that is left is to implement whatever options you would like this service to provide and start using it in your module.

Example of usage:
public class SomeClass {
 ICustomService mService;
 
 public void someMethod() {
  if (mService == null) {
   mService = ICustomService.Stub.asInterface(
     ServiceManager.getService("custom.service")
   );
  }
  
  mService.someServiceMethod();
 }
}

Friday, August 23, 2013

Controlled Environment

You are building a library of some sort, and you want to have a primary class with additional "extender" classes which all should be accessed through your primary one. Why? Don't know, hopefully you do since your the one making it. However, what is to stop anyone from accessing your "extender" classes directly? You could add your "extender" classes as sub-classes to the primary and make your constructors private or protected. But if your planing on expanding this library a great deal, you will end up with a pretty large file at the end. And splitting your "extender" classes into single files with private and protected constructors will just ensure that not even your primary class will be able to create instances of them. So what's left? I know that some languages actually has a feature which allows you to lock a class and define whom has access to create an instance. I have not worked with Java for long, so I don't know whether or not this feature exists in Java. But there are other ways of doing this, which will actually work in other OOP capable languages as well.

The trick here is Data Types. This is something that Java is very strict about, which makes this so much easier. What you need to do, is have a small class that only your primary can create an instance from. The easiest way is to place this as a sub-class to the primary class. This small class will be used to parse along as an argument to a static method in each "extender" class, who’s job is to return an instance of themselves. Meanwhile we will keep each "extender" constructor private, so that no other can access it. And we will make each class final, so that they cannot be extended in order to bypass this protection.

This is our primary class:
public final class MyPrimary {
    public MyExtender extender(String param) {
        MyTransport transport = new MyTransport();
        transport.arguments = new Object[] { (Object) param };

        return (MyExtender) MyExtender.getInstance( transport ).instance;
    }

    public final static interface MyDataType {}

    public final static class MyTransport {
        private MyTransport() {}

        Object[] arguments;
        MyDataType instance;
    }
}

This is one of our extenders:
public final class MyExtender implements MyDataType {
    public static MyTransport getInstance(MyTransport transport) {
        transport.instance = (MyDataType) new MyExtender( (String) transport.arguments[0] );

        return transport;
    }

    private MyExtender(String param) {
        ...
    }
}

In order to get an instance from MyExtender, you will need to parse through it's getInstance method. However, this method requires a MyTransport argument, which only the primary class can create. From the getInstance method, it will attach an instance of itself and then return the MyTransport object to the primary class. And by implementing MyDataType, you can use the same MyTransport class for any extender classes as they will all be able to be casted to the same data type.

That is all that there is to it. And like mentioned above, this can be used in any OOP language. In some however, you will need some manual checking of data types. PHP for an example does not really care about data types, so you will need to do a manual instanceOf check in the extender getInstance methods to make sure that the argument really is a MyTransport object. But that is not all that difficult to implement.

Saturday, July 13, 2013

Add missing Android Activity tools

One major issue with Android, is that Google have never implemented a proper way of detecting which Activity is currently in the foreground and which are behind. If you open multiple Activities in an application, and then flip the screen, all the callbacks (onCreate(), onResume() etc) will be invoked in all your Activities. The problem is that you might have some code in some of these Activities that you do not wish to have executed, unless the Activity holding that code, is currently placed in the foreground. Problem is that you can't check this state of an Activity. At least not in a proper way.

public Boolean isForeground() {
    ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
}

There is the possibility to use something like the above. There is just two issues regarding this. For one, google states that getSystemService(Context.ACTIVITY_SERVICE) is only intended for debugging and presenting task management user interfaces, and that it should never be used for core logic in an application. Second, you need to have android.permission.GET_TASKS added to the application in order to use this, which is not ideal for a simple task like this.

So what we can do instead, is extending the Activity class with a custom one which includes this missing feature and then extend our application Activities with our custom one instead.

public class ExtendedActivity extends Activity {
 
 private final static ArrayList mActivities = new ArrayList();
 
 private final static Object oLock = new Object();
 
 private Boolean mReCreate = false;
 
 @Override
 public void onSaveInstanceState(Bundle savedInstanceState) {
  savedInstanceState.putBoolean("mReCreate", true);

  super.onSaveInstanceState(savedInstanceState);
 }
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  
  if (savedInstanceState != null) {
   mReCreate = savedInstanceState.getBoolean("mReCreate");
  }
  
  synchronized (oLock) {
   mActivities.add( (mReCreate ? mActivities.size() : 0), this.getClass().getName());
  }
 }
 
 @Override
 protected void onDestroy() {
  super.onDestroy();
  
  synchronized (oLock) {
   mActivities.remove(this.getClass().getName());
  }
 }
 
 public final Boolean isForeground() {
  synchronized (oLock) {
   return mActivities.size() == 0 || mActivities.get(0).equals(this.getClass().getName());
  }
 }
}

What this does, it keep track of all active Activities and in which order they where opened. We only need to extend all of our Activities using this one, and then we can use isForeground() to check the state of an Activity Object.

public class MyActivity extends ExtendedActivity {

 @Override
 protected void onResume() {
  super.onResume();
  
  if ( isForeground() ) {
            // Do something
        }
 }
}

Sunday, June 30, 2013

Android Activity in Dialog Style

I was working on an Application where I had created a custom settings Activity (Not using the Android built-in settings tools), in order to adopt the same layout style as in the rest of the Application. It worked fine, and now I wanted to add some additional tablet styles as well. When using the Application on a tablet, I wanted a more dialog like view, something without a panel and did not take up the whole screen, but instead placed itself on top of the main activity. When searching the web for a solution to this, the only thing that I could find over and over again, was to add the Dialog Theme to the Activity via AndroidManifest.xml. I had two issues with this. For one, I did not want a Dialog like Activity for all devices, but only for tablets. And second, I have my own custom themes that I apply to all Activities. So I went another way which I was sure would work.

public class ActivityAppSettings extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  if (getApplication() != null && ((ApplicationBase) getApplication()).mTheme > 0) {
   setTheme( ((ApplicationBase) getApplication()).mTheme );
  }
  
  super.onCreate(savedInstanceState);
  
  if (getResources().getString(R.string.config_screen_type).equals("xlarge")) {
   requestWindowFeature(Window.FEATURE_NO_TITLE);
  
   Rect display = new Rect();
   getWindow().getDecorView().getWindowVisibleDisplayFrame(display);
   getWindow().setBackgroundDrawable(new ColorDrawable(0));

   getWindow().setLayout(
     (int) ((display.width() > display.height() ? display.height() : display.width()) * 0.7), 
     (int) (display.height() * 0.7)
   );
  }
  
  setContentView(R.layout.activity_app_settings);
 }
}

Sure enough it worked, but with one little issue. The space around the Activity was not transparent. It seamed that you could not change the window background from within the Activity. The panel however was removed, the size was set at 70% of the screen size and my custom theme had been applied to the content within, but with a black background surrounding it all.

I needed another work-around. I went to AndroidManifest.xml and applied Androids's Translucent theme to the Activity.

<activity android:theme="@android:style/Theme.Translucent"

The Activity theme would still be replaced by my Custom theme from within the Activity, but since you cannot change the window background from within there, the transparency would still stick. And then on Phones, you just apply a background to the main View, which you then configure as match_parent for both height and width, which will overlay the transparent window background, making it seam like a regular full-screen Activity.

Friday, June 28, 2013

Check Android Uptime

So you are programming an application for Android. You have made a custom caching system which should clear the cache after each boot. One way to do this, could be to create an onBoot Receiver and have that clear out your cache. However, why have your application run on each boot if the user might not even use it all that much? And what if the user installed some Privacy Guard Application and disabled your app's receiver?

The best way to handle this, is to have the cache cleared on the first launch of the application. But in order to do this, you will have to know whether or not this actually is the first launch or not. To help you with this, Android has two useful methods. One to provide the total amount of milliseconds that have past since 1970 (or something like that) and one to provide the total amount of milliseconds that have pasted since the device was booted. Extract the boot time from the total time, and you will have the exact timestamp from when the device was started. Now just save this to your shared preferences and compare it with a fresh timestamp on each application launch.

public class myclass extends something {

    /*
     * We can use this to avoid to much checking.
     * As long as this static property exists, we know that device has not been rebooted and there is no reason to do a check.
     */
    private static Boolean oCacheCheck = false;
    
    public SharedPreferences getCache() {
        SharedPreferences preferences = .getSharedPreferences("cache", 0x00000000);

        if (!oCacheCheck) {
            Long freshTime = System.currentTimeMillis() - SystemClock.elapsedRealtime();
            Long cachedTime = preferences.getLong("timestamp", 0);

            if (freshTime == cachedTime) {
                Editor edit = preferences.edit();
    
                edit.clear();
                edit.putLong("timestamp", freshTime);
                edit.commit();
            }

            oCacheCheck = true;
        }

        return preferences;
    }
}

if (freshTime == cachedTime) {

There is however one tiny issue with this way of checking last boot time, and that is that both methods might take a few milliseconds to execute (depending on the speed of the device) and you can only execute one at a time. This means that your calculation could differ each time you run it, making the comparison with the cached timestamp useless.

The solution to this problem is to check the difference between the two timestamps instead of comparing them to see if they are equal. The methods might take a few milliseconds to execute and any type of device will take several seconds, some even minutes, to boot. So we will just check to see if the two timestamps has a difference less than 3 seconds.

public class myclass extends something {

    /*
     * We can use this to avoid to much checking.
     * As long as this static property exists, we know that device has not been rebooted and there is no reason to do a check.
     */
    private static Boolean oCacheCheck = false;
    
    public SharedPreferences getCache() {
        SharedPreferences preferences = .getSharedPreferences("cache", 0x00000000);

        if (!oCacheCheck) {
            Long freshTime = System.currentTimeMillis() - SystemClock.elapsedRealtime();
            Long cachedTime = preferences.getLong("timestamp", 0);

            /*
             * If this is grater than 3 second, then we will most likely have a fresh application launch. 
             * No device, no mater how slow, takes 3 seconds or more to execute the time methods.
             * And no device, no mater how fast, can boot and launch the application in less than 3 seconds. 
             */
            if ((freshTime - cachedTime) > 3000) {
                Editor edit = preferences.edit();
    
                edit.clear();
                edit.putLong("timestamp", freshTime);
                edit.commit();
            }

            oCacheCheck = true;
        }

        return preferences;
    }
}

if ((freshTime - cachedTime) > 3000) {

If, when you extract the cached timestamp from the fresh one, get a difference on more than +3000 milliseconds, you can be sure that the device has been rebooted since your last application launch.

Wednesday, May 8, 2013

Multiple Encrypted Disks with Linux

This is a fallow up to the parent guide, Full Disk Encryption with Linux. If you have not yet read it, then you are advised to do so before continuing with this one.

Encrypting you entire hard drive is great. But what if you like me, have more than one hard drive in your computer? One thing that is not great, is having to enter multiple passwords on each boot. Well, you don't have to.

The magic word here is Key File. It is a small file containing some random bytes which can be used as a key to unlock an encryption without using a password. An if you are a person that sucks at coming up with great passwords, it will in most cases be more secure than any password you would assign an encryption. The insecure part is how you store it.

In this guide, we will store this key on our primary encrypted hard drive. It will not be accessible until the first drive has been unlocked. We can then use it to unlock any additional drives that you may have attached to your computer, and get away with only having to type in one single password to unlock multiple drives.


Preparing the hard drive


You can skip this step if you already have an additional and fully encrypted hard drive, and just want to know how to assign a key to it.

Otherwise, let' wipe your drive.

shred -v /dev/sd<Y>

Remember to change Y to the letter matching your additional drive.

Now let's partitioning this drive. We will just create one single partition which will hold the encryption. Also, we will not be using LVM in this guide as we do not need things like SWAP or an OS on it. We have this on the primary disk.

cryptsetup -y --cipher aes-xts-plain --key-size 512 luksFormat /dev/sd<Y>

We will also need it unlocked in order to use it.

cryptsetup luksOpen /dev/sd<Y> <Enc_Name_Add>

And last, creating a new file system inside the encryption.

mkfs.ext4 -L Additional <Enc_Name_Add>

You can use whatever file system or label that you wish.


Creating the key


dd if=/dev/urandom of=/root/encryption.key bs=4096 count=1
chmod 0440 /root/encryption.key

Now we have an 4096bit key and it is only accessible by root, which means that it is quite secure even on a booted system.


Assigning the key


In order to use this key, we need to assign it to the encryption.

cryptsetup luksAddKey /dev/sd<Y> /root/encryption.key

Now the encryption can be unlocked by both the assigned password and this key.


Adding the encryption to crypttab


We will also need to add the encryption and key to crypttab to let the boot loader know how to handle this. Open /etc/crypttab with a file editor and add the line below.

<Enc_Name_Add> /dev/sd<Y> /root/encryption.key luks


Adding the unlocked partition to fstab


If you would like the partition to be mounted during boot, you will need to add it to fstab. Open the file /etc/fstab and add the line below.

/dev/mapper/<Enc_Name_Add> /media/additional ext4 defaults 0 2


Update grub and boot loader


Now we just need to rebuilt the kernel image, reboot and we are done.

update-grub
update-initramfs -u

You can redo this guide for as many disks as you like.

Full Disk Encryption with Linux

A great thing about Linux, is the built-in subsystem DMCrypt, which, with some help from the brilliant Initrd, allows you to make a full disk encryption, including the root partition (Boot excluded of cause) and have it unlocked by a key file or password during boot, all without any third party software. This guide will show you how to encrypt your entire hard drive and set up your computer to unlock it on each boot using a password.

What is even better, is Logical Volume Manager.  This will allow us to create partitions inside a partition, which means that we only need to encrypt one partition on the hard drive, and then create whatever volumes we need, inside that single encrypted partition.

Because Ubuntu is the most used distro of all the available once, this guide will use this for the examples. But it should be easy enough to incorporate this guide into other distro's as well, especially since this guide will be using a terminal to do all of the work, and the shell is mostly the same across all distro's.

Before we can continue, you need to download Ubuntu (Or any distro  of your choice), and create a live CD or USB Pen. Then boot up the live system, and once in the UI, press Ctrl+Alt+F1 to enter a terminal.

This guide will assume that you know your way around Linux. So we will not cover anything about creating live disks or how the shell works. You can google it if you don't already know. This is all about the encryption part.


Erasing the hard drive


The first thing to do, is erasing any existing data on the hard drive and replacing it with random bytes. Even though the hard drive will be encrypted, attachers will still be able to see which part of the drive contains any data. This allows them to focus on that specific part of the drive, and making it much easier cracking it. By placing random bytes across the whole drive, we hide the real data which makes it much harder to determinant which parts of the drive contains anything worth cracking.

shred -v /dev/sd<X>

Remember to replace X with the letter matching your drive. 


Preparing the hard drive


Next we need to create a new partitioning table. By wiping the drive, the existing table was erased along with the rest of the content. Use fdisk, or another partition manager, to create the table below.

Device  Type  Size
/dev/sd<X>1  Primary  1GB
/dev/sd<X>2  Extended  Everything
/dev/sd<X>5  Logical  Everything


Creating the encryption


Now we create the encryption on /dev/sd<X>5. This is the partition that will store our logical volumes.

cryptsetup -y --cipher aes-xts-plain --key-size 512 luksFormat /dev/sd<X>

After you have typed in the password that you wish to use, we need to unlock the encryption in order to use it.

cryptsetup luksOpen /dev/sd<X> <Enc_Name>

The Enc_Name is the name that will be used for the device map. It will create /dev/mapper/<Enc_Name> which is the entry point (door if you will) to the device behind the encryption. Just replace Enc_Name with the name that you wish to use.


Creating the LVM volumes


In this guide, we will be creating 3 volumes. 1 for SWAP, one for root and one for home. You can of cause create whatever you need or want.

Before we can create the volumes, we need to initiate our encrypted volume for LVM and create the volume group that will store the volumes.

pvcreate /dev/mapper/<Enc_Name>
vgcreate <Vg_Name> /dev/mapper/<Enc_Name>

Replace Vg_Name with the name that you wish for your volume group.

Now we are ready to create the actual volumes.

lvcreate -n swap -L 6G <Vg_Name>
lvcreate -n system -L 25G <Vg_Name>
lvcreate -n home -l 100%FREE <Vg_Name>

We now have 3 new devices
  1. /dev/mapper/<Vg_Name>-swap
  2. /dev/mapper/<Vg_Name>-system
  3. /dev/mapper/<Vg_Name>-home 
You can change the names to something else if you'd like, and/or change the sizes. The last volume we created are assigned 100%FREE, which means whatever is left after creating the first two. 


Installing the OS


It is now time to get the OS installed. Press Ctrl+Alt+F7 to get back into the live system UI and select install. Once you get to the partitioning part, select manual. Now assign appropriate mount points to the 3 logical volumes that we created before and use /dev/sd<X>1 as boot. Continue the installation. Once done, do NOT reboot, instead press Ctrl+Alt+F1 again to get back into the terminal.


Set up chroot


Now that we have the OS installed, we need to make some changes to it, but before we can do that, we need for it to act as the main OS. In other words, we need some help from chroot.

mkdir /mnt/system
mount /dev/mapper/<Enc_Name>-system /mnt/system
mount /dev/sd<X>1 /mnt/system/boot
mount --rbind /dev /mnt/system/dev
mount --rbind /sys /mnt/system/sys
mount --rbind /proc /mnt/system/proc
chroot /mnt/system

You should now have entered a new apparent root directory and we are ready to make changes to the OS that you have just installed.


Set up crypttab


The first thing that we need to do here, is edit/create /etc/crypttab. This is a file which will tell the boot loader how to handle the encrypted partition, or more accurate, it will tell the system how initrd should be structured once we rebuild it.

Open  /etc/crypttab with a file editor like nano, and append the content below.

<Enc_Name> /dev/sd<X>5 none luks,retry=1

This will tell the boot loader that /dev/sd<X>5 contains a luks encrypted partition, which should be decrypted to the device map <Enc_Name>. The none part is where we could have assigned a key file, without it, a password prompt will be used instead.


Loading modules


The second thing to do, is have some specific modules loaded on boot, which are needed to unlock the partition.

Open the file /etc/initramfs-tools/modules

dm-crypt
aes-x86_64 (aes-i586 is you are using 32bit)
xts
sha256_generic
sha512_generic
ahci


Recompiling kernel image


And last, we regenerate initrd

update-initramfs -u

Reboot your computer. During boot, you will be prompted to enter a password. Enter the password and your hard drive will decrypted and the computer will continue it's regular boot.

Next time you need to upgrade or for other reasons reinstall your OS, all you have to do, is decrypt/unlock the encrypted partition and then fallow this guide from the parts after the installation of the OS. Everything above that is a one time thing.

Tuesday, May 7, 2013

Redirecting stderr to a function

So, I was writing a shell script where I have made a custom log function to be used for custom messages, errors and warning. However, I also wanted to store everything from stderr in this log file, which I thought would be no big deal. I just added the below code.

exec 2> >(Log)

And then I added my Log function to the script

Log() {
    if [ -z "$1" ]; then
        read Message
    else
        Message="$1"
    fi

    ....
}

And it was working great, until I tested it in Born Shell. Here I got an redirect error. So I needed to look around for another way to do this and I stumbled across an article from Chris Siebenmann.  This is a great example, but I needed to append this to the whole script and not just one single command. This however was easy enough, I just needed to append a sub-process to all of my script content.

Log() {
    if [ -z "$1" ]; then
        read Message
    else
        Message="$1"
    fi

    if [ -n "$Message" ]; then
        ....
    fi
}

exec 3>&1

(
    .... Execute everything from here


) 2>&1 >&3 3>&- | Log

Now everything from stderr will be parsed to the function, and it will work with both Bash and Sh.