void download_complete_handler(NetDownload *download) { printf("Loaded image with %lu bytes", download->length); printf("Heap free is %u bytes", heap_bytes_free()); #ifdef PBL_PLATFORM_APLITE GBitmap *bmp = gbitmap_create_with_png_data(download->data, download->length); #else GBitmap *bmp = gbitmap_create_from_png_data(download->data, download->length); #endif bitmap_layer_set_bitmap(bitmap_layer, bmp); // Save pointer to currently shown bitmap (to free it) if (current_bmp) { gbitmap_destroy(current_bmp); } current_bmp = bmp; // Free the memory now #ifdef PBL_PLATFORM_APLITE // gbitmap_create_with_png_data will free download->data #else free(download->data); #endif // We null it out now to avoid a double free download->data = NULL; netdownload_destroy(download); // Change tick handler to every minute now to save some battery tick_timer_service_unsubscribe(); tick_timer_service_subscribe(MINUTE_UNIT, tick_handler); time_t temp = time(NULL); last_check = localtime(&temp); }
static void cb_in_received_handler(DictionaryIterator *iter, void *context) { // Get the bitmap Tuple *size_tuple = dict_find(iter, KEY_SIZE); if (size_tuple){ if (data_image) { free(data_image); data_image = NULL; } data_size = size_tuple->value->uint32; data_image = malloc(data_size); } Tuple *image_tuple = dict_find(iter, KEY_IMAGE); Tuple *index_tuple = dict_find(iter, KEY_INDEX); if (index_tuple && image_tuple) { int32_t index = index_tuple->value->int32; APP_LOG(APP_LOG_LEVEL_DEBUG, "image received index=%ld size=%d", index, image_tuple->length); memcpy(data_image + index, &image_tuple->value->uint8, image_tuple->length); if (image_tuple->length < CHUNK_SIZE){ if (image){ gbitmap_destroy(image); image = NULL; } #ifdef PBL_COLOR image = gbitmap_create_from_png_data(data_image, data_size); #else image = gbitmap_create_with_data(data_image); #endif bitmap_layer_set_bitmap(image_layer, image); text_layer_set_text(webcam_status_text_layer, ""); layer_mark_dirty(bitmap_layer_get_layer(image_layer)); } } Tuple *message_tuple = dict_find(iter, KEY_MESSAGE); if (message_tuple){ text_layer_set_text(webcam_status_text_layer, message_tuple->value->cstring); } }
SDK_3_USAGE static GBitmap *create_bitmap_with_png_data(SimplyImage *image, void *data) { CreateDataContext *ctx = data; return ctx->data ? gbitmap_create_from_png_data(ctx->data, ctx->data_length) : NULL; }
static void cb_in_received_handler(DictionaryIterator *iter, void *context) { APP_LOG(APP_LOG_LEVEL_DEBUG, "Incoming data"); // Reset ErrorExists = 0; text_layer_set_text(error_text_layer, ""); // Set any messages Tuple *message_tuple = dict_find(iter, KEY_MESSAGE); if(message_tuple){ strcpy(msg_str, message_tuple->value->cstring); text_layer_set_text(message_text_layer, msg_str); layer_mark_dirty(text_layer_get_layer(message_text_layer)); } // Get the bitmap Tuple *size_tuple = dict_find(iter, KEY_SIZE); if(size_tuple){ if(data_image) free(data_image); data_size = size_tuple->value->uint32; data_image = malloc(data_size); } // Set the image Tuple *image_tuple = dict_find(iter, KEY_IMAGE); Tuple *index_tuple = dict_find(iter, KEY_INDEX); if (index_tuple && image_tuple) { int32_t index = index_tuple->value->int32; //APP_LOG(APP_LOG_LEVEL_DEBUG, "image received index=%ld size=%d", index, image_tuple->length); memcpy(data_image + index,&image_tuple->value->uint8,image_tuple->length); if(image_tuple->length < CHUNK_SIZE){ // Clear the image if(image){ gbitmap_destroy(image); image = NULL; } #ifdef PBL_COLOR image = gbitmap_create_from_png_data(data_image, data_size); #else image = gbitmap_create_with_data(data_image); #endif bitmap_layer_set_bitmap(image_layer, image); layer_mark_dirty(bitmap_layer_get_layer(image_layer)); text_layer_set_text(message_text_layer,""); layer_mark_dirty(text_layer_get_layer(message_text_layer)); vibes_short_pulse(); light_enable_interaction(); ImgLoaded = 1; Loading = 0; text_layer_set_text(username_text_layer, usr_str); text_layer_set_background_color(username_text_layer, GColorWhite); if(ErrorExists == 1){ text_layer_set_background_color(username_text_layer, GColorClear); } layer_mark_dirty(text_layer_get_layer(username_text_layer)); } } // Set any errors Tuple *error_tuple = dict_find(iter, KEY_ERROR); if(error_tuple){ text_layer_set_text(message_text_layer,""); layer_mark_dirty(text_layer_get_layer(message_text_layer)); // Clear the image if(image){ gbitmap_destroy(image); if(data_image){ free(data_image); } image = NULL; bitmap_layer_set_bitmap(image_layer, image); } Loading = 0; ErrorExists = 1; #ifdef PBL_COLOR image = gbitmap_create_with_resource(RESOURCE_ID_ERROR_B); text_layer_set_text_color(error_text_layer, GColorBlack); #else image = gbitmap_create_with_resource(RESOURCE_ID_ERROR_A); #endif bitmap_layer_set_bitmap(image_layer, image); layer_mark_dirty(bitmap_layer_get_layer(image_layer)); text_layer_set_text(error_text_layer, error_tuple->value->cstring); layer_mark_dirty(text_layer_get_layer(error_text_layer)); vibes_short_pulse(); light_enable_interaction(); } // Prepare the username Tuple *username_tuple = dict_find(iter, KEY_USERNAME); if(username_tuple){ strcpy(usr_str, username_tuple->value->cstring); prepend(usr_str, " "); //text_layer_set_text(username_text_layer, usr_str); } // Set the likes Tuple *likes_tuple = dict_find(iter, KEY_LIKES); if(likes_tuple){ strcpy(likes_str, likes_tuple->value->cstring); update_likes(likes_str); } // Set the comments Tuple *comments_tuple = dict_find(iter, KEY_COMMENTS); if(comments_tuple){ strcpy(comments_str, comments_tuple->value->cstring); update_comments(comments_str); } // Set the caption Tuple *caption_tuple = dict_find(iter, KEY_CAPTION); if(caption_tuple){ strcpy(capt_str, caption_tuple->value->cstring); update_caption(capt_str); } }