Code

Code

FileUriExposedException on Android 7.0

Uri photoURI = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", createImageFile());

http://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed

Get Download Directory in Android

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

SimpleDraweeView did not recycled in RecyclerView in Fresco Library

DraweeController draweeController = Fresco.newDraweeControllerBuilder()
        
.setOldController(mImage.getController())
        
.setUri(uri)
        
.setAutoPlayAnimations(true)
        
.build();



mImage.setController(draweeController);


Fast rebuild with instant run.

If I change the AndroidManifest.xml file, android studio performs a full build, but this is much faster to fist cleaning, and then building the project.So, I add a blank line to the manifest file, whenever I want android studio to full build the project. This is much effective to perform a manual full build.
http://stackoverflow.com/a/39329144/1015731

Move the task containing this activity to the back of the activity

public boolean moveTaskToBack(boolean nonRoot) {
    try {
        return ActivityManagerNative.getDefault().moveActivityTaskToBack(
                mToken, nonRoot);
    } catch (RemoteException e) {
        // Empty
    }
    return false;
}

Just use this.
moveTaskToBack(true);

Circles image using Fresco Programmatically

RoundingParams roundingParams = RoundingParams.fromCornersRadius(5f);
roundingParams.setRoundAsCircle(true);
mImage.getHierarchy().setRoundingParams(roundingParams);

http://frescolib.org/docs/rounded-corners-and-circles.html

Like search in SQLite3 using Python

connection.execute('SELECT name, code FROM table WHERE name like ?', ('%' + name + '%', ))

Update badge count in Android

Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE"); 

// package name setting 
intent.putExtra("badge_count_package_name", getComponentName().getPackageName()); 
intent.putExtra("badge_count_class_name", getComponentName().getClassName()); 

// badge count
intent.putExtra("badge_count", count);
sendBroadcast(intent);

JSON deep equal

Object.extend(Object, {
   deepEquals: function(o1, o2) {
     var k1 = Object.keys(o1).sort();
     var k2 = Object.keys(o2).sort();
     if (k1.length != k2.length) return false;
     return k1.zip(k2, function(keyPair) {
       if(typeof o1[keyPair[0]] == typeof o2[keyPair[1]] == "object"){
         return deepEquals(o1[keyPair[0]], o2[keyPair[1]])
       } else {
         return o1[keyPair[0]] == o2[keyPair[1]];
       }
     }).all();
   }
});

Usage:

var anObj = JSON.parse(jsonString1);
var anotherObj= JSON.parse(jsonString2);

if (Object.deepEquals(anObj, anotherObj))
   ...

Reference: http://stackoverflow.com/questions/4465244/compare-2-json-objects

flask url open basic format

import urllib

@app.route('/events.json')
def events():
    result = urllib.urlopen([URL]).read()
    return json.load(result)

Reset command in Git

https://git-scm.com/book/ko/v2/Git-%EB%8F%84%EA%B5%AC-Reset-%EB%AA%85%ED%99%95%ED%9E%88-%EC%95%8C%EA%B3%A0-%EA%B0%80%EA%B8%B0

위 링크를 살펴보면 Git의 HEAD, Index, Working Directory의 개념과,
Reset명령어를 이해할 수 있다.

여러 옵션이 있지만 가장 많이 쓸법한 Head에 있는 내용을 Working Directory까지 Reset하려면 --hard 옵션을 사용하면 된다.

라벨:

jQuery, using one instead of on or off

https://github.com/jquery/jquery/blob/f18ca7bfe0f5e3184bf1ed55daf1668702c5577a/src/event.js
jQuery 소스 중 아래 fn.guid = origFn.guid || ( origFn.guid = jQuery.guid++ ); 부분을 보면 guid가 같은 경우 새로운 함수를 assign 한다는 것을 알 수 있다. 따라서, 앞으로 off, on를 연속해서 사용할 필요 없이 one 만을 사용하면 더 간단하다.
 if ( one === 1 ) {
    origFn = fn;
    fn = function( event ) {

        // Can use an empty set, since event contains the info
        jQuery().off( event );
        return origFn.apply( this, arguments );
    };

    // Use same guid so caller can remove using origFn
    fn.guid = origFn.guid || ( origFn.guid = jQuery.guid++ );
}
return elem.each( function() {
    jQuery.event.add( this, types, fn, data, selector );
} );

Realtime log collection system.

결론부터 말하면 특별히 제한이 없으면 Logstash를 사용할 것 같고, 다른 경우라면 syslog계열을 사용할 것 같다.
Logstash https://www.elastic.co/kr/products/logstash 다양한 플러그인 지원하지만 자원소모가 많음. 여유가 있는 서버에 설치하거나 단독으로 사용하는 것이 좋겠다.
Filebeat https://www.elastic.co/kr/products/beats/filebeat 가볍도 의존성도 없다. 하지만 제한사항이 많다. 다양한 환경에 적응하기는 쉽지 않다.
Logagent https://sematext.com/logagent/ 아주 빨리 설치해서 사용할 수 있다. 하지만 아직 Logstash보다 기능이 뒤쳐진다.
rsyslog http://www.rsyslog.com/ 리눅스의 기본 데몬인 syslog처럼 사용할 수 있다. 사용이 아주 간편하고 익숙하다. (syslog를 기존에 사용해봤다는 전재에서)
syslog-ng https://syslog-ng.org/ rsyslog 처럼 가볍고 사용하기도 쉽다. 크게 다른점은 모르겠다.
Fluentd http://www.fluentd.org/ JSON형태로 로그를 저장한다. 때문에 플랫폼 제한이 없지간 Ruby로 작성되어 추가 개발은 쉽지만 속도 등 여러가지 한계가 있다.


Proguard : google.gms - Duplicate zip entry c.class 에러

In app.gradle you need to move following entry to the end of file :

apply plugin: 'com.google.gms.google-services'

java.sql.SQLSyntaxErrorException: ORA-0091 오류

java.sql.SQLSyntaxErrorException: ORA-00911: 문자가 부적합합니다

위와 같은 오류가 발생할 경우, SQL 구문 마지막에 세미콜론(;)이 있는지 확인하고 제거하면 해결된다.

JavaScript에서 this 키워드에 새로운 context를 대입할 수 있을까?

불가능하다. JavaScript object로 된 함수를 사용할 때 이런 고민할 하게되는데 참고.

You can call a method with a specified value for this (using method.apply()/method.call()) but you cannot re-assign the keyword, this.


라벨:

The WITH in Oracle, The subquery Factoring.

Oracle에서 사용가능한 WITH 구문은 SQL-99 표준으로 Oracle 9.2 부터 추가되었다. WITH 구문은 임시 테이블을 만들거나 인라인 뷰처럼 동작한다. 같은 구문을 여러번 반복해서 쓰거나 여러곳에서 참조할 경우 아주 유용하다. 쿼리도 상당히 간단해 진다는 장점이 있다.

  SELECT e.ename AS employee_name,         dc.dept_count AS emp_dept_count  FROM   emp e,         (SELECT deptno, COUNT(*) AS dept_count          FROM   emp          GROUP BY deptno) dc  WHERE  e.deptno = dc.deptno;      

WITH dept_count AS (    SELECT deptno, COUNT(*) AS dept_count    FROM   emp    GROUP BY deptno)  SELECT e.ename AS employee_name,         dc.dept_count AS emp_dept_count  FROM   emp e,         dept_count dc  WHERE  e.deptno = dc.deptno;  
Reference: https://oracle-base.com/articles/misc/with-clause


Invoking JavaScript code in an iframe from the parent page

document.getElementById('targetFrame').contentWindow.targetFunction();
Reference: http://stackoverflow.com/questions/251420/invoking-javascript-code-in-an-iframe-from-the-parent-page


ES6에서 let 과 var의 차이

let은 선언된 블록 유효 범위에 상관없이 전역 또는 함수 유효 범위를 갖는 var 키워드와는 달리, 변수가 사용되는 블록, 구문 또는 표현식 유효 범위를 갖는 변수를 선언한다.
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/let
function varTest() {
  var x = 31;
  if (true) {
    var x = 71;  // same variable!
    console.log(x);  // 71
  }
  console.log(x);  // 71
}

function letTest() {
  let x = 31;
  if (true) {
    let x = 71;  // different variable
    console.log(x);  // 71
  }
  console.log(x);  // 31
}

Video or Audio player sample using HTML5

<video controls poster="demo.jpg">     <source src="demo.mp4" type="video/mp4" />     <source src="demo.webm" type="video/webm"/>     <source src="demo.ogv" type="video/ogg"/>          <object>       <embed src="demo.mp4" type= "application/x-shockwave-flash" allowfullscreen="false" allowscriptaccess="always" />     </object>             HTML5 Video is required for this example  </video>


라벨:

Fix the "Requirement.parse('setuptools>=0.x'))" error

pip install --upgrade setuptools


Fixing the "numpy.distutils.system_info.NotFoundError: no lapack/blas resources found" Error while scipy installation

sudo apt-get install libblas-dev liblapack-dev libatlas-base-dev gfortran
pip install scipy

Format File Size using JavaScript

numeral(size).format('0,0.0 b');


jQuery Mobile Transitions for Link (A tags)

<a href="{link}" data-transition="flip" class="ui-btn"><p>link</p></a></code></pre><div dir="ltr">

Find multiple element id using jQuery

$('[id="a"]');  $('span[id="a"]');


Get Parameter value from URL with JSP

<% String flag = request.getParameter("flag"); %>


Plus(+) sign in Url Parameter

+ means a space only in application/x-www-form-urlencoded content, such as the query part of a URL:
http://www.example.com/path/foo+bar/path?query+name=query+value
query+name -> query name
query+value -> query value

But,
foo+bar -> foo+bar

Reference: http://stackoverflow.com/questions/2678551/when-to-encode-space-to-plus-or-20

Applying jQuery Flip Transitiosn in Mobile

$.mobile.changePage("#page2", { transition: "flip" });
Demo: http://jsfiddle.net/YjsPD/1204/


Rsync simple example (synchronize specific folders)

#!/bin/sh

rsync -azh ./build -e 'ssh -p 22' user@hostname:~/www

Alert dialog for Android


myButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle("Title");

        final EditText input = new EditText(getContext());
        input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        builder.setView(input);

        // Set up the buttons
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //m_Text = input.getText().toString();
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        builder.show();
    }
});

// or using layout

myButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);

        View view = View.inflate(mContext, R.layout.page, null);
        builder.setView(view);

        // Set up the buttons
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //m_Text = input.getText().toString();

                childDatabase.insert(new Children());

                setAdapter();
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        builder.show();
    }
});


Soundex Class for Java


// reference: http://en.wikipedia.org/wiki/Soundex
public class SoundexUtil {

	// public static void main(String[] args) {
	// 	System.out.println(soundex("Gauss")); // G200
	// 	System.out.println(soundex("AHN")); // A500
	// }

	public static String soundex(String text) {
		StringBuilder builder = new StringBuilder();
		int numberCounter = 0;

		for (int i = 0; i < text.length(); ++i) {
			char c = text.charAt(i);
			if (i == 0) {
				builder.append(c);

			} else {

				char number = 0;

				switch (Character.toLowerCase(c)) {
				case 'a':
				case 'e':
				case 'h':
				case 'i':
				case 'o':
				case 'u':
				case 'w':
				case 'y':
					continue;

				case 'b':
				case 'f':
				case 'p':
				case 'v':
					number = '1';
					break;

				case 'c':
				case 'g':
				case 'j':
				case 'k':
				case 'q':
				case 's':
				case 'x':
				case 'z':
					number = '2';
					break;

				case 'd':
				case 't':
					number = '3';
					break;

				case 'l':
					number = '4';
					break;

				case 'm':
				case 'n':
					number = '5';
					break;

				case 'r':
					number = '6';
					break;
				}

				if (number > 0
						&& builder.charAt(builder.length() - 1) != number) {
					builder.append(number);
					++numberCounter;
				}
			}
		}

		while (numberCounter < 3) {
			builder.append('0');
			++numberCounter;
		}

		String soundexText = builder.toString();
		return soundexText.substring(0, 4);
	}
}


Palindrome for java


public class Palindrome {

	public static void main(String[] args) {
		System.out.println(isPalindrome("amor roma"));
		System.out.println(isPalindrome("Was it a car or a cat I saw?"));
	}

	public static Boolean isPalindrome(String text) {
		StringBuilder builder = new StringBuilder();
		for (char c : text.toCharArray()) {
			if (Character.isAlphabetic(c)) {
				builder.append(Character.toLowerCase(c));
			}
		}

		text = builder.toString();
		int len = text.length();
		
		for (int i = 0; i < len / 2; ++i) {
			if (text.charAt(i) != text.charAt(len - i - 1)) {
				return false;
			}
		}
		return true;
	}
}


sort check (IsSorted function) for java


public class IsSorted {

	public static void main(String[] args) {
		System.out.println(isSorted(new int[] { 1, 2, 3, 4 })); // true
		System.out.println(isSorted(new int[] { 1, 2, 3, 0 })); // false
	}

	public static boolean isSorted(int[] items) {
		for (int i = 0; i < items.length - 1; ++i) {
			if (items[i] > items[i + 1]) {
				return false;
			}
		}
		return true;
	}
}


Sync system time in Linux using crontab


30 1 * * * root /usr/bin/rdate -s time.bora.net && /sbin/clock -w


post-commit for git


#!/bin/sh

REPOS="$1"
REV="$2"

#mailer.py commit "$REPOS" "$REV" /path/to/mailer.conf
MESSAGE=$(svnlook propget --revprop -r $REV $REPOS svn:log)

cat <<EOF | /root/monitor/sendmail.py -s "[COMMIT] $REPOS:$REV" -r user@domain
REPOS: $REPOS
REV: $REV
MESSAGE:
$MESSAGE
EOF

sendmail using python


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import smtplib
import datetime

from optparse import OptionParser

from smtplib import SMTP as SMTP
from email.MIMEText import MIMEText

DEFAULT_FROM = 'no-reply@domain.com'
DEFAULT_DOMAIN = 'smtp.domain.com'

class SendMail(object):
  username = None
  password = None
  host = None
  port = None

  def __init__(self, username, password, host, port=25):
    self.username = username
    self.password = password
    self.host = host
    self.port = port

  def send(self, subject='', content='', recipient=None):
    conn = SMTP(host=self.host, port=self.port)
    conn.set_debuglevel(False)

    if self.username and self.password:
      conn.login(self.username, self.password)

    msg = MIMEText(content)
    msg['Subject'] = subject
    msg['From'] = DEFAULT_FROM
    msg['To'] = recipient

    try:
      conn.sendmail(self.username, recipient.split(','), msg.as_string())

    finally:
      conn.close()

    print 'Sending...'

if __name__ == '__main__':
  parser = OptionParser()
  parser.add_option('-s', '--subject', dest='subject', default='no subject', help='Subject of mail', metavar='SUBJECT')
  parser.add_option('-r', '--recipients', dest='recipients', help='Recipients of mail')
  parser.add_option('--host', dest='host', default=DEFAULT_DOMAIN, help='SMTP server host')
  parser.add_option('--port', dest='port', default='25', help='SMTP server port')
  parser.add_option('--username', dest='username', default=None, help='SMTP username')
  parser.add_option('--password', dest='password', default=None, help='SMTP password')
  
  (options, args) = parser.parse_args()

  lines = []
  for line in sys.stdin:
    stripped = line.strip()
    #if not stripped: break
    lines.append(stripped)
  
  options.content = '\r\n'.join(lines)
  
  sendmail = SendMail(
    username=options.username, 
    password=options.password, 
    host=options.host, 
    port=options.port
    )
    
  sendmail.send(subject=options.subject, content=options.content, recipient=options.recipients)


Swap using XOR algorithm in C# Raw


// http://en.wikipedia.org/wiki/XOR_swap_algorithm
private void Swap(ref int x, ref int y)
{
    x ^= y;
    y ^= x;
    x ^= y;
}


라벨:

GetListItems using Func Generic in C#


Func, ListItem[]> GetListItems = (items) =>
{
    List list = new List();
    foreach(int i in items)
    {
        string item = i.ToString("D2");
        list.Add(new ListItem()
        {
            Text = item,
            Value = item
        });
    }

    return list.ToArray();
};

dropListYear.Items.AddRange(GetListItems(Enumerable.Range(DateTime.Now.Year - 99, 100).Reverse().ToList()));
dropListMonth.Items.AddRange(GetListItems(Enumerable.Range(1, 12).ToList()));
dropListDay.Items.AddRange(GetListItems(Enumerable.Range(1, 31).ToList()));


URL query string parameter to mongoose parameters Raw


exports.query = function(query) {
  var operator = {};
  if (query) {

    var tokenize = function(operator, str) {
      var data = str.split(operator);
      if (data.length > 1) {
        return data;
      }
      return null;
    };

    var tokens = query.split(',');

    for (var i = 0; i < tokens.length; ++i) {
      var selector = tokenize('<=', tokens[i]);
      if (selector) {
        operator[selector[0]] = operator[selector[0]] || {};
        operator[selector[0]]['$lte'] = selector[1];
        continue;
      }

      selector = tokenize('>=', tokens[i]);
      if (selector) {
        operator[selector[0]] = operator[selector[0]] || {};
        operator[selector[0]]['$gte'] = selector[1];
        continue;
      }

      selector = tokenize('<', tokens[i]);
      if (selector) {
        operator[selector[0]] = operator[selector[0]] || {};
        operator[selector[0]]['$lt'] = selector[1];
        continue;
      }

      selector = tokenize('>', tokens[i]);
      if (selector) {
        operator[selector[0]] = operator[selector[0]] || {};
        operator[selector[0]]['$gt'] = selector[1];
        continue;
      }

      selector = tokenize('!=', tokens[i]);
      if (selector) {
        operator[selector[0]] = operator[selector[0]] || {};
        operator[selector[0]]['$net'] = selector[1];
        continue;
      }

      selector = tokenize('=', tokens[i]);
      if (selector) {
        operator[selector[0]] = operator[selector[0]] || {};
        operator[selector[0]] = selector[1];
        continue;
      }

      selector = tokenize('$regex', tokens[i]);
      if (selector) {
        operator[selector[0]] = operator[selector[0]] || {};
        operator[selector[0]] =  new RegExp(selector[1], 'i');
        continue;
      }
    }
  }

  return operator;
};



Subscribe to: Posts (Atom)