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

changeset 0
e8ccd40d0ef6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Touchgui/plugins/org.apache.cordova.dialogs/src/ios/CDVNotification.m	Thu Jun 04 14:50:33 2015 +0200
     1.3 @@ -0,0 +1,157 @@
     1.4 +/*
     1.5 + Licensed to the Apache Software Foundation (ASF) under one
     1.6 + or more contributor license agreements.  See the NOTICE file
     1.7 + distributed with this work for additional information
     1.8 + regarding copyright ownership.  The ASF licenses this file
     1.9 + to you under the Apache License, Version 2.0 (the
    1.10 + "License"); you may not use this file except in compliance
    1.11 + with the License.  You may obtain a copy of the License at
    1.12 +
    1.13 + http://www.apache.org/licenses/LICENSE-2.0
    1.14 +
    1.15 + Unless required by applicable law or agreed to in writing,
    1.16 + software distributed under the License is distributed on an
    1.17 + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    1.18 + KIND, either express or implied.  See the License for the
    1.19 + specific language governing permissions and limitations
    1.20 + under the License.
    1.21 + */
    1.22 +
    1.23 +#import "CDVNotification.h"
    1.24 +#import <Cordova/NSDictionary+Extensions.h>
    1.25 +#import <Cordova/NSArray+Comparisons.h>
    1.26 +
    1.27 +#define DIALOG_TYPE_ALERT @"alert"
    1.28 +#define DIALOG_TYPE_PROMPT @"prompt"
    1.29 +
    1.30 +static void soundCompletionCallback(SystemSoundID ssid, void* data);
    1.31 +
    1.32 +@implementation CDVNotification
    1.33 +
    1.34 +/*
    1.35 + * showDialogWithMessage - Common method to instantiate the alert view for alert, confirm, and prompt notifications.
    1.36 + * Parameters:
    1.37 + *  message       The alert view message.
    1.38 + *  title         The alert view title.
    1.39 + *  buttons       The array of customized strings for the buttons.
    1.40 + *  defaultText   The input text for the textbox (if textbox exists).
    1.41 + *  callbackId    The commmand callback id.
    1.42 + *  dialogType    The type of alert view [alert | prompt].
    1.43 + */
    1.44 +- (void)showDialogWithMessage:(NSString*)message title:(NSString*)title buttons:(NSArray*)buttons defaultText:(NSString*)defaultText callbackId:(NSString*)callbackId dialogType:(NSString*)dialogType
    1.45 +{
    1.46 +    CDVAlertView* alertView = [[CDVAlertView alloc]
    1.47 +        initWithTitle:title
    1.48 +                  message:message
    1.49 +                 delegate:self
    1.50 +        cancelButtonTitle:nil
    1.51 +        otherButtonTitles:nil];
    1.52 +
    1.53 +    alertView.callbackId = callbackId;
    1.54 +
    1.55 +    NSUInteger count = [buttons count];
    1.56 +
    1.57 +    for (int n = 0; n < count; n++) {
    1.58 +        [alertView addButtonWithTitle:[buttons objectAtIndex:n]];
    1.59 +    }
    1.60 +
    1.61 +    if ([dialogType isEqualToString:DIALOG_TYPE_PROMPT]) {
    1.62 +        alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
    1.63 +        UITextField* textField = [alertView textFieldAtIndex:0];
    1.64 +        textField.text = defaultText;
    1.65 +    }
    1.66 +
    1.67 +    [alertView show];
    1.68 +}
    1.69 +
    1.70 +- (void)alert:(CDVInvokedUrlCommand*)command
    1.71 +{
    1.72 +    NSString* callbackId = command.callbackId;
    1.73 +    NSString* message = [command argumentAtIndex:0];
    1.74 +    NSString* title = [command argumentAtIndex:1];
    1.75 +    NSString* buttons = [command argumentAtIndex:2];
    1.76 +
    1.77 +    [self showDialogWithMessage:message title:title buttons:@[buttons] defaultText:nil callbackId:callbackId dialogType:DIALOG_TYPE_ALERT];
    1.78 +}
    1.79 +
    1.80 +- (void)confirm:(CDVInvokedUrlCommand*)command
    1.81 +{
    1.82 +    NSString* callbackId = command.callbackId;
    1.83 +    NSString* message = [command argumentAtIndex:0];
    1.84 +    NSString* title = [command argumentAtIndex:1];
    1.85 +    NSArray* buttons = [command argumentAtIndex:2];
    1.86 +
    1.87 +    [self showDialogWithMessage:message title:title buttons:buttons defaultText:nil callbackId:callbackId dialogType:DIALOG_TYPE_ALERT];
    1.88 +}
    1.89 +
    1.90 +- (void)prompt:(CDVInvokedUrlCommand*)command
    1.91 +{
    1.92 +    NSString* callbackId = command.callbackId;
    1.93 +    NSString* message = [command argumentAtIndex:0];
    1.94 +    NSString* title = [command argumentAtIndex:1];
    1.95 +    NSArray* buttons = [command argumentAtIndex:2];
    1.96 +    NSString* defaultText = [command argumentAtIndex:3];
    1.97 +
    1.98 +    [self showDialogWithMessage:message title:title buttons:buttons defaultText:defaultText callbackId:callbackId dialogType:DIALOG_TYPE_PROMPT];
    1.99 +}
   1.100 +
   1.101 +/**
   1.102 +  * Callback invoked when an alert dialog's buttons are clicked.
   1.103 +  */
   1.104 +- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
   1.105 +{
   1.106 +    CDVAlertView* cdvAlertView = (CDVAlertView*)alertView;
   1.107 +    CDVPluginResult* result;
   1.108 +
   1.109 +    // Determine what gets returned to JS based on the alert view type.
   1.110 +    if (alertView.alertViewStyle == UIAlertViewStyleDefault) {
   1.111 +        // For alert and confirm, return button index as int back to JS.
   1.112 +        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:(int)(buttonIndex + 1)];
   1.113 +    } else {
   1.114 +        // For prompt, return button index and input text back to JS.
   1.115 +        NSString* value0 = [[alertView textFieldAtIndex:0] text];
   1.116 +        NSDictionary* info = @{
   1.117 +            @"buttonIndex":@(buttonIndex + 1),
   1.118 +            @"input1":(value0 ? value0 : [NSNull null])
   1.119 +        };
   1.120 +        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:info];
   1.121 +    }
   1.122 +    [self.commandDelegate sendPluginResult:result callbackId:cdvAlertView.callbackId];
   1.123 +}
   1.124 +
   1.125 +static void playBeep(int count) {
   1.126 +    SystemSoundID completeSound;
   1.127 +    NSInteger cbDataCount = count;
   1.128 +    NSURL* audioPath = [[NSBundle mainBundle] URLForResource:@"CDVNotification.bundle/beep" withExtension:@"wav"];
   1.129 +    #if __has_feature(objc_arc)
   1.130 +        AudioServicesCreateSystemSoundID((__bridge CFURLRef)audioPath, &completeSound);
   1.131 +    #else
   1.132 +        AudioServicesCreateSystemSoundID((CFURLRef)audioPath, &completeSound);
   1.133 +    #endif
   1.134 +    AudioServicesAddSystemSoundCompletion(completeSound, NULL, NULL, soundCompletionCallback, (void*)(cbDataCount-1));
   1.135 +    AudioServicesPlaySystemSound(completeSound);
   1.136 +}
   1.137 +
   1.138 +static void soundCompletionCallback(SystemSoundID  ssid, void* data) {
   1.139 +    int count = (int)data;
   1.140 +    AudioServicesRemoveSystemSoundCompletion (ssid);
   1.141 +    AudioServicesDisposeSystemSoundID(ssid);
   1.142 +    if (count > 0) {
   1.143 +        playBeep(count);
   1.144 +    }
   1.145 +}
   1.146 +
   1.147 +- (void)beep:(CDVInvokedUrlCommand*)command
   1.148 +{
   1.149 +    NSNumber* count = [command.arguments objectAtIndex:0 withDefault:[NSNumber numberWithInt:1]];
   1.150 +    playBeep([count intValue]);
   1.151 +}
   1.152 +
   1.153 +
   1.154 +@end
   1.155 +
   1.156 +@implementation CDVAlertView
   1.157 +
   1.158 +@synthesize callbackId;
   1.159 +
   1.160 +@end

mercurial