Translate

Monday, September 26, 2016

Android: Get a List of Local Address in Device

This code snippet basically collects an array of Local Addresses (Inet4Address).

We use instaceof Inet4Address to avoid duplicates with IPv4 ang IPv6.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public static Inet4Address[] getLocalAddrasses(ConnectivityManager connManager) throws SocketException {
        ArrayList<Inet4Address> results = new ArrayList<Inet4Address>(1);
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface iface = interfaces.nextElement();
            if (!iface.isUp() || iface.isLoopback() || !iface.supportsMulticast()) {
                continue;
            }
            Enumeration<InetAddress> addresses = iface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress addr = addresses.nextElement();
                if (addr instanceof Inet4Address) {
                    results.add((Inet4Address) addr);
                }
            }
        }
        return results.toArray(new Inet4Address[results.size()]);
    }

Android: Restrict taking screenshots!

Somehow you wanted to block taking a snapshot on a certain application to disallow users share critical information(s).

I've seen this feature in some Online Banking and Finance apps. I got a little curious about it. So here is a simple code snippet;

1
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
Note: Make sure you call this before the setContentView inside onCreate.

Android: Blocking swipe-to-refresh on minimized AppBar

If you are designing an User-Interface that is using a CollapsingToolbarLayout and SwipeReshLayout, you may notice discomfort when flipping down the content, seen in CollapsingToolbarLayout that immediately trigger swipe-to-refresh.

Here I'm sharing a code below.


 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
/**
 * Block SwipeRefreshLayout in collapsed and idle state.
 */
public class AppBarRefreshAdjustListener implements AppBarLayout.OnOffsetChangedListener {

    private static final long BLOCK_DELAY = 1000L;

    private final SwipeRefreshLayout mSwipeRefreshLayout;
    private int mVerticalOffset;
    private Handler mHandler = new Handler();

    public AppBarRefreshAdjustListener(SwipeRefreshLayout refreshLayout) {
        mSwipeRefreshLayout = refreshLayout;
        handleOffsetChanged(mVerticalOffset);
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        handleOffsetChanged(verticalOffset);
    }

    private void handleOffsetChanged(int verticalOffset) {
        if (verticalOffset == 0) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler.postDelayed(() -> {
                mSwipeRefreshLayout.setEnabled(mVerticalOffset == 0);
            }, BLOCK_DELAY);
        } else {
            mSwipeRefreshLayout.setEnabled(false);
        }
        mVerticalOffset = verticalOffset;
    }
}

This class solves the problem - blocking SwipeRefreshLayout on clotting time / AppBar deployment:

1
appBarLayout addOnOffsetChangedListener (new AppBarRefreshAdjustListener (swipeRefreshLayout) );

Update Vodafone HG553 Router to OpenWRT – Barrier Breaker 14.07

Upgrade your Vodafone HG553 to OpenWRT – Barrier Breaker 14.07:
Why OpenWrt? We can do a lot of things with OpenWRT, Like Share your Hardisk via wifi, torrent downloader, EasyHotSpot Login, etc…
Here are the Step you need to do:
1. Download the Firmware from this link:
2. Do a Clear Update with 30 second Reset procedure, here is the step:
-Turn Off the Router by pressing the switch behind the Router.
-Stick the little hole which containing a button, behind the Router under the Reset label. Keep Stick it then turn on the Router.
-Release the Stick after 30 seconds from the Router being turn on. The Indicator lamp will remain blue.
-Connect the Lan Cable Port 1 to the PC.
-Now, Open your pc and go to the network settings, set your IP address for the Lan card to: 192.168.1.2, subnet mask: 255.255.255.0, and Default Gateway: 192.168.1.1
-Open Internet Browser (Firefox, Chrome or IE), type in the address bar: //192.168.1.1
-Click “CHOOSE FILE”, and choose the firmware that we already download in step 1.
-Click “Upgrade”. Wait until the process done, it is about 2 minutes.
3. After the process done, there will be 2 indicator lamps in blue appear in the Router. And when you type //192.168.1.1, you will get the Login page of OpenWRT
4. Login with user: root and password: admin
Congratulation, your Router upgraded to OpenWRT – Barrier Breaker 14.07

Gain Access to your OpenWRT Router


The first step after we Upgrade our router to OpenWRT, is to gain access to the it. In this tutorial, today I will teach you how to access it using even with ethernet/LAN Cable or Wifi.

Here are the steps:

1. Download Winscp and Putty. Or JuiceSSH for Android OS.

2. Connect the LAN Cable, from the Router to the PC.

3. Open Browser and type 192.168.1.1 you can try 192.168.0.1 if it is failed. Then login.

4. In the upper click Network -> Wifi.

5. In the second row of Wireless Overview, choose enable (this is to activate the Router’s Wifi).

6. Don’t forget to setup the Wifi Password, so that other people cannot access. You can do this by selecting the Security Tab in the below section. If you already set it up, press “Save & Apply”.

7. Now, in the upper section, click Network -> Interface. If there is no LAN interface, then click Add Interface. Name the interface with “LAN”, then set the Protocol to “static Address”. Checklist “Create a bridge over multiple interfaces”, then checklick “Cover the following interfaces”. Checklist “VLAN interface: ‘eth0.1 (lan)'” and “Wireless Network: Master ‘OpenWrt (lan)'” then press Submit. Insert ipv4 addess: 192.168.1.44 (you can change 44 with others, but make sure it will not used by other router if you will use it pairing with other router), subnet mask: 255.255.255.0 then ipv4 gateway: 192.168.1.1. Click Save & Apply.

8. In this step, your Router now can be access using LAN Cable or Wifi. Connect your device through LAN Cable to port 1 or through Wifi.

9. pen Putty or JuiceSSH and Set the ip address 192.168.1.44 (look at step 7, use that ip), port 23, Connection: telnet. Save Configuration for future usage. Then Click Open.

10. If it success then will appear “root@openwrt:”

11. Now we set openwrt to be access via SSH. Type passwd in the commandline and press Enter, now type the password you want for the SSH (it will not show asterixs or character you type) press Enter then type the password again for confirmation then press Enter.

12. Now you router cannot be access via telnet, but we can access via SSH or SCP.

13. If you’re using PC, run WinSCP. If you’re using Android, re-run JuiceSSH. Create new Configuration with ip address 192.168.1.44 (make sure this is the ipv4 address that you set on step 7). Port 22, username root, and password is the password that you set in putty. Connection type: SCP if you’re using WinSCP, SSH if you use JuiceSSH.Save the configuration and then Open.

14. If it success then the application will alert you about SSH Fingerprint. Choose Update or Open. If it prompt for username and password, fill it again. If it success it will show “root@openwrt:” and in WinScp it will show you the folders. Congratulation, now you can access your Router Via SSH / SCP for transfer files. Note: in JuiceSSH, you need to do “cd /” after you login, then you can ‘ls’.

StarPrint + OpenWRT + Printer Sharing


Before i start the tutorial, i want to say thanks to Hamam Jafar from OpenWrt Indonesia Facebook group, to point to this great android application called “Star Print”.
Now lets start.

First of all, i want to tell you the device that i use:

1. Samsung galaxy Note II with DN4 v2.1.
2. Vodafone HG553 with OpenWrt Barrier Breaker version 14.07.
3. CANON Pixma MP237.

Here are the steps:

1. Connect to your modem via ssh or putty. Type in “opkg update” to update the list of packages. Then you need to install the usb driver, i’m using this command “opkg install luci-app-p910nd kmod-usb-printer p910nd”.

2. After the installation complete and there is no error, now connect the printer usb port to the router, turn it on. Back to putty, and type “dmesg”, if you find something similar like this:

printer.c: usblp0: USB Bidirectional printer 2 if 0 alt 0 proto 2 vid 0x04A9 pid 0x1094 usb.c: USB disconnect on ice 01:02.0-1 address 2 hub.c: new USB ice 01:02.0-1, assigned address 3 printer.c: usblp1: USB Bidirectional printer 3 if 0 alt 0 proto 2 vid 0x04A9 pid 0x1094

then your printer already installed.

3. Now we go to luci interface. For me it is 192.168.1.1 in the browser. Then login, choose the Services -> p910nd – Printer Server. Check the enable checklist then in the interface, choose LAN. Port: 9100. Checklist the bidirectional mode. Then press Save & Apply.

4. Now. Go to System -> Startup. Look at p910nd. To make sure the service is started. Press the start button beside the p910nd.
5. Now. To make sure the printer service is really ready. Go to putty again then type “netstat -an”

If you see;
Tcp 0 *.*.*.*:9100
Then the service is running.

6. Open your android device. Install Star Print here: StarPrint

Then open it. Slide it to Print Preview. On the printer option, click on “[No printer Selected]”. Press the Shared Printer in the middle bottom side. Type the Printer name to anything you like. Click on the IPP in the type options, Change it to “AppSocket(JetDirect)”. Type the Host to 192.168.1.1 and the port to 9100. Type the Shared name (i type mp237). Next click the select driver in the driver Options. Choose the manufacture of the printer. Then choose the type of printer that you got. After that press Ok.

7. Now connect your android device to the router via Wifi. Then open again the Star Print application. Choose the document you want to print. Then press print.