michael@0: /* michael@0: Licensed to the Apache Software Foundation (ASF) under one michael@0: or more contributor license agreements. See the NOTICE file michael@0: distributed with this work for additional information michael@0: regarding copyright ownership. The ASF licenses this file michael@0: to you under the Apache License, Version 2.0 (the michael@0: "License"); you may not use this file except in compliance michael@0: with the License. You may obtain a copy of the License at michael@0: michael@0: http://www.apache.org/licenses/LICENSE-2.0 michael@0: michael@0: Unless required by applicable law or agreed to in writing, michael@0: software distributed under the License is distributed on an michael@0: "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: KIND, either express or implied. See the License for the michael@0: specific language governing permissions and limitations michael@0: under the License. michael@0: */ michael@0: michael@0: #import "CDVNotification.h" michael@0: #import michael@0: #import michael@0: michael@0: #define DIALOG_TYPE_ALERT @"alert" michael@0: #define DIALOG_TYPE_PROMPT @"prompt" michael@0: michael@0: static void soundCompletionCallback(SystemSoundID ssid, void* data); michael@0: michael@0: @implementation CDVNotification michael@0: michael@0: /* michael@0: * showDialogWithMessage - Common method to instantiate the alert view for alert, confirm, and prompt notifications. michael@0: * Parameters: michael@0: * message The alert view message. michael@0: * title The alert view title. michael@0: * buttons The array of customized strings for the buttons. michael@0: * defaultText The input text for the textbox (if textbox exists). michael@0: * callbackId The commmand callback id. michael@0: * dialogType The type of alert view [alert | prompt]. michael@0: */ michael@0: - (void)showDialogWithMessage:(NSString*)message title:(NSString*)title buttons:(NSArray*)buttons defaultText:(NSString*)defaultText callbackId:(NSString*)callbackId dialogType:(NSString*)dialogType michael@0: { michael@0: CDVAlertView* alertView = [[CDVAlertView alloc] michael@0: initWithTitle:title michael@0: message:message michael@0: delegate:self michael@0: cancelButtonTitle:nil michael@0: otherButtonTitles:nil]; michael@0: michael@0: alertView.callbackId = callbackId; michael@0: michael@0: NSUInteger count = [buttons count]; michael@0: michael@0: for (int n = 0; n < count; n++) { michael@0: [alertView addButtonWithTitle:[buttons objectAtIndex:n]]; michael@0: } michael@0: michael@0: if ([dialogType isEqualToString:DIALOG_TYPE_PROMPT]) { michael@0: alertView.alertViewStyle = UIAlertViewStylePlainTextInput; michael@0: UITextField* textField = [alertView textFieldAtIndex:0]; michael@0: textField.text = defaultText; michael@0: } michael@0: michael@0: [alertView show]; michael@0: } michael@0: michael@0: - (void)alert:(CDVInvokedUrlCommand*)command michael@0: { michael@0: NSString* callbackId = command.callbackId; michael@0: NSString* message = [command argumentAtIndex:0]; michael@0: NSString* title = [command argumentAtIndex:1]; michael@0: NSString* buttons = [command argumentAtIndex:2]; michael@0: michael@0: [self showDialogWithMessage:message title:title buttons:@[buttons] defaultText:nil callbackId:callbackId dialogType:DIALOG_TYPE_ALERT]; michael@0: } michael@0: michael@0: - (void)confirm:(CDVInvokedUrlCommand*)command michael@0: { michael@0: NSString* callbackId = command.callbackId; michael@0: NSString* message = [command argumentAtIndex:0]; michael@0: NSString* title = [command argumentAtIndex:1]; michael@0: NSArray* buttons = [command argumentAtIndex:2]; michael@0: michael@0: [self showDialogWithMessage:message title:title buttons:buttons defaultText:nil callbackId:callbackId dialogType:DIALOG_TYPE_ALERT]; michael@0: } michael@0: michael@0: - (void)prompt:(CDVInvokedUrlCommand*)command michael@0: { michael@0: NSString* callbackId = command.callbackId; michael@0: NSString* message = [command argumentAtIndex:0]; michael@0: NSString* title = [command argumentAtIndex:1]; michael@0: NSArray* buttons = [command argumentAtIndex:2]; michael@0: NSString* defaultText = [command argumentAtIndex:3]; michael@0: michael@0: [self showDialogWithMessage:message title:title buttons:buttons defaultText:defaultText callbackId:callbackId dialogType:DIALOG_TYPE_PROMPT]; michael@0: } michael@0: michael@0: /** michael@0: * Callback invoked when an alert dialog's buttons are clicked. michael@0: */ michael@0: - (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex michael@0: { michael@0: CDVAlertView* cdvAlertView = (CDVAlertView*)alertView; michael@0: CDVPluginResult* result; michael@0: michael@0: // Determine what gets returned to JS based on the alert view type. michael@0: if (alertView.alertViewStyle == UIAlertViewStyleDefault) { michael@0: // For alert and confirm, return button index as int back to JS. michael@0: result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:(int)(buttonIndex + 1)]; michael@0: } else { michael@0: // For prompt, return button index and input text back to JS. michael@0: NSString* value0 = [[alertView textFieldAtIndex:0] text]; michael@0: NSDictionary* info = @{ michael@0: @"buttonIndex":@(buttonIndex + 1), michael@0: @"input1":(value0 ? value0 : [NSNull null]) michael@0: }; michael@0: result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:info]; michael@0: } michael@0: [self.commandDelegate sendPluginResult:result callbackId:cdvAlertView.callbackId]; michael@0: } michael@0: michael@0: static void playBeep(int count) { michael@0: SystemSoundID completeSound; michael@0: NSInteger cbDataCount = count; michael@0: NSURL* audioPath = [[NSBundle mainBundle] URLForResource:@"CDVNotification.bundle/beep" withExtension:@"wav"]; michael@0: #if __has_feature(objc_arc) michael@0: AudioServicesCreateSystemSoundID((__bridge CFURLRef)audioPath, &completeSound); michael@0: #else michael@0: AudioServicesCreateSystemSoundID((CFURLRef)audioPath, &completeSound); michael@0: #endif michael@0: AudioServicesAddSystemSoundCompletion(completeSound, NULL, NULL, soundCompletionCallback, (void*)(cbDataCount-1)); michael@0: AudioServicesPlaySystemSound(completeSound); michael@0: } michael@0: michael@0: static void soundCompletionCallback(SystemSoundID ssid, void* data) { michael@0: int count = (int)data; michael@0: AudioServicesRemoveSystemSoundCompletion (ssid); michael@0: AudioServicesDisposeSystemSoundID(ssid); michael@0: if (count > 0) { michael@0: playBeep(count); michael@0: } michael@0: } michael@0: michael@0: - (void)beep:(CDVInvokedUrlCommand*)command michael@0: { michael@0: NSNumber* count = [command.arguments objectAtIndex:0 withDefault:[NSNumber numberWithInt:1]]; michael@0: playBeep([count intValue]); michael@0: } michael@0: michael@0: michael@0: @end michael@0: michael@0: @implementation CDVAlertView michael@0: michael@0: @synthesize callbackId; michael@0: michael@0: @end