Touchgui/plugins/org.apache.cordova.dialogs/src/ios/CDVNotification.m

Thu, 04 Jun 2015 14:50:33 +0200

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 04 Jun 2015 14:50:33 +0200
changeset 0
e8ccd40d0ef6
permissions
-rw-r--r--

Genesis of lecture sources for Droidcon Berlin 2015 in Postbahnhof.

michael@0 1 /*
michael@0 2 Licensed to the Apache Software Foundation (ASF) under one
michael@0 3 or more contributor license agreements. See the NOTICE file
michael@0 4 distributed with this work for additional information
michael@0 5 regarding copyright ownership. The ASF licenses this file
michael@0 6 to you under the Apache License, Version 2.0 (the
michael@0 7 "License"); you may not use this file except in compliance
michael@0 8 with the License. You may obtain a copy of the License at
michael@0 9
michael@0 10 http://www.apache.org/licenses/LICENSE-2.0
michael@0 11
michael@0 12 Unless required by applicable law or agreed to in writing,
michael@0 13 software distributed under the License is distributed on an
michael@0 14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0 15 KIND, either express or implied. See the License for the
michael@0 16 specific language governing permissions and limitations
michael@0 17 under the License.
michael@0 18 */
michael@0 19
michael@0 20 #import "CDVNotification.h"
michael@0 21 #import <Cordova/NSDictionary+Extensions.h>
michael@0 22 #import <Cordova/NSArray+Comparisons.h>
michael@0 23
michael@0 24 #define DIALOG_TYPE_ALERT @"alert"
michael@0 25 #define DIALOG_TYPE_PROMPT @"prompt"
michael@0 26
michael@0 27 static void soundCompletionCallback(SystemSoundID ssid, void* data);
michael@0 28
michael@0 29 @implementation CDVNotification
michael@0 30
michael@0 31 /*
michael@0 32 * showDialogWithMessage - Common method to instantiate the alert view for alert, confirm, and prompt notifications.
michael@0 33 * Parameters:
michael@0 34 * message The alert view message.
michael@0 35 * title The alert view title.
michael@0 36 * buttons The array of customized strings for the buttons.
michael@0 37 * defaultText The input text for the textbox (if textbox exists).
michael@0 38 * callbackId The commmand callback id.
michael@0 39 * dialogType The type of alert view [alert | prompt].
michael@0 40 */
michael@0 41 - (void)showDialogWithMessage:(NSString*)message title:(NSString*)title buttons:(NSArray*)buttons defaultText:(NSString*)defaultText callbackId:(NSString*)callbackId dialogType:(NSString*)dialogType
michael@0 42 {
michael@0 43 CDVAlertView* alertView = [[CDVAlertView alloc]
michael@0 44 initWithTitle:title
michael@0 45 message:message
michael@0 46 delegate:self
michael@0 47 cancelButtonTitle:nil
michael@0 48 otherButtonTitles:nil];
michael@0 49
michael@0 50 alertView.callbackId = callbackId;
michael@0 51
michael@0 52 NSUInteger count = [buttons count];
michael@0 53
michael@0 54 for (int n = 0; n < count; n++) {
michael@0 55 [alertView addButtonWithTitle:[buttons objectAtIndex:n]];
michael@0 56 }
michael@0 57
michael@0 58 if ([dialogType isEqualToString:DIALOG_TYPE_PROMPT]) {
michael@0 59 alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
michael@0 60 UITextField* textField = [alertView textFieldAtIndex:0];
michael@0 61 textField.text = defaultText;
michael@0 62 }
michael@0 63
michael@0 64 [alertView show];
michael@0 65 }
michael@0 66
michael@0 67 - (void)alert:(CDVInvokedUrlCommand*)command
michael@0 68 {
michael@0 69 NSString* callbackId = command.callbackId;
michael@0 70 NSString* message = [command argumentAtIndex:0];
michael@0 71 NSString* title = [command argumentAtIndex:1];
michael@0 72 NSString* buttons = [command argumentAtIndex:2];
michael@0 73
michael@0 74 [self showDialogWithMessage:message title:title buttons:@[buttons] defaultText:nil callbackId:callbackId dialogType:DIALOG_TYPE_ALERT];
michael@0 75 }
michael@0 76
michael@0 77 - (void)confirm:(CDVInvokedUrlCommand*)command
michael@0 78 {
michael@0 79 NSString* callbackId = command.callbackId;
michael@0 80 NSString* message = [command argumentAtIndex:0];
michael@0 81 NSString* title = [command argumentAtIndex:1];
michael@0 82 NSArray* buttons = [command argumentAtIndex:2];
michael@0 83
michael@0 84 [self showDialogWithMessage:message title:title buttons:buttons defaultText:nil callbackId:callbackId dialogType:DIALOG_TYPE_ALERT];
michael@0 85 }
michael@0 86
michael@0 87 - (void)prompt:(CDVInvokedUrlCommand*)command
michael@0 88 {
michael@0 89 NSString* callbackId = command.callbackId;
michael@0 90 NSString* message = [command argumentAtIndex:0];
michael@0 91 NSString* title = [command argumentAtIndex:1];
michael@0 92 NSArray* buttons = [command argumentAtIndex:2];
michael@0 93 NSString* defaultText = [command argumentAtIndex:3];
michael@0 94
michael@0 95 [self showDialogWithMessage:message title:title buttons:buttons defaultText:defaultText callbackId:callbackId dialogType:DIALOG_TYPE_PROMPT];
michael@0 96 }
michael@0 97
michael@0 98 /**
michael@0 99 * Callback invoked when an alert dialog's buttons are clicked.
michael@0 100 */
michael@0 101 - (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
michael@0 102 {
michael@0 103 CDVAlertView* cdvAlertView = (CDVAlertView*)alertView;
michael@0 104 CDVPluginResult* result;
michael@0 105
michael@0 106 // Determine what gets returned to JS based on the alert view type.
michael@0 107 if (alertView.alertViewStyle == UIAlertViewStyleDefault) {
michael@0 108 // For alert and confirm, return button index as int back to JS.
michael@0 109 result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:(int)(buttonIndex + 1)];
michael@0 110 } else {
michael@0 111 // For prompt, return button index and input text back to JS.
michael@0 112 NSString* value0 = [[alertView textFieldAtIndex:0] text];
michael@0 113 NSDictionary* info = @{
michael@0 114 @"buttonIndex":@(buttonIndex + 1),
michael@0 115 @"input1":(value0 ? value0 : [NSNull null])
michael@0 116 };
michael@0 117 result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:info];
michael@0 118 }
michael@0 119 [self.commandDelegate sendPluginResult:result callbackId:cdvAlertView.callbackId];
michael@0 120 }
michael@0 121
michael@0 122 static void playBeep(int count) {
michael@0 123 SystemSoundID completeSound;
michael@0 124 NSInteger cbDataCount = count;
michael@0 125 NSURL* audioPath = [[NSBundle mainBundle] URLForResource:@"CDVNotification.bundle/beep" withExtension:@"wav"];
michael@0 126 #if __has_feature(objc_arc)
michael@0 127 AudioServicesCreateSystemSoundID((__bridge CFURLRef)audioPath, &completeSound);
michael@0 128 #else
michael@0 129 AudioServicesCreateSystemSoundID((CFURLRef)audioPath, &completeSound);
michael@0 130 #endif
michael@0 131 AudioServicesAddSystemSoundCompletion(completeSound, NULL, NULL, soundCompletionCallback, (void*)(cbDataCount-1));
michael@0 132 AudioServicesPlaySystemSound(completeSound);
michael@0 133 }
michael@0 134
michael@0 135 static void soundCompletionCallback(SystemSoundID ssid, void* data) {
michael@0 136 int count = (int)data;
michael@0 137 AudioServicesRemoveSystemSoundCompletion (ssid);
michael@0 138 AudioServicesDisposeSystemSoundID(ssid);
michael@0 139 if (count > 0) {
michael@0 140 playBeep(count);
michael@0 141 }
michael@0 142 }
michael@0 143
michael@0 144 - (void)beep:(CDVInvokedUrlCommand*)command
michael@0 145 {
michael@0 146 NSNumber* count = [command.arguments objectAtIndex:0 withDefault:[NSNumber numberWithInt:1]];
michael@0 147 playBeep([count intValue]);
michael@0 148 }
michael@0 149
michael@0 150
michael@0 151 @end
michael@0 152
michael@0 153 @implementation CDVAlertView
michael@0 154
michael@0 155 @synthesize callbackId;
michael@0 156
michael@0 157 @end

mercurial