Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

import android.provider.MediaStore;
import android.util.Log;

public class Main {
    public static final String TAG = "AirImagePicker";
    public static final int GALLERY_IMAGES_ONLY_ACTION = 0;
    public static final int GALLERY_VIDEOS_ONLY_ACTION = 1;
    public static final int CAMERA_IMAGE_ACTION = 2;
    public static final int CAMERA_VIDEO_ACTION = 3;
    public static final int CROP_ACTION = 4;

    public static Boolean isCameraAvailable(Activity activity) {
        Log.d(TAG, "[AirImagePickerUtils] Entering isCameraAvailable");
        Boolean hasCameraFeature = activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
        Boolean hasFrontCameraFeature = activity.getPackageManager()
                .hasSystemFeature("android.hardware.camera.front");
        Boolean isAvailable = (hasFrontCameraFeature || hasCameraFeature)
                && (isActionAvailable(activity, CAMERA_IMAGE_ACTION)
                        || isActionAvailable(activity, CAMERA_VIDEO_ACTION));

        Log.d(TAG, "[AirImagePickerUtils] Exiting isCameraAvailable");
        return isAvailable;
    }

    public static Boolean isActionAvailable(Activity activity, int action) {
        Log.d(TAG, "[AirImagePickerUtils] Entering isActionAvailable");
        if (action == CROP_ACTION) {
            return isCropAvailable(activity);
        }

        final PackageManager packageManager = activity.getPackageManager();
        List<ResolveInfo> list = packageManager.queryIntentActivities(getIntentForAction(action),
                PackageManager.MATCH_DEFAULT_ONLY);

        Log.d(TAG, "[AirImagePickerUtils] Exiting isActionAvailable");

        return list.size() > 0;
    }

    public static Boolean isCropAvailable(Activity activity) {
        Log.d(TAG, "[AirImagePickerUtils] isCropAvailable");

        final PackageManager packageManager = activity.getPackageManager();
        Intent intent = getIntentForAction(CROP_ACTION);
        intent.setType("image/*");
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);

        Log.d(TAG, "[AirImagePickerUtils] Exiting isCropAvailable");

        return list.size() > 0;
    }

    public static Intent getIntentForAction(int action) {
        Log.d(TAG, "[AirImagePickerUtils] Entering getIntentForAction");
        Intent intent;
        switch (action) {
        case GALLERY_IMAGES_ONLY_ACTION:
            intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            Log.d(TAG, "[AirImagePickerUtils] Exiting getIntentForAction");
            return intent;

        case GALLERY_VIDEOS_ONLY_ACTION:
            intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("video/*");
            Log.d(TAG, "[AirImagePickerUtils] Exiting getIntentForAction");
            return intent;

        case CAMERA_IMAGE_ACTION:
            Log.d(TAG, "[AirImagePickerUtils] Exiting getIntentForAction");
            return new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        case CAMERA_VIDEO_ACTION:
            Log.d(TAG, "[AirImagePickerUtils] Exiting getIntentForAction");
            return new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

        case CROP_ACTION:
            Log.d(TAG, "[AirImagePickerUtils] Exiting getIntentForAction");
            return new Intent("com.android.camera.action.CROP");

        default:
            Log.d(TAG, "[AirImagePickerUtils] Exiting getIntentForAction");
            return null;
        }
    }
}