/*
 * Copyright 2013 wada811<at.wada811@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package at.wada811.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.widget.Toast;

public class AndroidUtils {

    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // Android Build Version 判定
    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    /**
     * Android OS バージョンが指定のバージョン以上の場合は true
     * 
     * @param versionCode
     * @return
     */
    public static boolean isMoreThanBuildVersion(int versionCode){
        return Build.VERSION.SDK_INT >= versionCode;
    }

    /**
     * Android OS バージョンが指定のバージョン未満の場合は true
     * 
     * @param versionCode
     * @return
     */
    public static boolean isLessThanBuildVersion(int version_code){
        return Build.VERSION.SDK_INT < version_code;
    }

    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // Application Meta Info
    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    /**
     * バージョンコードを取得する
     * 
     * @param context
     * @return
     */
    public static int getVersionCode(Context context){
        PackageManager pm = context.getPackageManager();
        int versionCode = 0;
        try{
            PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
            versionCode = packageInfo.versionCode;
        }catch(NameNotFoundException e){
            e.printStackTrace();
        }
        return versionCode;
    }

    /**
     * バージョン名を取得する
     * 
     * @param context
     * @return
     */
    public static String getVersionName(Context context){
        PackageManager pm = context.getPackageManager();
        String versionName = "";
        try{
            PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = packageInfo.versionName;
        }catch(NameNotFoundException e){
            e.printStackTrace();
        }
        return versionName;
    }

    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // Toast 表示
    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    /**
     * Toastを表示する
     * 
     * @param context
     * @param message
     */
    public static void showToast(final Context context, final String message){
        if(ThreadUtils.isMainThread()){
            Toast.makeText(context, message, Toast.LENGTH_LONG).show();
        }else{
            ((Activity)context).runOnUiThread(new Runnable(){
                @Override
                public void run(){
                    Toast.makeText(context, message, Toast.LENGTH_LONG).show();
                }
            });
        }
    }

    /**
     * Toastを表示する
     * 
     * @param context
     * @param resId
     */
    public static void showToast(final Context context, final int resId){
        if(ThreadUtils.isMainThread()){
            Toast.makeText(context, resId, Toast.LENGTH_LONG).show();
        }else{
            ((Activity)context).runOnUiThread(new Runnable(){
                @Override
                public void run(){
                    Toast.makeText(context, resId, Toast.LENGTH_LONG).show();
                }
            });
        }
    }

}

@@ -880,16 +880,158 @@
 oid.
-os.Build
+graphics.Bitmap;%0D%0Aimport android.os.Build;%0D%0Aimport android.view.Gravity;%0D%0Aimport android.view.ViewGroup.LayoutParams;%0D%0Aimport android.widget.ImageView
 ;%0D%0Ai
@@ -4241,11 +4241,1294 @@
    %7D%0D%0A%0D%0A
+    /**%0D%0A     * Toast%E3%82%92%E8%A1%A8%E7%A4%BA%E3%81%99%E3%82%8B%0D%0A     * %0D%0A     * @param context%0D%0A     * @param bitmap%0D%0A     */%0D%0A    public static void showToast(final Context context, final Bitmap bitmap)%7B%0D%0A        if(ThreadUtils.isMainThread())%7B%0D%0A            Toast toast = new Toast(context);%0D%0A            ImageView image = new ImageView(context);%0D%0A            image.setImageBitmap(bitmap);%0D%0A            image.setLayoutParams(new LayoutParams(bitmap.getWidth(), bitmap.getHeight()));%0D%0A            toast.setView(image);%0D%0A            toast.setGravity(Gravity.CENTER, 0, 0);%0D%0A            toast.setDuration(Toast.LENGTH_SHORT);%0D%0A            toast.show();%0D%0A        %7Delse%7B%0D%0A            ((Activity)context).runOnUiThread(new Runnable()%7B%0D%0A                @Override%0D%0A                public void run()%7B%0D%0A                    Toast toast = new Toast(context);%0D%0A                    ImageView image = new ImageView(context);%0D%0A                    image.setImageBitmap(bitmap);%0D%0A                    image.setLayoutParams(new LayoutParams(bitmap.getWidth(), bitmap.getHeight()));%0D%0A                    toast.setView(image);%0D%0A                    toast.setGravity(Gravity.CENTER, 0, 0);%0D%0A                    toast.setDuration(Toast.LENGTH_SHORT);%0D%0A                    toast.show();%0D%0A                %7D%0D%0A            %7D);%0D%0A        %7D%0D%0A    %7D%0D%0A%0D%0A
 %7D%0D%0A

add image toast for debug