About the app =>
I have attempted at designing a simple chat app after being given as an assignment by one of the companies that came to the campus (IITR). The problem statement was to design a chat application using the telegram APIs. The app should have some special features such as chat heads and should allow authorization through phone no. only (just like telegram). I was given a period of 5 days to finish this app. But back then, I was a newbie in this field and so had to spend 3 days of the time learning all I could about app development, especially chat apps. I had acquired enough knowledge but not as much as was needed for developing a successful, working app. Moreover, I just couldn't get any hint of using/integrating Telegram APIs to my app.(Few days later, after submitting the assignment, I came to know that it just wasn't possible because their API was not an SDK or a library). So, I followed one of the web tutorials at appsrox.com step by step and this is what I got in the end.
Yes, the app doesn't run because I got stuck with the backend integration but I was successful in eliminating the compiling errors. Instead of Telegram APIs, I tried to integrate it with Google Cloud Messenger but that too went in vain. Now, talking about how the app is supposed to work and look. -
Layout -The main screen is like an inbox. Its layout has been defined in main_list_item.xml of the layout folder. It contains list of contacts with whom you have conversed, along with the message counts. It has been implemented through a list view with a custom row layout. To add new contacts(using their email id) and to access the settings, there are two buttons on the action bar. The one that looks like a plus sign, would open a dialog box where you can fill in the details(email) of your new contact. The other one opens the setting screen. In the setting screen you can enable/disable notifications and change your incoming message tone. It also shows your display name and chat email id. There are also two other fields that are for developer use only (to get the app working you need to fill in the GCM sender id and Server URL) . This activity can be created using the template provided in Android Studio. All the necessary fragments and layouts for its implementation have been defined under xml folder. The last screen is the chat screen which opens on clicking at any of the added contacts. It has an input field to write a message and a send button near the bottom. The message list is implemented through fragments. This fragment has been defined in list-row.xml. It contains two text views to show the message and its sent/received time respectively. The chat screen layout itself is defined in activity-chat.xml. The design of message box is implemented through box.xml in drawable folder. At the top you can see your contact name with a default picture and the online/offline state and a button to edit contact name.
Now talking about programming and the classes. To implement the above screens, there are three classes - MainActivity, SettingsActivity and ChatActivity, respectively. Besides these, there is a Common class which extends from application which since gets instantiated first before all the classes, contains all the global variables. There are also two classes, EditContactDialog and AddContactDialog. These are related to the dialog boxes that gets called through action bar of chat screen and main screen respectively. There is one MessagesFragment class that has methods to show the message, time in the chat activity. SettingsActivity also uses another class called AppCompatPreferenceActivity. The SQL based actions are through the DataProvider class and the broadcast receiver is implemented through GcmBroadcastReceiver class. Other classes - GcmUtil, ServerUtilities are for integration to the server. The PushNotificationService creates a notification everytime a message gets received. Lastly, the Constants interface is where you need to fit in your GCM sender id and Server URL.
Sending a message - Once the send button is clicked after typing the message, a call to GcmUtil class is made. It has a method called register() which takes Sender ID as an argument and creates a registration ID to be sent to the server. This is done through ServerUtil class which contains utility method to send HTTP POST request to a server. The server then transmits the data to the GCM server from where it is delivered to the receiving contact.
Receiving a message - The important advantage of GCM is that it uses push notification mechanism where server sends messages to a device so that it need not poll the server continuously for messages which drains battery power. On receiving a message, the PushNotificationService creates a notification and calls the broadcast receiver. The receiver generates a notification, generates the sound and increments the message count. Thus the user gets to know that a new message has been received.
So that is a complete description about my work in designing this app.