Thursday, December 31, 2015

Android soft key menu 보유 여부 확인

    public boolean hasSoftKeys() {
        boolean hasSoftwareKeys = true;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            final Display d = getWindowManager().getDefaultDisplay();

            final DisplayMetrics realDisplayMetrics = new DisplayMetrics();
            d.getRealMetrics(realDisplayMetrics);

            final int realHeight = realDisplayMetrics.heightPixels;
            final int realWidth = realDisplayMetrics.widthPixels;

            final DisplayMetrics displayMetrics = new DisplayMetrics();
            d.getMetrics(displayMetrics);

            final int displayHeight = displayMetrics.heightPixels;
            final int displayWidth = displayMetrics.widthPixels;

            hasSoftwareKeys = (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
        } else {
            boolean hasMenuKey = ViewConfiguration.get(this).hasPermanentMenuKey();
            boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
            hasSoftwareKeys = !hasMenuKey && !hasBackKey;
        }
        return hasSoftwareKeys;
    }

Sunday, December 27, 2015

Android Volley와 Gson 예제

AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>

build.gradle
    compile 'com.google.code.gson:gson:2.5'
    compile 'com.github.bumptech.glide:volley-integration:1.3.1'

mt_rand.php
<?php
$ret = array('value' => mt_rand()+1000000000);
echo json_encode($ret);
?>

    public class Uid {
        private String value;
        public String getValue() {
            return value;
        }
    }
    private RequestQueue mRequestQueue;

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

        String url ="http://192.168.29.188/mt_rand.php";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d("MainActivity", response.toString());

                        Uid uid = new Gson().fromJson(response.toString(), Uid.class);

                    }
                },
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d("MainActivity", error.toString());
                    }
                }
        );

        mRequestQueue = Volley.newRequestQueue(this);
        mRequestQueue.add(request);
    }

Tuesday, December 22, 2015

MySQL C API에서 /var/lib/mysql/mysql.sock으로 접속이 안될때

MySQL Server를 설치한다.
yum -y install mysql mysql-server (or apt-get install mysql mysql-server)

Enable the MySQL service:
/sbin/chkconfig mysqld on

Start the MySQL server:
/sbin/service mysqld start

vi /etc/my.cnf
아래와 같이 클라이언트를 추가한다.
이미 있다면 수정하여 준다.

 [client]
 #password       = your_password
 host            = 127.0.0.1
 port            = 3306
 socket          = /var/run/mysql/mysql.sock

socket을 /var/lib/mysql/mysql.sock로 수정해주고
host를 127.0.0.1 또는 localhost로 수정해준다.
host는 여기에 입력한 대로 mysql_connect에서 호출해야 한다.(localhost로 입력했으면 127.0.0.1로는 접속안됨)

Monday, December 21, 2015

iOS 디바이스 UUID 획득하기

//처음에 UUID를 KeyChain에서 불러오는데 nil이라면 UUID를 생성해서 KeyChain에 저장한다.
//저장 후에 다시 함수를 호출 하면 저장된 값을 리턴한다.
NSString* getUUID()
{
    // initialize keychaing item for saving UUID.
    KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"UUID" accessGroup:nil];
   
    NSString *uuid = [wrapper objectForKey:(__bridge id)(kSecAttrAccount)];
   
    if( uuid == nil || uuid.length == 0)
    {
        // if there is not UUID in keychain, make UUID and save it.
        CFUUIDRef uuidRef = CFUUIDCreate(NULL);
        CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
        CFRelease(uuidRef);
        uuid = [NSString stringWithString:(__bridge NSString *) uuidStringRef];
        CFRelease(uuidStringRef);
       
        // save UUID in keychain
        [wrapper setObject:uuid forKey:(__bridge id)(kSecAttrAccount)];
    }
   
    return uuid;
}

Android 디바이스 UUID 획득하기

public static String getDeviceUUID(final Context context) {
        // preference에서 저장된 것이 있는지 확인해봄
        final SharedPreferences prefs = context.getSharedPreferences("UUIDTEST", MODE_PRIVATE);
        final String id = prefs.getString("UUID", null);

        UUID uuid = null;
        if (id != null) {
            uuid = UUID.fromString(id);
        } else {
            final String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            try {
                // 예전에 아래와 같은 고정값이 리턴되는 문제가 있었음
                if (!"9774d56d682e549c".equals(androidId)) {
                    uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8"));
                } else {
                    final String deviceId = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
                    uuid = deviceId != null ? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")) : UUID.randomUUID();
                }
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }

            final SharedPreferences.Editor editor = prefs.edit();
            editor.putString("UUID", uuid.toString());
            editor.commit();
        }

        return uuid.toString();
    }