Monthly Archives: March 2017

React Communicating between Parent and Child Component

App.tsx

import * as React from 'react'
import {Home} from "./components/Home";
export interface AppProps { compiler: string; framework: string; }

class App extends React.Component<AppProps,undefined> {

    onGreeting()
    {
        alert("Hello World");
    }

    render() {
        return (
            <div>
                <Home greet={this.onGreeting}/>
            </div>
        );
    }
}

export default App;

components/Home.tsx

import * as React from 'react'
export interface HomeProps { greet?:any }

export class Home extends React.Component<HomeProps,undefined> {

    render() {
        return (
            <div>
                Hello World

                <div>
                    <button onClick={this.props.greet}>Say Hello</button>
                </div>
            </div>
        );
    }
}

References
https://github.com/mhdr/ReactJSSamples/tree/master/009

React Stateless Components

App.tsx

import * as React from 'react'
import {Home} from "./components/Home";
export interface AppProps { compiler: string; framework: string; }

class App extends React.Component<AppProps,undefined> {
    render() {
        return (
            <div>
                Hello World

                <Home Link="Home"/>
            </div>
        );
    }
}

export default App;

components/Home.tsx

import * as React from 'react'
export interface HomeProps {Link?:string }

export const Home = (props: HomeProps) => {
    return (
        <div>
            <a href="#">{props.Link}</a>
        </div>
    );
};

References
https://github.com/mhdr/ReactJSSamples/tree/master/008

Android Transition animations between activities

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Button buttonShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonShow= (Button) findViewById(R.id.buttonShow);

        buttonShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,AnimatedActivity.class);

                startActivity(intent);
                overridePendingTransition(R.anim.activity_back_in,R.anim.activity_back_out);
            }
        });
    }
}

 

AnimatedActivity.java

public class AnimatedActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animated);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.activity_back_in,R.anim.activity_back_out);
    }
}

 

References
https://github.com/mhdr/AndroidSamples/tree/master/059
https://developer.android.com/training/animation/cardflip.html

Android Working with Snackbar

build.gradle

compile 'com.android.support:appcompat-v7:25.3.0'

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Button buttonShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonShow= (Button) findViewById(R.id.buttonShow);

        buttonShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v,"Hello World",Snackbar.LENGTH_LONG).show();
            }
        });
    }
}

Snackbar Actions

public void showSnackbar(View view, String message, int duration)
{
    // Create snackbar
    final Snackbar snackbar = Snackbar.make(view, message, duration);

    // Set an action on it, and a handler
    snackbar.setAction("DISMISS", new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            snackbar.dismiss();
        }
    });

    snackbar.show();
}

References
https://github.com/mhdr/AndroidSamples/tree/master/058
https://spin.atomicobject.com/2017/07/10/android-snackbar-tutorial/

Android Working with Floating Action Button

build.gradle

compile 'com.android.support:design:25.3.0'

/res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mahmood.a057.MainActivity">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_margin="8dp"
        android:visibility="visible"
        app:elevation="8dp"
        app:fabSize="normal"
        app:backgroundTint="#FF0000"
        app:srcCompat="@drawable/ic_backup" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    FloatingActionButton floatingActionButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        floatingActionButton= (FloatingActionButton) findViewById(R.id.floatingActionButton);
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"Hello World",Toast.LENGTH_LONG).show();
            }
        });
    }
}

Refereneces
https://github.com/mhdr/AndroidSamples/tree/master/057
https://www.youtube.com/watch?v=nt31X9RaHQU&index=3&list=PLBA5zvAwnCrAG1ghh1XRsTN-Fqh3uKpli

Android Floating Label with TextInputLayout

build.gralde

compile 'com.android.support:design:25.3.0'

/res/layout/activity_main.xml

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

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/editTextUserName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:hint="UserName"/>

    </android.support.design.widget.TextInputLayout>


    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/editTextPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:hint="Password"/>
    </android.support.design.widget.TextInputLayout>


</LinearLayout>

References
https://github.com/mhdr/AndroidSamples/tree/master/056

Tuning the Linux kernel for better shadowsocks network throughput

add these lines to /etc/sysctl.conf :

# max open files
fs.file-max = 51200
# max read buffer
net.core.rmem_max = 67108864
# max write buffer
net.core.wmem_max = 67108864
# default read buffer
net.core.rmem_default = 65536
# default write buffer
net.core.wmem_default = 65536
# max processor input queue
net.core.netdev_max_backlog = 4096
# max backlog
net.core.somaxconn = 4096

# resist SYN flood attacks
net.ipv4.tcp_syncookies = 1
# reuse timewait sockets when safe
net.ipv4.tcp_tw_reuse = 1
# turn off fast timewait sockets recycling
net.ipv4.tcp_tw_recycle = 0
# short FIN timeout
net.ipv4.tcp_fin_timeout = 30
# short keepalive time
net.ipv4.tcp_keepalive_time = 1200
# outbound port range
net.ipv4.ip_local_port_range = 10000 65000
# max SYN backlog
net.ipv4.tcp_max_syn_backlog = 4096
# max timewait sockets held by system simultaneously
net.ipv4.tcp_max_tw_buckets = 5000
# turn on TCP Fast Open on both client and server side
net.ipv4.tcp_fastopen = 3
# TCP receive buffer
net.ipv4.tcp_rmem = 4096 87380 67108864
# TCP write buffer
net.ipv4.tcp_wmem = 4096 65536 67108864
# turn on path MTU discovery
net.ipv4.tcp_mtu_probing = 1

# for high-latency network
net.ipv4.tcp_congestion_control = hybla

# for low-latency network, use cubic instead
net.ipv4.tcp_congestion_control = cubic
sysctl -p /etc/sysctl.conf

References
https://shadowsocks.org/en/config/advanced.html
https://linode.com/docs/networking/vpn/create-a-socks5-proxy-server-with-shadowsocks-on-ubuntu-and-centos7/
https://maxlv.net/optimize-a-shadowsocks-server/

Tuning the Linux kernel for better network throughput

add these lines to /etc/sysctl.conf :

fs.file-max = 100000
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_syncookies = 1
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.core.rmem_default=65536
net.core.wmem_default=65536
net.ipv4.route.flush=1

and then :

sysctl -p /etc/sysctl.conf

References
http://www.techrepublic.com/blog/linux-and-open-source/tuning-the-linux-kernel-for-more-aggressive-network-throughput/
https://wwwx.cs.unc.edu/~sparkst/howto/network_tuning.php
https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Web_Platform/5/html/Administration_And_Configuration_Guide/jgroups-perf-udpbuffer.html
http://www.nateware.com/linux-network-tuning-for-2013.html
http://www.slashroot.in/linux-network-tcp-performance-tuning-sysctl

Android Animating ListView with LayoutTransition

MainActivity.java

public class MainActivity extends AppCompatActivity {

    ArrayList<String> listViewItems = new ArrayList<>();
    ArrayAdapter<String> adapter;
    int count = 0;

    Button buttonAdd;
    Button buttonDelete;
    ListView listView;

    LayoutTransition itemLayoutTransition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listViewItems);

        buttonAdd = (Button) findViewById(R.id.buttonAdd);
        buttonDelete = (Button) findViewById(R.id.buttonDelete);
        listView = (ListView) findViewById(R.id.listView);

        Animator scaleUp = ObjectAnimator.ofPropertyValuesHolder((Object) null, PropertyValuesHolder.ofFloat(View.SCALE_X, 0, 1), PropertyValuesHolder.ofFloat(View.SCALE_Y, 0, 1));
        final Animator scaleDown = ObjectAnimator.ofPropertyValuesHolder((Object) null, PropertyValuesHolder.ofFloat(View.SCALE_X, 1, 0), PropertyValuesHolder.ofFloat(View.SCALE_Y, 1, 0));

        itemLayoutTransition = new LayoutTransition();

        itemLayoutTransition.enableTransitionType(LayoutTransition.CHANGING);

        //itemLayoutTransition.setAnimateParentHierarchy(false);

        //itemLayoutTransition.disableTransitionType(LayoutTransition.CHANGE_APPEARING);
        //itemLayoutTransition.disableTransitionType(LayoutTransition.CHANGE_DISAPPEARING);
        //itemLayoutTransition.disableTransitionType(LayoutTransition.APPEARING);
        //itemLayoutTransition.disableTransitionType(LayoutTransition.DISAPPEARING);


/*        itemLayoutTransition.setAnimator(LayoutTransition.CHANGE_APPEARING, scaleUp);
        itemLayoutTransition.setDuration(LayoutTransition.CHANGE_APPEARING,1000);
        itemLayoutTransition.setInterpolator(LayoutTransition.CHANGE_APPEARING,new OvershootInterpolator());*/

        itemLayoutTransition.setAnimator(LayoutTransition.APPEARING, scaleUp);
        itemLayoutTransition.setDuration(LayoutTransition.APPEARING,1000);
        itemLayoutTransition.setInterpolator(LayoutTransition.APPEARING,new OvershootInterpolator());

/*        itemLayoutTransition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING, scaleDown);
        itemLayoutTransition.setDuration(LayoutTransition.CHANGE_DISAPPEARING,1000);
        itemLayoutTransition.setInterpolator(LayoutTransition.CHANGE_DISAPPEARING,new OvershootInterpolator());*/

        itemLayoutTransition.setAnimator(LayoutTransition.DISAPPEARING, scaleDown);
        itemLayoutTransition.setDuration(LayoutTransition.DISAPPEARING,1000);
        itemLayoutTransition.setInterpolator(LayoutTransition.DISAPPEARING,new OvershootInterpolator());

        listView.setLayoutTransition(itemLayoutTransition);

        itemLayoutTransition.addTransitionListener(transitionListener);
        listView.setAdapter(adapter);

        buttonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count++;

                String item = String.format("Item %d", count);
                listViewItems.add(item);

                adapter.notifyDataSetChanged();
            }
        });

        buttonDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listViewItems.size() > 0) {
                    itemLayoutTransition.removeChild(listView, listView.getChildAt(0));
                }
            }
        });
    }

    LayoutTransition.TransitionListener transitionListener=new LayoutTransition.TransitionListener() {
        @Override
        public void startTransition(LayoutTransition transition, ViewGroup container, View view, int transitionType) {
            listView.setHasTransientState(true);
        }

        @Override
        public void endTransition(LayoutTransition transition, ViewGroup container, View view, int transitionType) {
            if (transitionType == LayoutTransition.DISAPPEARING) {
                if (listViewItems.size() > 0) {
                    listViewItems.remove(0);
                    adapter.notifyDataSetChanged();

                    listView.getChildAt(0).setScaleX(1f);
                    listView.getChildAt(0).setScaleY(1f);
                }
            }

            listView.setHasTransientState(false);
        }
    };
}

References
https://github.com/mhdr/AndroidSamples/tree/master/055