📜 ⬆️ ⬇️

Katya, Go, Dcoin and Android



The continuation of the very story.

The first part is here , the second is here , the third is here .

4.5 years ago I had the imprudence to start writing my cryptocurrency in a completely inappropriate language for this business - in PHP. As a result, of course, I wrote (I am stubborn), but it turned out to be a crutch on a crutch and that it worked at all was just some kind of magic.
')
I just want to warn you, I’m a self-taught programmer and I write code, to say the least, imperfectly.

It all started with the fact that I broke up with a girl named Katya and on the same day (April 4, 2015) decided to study Go and rewrite my cryptocurrency. I can’t write about Katya not under the spoilers; Habr is still for IT-related articles, and not for love stories and harsh IT people who are interested in Go, can simply not pay attention to the spoilers “about Katya”.

A total of 8 months: the application runs on Win ( 64/32 ), OSX (64/32), Linux ( 64/32 ), FreeBSD ( 64/32 ), Android , IOS .

General code ~ 73k lines, code for different OS somewhere a few hundred lines.

40k - processing / generation of blocks / tr-s, 17.5k - controllers for the interface, 15.5k - templates.

PostgreSQL, SQLite, MySQL are supported.

Those who will test my creation, I warn you - there may be bugs, and if you have time, drop a line about them, please, at darwin@dcoin.club or in a personal on Habré. Suggestions and advice are also welcome.

In the first three parts, I talked about how a web server functions in dcoin, about html / template , databases, smooth application termination, encryption and parsing of blocks.

In this article I will tell you about working with Go on Android.

Start


It's funny to say, but my first android came to me this summer. Before that, I simply could not find a reason to replace my Nokia 1200. I bought a cheap ZTE for 3000 rubles with 512 memory and 2 cores. For testing the very thing. And you can also call from it.

I wanted to do a compilation through github.com/golang/mobile in apk. Looked at the manual, everything seems simple. Almost immediately it turned out to compile the binary and run it under the root on the android. I was glad that everything was going like clockwork and it seemed that in a couple of days I would have apk, running which I would see Dcoin.

Apk was not difficult to assemble. In principle, all I needed was to automatically open 127.0.0.1 : 8089 in the browser. This is where I stalled. A few days googled and experimented, and all that could be achieved is the drawing of a picture in which I ask the user to open the desired host in the browser.

I decided to go through aar. Those. add it as a library to android studio. And by means of the studio already open a browser or webview. But sqlite stubbornly did not want to compile, it turned out that there was no error in the C compiler and there was no solution to the problem at the time (by the way, there are already).

About Katya
(Ending from the previous part ): I wrote to her in VK, said that she had forgotten the bodies at home, and now she had a friend. I wrote that I would wait for her. Then he received "Do not call and do not write me more !!!!". He didn’t ask any questions, called the next apartment, asked me to send the flowers to Kate when she was home. I came home, a few hours later I decided to rewrite Dcoin to Go.

A couple of days later I wrote to her that I was leaving for several months with my head in my project and asked me not to bother me over nothing.

A week later, Kati came sms-ka hello. Well, how are you? ”. I did not answer. A week later, another "hello. how are you?". I did not answer again.


GoNativeActivity


At some point it became obvious that without this magic file nothing will come of it. I started experimenting, made a few changes, generate apk and nothing changes. After a couple of days I could not stand it and decided to write to one of the gomobile developers, the answer came rather quickly. It turns out that after the changes in GoNativeActivity go go github.com/c-darwin/mobile/cmd/gomobile should be called to generate the .dex file and only after that go install github.com/c-darwin/mobile/cmd/gomobile.

Having learned to edit GoNativeActivity, I got a huge opportunity. It was necessary only to be able to write in Java. And I did not know how, and now I do not know how. But something still could do. Here is my GoNativeActivity. A little later, I figured out how to create my AndroidManifest.xml, which gave me even more options. In the end, instead of working in the browser, I was able to get to work in WebView, here is my webview implementation.

About Katya
About a week later, she wrote that she urgently needed 7 tr. in debt, because she does not have enough to pay for the apartment. I answered "OK". The next day she came to my house.

Notifications


I wanted to make a notification when money came in or an incoming request to exchange coins for Fiat arrived. After googling and exploring stackoverflow, you get this code:

public void notif(String title, String text) { Intent intent = new Intent("org.golang.app.MainActivity"); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); //     ,   mBuilder.setSmallIcon(R.drawable.icon); mBuilder.setContentTitle(title); mBuilder.setContentText(text); Intent resultIntent = new Intent(this, MainActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(MainActivity.class); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // notificationID allows you to update the notification later on. mNotificationManager.notify(2, mBuilder.build()); } 

It remains only to understand how this notif tugging out of Go. Here I am stuck for another week. It turned out that you need to use a certain beast called JNI . It turns out something like this: Go calls C, which starts the Java machine and pulls my notif through it. Horror. Especially considering that in C, I, like in Java, are almost complete zero.

In short, after much agony, I could still write working code and even understand what was going on in it:

 package notif /* #cgo LDFLAGS: -llog -landroid #include <android/log.h> #include <jni.h> #include <stdlib.h> #define LOG_FATAL(...) __android_log_print(ANDROID_LOG_FATAL, "Go/fatal", __VA_ARGS__) void notif_manager_init(void* java_vm, void* ctx, char* title, char* text) { JavaVM* vm = (JavaVM*)(java_vm); JNIEnv* env; int err; int attached = 0; err = (*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6); if (err != JNI_OK) { if (err == JNI_EDETACHED) { //   JM if ((*vm)->AttachCurrentThread(vm, &env, 0) != 0) { LOG_FATAL("cannot attach JVM"); } attached = 1; } else { LOG_FATAL("GetEnv unexpected error: %d", err); } } //   jstring  title jstring javaTitle = (jstring)(*env)->NewStringUTF(env, (const char *)title); //   jstring  text jstring javaText = (jstring)(*env)->NewStringUTF(env, (const char *)text); //  ,     jclass cls = (*env)->GetObjectClass(env, ctx); //  .Ljava/lang/String;Ljava/lang/String -     V(Void) -  jmethodID nJmethodID = (*env)->GetMethodID(env, cls, "notif", "(Ljava/lang/String;Ljava/lang/String;)V"); //    (jstring)(*env)->CallObjectMethod(env, ctx, nJmethodID, javaTitle, javaText); if (attached) { //   JM (*vm)->DetachCurrentThread(vm); } } */ import "C" import ( "github.com/c-darwin/mobile/internal/mobileinit" ) func SendNotif(title string, text string) { ctx := mobileinit.Context{} C.notif_manager_init(ctx.JavaVM(), ctx.AndroidContext(), C.CString(title), C.CString(text)) } 

By analogy, I got a package get_files_dir.go that gets the working directory.

About Katya
I do not know what she had in her head, she probably thought that I would pester her. She was kind of weird all evening. I was cold and tried to avoid any contact with her. We ate, drank tea. Katya said she needed to go home. I called a taxi.
When accompanied her to the taxi asked:

- We are not together right? Those. I can do what I want?
She: - In the sense of "what I want"? No, we are together (moves up to me)
I: - Well, we didn’t have a first date, we have not started everything from the beginning.
She: - So it was just that.
I: - What a date this is, you just came for the money.

We were already at the door of the taxi, I got her down, paid the driver, she left.

On Go, we got this code to call the notification:

 // +build android package sendnotif import ( "github.com/c-darwin/mobile/notif" ) func SendMobileNotification(title, text string) { notif.SendNotif(title, text) } 

About Katya
In the morning the phone rang, it was Katya. I did not pick up the phone.

Service


I noticed that the web server was constantly falling. It was impossible to work with a wallet. Googled, I realized that we need to do the service. This time there were not so many difficulties, the source is here . ShortcutIcon () creates an icon on the desktop.

About Katya
The next day, she sent a sms-ku "hello. I called yesterday, you did not pick up. " I did not respond.

Access to the icon


When notifications appear, you need to specify an icon. I had to figure out how to work with android resources. Approximately realized that you need to generate R. jar and connect it when generating a dex-file.

Googled how to generate java files, this command came up:

 aapt package -v -f -J /home/z/go-projects/src/github.com/c-darwin/dcoin-go/ -S /home/z/go-projects/src/github.com/c-darwin/dcoin-go/res/ -M /home/z/go-projects/src/github.com/c-darwin/dcoin-go/AndroidManifest.xml -I /home/z/android-sdk-linux/platforms/android-22/android.jar 

The resulting R.java is placed in R / org / golang / app /:

 mv R.java /home/z/go-projects/src/github.com/c-darwin/dcoin-go/R/org/golang/app/ 

And generate R.jar:

 cd R && jar cfv /home/z/go-projects/src/github.com/c-darwin/dcoin-go/R.jar 

Generate unsigned apk:

 aapt package -v -f -J /home/z/go-projects/src/github.com/c-darwin/dcoin-go/ -S /home/z/go-projects/src/github.com/c-darwin/dcoin-go/res/ -M /home/z/go-projects/src/github.com/c-darwin/dcoin-go/AndroidManifest.xml -I /home/z/android-sdk-linux/platforms/android-22/android.jar -F unsigned.apk 

About Katya
A week later, she wrote that she received a ZP. and wants to throw money on my card. I did not answer anything. A few days later she wrote “hello. how are you?". I ignored again.

Pull into the root of resources.arsc:

 unzip unsigned.apk -d apk && mv apk/resources.arsc . 

Next you need to fix gendex.go from gomobile:

 cmd := exec.Command( "javac", "-source", "1.7", "-target", "1.7", "-bootclasspath", platform+"/android.jar", "-classpath", "/home/z/go-projects/src/github.com/c-darwin/dcoin-go/R.jar:"+androidHome+"/extras/android/m2repository/com/android/support/support-v4/22.2.1/support-v4-22.2.1-sources.jar", "-d", tmpdir+"/work", ) 

About Katya
About a week later, I finally ran Dcoin on the nodes and saw how the generation of the blocks was going on and how they were flying between the nodes, filling the databases. It seemed to me that this can be considered the end of my temporary retreat and I wrote to Katya.

We generate new Dex:

 ANDROID_HOME=/home/z/android-sdk-linux go generate github.com/c-darwin/mobile/cmd/gomobile/ 

Then the gomobile binary itself:

 go install github.com/c-darwin/mobile/cmd/gomobile/ 

And finally we get our apk:

 CGO_ENABLED=1 GOOS=android ANDROID_HOME=/home/z/android-sdk-linux gomobile build -v github.com/c-darwin/dcoin-go 

As a result, I got this bash script for generating apk:

 ./bindata.sh echo "######## generate R.java ########" aapt package -v -f -J /home/z/go-projects/src/github.com/c-darwin/dcoin-go/ -S /home/z/go-projects/src/github.com/c-darwin/dcoin-go/res/ -M /home/z/go-projects/src/github.com/c-darwin/dcoin-go/AndroidManifest.xml -I /home/z/android-sdk-linux/platforms/android-22/android.jar mv R.java /home/z/go-projects/src/github.com/c-darwin/dcoin-go/R/org/golang/app/ echo "######## generate R.jar ########" cd R jar cfv /home/z/go-projects/src/github.com/c-darwin/dcoin-go/R.jar . cd ../ echo "######## generate unsigned.apk ########" aapt package -v -f -J /home/z/go-projects/src/github.com/c-darwin/dcoin-go/ -S /home/z/go-projects/src/github.com/c-darwin/dcoin-go/res/ -M /home/z/go-projects/src/github.com/c-darwin/dcoin-go/AndroidManifest.xml -I /home/z/android-sdk-linux/platforms/android-22/android.jar -F unsigned.apk echo "######## extract resources.arsc ########" unzip unsigned.apk -d apk mv apk/resources.arsc . rm -rf apk unsigned.apk ANDROID_HOME=/home/z/android-sdk-linux go generate github.com/c-darwin/mobile/cmd/gomobile/ go install github.com/c-darwin/mobile/cmd/gomobile/ CGO_ENABLED=1 GOOS=android ANDROID_HOME=/home/z/android-sdk-linux gomobile build -v github.com/c-darwin/dcoin-go 

About Katya
She responded very positively to the fact that I came in contact with her, agreed to meet. Then she said that everything was bad for her, the girlfriend with whom she rented the apartment moved out and that she was crying every day because of stress, she also decided to quit. I decided that when we met, I would suggest that she move to live with me and not bathe about anything.

Conclusion


In the next, final article I will tell you about gomobile and IOS. And finally, there will be a final about Katya.

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


All Articles