com.polyvi.xface.extension.XCalendarExt.java Source code

Java tutorial

Introduction

Here is the source code for com.polyvi.xface.extension.XCalendarExt.java

Source

/*
 Copyright 2012-2013, Polyvi Inc. (http://polyvi.github.io/openxface)
 This program is distributed under the terms of the GNU General Public License.
    
 This file is part of xFace.
    
 xFace is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 xFace is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with xFace.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.polyvi.xface.extension;

import java.util.Calendar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.widget.DatePicker;
import android.widget.TimePicker;

public class XCalendarExt extends XExtension {

    private static final String COMMAND_GET_TIME = "getTime";
    private static final String COMMAND_GET_DATE = "getDate";

    @Override
    public void sendAsyncResult(String result) {
    }

    @Override
    public boolean isAsync(String action) {
        return true;
    }

    @Override
    public XExtensionResult exec(String action, JSONArray args, XCallbackContext callbackCtx) throws JSONException {

        Calendar calendar = Calendar.getInstance();

        if (COMMAND_GET_TIME.equals(action)) {
            // ???
            int hours = calendar.get(Calendar.HOUR_OF_DAY);
            int minutes = calendar.get(Calendar.MINUTE);

            if (2 == args.length()) {
                hours = args.getInt(0);
                minutes = args.getInt(1);
            }

            getTime(hours, minutes, callbackCtx);
        } else if (COMMAND_GET_DATE.equals(action)) {
            // ???
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH) + 1;
            int day = calendar.get(Calendar.DAY_OF_MONTH);

            if (3 == args.length()) {
                year = args.getInt(0);
                month = args.getInt(1);
                day = args.getInt(2);
            }

            getDate(year, month, day, callbackCtx);
        }

        XExtensionResult er = new XExtensionResult(XExtensionResult.Status.NO_RESULT);
        return er;
    }

    /**
     * ??
     *
     * @param hour
     *            ??
     * @param minute
     *            ?
     * */
    private void getTime(final int hour, final int minute, final XCallbackContext callbackCtx) {
        mExtensionContext.getSystemContext().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        setTime(hourOfDay, minute, callbackCtx);
                    }
                }, hour, minute, true).show();
            }
        });
    }

    /**
     * ??
     *
     * @param year
     *            ?
     * @param month
     *            ?
     * @param day
     *            ?
     * */
    private void getDate(final int year, final int month, final int day, final XCallbackContext callbackCtx) {
        mExtensionContext.getSystemContext().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        setDate(year, monthOfYear, dayOfMonth, callbackCtx);
                    }
                }, year, (month - 1), day).show();// 0
                                                  // ?1??
                                                  // js??1
            }
        });
    }

    /**
     * ??js
     *
     * @param hour
     *            ?
     * @param minute
     *            
     * */
    private void setTime(int hour, int minute, XCallbackContext callbackCtx) {
        JSONObject time = new JSONObject();
        try {
            time.put("hour", hour);
            time.put("minute", minute);
            callbackCtx.success(time);
        } catch (JSONException e) {
            callbackCtx.error();
        }
    }

    /**
     * ??js
     *
     * @param year
     *            
     * @param month
     *            
     * @param day
     *            
     * */
    private void setDate(int year, int month, int day, XCallbackContext callbackCtx) {
        JSONObject date = new JSONObject();
        try {
            date.put("year", year);
            date.put("month", (month + 1));// 0js1
            date.put("day", day);
            callbackCtx.success(date);
        } catch (JSONException e) {
            callbackCtx.error();
        }
    }
}