Dec 11, 2014

날짜 표현 DateFormat, Custom Date Text View



Date today;
String output;
SimpleDateFormat formatter;

formatter = new SimpleDateFormat(pattern, currentLocale);
today = new Date();
output = formatter.format(today);
System.out.println(pattern + " " + output);



Customized Date and Time Formats
PatternOutput
dd.MM.yy30.06.09
yyyy.MM.dd G 'at' hh:mm:ss z2009.06.30 AD at 08:29:36 PDT
EEE, MMM d, ''yyTue, Jun 30, '09
h:mm a8:29 PM
H:mm8:29
H:mm:ss:SSS8:28:36:249
K:mm a,z8:29 AM,PDT
yyyy.MMMMM.dd GGG hh:mm aaa2009.June.30 AD 08:29 AM



Custom Date Text View

public class MDateView extends View {
 private final static String FORMAT_DEFULAT = "yyyy.MM.dd";

 private boolean isTickable = false;
 private boolean isColon = true;
 private Calendar mCalendar;
 private Runnable mTickerRunnable;
 private Handler mHandler;
 private String mFormat = FORMAT_DEFULAT;
 private String mDateString = "", mTimeString = "";
 private Paint mDatePaint, mTimePaint;

 public MDateView(Context context) {
  super(context);
  initClock(context);
 }

 public MDateView(Context context, AttributeSet attrs) {
  super(context, attrs);
  initClock(context);
 }

 private void initClock(Context context) {
  if (mCalendar == null) {
   mCalendar = Calendar.getInstance();
  }

  mDatePaint = new Paint(Paint.ANTI_ALIAS_FLAG);

  mDatePaint.setColor(Color.argb(255, 255, 255, 255));
  mDatePaint.setTextSize(18);
  mDatePaint.setTextAlign(Align.LEFT);

  mTimePaint = new Paint(Paint.ANTI_ALIAS_FLAG);

  mTimePaint.setColor(Color.argb(255, 255, 255, 255));
  mTimePaint.setTextSize(24);
  mTimePaint.setTextAlign(Align.LEFT);
 }

 @Override
 protected void onDraw(Canvas canvas) {
  canvas.drawText(mDateString, 5, 35, mDatePaint);

  int offsetX = 92;
  float time_X_Pos = (120 - mTimePaint.measureText(mTimeString, 0, mTimeString.length())) / 2.0f;// center align
  canvas.drawText(mTimeString, offsetX + time_X_Pos, 35, mTimePaint);

 }

 @Override
 protected void onAttachedToWindow() {
  super.onAttachedToWindow();
  isTickable = true;
  mHandler = new Handler();

  mTickerRunnable = new Runnable() {
   public void run() {
    if (!isTickable)
     return;

    mCalendar.setTimeInMillis(System.currentTimeMillis());
    mDateString = DateFormat.format(mFormat, mCalendar).toString();
    mTimeString = DateFormat.format("k" + (isColon ? ":" : " ") + "mm", mCalendar).toString();
    invalidate();

    long now = SystemClock.uptimeMillis();
    long next = now + (1000 - now % 1000);
    isColon = !isColon;
    mHandler.postAtTime(mTickerRunnable, next);
   }
  };
  mTickerRunnable.run();
 }

 @Override
 protected void onDetachedFromWindow() {
  super.onDetachedFromWindow();
  isTickable = false;
 }

 public void setFormat(String format) {
  if (mFormat == format)
   return;
  mFormat = format;
 }

Nov 18, 2014

Change keymap

The tools to manipulate the keyboard layout on the virtual consoles are loadkeys, dumpkeys and showkeys. Read their manpages and inform yourself about their intricacies.

Note that these tools only work in a virtual console, not in a terminal emulator in X.

Save your current keyboard layout:
$ dumpkeys > backup.kmap

In case something goes wrong you might be able restore your keymap using the command:
$ sudo loadkeys backup.kmap
If the keyboard is so messed up that you can't even do this then your only option not involving ancient kernel magic is to reboot.

Check which keycodes are assigned to your keys:
$ showkey
Now press the ESC key and the CAPSLOCK key. The keycodes should show up on the screen. Note the keycodes. On my system the ESC has the keycode 1 and CAPSLOCK has the keycode 58. showkey will terminate after 10 seconds of inactivity.

Note the names of the ESC and CAPSLOCK keys from dumpkeys:

$ dumpkeys | grep 1
...
keycode   1 = Escape
...
$ dumpkeys | grep 58
...
keycode  58 = CtrlL_Lock
...
Note the keymap line from dumpkeys:

$ dumpkeys | head -1
keymaps 0-127

Create a keymap file which switches ESC and CAPSLOCK:
keymaps 0-127
keycode   1 = CtrlL_Lock
keycode  58 = Escape

Load the keymap:
$ sudo loadkeys swap_esc_capslock.kmap



original link

Nov 25, 2013

기기의 DPI 확인

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
Log.d("TEST", "DPI = " + displayMetrics.densityDpi);

Oct 30, 2013

android(java) 파일 저장 후 리부팅 시 저장안되어 있는 현상

android에서 FileOutputStream 의 write();,  flush(); 했는데도 리부팅 시 간헐적으로 파일이 생성되지 않았거나 파일 내 데이터가 변하지 않을 때가 있다.

FileOutputStream 의 flush(); 가 버퍼를 비우는 기능만 수행하고 실제 physical device에 데이터를 저장하지 않아 파일에 데이터를 쓰기까지의 딜레이가 생기는 것이다.

이 때 fos.getFD().sync();의 메서드로 파일시스템 내부의 캐쉬를 모두 physical device로 저장한다.



FileOutputStream fos = this.openFileOutput(filename, Context.MODE_PRIVATE);
fos.write(obj.toString().getBytes());
fos.flush();
fos.getFD().sync();
fos.close();

Oct 7, 2013

Shuffle 기능


 List< Integer > numbers;
Collections.shuffle( numbers );

// 0부터 6번째 인덱스 데이터 가지고 옴
        List< Integer > winningCombination = numbers.subList( 0 , 6 );

// 정렬
Collections.sort( winningCombination );