Sunday, July 30, 2017

Electron - Passing Data On Window Creation

Passing data from Main to another window can be achieved by using ipcRenderer. That feature is surely good if we need 2-way communication between Main and its rendered window.

But, what if we only to do it on its initiation? Isn't there any simpler way to do it? 

Yes, there it is. 

I can't find it in its documentation, but I found it accidentally. 

Actually, we can pass any data on BrowserWindow creation process. 

In my case, I need to pass an URL to be loaded on the webview. So, here's mine :



And here's how I got that URL on the page.html I loaded


My conclusion, even ipcRenderer is good for communication between Main and Rendered process, but if it's only on window initiation, we can do pass it on BrowserWindow creation. It'll be simpler and more effective in my opinion.


If you have a more effective way, comment this post.

Nothing is perfect, right?



Monday, July 18, 2016

Android Google Map Multiple Line Snippet on Info Window


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

 @Override
 public View getInfoWindow(Marker arg0) {
  return null;
 }

 @Override
 public View getInfoContents(Marker marker) {

  Context context = getApplicationContext(); //or getActivity(), YourActivity.this, etc.

  LinearLayout info = new LinearLayout(context);
  info.setOrientation(LinearLayout.VERTICAL);

  TextView title = new TextView(context);
  title.setTextColor(Color.BLACK);
  title.setGravity(Gravity.CENTER);
  title.setTypeface(null, Typeface.BOLD);
  title.setText(marker.getTitle());

  TextView snippet = new TextView(context);
  snippet.setTextColor(Color.GRAY);
  snippet.setText(marker.getSnippet());

  info.addView(title);
  info.addView(snippet);

  return info;
 }
});

Friday, July 1, 2016

Android AutoCompleteTextView Geocoder

main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <AutoCompleteTextView
        android:id="@+id/text1"
        android:layout_width="280dp"
        android:layout_height="wrap_content"        
        android:textSize="15dp"/>

</LinearLayout>


MainActivity.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    AutoCompleteTextView text1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
  
  text1 = (AutoCompleteTextView ) findViewById(R.id.text1);
        text1.setThreshold(3);
        text1.setAdapter(new CustomAdapter(this,android.R.layout.simple_list_item_1));
    }

    private ArrayList<String> getAddressInfo(String locationName) {
        ArrayList<String> list = new ArrayList<>();
        Geocoder geocoder = new Geocoder(this, Locale.getDefault());        
        try {
            List<Address> a = geocoder.getFromLocationName(locationName, 5);
            for (int i = 0; i < a.size(); i++) {
                String city = a.get(i).getLocality();
                String country = a.get(i).getCountryName();
                String address = a.get(i).getAddressLine(0);
                list.add(address +(city!=null?", " + city :"") +(country!=null? ", " + country:"") );
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    class CustomAdapter extends ArrayAdapter implements Filterable {
        private ArrayList<String> resultList;

        public CustomAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }

        @Override
        public int getCount() {
            return resultList.size();
        }

        @Override
        public String getItem(int index) {
            return resultList.get(index);
        }

        @Override
        public Filter getFilter() {
            Filter filter = new Filter() {
                @Override
                protected FilterResults performFiltering(CharSequence constraint) {
                    FilterResults filterResults = new FilterResults();
                    if (constraint != null) {
                        // Retrieve the autocomplete results.
                        resultList = getAddressInfo(constraint.toString());

                        // Assign the data to the FilterResults
                        filterResults.values = resultList;
                        filterResults.count = resultList.size();
                    }
                    return filterResults;
                }

                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
                    if (results != null && results.count > 0) {
                        notifyDataSetChanged();
                    } else {
                        notifyDataSetInvalidated();
                    }
                }
            };
            return filter;
        }
    }

Tuesday, May 3, 2016

Android Fast Unzip Programatically


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * @credit http://stackoverflow.com/a/21159437/1730484
 */
public class Decompress {
    private String _zipFile;
    private String _location;

    public interface DecompressListener {
        public void onFinish(String zipfile);
    }

    public Decompress(String zipFile, String location) {
        _zipFile = zipFile;
        _location = location;

        hanldeDirectory("");
    }

    public void unzip(DecompressListener listener) {

        try {
            FileInputStream inputStream = new FileInputStream(_zipFile);
            ZipInputStream zipStream = new ZipInputStream(inputStream);
            ZipEntry zEntry = null;
            while ((zEntry = zipStream.getNextEntry()) != null) {
                Log.d("Unzip", "Unzipping " + zEntry.getName() + " at "
                        + _location);

                if (zEntry.isDirectory()) {
                    hanldeDirectory(zEntry.getName());
                } else {
                    FileOutputStream fout = new FileOutputStream(
                            this._location + "/" + zEntry.getName());
                    BufferedOutputStream bufout = new BufferedOutputStream(fout);
                    byte[] buffer = new byte[1024];
                    int read = 0;
                    while ((read = zipStream.read(buffer)) != -1) {
                        bufout.write(buffer, 0, read);
                    }

                    zipStream.closeEntry();
                    bufout.close();
                    fout.close();
                }
            }
            zipStream.close();
            Log.d("Unzip", "Unzipping complete. path :  " + _location);
        } catch (Exception e) {
            Log.d("Unzip", "Unzipping failed");
            e.printStackTrace();
        } finally {
            if (listener != null)
                listener.onFinish(_zipFile);
        }

    }

    public void hanldeDirectory(String dir) {
        File f = new File(this._location + dir);
        if (!f.isDirectory()) {
            f.mkdirs();
        }
    }
}

Monday, April 25, 2016

Gradle Jar with Depedencies

jar {
  manifest { 
    attributes "Main-Class": "your.java.application.class.Main"
  }  

  from {
    configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
  }
}