FIRMessagingUtilities.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FirebaseMessaging/Sources/FIRMessagingUtilities.h"
  17. #import "FirebaseMessaging/Sources/FIRMessagingLogger.h"
  18. #import <GoogleUtilities/GULAppEnvironmentUtil.h>
  19. static const uint64_t kBytesToMegabytesDivisor = 1024 * 1024LL;
  20. #pragma mark - Time
  21. int64_t FIRMessagingCurrentTimestampInSeconds(void) {
  22. return (int64_t)[[NSDate date] timeIntervalSince1970];
  23. }
  24. int64_t FIRMessagingCurrentTimestampInMilliseconds(void) {
  25. return (int64_t)(FIRMessagingCurrentTimestampInSeconds() * 1000.0);
  26. }
  27. #pragma mark - App Info
  28. NSString *FIRMessagingCurrentAppVersion(void) {
  29. NSString *version = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
  30. if (![version length]) {
  31. FIRMessagingLoggerError(kFIRMessagingMessageCodeUtilities000,
  32. @"Could not find current app version");
  33. return @"";
  34. }
  35. return version;
  36. }
  37. NSString *FIRMessagingBundleIDByRemovingLastPartFrom(NSString *bundleID) {
  38. NSString *bundleIDComponentsSeparator = @".";
  39. NSMutableArray<NSString *> *bundleIDComponents =
  40. [[bundleID componentsSeparatedByString:bundleIDComponentsSeparator] mutableCopy];
  41. [bundleIDComponents removeLastObject];
  42. return [bundleIDComponents componentsJoinedByString:bundleIDComponentsSeparator];
  43. }
  44. NSString *FIRMessagingAppIdentifier(void) {
  45. NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
  46. #if TARGET_OS_WATCH
  47. // The code is running in watchKit extension target but the actually bundleID is in the watchKit
  48. // target. So we need to remove the last part of the bundle ID in watchKit extension to match
  49. // the one in watchKit target.
  50. return FIRMessagingBundleIDByRemovingLastPartFrom(bundleID);
  51. #else
  52. return bundleID;
  53. #endif
  54. }
  55. uint64_t FIRMessagingGetFreeDiskSpaceInMB(void) {
  56. NSError *error;
  57. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  58. NSDictionary *attributesMap =
  59. [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject]
  60. error:&error];
  61. if (attributesMap) {
  62. uint64_t totalSizeInBytes __unused = [attributesMap[NSFileSystemSize] longLongValue];
  63. uint64_t freeSizeInBytes = [attributesMap[NSFileSystemFreeSize] longLongValue];
  64. FIRMessagingLoggerDebug(
  65. kFIRMessagingMessageCodeUtilities001, @"Device has capacity %llu MB with %llu MB free.",
  66. totalSizeInBytes / kBytesToMegabytesDivisor, freeSizeInBytes / kBytesToMegabytesDivisor);
  67. return ((double)freeSizeInBytes) / kBytesToMegabytesDivisor;
  68. } else {
  69. FIRMessagingLoggerError(kFIRMessagingMessageCodeUtilities002,
  70. @"Error in retreiving device's free memory %@", error);
  71. return 0;
  72. }
  73. }
  74. NSSearchPathDirectory FIRMessagingSupportedDirectory(void) {
  75. #if TARGET_OS_TV
  76. return NSCachesDirectory;
  77. #else
  78. return NSApplicationSupportDirectory;
  79. #endif
  80. }