Monthly Archives: January 2016

Speedup WebView for Android

http://stackoverflow.com/questions/32304237/android-webview-loading-data-performance-very-slow

 

Advertisements

Fast Touch Events for your Web App

Try the demo

Via – http://phonegap-tips.com/articles/fast-touch-event-handling-eliminate-click-delay.html

Mobile WebKits attempt to synthesize touch events into into click events. These same browser engines also have a double-tap-to-zoom function. This brings up the issue of distinguishing between a tap and double tap. What is a WebKit to do?

The answer is pretty simple. WebKit adds a small delay of 300 milliseconds after a single tap to see if you will tap a second time. If no second tap happens within 300 milliseconds, the gesture is interpreted as a single tap which is subsequently translated into a “click” event.

Great!

You already know that you can disable pinch and tap zooming in your app, provided you have designed your app to have nice, large buttons and text. Unfortunately, even after you do this, WebKit maintains the 300 millisecond delay after a single tap. To make matters worse, the delay is only triggered on fast taps. If you touchstart, pause briefly, then touchend you will experience no delay. Most users will tap fast enough to experience the delay some, if not most, of the time. This small delay can create a perceived slowness in your app, and therefore, it must be eliminated with extreme prejudice.

Libraries like fastclick will eliminate this delay for you, and will also take care of some tricky edge cases, but basic fast tap handling is pretty easy. The example code below demonstrates fast tap handling with the jQuery special event API, but the process will be similar with most other JavaScript event-handling libraries.

$.event.special.tap = {
  setup: function() {
    var self = this,
      $self = $(self);

    // Bind touch start
    $self.on('touchstart', function(startEvent) {
      // Save the target element of the start event
      var target = startEvent.target;

      // When a touch starts, bind a touch end handler exactly once,
      $self.one('touchend', function(endEvent) {
        // When the touch end event fires, check if the target of the
        // touch end is the same as the target of the start, and if
        // so, fire a click.
        if (target == endEvent.target) {
          $.event.simulate('tap', self, endEvent);
        }
      });
    });
  }
};

This is the basis for any sort of fast tap handling. In some cases, this may even be good enough. However, there are many ways to make this more robust.

Time Threshold

If a user starts a touch, holds, and then releases after one second, is that a tap? Maybe not, here’s how you can add a configurable time threshold to abort a tap event if the time between touchstart and touchend is too long.

$.event.special.tap = {
  // Abort tap if touch lasts longer than half a second
  timeThreshold: 500,
  setup: function() {
    var self = this,
      $self = $(self);

    // Bind touch start
    $self.on('touchstart', function(startEvent) {
      // Save the target element of the start event
      var target = startEvent.target,
        timeout;

      function removeTapHandler() {
        clearTimeout(timeout);
        $self.off('touchend', tapHandler);
      };

      function tapHandler(endEvent) {
        removeTapHandler();

        // When the touch end event fires, check if the target of the
        // touch end is the same as the target of the start, and if
        // so, fire a click.
        if (target == endEvent.target) {
          $.event.simulate('tap', self, endEvent);
        }
      };

      // Remove the tap handler if the timeout expires
      timeout = setTimeout(removeTapHandler, $.event.special.tap.timeThreshold);

      // When a touch starts, bind a touch end handler
      $self.on('touchend', tapHandler);
    });
  }
};

Distance Threshold

If the user starts a touch, moves 200 pixels to the left, then releases, is that a tap? Maybe not. Here’s how you can add a configurable distance threshold to abort a tap event if the user moves his or her finger too far.

$.event.special.tap = {
  // Abort tap if touch moves further than 10 pixels in any direction
  distanceThreshold: 10,
  // Abort tap if touch lasts longer than half a second
  timeThreshold: 500,
  setup: function() {
    var self = this,
      $self = $(self);

    // Bind touch start
    $self.on('touchstart', function(startEvent) {
      // Save the target element of the start event
      var target = startEvent.target,
        touchStart = startEvent.originalEvent.touches[0],
        startX = touchStart.pageX,
        startY = touchStart.pageY,
        threshold = $.event.special.tap.distanceThreshold,
        timeout;

      function removeTapHandler() {
        clearTimeout(timeout);
        $self.off('touchmove', moveHandler).off('touchend', tapHandler);
      };

      function tapHandler(endEvent) {
        removeTapHandler();

        // When the touch end event fires, check if the target of the
        // touch end is the same as the target of the start, and if
        // so, fire a click.
        if (target == endEvent.target) {
          $.event.simulate('tap', self, endEvent);
        }
      };

      // Remove tap and move handlers if the touch moves too far
      function moveHandler(moveEvent) {
        var touchMove = moveEvent.originalEvent.touches[0],
          moveX = touchMove.pageX,
          moveY = touchMove.pageY;

        if (Math.abs(moveX - startX) > threshold ||
            Math.abs(moveY - startY) > threshold) {
          removeTapHandler();
        }
      };

      // Remove the tap and move handlers if the timeout expires
      timeout = setTimeout(removeTapHandler, $.event.special.tap.timeThreshold);

      // When a touch starts, bind a touch end and touch move handler
      $self.on('touchmove', moveHandler).on('touchend', tapHandler);
    });
  }
};

Other considerations

Reading the fastclick source is the best way to understand all the nuances of fast click handling on touch devices. Fastclick will take care of some quirks in older mobile browsers, different requirements for certain input elements, as well as eliminating “phantom” clicks.

How To Install Node.js on a CentOS 7

via – https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-a-centos-7-server

 

Introduction

Node.js is a Javascript platform for server-side programming. It allows users to easily create networked applications that require backend functionality. By using Javascript as both the client and server language, development can be fast and consistent.

In this guide, we will show you a few different ways of getting Node.js installed on a CentOS 7 server so that you can get started. Most users will want to use the EPEL installation instructions or the NVM installation steps.

Install Node from Source

One way of acquiring Node.js is to obtain the source code and compile it yourself.

To do so, you should grab the source code from the project’s website. On the downloads page, right click on the “Source Code” link and click “Copy link address” or whatever similar option your browser gives you.

On your server, use wget and paste the link that you copied in order to download the archive file:

wget http://nodejs.org/dist/v0.10.30/node-v0.10.30.tar.gz

Extract the archive and move into the new directory by typing:

tar xzvf node-v* && cd node-v*

There are a few packages that we need to download from the CentOS repositories in order to compile the code. Use yum to get these now:

sudo yum install gcc gcc-c++

Now, we can configure and compile the software:

./configure
make

The compilation will take quite awhile. When it is finished, you can install the software onto your system by typing:

sudo make install

To check that the installation was successful, you can ask Node to display its version number:

node --version
v0.10.30

If you see the version number, then the installation was completed successfully.

Install a Package from the Node Site

Another option for installing Node.js on your server is to simply get the pre-built packages from the Node.js website and install them.

You can find the Linux binary packages here. Since CentOS 7 only comes in the 64-bit architecture, right click on the link under “Linux Binaries (.tar.gz)” labeled “64-bit”. Select “Copy link address” or whatever similar option your browser provides.

On your server, change to your home directory and use the wget utility to download the files. Paste the URL you just copied as the argument for the command:

cd ~
wget http://nodejs.org/dist/v0.10.30/node-v0.10.30-linux-x64.tar.gz

Note: Your version number in the URL is likely to be different than the one above. Use the address you copied from the Node.js site rather than the specific URL provided in this guide.

Next, we will extract the binary package into our system’s local package hierarchy with the tar command. The archive is packaged within a versioned directory, which we can get rid of by passing the --strip-components 1 option. We will specify the target directory of our command with the -C command:

sudo tar --strip-components 1 -xzvf node-v* -C /usr/local

This will install all of the components within the /usr/local branch of your system.

You can verify that the installation was successful by asking Node for its version number:

node --version
v0.10.30

The installation was successful and you can now begin using Node.js on your CentOS 7 server.

Install Node from the EPEL Repository

An alternative installation method uses the EPEL (Extra Packages for Enterprise Linux) repository that is available for CentOS and related distributions.

To gain access to the EPEL repo, you must modify the repo-list of your installation. Fortunately, we can reconfigure access to this repository by installing a package available in our current repos called epel-release.

sudo yum install epel-release

Now that you have access to the EPEL repository, you can install Node.js using your regular yumcommands:

sudo yum install nodejs

Once again, you can check that the installation was successful by asking Node to return its version number:

node --version
v0.10.30

Many people will also want access to npm to manage their Node packages. You can also get this from EPEL by typing:

sudo yum install npm

Install Node Using the Node Version Manager

Another way of installing Node.js that is particularly flexible is through NVM, the Node version manager. This piece of software allows you to install and maintain many different independent versions of Node.js, and their associated Node packages, at the same time.

To install NVM on your CentOS 7 machine, visit the project’s GitHub page. Copy the curl or wgetcommand from the README file that displays on the main page. This will point you towards the most recent version of the installation script.

Before piping the command through to bash, it is always a good idea to audit the script to make sure it isn’t doing anything you don’t agree with. You can do that by removing the | bash segment at the end of the curl command:

curl https://raw.githubusercontent.com/creationix/nvm/v0.13.1/install.sh

Take a look and make sure you are comfortable with the changes it is making. When you are satisfied, run the command again with | bash appended at the end. The URL you use will change depending on the latest version of NVM, but as of right now, the script can be downloaded and executed by typing:

curl https://raw.githubusercontent.com/creationix/nvm/v0.13.1/install.sh | bash

This will install the nvm script to your user account. To use it, you must first source your .bash_profile:

source ~/.bash_profile

Now, you can ask NVM which versions of Node it knows about:

nvm list-remote
. . .
v0.10.29
v0.10.30
 v0.11.0
 v0.11.1
 v0.11.2
 v0.11.3
 v0.11.4
 v0.11.5
 v0.11.6
 v0.11.7
 v0.11.8
 v0.11.9
v0.11.10
v0.11.11
v0.11.12
v0.11.13

You can install a version of Node by typing any of the releases you see. For instance, to get version 0.10.30, you can type:

nvm install v0.10.30

You can see the different versions you have installed by typing:

nvm list
->  v0.10.30
      system

You can switch between them by typing:

nvm use v0.10.30
Now using node v0.10.30

To set this version as the default, type:

nvm alias default v0.10.30
default -> v0.10.30

You can verify that the install was successful using the same technique from the other sections, by typing:

node --version
v0.10.30

From the version number output, we can tell that Node is installed on our machine as we expected.

Conclusion

As you can see, there are quite a few different ways of getting Node.js up and running on your CentOS 7 server. If one of the installation methods is giving you problems, try one of the other options.

Error: sudo: /etc/sudo.conf is owned by uid 1000, should be 0

I have an Amazon EC2 instance. I can login just fine, but neither “su” nor “sudo” work now (they worked fine previously):

  • “su” requests a password, but I login using ssh keys, and I don’t think the root user even has a password.
  • “sudo <anything>” does this:

sudo: /etc/sudoers is owned by uid 222, should be 0 
sudo: no valid sudoers sources found, quitting 

I probably did “chown ec2-user /etc/sudoers” (or, more likely “chown -R ec2-user /etc” because I was sick of rsync failing), so this is my fault.

How do I recover? I stopped the instance and tried the “View/Change User Data” option on the AWS EC2 console, but this didn’t help.

EDIT: I realize I could kill this instance and create a new one, but was hoping to avoid something that extreme.

 

sudo: /etc/sudo.conf is owned by uid 1000, should be 0

Connect CentOS to AWS Linux

  1. Use the chmod command to make sure your private key file isn’t publicly viewable. For example, if the name of your private key file is my-key-pair.pem, use the following command:
    chmod 400 /path/my-key-pair.pem
  2. Use the ssh command to connect to the instance. You’ll specify the private key (.pem) file and user_name@public_dns_name. For Amazon Linux, the user name is ec2-user. For RHEL5, the user name is either root or ec2-user. For Ubuntu, the user name is ubuntu. For Fedora, the user name is either fedora or ec2-user. For SUSE Linux, the user name is either root or ec2-user. Otherwise, if ec2-user and root don’t work, check with your AMI provider.
    ssh -i /path/my-key-pair.pem ec2-user@ec2-198-51-100-1.compute-1.amazonaws.com

    You’ll see a response like the following.

    The authenticity of host 'ec2-198-51-100-1.compute-1.amazonaws.com (10.254.142.33)'
    can't be established.
    RSA key fingerprint is 1f:51:ae:28:bf:89:e9:d8:1f:25:5d:37:2d:7d:b8:ca:9f:f5:f1:6f.
    Are you sure you want to continue connecting (yes/no)?
  3. (Optional) Verify that the fingerprint in the security alert matches the fingerprint that you obtained in step 1. If these fingerprints don’t match, someone might be attempting a “man-in-the-middle” attack. If they match, continue to the next step.
  4. Enter yes.

    You’ll see a response like the following.

    Warning: Permanently added 'ec2-198-51-100-1.compute-1.amazonaws.com' (RSA) 
    to the list of known hosts.