com.oxgcp.photoList.PhotolistModule.java Source code

Java tutorial

Introduction

Here is the source code for com.oxgcp.photoList.PhotolistModule.java

Source

/**
 * This file was auto-generated by the Titanium Module SDK helper for Android
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 *
 */
package com.oxgcp.photoList;

import com.google.gson.Gson;
import org.json.JSONObject;
import org.json.JSONArray;

import java.util.HashMap;
import java.util.ArrayList;
import java.lang.Math;

import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiC;

import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.util.Log;
import org.appcelerator.titanium.util.TiConfig;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.TiBlob;

import android.app.Activity;

import android.net.Uri;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;
import android.media.ExifInterface;

@Kroll.module(name = "Photolist", id = "com.oxgcp.photoList")
public class PhotolistModule extends KrollModule {

    // Standard Debugging variables
    private static final String LCAT = "PhotolistModule";
    private static final boolean DBG = TiConfig.LOGD;

    // You can define constants with @Kroll.constant, for example:
    // @Kroll.constant public static final String EXTERNAL_NAME = value;

    public PhotolistModule() {
        super();
    }

    @Kroll.onAppCreate
    public static void onAppCreate(TiApplication app) {
        Log.d(LCAT, "inside onAppCreate");
        // put module init code that needs to run when the application is created
    }

    // Methods
    @Kroll.method
    public String example() {
        Log.d(LCAT, "example called");
        return "hello world";
    }

    @Kroll.method
    public KrollDict getPhotos(Integer date, Integer limit) {
        Uri externalPhotosUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        // Uri internalPhotosUri = MediaStore.Images.Media.INTERNAL_CONTENT_URI;
        Activity activity = this.getActivity();
        Gson gson = new Gson();

        String orderBy;
        if (date == null)
            date = 0;
        if (limit == null) {
            orderBy = MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC";
        } else {
            orderBy = MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC LIMIT " + limit;
        }

        String[] where = { date.toString() };

        HashMap<String, String> obj = new HashMap<String, String>();
        // HashMap<String, KrollDict> photos = new HashMap<String, KrollDict>();
        // KrollDict dict;

        // String[] projection = new String[]{
        //    MediaStore.Images.Media._ID,
        //    MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
        //    MediaStore.Images.Media.DATE_TAKEN
        // };

        Cursor externalList = activity.managedQuery(externalPhotosUri, null,
                MediaStore.Images.ImageColumns.DATE_TAKEN + " > ? ", where, orderBy);
        // null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC WHERE " + MediaStore.Images.ImageColumns._ID + " > " + id + " LIMIT " + limit);
        // Cursor internalList = activity.managedQuery(internalPhotosUri, null, null, null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");

        // ArrayList<String> arrayList = new ArrayList<String>();
        KrollDict arrayList = new KrollDict(externalList.getCount());

        externalList.moveToFirst();
        // internalList.moveToFirst();

        Log.d("TiAPI", "externalList's count: " + externalList.getCount());
        if (externalList.getCount() > 0) {
            for (Integer i = 0; !externalList.isAfterLast(); i++) {

                obj.put("path", externalList.getString(externalList.getColumnIndex(MediaStore.MediaColumns.DATA)));
                obj.put("date", externalList
                        .getString(externalList.getColumnIndex(MediaStore.Images.ImageColumns.DATE_TAKEN)));

                // dict = new KrollDict(obj);

                arrayList.put(i.toString(), new KrollDict(obj)); //add the item
                externalList.moveToNext();
            }
        }

        // Log.d("TiAPI", "internalList's count: " + internalList.getCount());
        // if (internalList.getCount() > 0) {
        //    while(!internalList.isAfterLast()) {
        //          arrayList.add(internalList.getString(internalList.getColumnIndex(MediaStore.MediaColumns.DATA))); //add the item
        //          internalList.moveToNext();
        //    }
        // }

        return arrayList;//gson.toJson(arrayList);
    }

    @Kroll.method
    public KrollDict getExifData(String fileName) {
        try {
            ExifInterface exif = new ExifInterface(fileName);

            HashMap<String, String> tag = new HashMap<String, String>();

            tag.put("hasThumbnail", exif.hasThumbnail() ? "true" : null);
            tag.put("height", exif.getAttribute("ImageLength"));
            tag.put("width", exif.getAttribute("ImageWidth"));
            tag.put("alt", exif.getAttribute("GPSAltitude"));
            tag.put("altRef", exif.getAttribute("GPSAltitudeRef"));
            tag.put("lat", exif.getAttribute("GPSLatitude"));
            tag.put("latRef", exif.getAttribute("GPSLatitudeRef"));
            tag.put("lon", exif.getAttribute("GPSLongitude"));
            tag.put("lonRef", exif.getAttribute("GPSLongitudeRef"));
            tag.put("date", exif.getAttribute("GPSDateStamp"));
            tag.put("time", exif.getAttribute("GPSTimeStamp"));

            return new KrollDict(tag);//new JSONObject(tag).toString();
        } catch (Exception e) {
            return null;
        }
    }

    @Kroll.method
    public TiBlob getThumbnail(String fileName) {
        try {
            ExifInterface exif = new ExifInterface(fileName);

            byte[] thumbnail = exif.getThumbnail();

            Log.d("TiAPI", "thumbnail's length: " + thumbnail.length);

            return TiBlob.blobFromImage(BitmapFactory.decodeByteArray(thumbnail, 0, thumbnail.length));
        } catch (Exception e) {
            return null;
        }
    }

    // Properties
    @Kroll.getProperty
    public String getExampleProp() {
        Log.d(LCAT, "get example property");
        return "hello world";
    }

    @Kroll.setProperty
    public void setExampleProp(String value) {
        Log.d(LCAT, "set example property: " + value);
    }

}