Monday, September 16, 2013

Write and Read (Serialize) Object to memory

If you want to save object like HashMap, Array and so on to application memory,
use code below. Please note, all objects fields should be Serializable.

Types like String, int, HashMap... are already serializable, but if you create a class of your own, you should verify that that it implements Serializable, and so all of his content recursively.

Write to file:
    /**
     * Writes Serializable object into a file
     */
    public static void writeObjectToFile(Context context, Object object, String filename)
    {
        ObjectOutputStream objectOut = null;
        try
        {
            FileOutputStream fileOut = context.openFileOutput(filename,Activity.MODE_PRIVATE);
            objectOut = new ObjectOutputStream(fileOut);
            objectOut.writeObject(object);
            fileOut.getFD().sync();

        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (objectOut != null)
            {
                try
                {
                    objectOut.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
 
 
Read from file:
    /**
     * Reads a Serializable object from a file
     */
    public static Object readObjectFromFile(Context context, String filename)
    {
        ObjectInputStream objectIn = null;
        Object object = null;
        try
        {
            FileInputStream fileIn = context.getApplicationContext().openFileInput(filename);
            objectIn = new ObjectInputStream(fileIn);
            object = objectIn.readObject();

        }
        catch (FileNotFoundException e)
        {
            // Do nothing
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (objectIn != null)
            {
                try
                {
                    objectIn.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }

        return object;
    }

Monday, September 9, 2013

How to use Google Maps V2 - Keystore orientation



1. Locate your keystore file:

    For debugging you should use the debug keystore file located in:
    C:\Users\USER_NAME_HERE\.android\debug.keystore

    For release version you should use the generated keystore
    (the same one that you use when you'll sign your applicatoin for distrebution)

2. Execute command (cmd) and extract SHA1 string:

    For debug:
keytool -v -list -alias androiddebugkey -keystore 
YOUR_DEBUG_KEYSTORE_FILE_PATH_HERE -storepass android 
-keypass android

    For release:
keytool -v -list -alias YOUR_ALIAS_HERE -keystore 
YOUR_KEYSTORE_FILE_PATH_HERE -storepass YOUR_GENERAL_PASSWORD_HERE 
-keypass YOUR_ALIAS_PASSWORD_HERE

3. Open https://code.google.com/apis/console/

4. Go to services.

5. Enable google maps V2.

6. Go to API access menu item.

7. Press on "Create new Android key".

8. Paste your sha1 code with the application package:
(e.g. 40:DC:19:FF:11:33:B9:61:64:54:79:7A:84:5C:C8:F8:AF:CC:FC:1C;
com.company.appname)

9. Copy your API key from the Simple API Access    
    (should look like : AIzzSyACccKZeD-4lmBrtZI0JSrxBOAMnK1oEIk).

10. Paste your API key in your AndroidManifest.xml file:
<meta-data android:name="com.google.android.maps.v2.API_KEY"
 android:value="AIzzSyACccKZeD-4lmBrtZI0JSrxBOAMnK1oEIk"/>

Monday, August 26, 2013

How Android developer should get resources

Hi Designers,
Few very important conventions

  1. Names should be meaningful.
    Bad: "logo".
    Good: "main_application_logo"
  2. No capital letters, spaces or dashes in names.
  3. Icons resources should starts with "ic_".
    For example: "ic_home_logo".
  4. Backgrounds resources (also ninepatches) should starts with "bg_".
    For example: "bg_enter_button".
  5. Resources should be separated to folders: xhpi, hdpi, mdpi (and maybe xxhdpi).
  6. Resource name should be consistent for all folders.
    Bad: "ic_home_logo_xhdpi",  "ic_home_logo_hdpi",  "ic_home_logo_mdpi".
    Good: "ic_home_logo" in different sizes in each folder.

Please please please follow these conventions.
You are driving me crazy!

Friday, July 26, 2013

Introduction to XMPP / Jabber






What is Xmpp?

The Extensible Messaging and Presence Protocol (XMPP - originally named Jabber) is an open technology for real-time communication, using the Extensible Markup Language (XML) as the base format for exchanging information. In essence, XMPP provides a way to send small pieces of XML from one entity to another in close to real time.


Xmpp and Jabber, what is the difference?

Xmpp protocol was originally named Jabber. That's all.


ConnectionConfiguration:

Configuration to use while establishing the connection to the server.
Here you can set the Host, Port and Service details:

Google Hangout (talk) configuration for example:

public static final String HOST = "talk.google.com";
public static final int PORT = 5222;
public static final String SERVICE = "gmail.com";

ConnectionConfiguration connectionConfiguration = 
    new ConnectionConfiguration(HOST, PORT, SERVICE);


XmppConnection

Creates a socket connection to a XMPP server.
Gets connectionConfiguration in the constructor.
XmppConnection xmppConnection =
    new XMPPConnection(connectionConfiguration);


Tuesday, July 23, 2013

MultiUserChat with XMPP - Android Tutorial






In this post I will show you how to build an application with multi user chat running on XMPP server.

For a small introduction read this post: Introduction to xmpp
Now let's go fucking coding!
You can download the Jar file from here

Step 1 - Define some parameters

    public static final String HOST = "jabber.cz";
    public static final int PORT = 5222;
    public static final String SERVICE = "jabber.cz";

    String mNickName;
    String mGroupChatName;
    private MultiUserChatmMultiUserChat;
    private XMPPConnectionmXmppConnection;
    private ConnectionConfiguration mConnectionConfiguration;

Step 2 - Create a connection

 mConnectionConfiguration = 
new ConnectionConfiguration(HOST, PORT, SERVICE);
mConnectionConfiguration.setRosterLoadedAtLogin(false);
mXmppConnection = new XMPPConnection(mConnectionConfiguration);
mConnectionConfiguration.setSASLAuthenticationEnabled(true);
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
mXmppConnection.connect();

Step 3 - Login and set status

mXmppConnection.login(USERNAME, PASSWORD, NICK_NAME);
// Set the status to available
Presence presence = new Presence(Presence.Type.available);
mXmppConnection.sendPacket(presence);

Step 4 - Set the Mutliuser Chat

// Create a MultiUserChat using a Connection for a room 
// (room name as the second parameter)
mMultiUserChat = new MultiUserChat(mXmppConnection, mGroupChatName);
mMultiUserChat.create(mNickName);
mMultiUserChat.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));
mMultiUserChat.join(mNickName);

Step 5 - Received messages

// Add a packet listener to get messages sent to us
PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);
mXmppConnection.addPacketListener(new PacketListener()
{
    @Override
    public void processPacket(Packet packet) 
    {
        Message message = (Message) packet;
        if (message.getBody() != null)
        {
            String from = message.getFrom();
            String Body = message.getBody();
            // Add incoming message to the list view or similar
        }
    }
}, filter);

Step 6 - Send message

// import org.jivesoftware.smack.packet.Message;
Message msg = new Message(to, Message.Type.groupchat);
msg.setBody(msgText);
mMultiUserChat.sendMessage(msg);