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