Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Android

CPDroid (CodeProject on Fly !!)

4.94/5 (42 votes)
20 Mar 2014CPOL5 min read 67.7K   694  
A lazy and attractive tool for CodeProject

Introduction

Before starting my article, at the outset I would like to say that I am very addicted to reading articles on CodeProject. Also, I am a small contributor in this community and have given a couple of articles to it. I try to read almost all good articles in this community. Sometimes, I found that I have time but I don’t have my laptop to browse article (usually on long drive to my native place), so I surf CodeProject site in my mobile browser, which is not attractive. So in the process of solving my problem, I decided to make a native app for CodeProject and finally I developed an application which is known as “CPDroid” in the Android market. I would like to thank Luc Pattyn for his beautiful effort in CPVanity tool for CodeProject as I get most of the ideas from his article for CPDroid. You can install it from Android market here.

Anyway let’s start a little introduction for CPDroid. CPDroid is a native application for Android based mobile phone. I divided the application component in three parts.

  1. The first part consists of author search on the basis of their CodeProject member id. It will fetch author information and respective data in a neat and clean format.
  2. The second part gives latest feed of CodeProject articles and tips.
  3. The third part is just about me ? and license.

CPDroid Functionality

Functionality of CPDroid is very simple and straightforward. Let us summarize it as:

  • Search result of author is very handy and attractive. User will get author information with their respective points and other details like number of articles, messages, etc. along with list of articles and reputation graph.
  • User can read every article on click of listview and it will open article in best readable format of webview.
  • Get latest feed of CodeProject articles using Android SAX parser technique.

Main UI

Main user interface is navigator for different parts. This is Android activity which calls three different component buttons. Intent is used to fire associated activity for search author and latest feeds. Because Main UI consists of simple functionality, then I would not go too much in this section and it is better to give how I made rounded buttons in UI for navigation to section.

XML
<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
     <bitmap android:src="@drawable/btnsearchall_pressed"
        android:gravity="center" />        
    </item> 
     <item android:state_enabled="true" >
     <bitmap android:src="@drawable/btnsearchall"
        android:gravity="center" />        
    </item>
</selector>

In Android, we can highlight the button through custom images. To indicate the different button states (focused, selected, etc.), you can define a different image for each state. E.g., a blue image by default, an orange one for when focused, and a yellow one for when pressed. An easy way to do this is with an XML drawable "selector." In this code, when button is pressed, it will call drawable source image button pressed and when it is not, it will call different drawable. UI is simply delegated by the Android operating system by mapping it elegantly. And this is the beauty of Android OS which attracts me to jump in Android.

Search Author

This is my favourite section and USP of CPDroid. User can perform search author with respective member Id. Though it is very tough to search author with id instead of name, I can’t put in more effort on custom URL and complex Regex operation to get required data of author. Actually life is easy if CodeProject would have any web service for this. Every data is coming from parsed HTML which I must say it is not good for a stable application. And this is the big fear of application for down as well. Anyway, I hope CodeProject will provide some web service in future to extend the community.

Anyway Search author result is separately handled by the model and parsing of HTML is handled by thread which delivers to view by Handler class. A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

There are two main uses for a Handler:

  1. To schedule messages and runnables to be executed as some point in the future; and
  2. To enqueue an action to be performed on a different thread than your own.
C#
public void GetCpAuthorSummary(final ICPAuthorSummary cpAuthorNameCallback) {
    new Thread(new Runnable() {

        @Override
        public void run() {

            try {
                 summaryList = cp.GetAuthorSummary();
                 cp.GetArticlePage();
                 authorName = cp.GetName();
                 handler.post(new Runnable() {
                    @Override
                    public void run() {
                    cpAuthorNameCallback.AuthorSummaryResult
                        (true, authorName, summaryList);
                    }
                });
            } catch (Exception ex) {
                cpAuthorNameCallback.AuthorSummaryResult(false,
                    authorName, summaryList);
            }
        }
    }).start();
}

Here, I used Interface ICPAuthorSummary for getting results from thread and further implement interface in Search author Activity. Boolean result of Interface ensures when to publish data in view.

Image Viewer

Once search is performed, it will gives Author CodeProject profile Image with other relative details. For image viewer, I used to download image from URL which extracts image from application context and HTML, then downloads to stream and generates Drawable Image source.

C#
public Drawable ImageOperations(Context ctx, String url, String saveFilename) {
    try {
        InputStream is = (InputStream) this.fetch(url);
        Drawable d = Drawable.createFromStream(is, "src");
        return d;
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

public Object fetch(String address) throws MalformedURLException,IOException {
    URL url = new URL(address);
    Object content = url.getContent();
    return content;
}

Author Stats, Articles and Reputation

This section will have three tabs associated with it. First tab “Stats” gives listView of stats of articles. Here, I put in more effort to get regular expression for stats.

Articles will gives article for respective searched author. All articles will be published with their article details like View, Bookmark, votes, Popularity and Ratings. You can directly click on any article and read it in webview. Reputation is not very attractive, it just gives the reputation graph.

Latest Article Feed

For this section, thanks God!! CodeProject gives RSS feed for latest article. So I used Android SAX parser to get feed in view. I’m not going to give details on Android SAX parser because for this, it must be covered in a full phase article which I will surely give you in a separate effort of an article thread.

Conclusion

CPDroid is a simple, attractive and easy to use native application in Android. However, I did not implement a lot of functionality in the application because of absence of CodeProject Web service. It’s really hard to parse HTML content and get data using complex regular expression. But if this application becomes popular, I will defiantly release next version of CPDroid.

Future Component

The future idea of few components is running in mind to add more functionality in CPDroid. But I would like to release it in the next version if application will give good response from Android market. Some components are below:

  • Bookmark functionality to bookmark favorite author
  • Save article in PDF
  • Menu Control navigator
  • Show Famous Author Awards

History

  • Release Version: 1.0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)