📜 ⬆️ ⬇️

Quickly create a JavaScript info widget

Modern mobile phones have large screens. Naturally, the desire to use part of the site to display some important relevant information. Android even has a special entity for this, which is called AppWidget (widget). Using widgets directly on the desktop of your phone or tablet, in addition to application icons, you can display the weather forecast, phone account balance, upcoming events, and selected contacts — yes, almost anything, since the data for the widgets are provided by applications.

I really like this Android feature (by the way, it's interesting, but on other platforms is there something like that?). I use it from the very beginning. That's just there is a certain category of programs that provide widgets, in which I missed something all the time. I now mean programs that display the current balance of the phone on the screen. There are many such programs. To get the current balance, some make a USSD or SMS request, while others use an automated service to manage the subscriber with their tariff via the Internet, such as CCIS at MTS or service guide at Megafon. I like the second variant more, because through the Internet service one can get information by several different phones, and by SMS or USSD only by the phone from which the request is made. Well, for tablets without telephony, the first method is generally closed.

In general, I tried many such programs, some were better than others, but they didn’t always show what I wanted (until recently I used balance by ). For example, I wanted to see how much traffic was left on my phone, how much was on the modem. It's so simple - here it is this information, on the site! If information about the balance is taken from there, then why not take information about the traffic? If only a regular expression could be written for this, I would have done it a long time ago. But you just had to wait for the program update. And the updates came out, a bunch of other operators were updated, but the traffic indicator I needed did not appear. In addition, I did not find a program that would maintain the dynamics of changes in the balance so that you can quickly see at what point the extra money suddenly began to be written off. And with MT tariffing this may well happen.
')
Next is my solution to the problem ...

Since I wanted to explore Android, I decided to write my own such program. But immediately get rid of the main, in my opinion, lack of existing programs. The entire set of supported cellular operators and other accounts, the balance in which you have to look, must be removed from the program in the plug-ins. And these modules should not be written in complex Java, but in plain JavaScript. Already in the development process, I realized that this opens up more opportunities. You can absolutely not be limited to mobile operators and Internet providers. You can display almost any information that is presented in numerical or text form. For example, currency rates and tracking of EMS mailings are very simple.

I called the program AnyBalance . And plug-ins to it AnyBalance providers . I would like AnyBalance providers to be open source, so that those who are not satisfied with the functionality of any provider can easily add it. Well, or ask about this friend who knows Javascript. For this purpose, I created a project on the Google code , and placed the documentation and examples there. I urge the community to look and appreciate my work. And, if possible, add your operator to it :) When more AnyBalance providers are written, AnyBalance will be able to search and add providers directly from the repository. The program itself is available from the Android Market . There are several operating providers in it, such as Megafon Moscow, Currency rates , EMS tracking, and a couple of providers to get the balance of game currency from games. Sources of all providers in the repository .

A lot of information about how providers are made is written on the project page on google code . I'll tell you briefly here.

Each provider declares a set of counters that it represents, and also contains Javascript, which receives these counters. When updating the widget, AnyBalance creates a WebView in which the provider’s main function is launched. Results (counters) are saved by AnyBalance for displaying on widgets, plotting and statistics. Actually, the provider itself is extremely simple. Its function is reduced only to obtaining the actual values ​​of the counters.

Extremely simple classic example - Hello world .

But how the data should look like is determined by the user. First, the user can choose which counters to show and which not. Secondly, it selects the widget view. Now in AnyBalance there are three types of widgets - two types for numerical values ​​and one for text values. The user can choose which counters to show in which fields of the widget.

AnyBalance has the ability to show graphs and statistics, which I lacked in other programs. Here is an example of what it looks like.



Currently I have completely switched to AnyBalance. I hope it will be about the taste of you and help, in addition to displaying the balance, to solve a long overdue problem of displaying, for example, the amount of gold in a pack or the presence of a certain product on the Internet right on the phone screen :) I will also be grateful to everyone who adds their providers project or refine existing.

Also concerned about the issue of security, is it possible to write malicious providers :). All account settings are stored in SharedPreferences, so in theory one provider can access the settings of the other. I still do not know how to fix it.

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


All Articles