Search in sources :

Example 1 with KrollDict

use of org.appcelerator.kroll.KrollDict in project titanium-barcode by mwaylabs.

the class TitaniumBarcodeModule method getDictForResult.

private KrollDict getDictForResult(final String result) {
    final KrollDict dict = new KrollDict();
    dict.put("barcode", result);
    return dict;
}
Also used : KrollDict(org.appcelerator.kroll.KrollDict)

Example 2 with KrollDict

use of org.appcelerator.kroll.KrollDict in project benCoding.AlarmManager by benbahrenburg.

the class AlarmManagerProxy method addAlarmService.

@Kroll.method
public void addAlarmService(@SuppressWarnings("rawtypes") HashMap hm) {
    @SuppressWarnings("unchecked") KrollDict args = new KrollDict(hm);
    if (!args.containsKeyAndNotNull("service")) {
        throw new IllegalArgumentException("Service name (service) is required");
    }
    if (!args.containsKeyAndNotNull("minute") && !args.containsKeyAndNotNull("second")) {
        throw new IllegalArgumentException("The minute or second field is required");
    }
    Calendar calendar = null;
    boolean isRepeating = hasRepeating(args);
    long repeatingFrequency = 0;
    if (isRepeating) {
        repeatingFrequency = repeatingFrequency(args);
    }
    //If seconds are provided but not years, we just take the seconds to mean to add seconds until fire
    boolean secondBased = (args.containsKeyAndNotNull("second") && !args.containsKeyAndNotNull("year"));
    //If minutes are provided but not years, we just take the minutes to mean to add minutes until fire
    boolean minuteBased = (args.containsKeyAndNotNull("minute") && !args.containsKeyAndNotNull("year"));
    //Based on what kind of duration we build our calendar
    if (secondBased) {
        calendar = getSecondBasedCalendar(args);
    } else if (minuteBased) {
        calendar = getMinuteBasedCalendar(args);
    } else {
        calendar = getFullCalendar(args);
    }
    //Get the requestCode if provided, if none provided
    //we use 192837 for backwards compatibility
    int requestCode = args.optInt("requestCode", AlarmmanagerModule.DEFAULT_REQUEST_CODE);
    String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
    utils.debugLog("Creating Alarm Notification for: " + sdf.format(calendar.getTime()));
    AlarmManager am = (AlarmManager) TiApplication.getInstance().getApplicationContext().getSystemService(TiApplication.ALARM_SERVICE);
    Intent intent = createAlarmServiceIntent(args);
    if (isRepeating) {
        utils.debugLog("Setting Alarm to repeat at frequency " + repeatingFrequency);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(TiApplication.getInstance().getApplicationContext(), requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), repeatingFrequency, pendingIntent);
    } else {
        PendingIntent sender = PendingIntent.getBroadcast(TiApplication.getInstance().getApplicationContext(), requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        utils.debugLog("Setting Alarm for a single run");
        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
    }
    utils.infoLog("Alarm Service Request Created");
}
Also used : GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) AlarmManager(android.app.AlarmManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) KrollDict(org.appcelerator.kroll.KrollDict) SimpleDateFormat(java.text.SimpleDateFormat)

Example 3 with KrollDict

use of org.appcelerator.kroll.KrollDict in project benCoding.AlarmManager by benbahrenburg.

the class AlarmManagerProxy method cancelAlarmNotification.

@Kroll.method
public void cancelAlarmNotification(@Kroll.argument(optional = true) Object requestCode) {
    // To cancel an alarm the signature needs to be the same as the submitting one.
    utils.debugLog("Cancelling Alarm Notification");
    //Set the default request code
    int intentRequestCode = AlarmmanagerModule.DEFAULT_REQUEST_CODE;
    //If the optional code was provided, cast accordingly
    if (requestCode != null) {
        if (requestCode instanceof Number) {
            intentRequestCode = ((Number) requestCode).intValue();
        }
    }
    utils.debugLog(String.format("Cancelling requestCode = {%d}", intentRequestCode));
    //Create a placeholder for the args value
    HashMap<String, Object> placeholder = new HashMap<String, Object>(0);
    KrollDict args = new KrollDict(placeholder);
    //Create the Alarm Manager
    AlarmManager am = (AlarmManager) TiApplication.getInstance().getApplicationContext().getSystemService(TiApplication.ALARM_SERVICE);
    Intent intent = createAlarmNotifyIntent(args, intentRequestCode);
    PendingIntent sender = PendingIntent.getBroadcast(TiApplication.getInstance().getApplicationContext(), intentRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    am.cancel(sender);
    utils.debugLog("Alarm Notification Canceled");
}
Also used : HashMap(java.util.HashMap) AlarmManager(android.app.AlarmManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) KrollDict(org.appcelerator.kroll.KrollDict)

Example 4 with KrollDict

use of org.appcelerator.kroll.KrollDict in project Utterance by benbahrenburg.

the class SpeechToTextProxy method startSpeechToText.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Kroll.method
public void startSpeechToText(HashMap hm) {
    KrollDict args = new KrollDict(hm);
    try {
        //start the speech recognition intent passing required data
        Intent listenIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        //indicate package
        listenIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
        if (args.containsKeyAndNotNull(PROPERTY_PROMPT)) {
            //message to display while listening
            listenIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, args.getString(PROPERTY_PROMPT));
        }
        //set speech model
        listenIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, args.optString(LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM));
        //specify number of results to retrieve
        listenIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, args.optInt(MAX_RESULTS, 10));
        final Activity activity = TiApplication.getAppCurrentActivity();
        final TiActivitySupport activitySupport = (TiActivitySupport) activity;
        activitySupport.launchActivityForResult(listenIntent, INTENT_ID, this);
        if (hasListeners(EVENT_STARTED)) {
            HashMap<String, Object> event = new HashMap<String, Object>();
            event.put("success", true);
            fireEvent(EVENT_STARTED, event);
            Log.d(UtteranceModule.MODULE_FULL_NAME, "event: " + EVENT_STARTED + " fired");
        } else {
            Log.d(UtteranceModule.MODULE_FULL_NAME, "event: " + EVENT_STARTED + " not found");
        }
    } catch (Exception error) {
        Log.e(UtteranceModule.MODULE_FULL_NAME, error.getMessage());
        error.printStackTrace();
    }
}
Also used : HashMap(java.util.HashMap) TiActivitySupport(org.appcelerator.titanium.util.TiActivitySupport) Activity(android.app.Activity) Intent(android.content.Intent) RecognizerIntent(android.speech.RecognizerIntent) KrollDict(org.appcelerator.kroll.KrollDict)

Example 5 with KrollDict

use of org.appcelerator.kroll.KrollDict in project benCoding.AlarmManager by benbahrenburg.

the class AlarmManagerProxy method addAlarmNotification.

@Kroll.method
public void addAlarmNotification(@SuppressWarnings("rawtypes") HashMap hm) {
    @SuppressWarnings("unchecked") KrollDict args = new KrollDict(hm);
    if (!args.containsKeyAndNotNull("minute") && !args.containsKeyAndNotNull("second")) {
        throw new IllegalArgumentException("The minute or second field is required");
    }
    if (!args.containsKeyAndNotNull(TiC.PROPERTY_CONTENT_TITLE)) {
        throw new IllegalArgumentException("The context title field (contentTitle) is required");
    }
    if (!args.containsKeyAndNotNull(TiC.PROPERTY_CONTENT_TEXT)) {
        throw new IllegalArgumentException("The context text field (contentText) is required");
    }
    Calendar calendar = null;
    boolean isRepeating = hasRepeating(args);
    long repeatingFrequency = 0;
    if (isRepeating) {
        repeatingFrequency = repeatingFrequency(args);
    }
    //If seconds are provided but not years, we just take the seconds to mean to add seconds until fire
    boolean secondBased = (args.containsKeyAndNotNull("second") && !args.containsKeyAndNotNull("year"));
    //If minutes are provided but not years, we just take the minutes to mean to add minutes until fire
    boolean minuteBased = (args.containsKeyAndNotNull("minute") && !args.containsKeyAndNotNull("year"));
    //Based on what kind of duration we build our calendar
    if (secondBased) {
        calendar = getSecondBasedCalendar(args);
    } else if (minuteBased) {
        calendar = getMinuteBasedCalendar(args);
    } else {
        calendar = getFullCalendar(args);
    }
    //Get the requestCode if provided, if none provided
    //we use 192837 for backwards compatibility
    int requestCode = args.optInt("requestCode", AlarmmanagerModule.DEFAULT_REQUEST_CODE);
    String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
    utils.debugLog("Creating Alarm Notification for: " + sdf.format(calendar.getTime()));
    //Create the Alarm Manager
    AlarmManager am = (AlarmManager) TiApplication.getInstance().getApplicationContext().getSystemService(TiApplication.ALARM_SERVICE);
    Intent intent = createAlarmNotifyIntent(args, requestCode);
    PendingIntent sender = PendingIntent.getBroadcast(TiApplication.getInstance().getApplicationContext(), requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    if (isRepeating && !intent.hasExtra("notification_repeat_ms")) {
        utils.debugLog("Setting Alarm to repeat");
        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), repeatingFrequency, sender);
    } else {
        utils.debugLog("Setting Alarm for a single run");
        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
    }
    utils.infoLog("Alarm Notification Created");
}
Also used : GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) AlarmManager(android.app.AlarmManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) KrollDict(org.appcelerator.kroll.KrollDict) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

KrollDict (org.appcelerator.kroll.KrollDict)8 Intent (android.content.Intent)6 AlarmManager (android.app.AlarmManager)5 PendingIntent (android.app.PendingIntent)5 HashMap (java.util.HashMap)4 Calendar (java.util.Calendar)3 GregorianCalendar (java.util.GregorianCalendar)3 SimpleDateFormat (java.text.SimpleDateFormat)2 Activity (android.app.Activity)1 RecognizerIntent (android.speech.RecognizerIntent)1 TiActivitySupport (org.appcelerator.titanium.util.TiActivitySupport)1