📜 ⬆️ ⬇️

Habrachtets or how I did this summer

Hello!

Inspired by the posts that need to start doing and everything will turn out, in this material I would like to share a story about the result of numerous inspirations and my experience in developing an application for the Android OS. I will try to describe the process from the emergence of the idea to its implementation.

Last summer gave me two and a half months of complete freedom, half of which I spent on writing the program. I’m not a newcomer - my diploma was a fairly serious first project, but I think that this particular application will bring me fame will be interesting for you.
What I had at the source:

It would seem that. And then Habr?
')
And where. Your attention - Habrachtets:

image

As I have already mentioned - at the end of June of this year, I already faced a couple of months free from any kind of employment. And I made a habit, waking up in the morning, to open on the tablet a couple of three news applications, carefully suggested by the Market, and carefully study them. And that is very convenient. A cup of steaming coffee, a view from the balcony, a sandwich and home wireless Wi-Fi now made the start of my day.

Only Habraklient’s absence added his spoon of tar - he had to open the Opera, go to the site, scroll, adjust ... Not that it’s inconvenient, but still it’s not so easy to press and punch with one finger, especially with a cup of coffee in hand. Somewhere in one of those moments, I thought. And he decided. Attempt to write a client for a site in the image of applications that I already had.

What was the source:

What was the task:

The source material multiplied by four weeks (meaning really working days from those two months - that's how I went to the sea) brought its fruits - now I can enjoy reading Habra in any place where there is internet.

Next, some details.

Data and processing

Technically, the application is quite simple. I naturally did not have access to the Habr API. I had to spin. So, in stages.
Habrachtets makes a request to the address of the habr and receives the HTML code using HttpUrlConnection . This operation is, for obvious reasons, asynchronous - it should not and therefore does not block the interface. The sequence of actions is simple - pressing a button, launching a background task, connecting to Habr, receiving a code and transmitting it to an interested Activity as a String object nested in a special Intent via an alert ( Broadcast ). Nothing special.

Gourmet: However, there was a snag in the sequence described above. To transmit data within the application (and the system in general), intent objects are used — Intent sent in alerts — Broadcast . So here. A couple of times the internal contents of the posts did not want to load. Those. even the logs showed that the application found Habr, loaded the page code, but then ... nothing. At all. Silence. The application did not hang, but nothing happened. Restarts did not help. Content did not appear. The problem was left until better times and was explained almost by accident.

The fact is that all new and fresh posts were loaded easily and naturally. But to read something older and with more comments was no longer possible. A careful (!) Reading of the logs revealed a strange line about a system error - !!! FAILED BINDER TRANSACTION !!! It's hard not to notice, right? So I did not notice. The only line that is immediately hammered by other "garbage". There was a problem in it. Googling showed that this occurs if the data used for transmission exceeds a certain limit. Began to measure the size of the line - 600 000 characters. But there were not even 100 comments! In general, he added cleaning procedures, conducted several tests and found out a limit equal to 250 thousand characters per line. Now, if even after cleaning the size is exceeded, the line is trimmed and ... not all comments will be available (* crying *).
Solve the problem can be relatively easy, at least to transfer data in a couple of steps. I think that it will be solved in the next update.

Further, the HTML code of the web pages from the site is processed into the components of interest. For this, the Jsoup library is used - a great thing that allows you to extract all the necessary data on the specified tags and attributes. One row table is enough:
 Document document = Jsoup.parse(html); 

Well, after that the analysis of the elements.

For gourmets: a Document class object obtained after one-line processing provides two options for obtaining the necessary internal elements. The first with the help of "ordinary" getters:
 //         Elements contentElements = document.getElementsByAttributeValueContaining("class","hentry"); 

The second with the more convenient general select() method
 //  : document.select(".page-navigation .info-text [class*=live] "); 

As you can see, the second option is much more convenient, allowing you to reduce the amount of code. One line searches for elements with a class attribute equal to “page-navigation” and “info-text”, as well as containing the word “live”. However, there were no significant performance benefits.

"Catching" necessary and not very tags and attributes conducted manually. I took one page of each type and looked with my eyes, which elements I needed, which did not. Special thanks to the developers - everything was clear and understandable. Similarly acted with the main links to Habr.

After processing HTML, a separate object of the corresponding type is created, be it a post, a question or an event, which are subsequently written into the corresponding table with the key = id. The tables are made static and are available to the entire application via DataManager , a singleton class. In memory, they are saved using the java.io package and Context.getDir () .

For gourmets: I think that the Android developers, having read the previous lines, will grind meaningfully and think to themselves, something like "me too ...". The fact is that it is not necessary (and I already understood it) to use java.io in this case, and naturally the database is better. I caught a bunch of errors associated with just errors in reading and writing, because in Java objects are passed by reference, which need to be constantly updated, which of course can be trivially forgot to do. Well, of course, the speed lost to checks and updates is not particularly pleasing and adds speed to the application, and the decrease in this case is linear and depends on the number of memorized elements. I repent, made both easier and faster. Not the fact that it became faster and certainly did not become easier.
In general, will be redone. 100%.

With the display of the main text of the post (and any other material) there was a small hitch. Many applications use WebView for this purpose - a special view for web pages, a very powerful tool capable of loading material from the network, displaying an existing one. For me, his "lack" was power. For example, annotations are implemented as drop-down list items — using WebView would require more computational resources. Therefore, the choice fell on the TextView , which received a Html.fromHtml(…) object from the Html.fromHtml(…) method. It’s quite an effective way, which allows you to customize the display of the necessary tags (for example, “code” is my favorite) using ImageGetter and upload images from ImageGetter .

For gourmets: About pictures and ImageGetter . By default, loading in the background is not possible - the interface is blocked. It is easy and simple to solve - a background task, loading to the cache directory and updating TextView at the end.
For some reason, it is not without problems. Some pictures, mostly from external resources, not habrastorage, are loaded through one. Cause until catch failed. I'm working on it.

Buns

Edit mode allows you to specify materials of interest and download their contents. The program will record everything in memory, and later you can read them, for example, on the way to work in the subway.
It is possible to clean as a whole all the lists, at once the entire tape, favorites, and it is possible to select several separate ones.
If you do not want to spend money on traffic, and an interesting link is visible in the annotation to the post - a long press will help display a list of all available links. The same is valid for links in responses and comments.
Optionally, you can send a link to the post to your email. I do not need, in principle, it may be necessary for people with an account to comment on the post they liked later. In the future I will finish adding events to the calendar, while in the alpha version.
As for the display of the video - replace it with a regular link. The only thing that can not be imagined is Podcasts. Honestly, I haven't even tried yet. You may still have to go back to the WebView .
And, of course, there is a download without pictures for “very” mobile networks.

Interface

The main type of the program, as can be seen from the first screenshot, is tabs that repeat the three main and most interesting (I wrote for myself) the Habr section. Toward the end of the development process, the idea was to use a slide effect - switching between sections using a sliding gesture with your finger. However, the desire to more correspond to the original overpowered. In addition, the main view and it was by that time already so complex that it would require a decent time for rework.
The colors used in the program, as well as the location of the main elements - the date, the name of the author, the themes are also taken from the habr. Pictures and icons (except for those in the menu - they stuck them from sdk ) I drew myself. And yes ... I am not a designer (* sighs sadly *). But he tried, that really here.

For gourmets: Still, you should not make so rich with the details of the elements of lists. It brakes decently, even though the border is “well, it is impossible to use it” and was not crossed. I use. The solution to this problem may be, for example, a special adapter with caching, loading and deleting elements as they appear on the device screen. For example such.

Next - fewer letters, more screenshots:

image
image
image
image
image

Performance

In view of the features of the technology (Galaxy Tab without 3G, model P1010) I worked exclusively with Wi fi. Home line at 8 Mbit. In several cafes subjectively less. I didn't really feel the difference.
The main question is how much traffic is consumed. The answer is as much as access to Habra through a regular browser.
No less important is how fast everything works. The answer is fast enough. As already mentioned, for new posts, questions and events - processing takes place quickly, up to a maximum of 2-3 seconds, as measured. If a lot of comments, and even the material is voluminous - maybe more. Record on measurements - 10s. Well, yes? In defense I will say that it really was once. On average - less than 5 seconds.
The numbers are valid for displaying text, the pictures are loaded asynchronously and appear after loading, so I did not take into account the effect on reading.
List scrolling speed - ListView - is worth a special mention. It slows down, but tolerable. He worked with lists of up to 70 items, more like there was no need.

findings

The process of writing a program is awesome. Indeed, I liked writing very much, even, probably, more than actually using the application. Still, programmers are creative people, whatever they say. This is really euphoria - write a couple of lines and see the result.
I tried myself in the “theoretical” role of a freelancer. And I tell you, it does not gush. At home, one with a computer and a TV that creates the background, almost the whole day (I have already learned all the advertising). In the same office at least some kind of communication happens. Living, and. Dine - one, because everything is at work. And if you also come across an interesting problem, it may not let go of the weekend. I caught myself thinking about the problem with the number of characters in a line, even in the movies I caught. What is there - I explained to her father and friend! About intents, broadkasy. And he is my metallurgist, she is a biologist ...
I hope I managed to put into practice the knowledge that I gained at the university. Both practical and theoretical. And, of course, get new ones. And most importantly learn about some of their shortcomings. Like, for example, “I will do it to work” or write a bunch of useful methods, and then forget everything and still write the code again. I repent, poddramyvala and the overall organization of the process.

the main thing

The main thing that I understood for myself. Any project (not even the author) needs to be constantly inspired to grow and improve further. Anything can be a source of inspiration - the prospects are monetary, moral, competitive, it’s necessary to work, etc. When a project begins to fulfill its tasks, while satisfying everyone - both authors and users - its development stops. For Habrachtets, I am the author and only user at the moment, and everything in it suits me. But I would not want it to stop its development. That is why I told you about it.
So, you have the floor.

Source: https://habr.com/ru/post/129738/


All Articles