1
0

ApplicationBase.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include "ApplicationBase.h"
  2. #include "foundation/error.h"
  3. #include "application/features.h"
  4. #include <stdio.h> // for sprintf
  5. #ifdef __ANDROID__
  6. #include <android/log.h>
  7. #endif
  8. ApplicationBase::ApplicationBase()
  9. {
  10. data_path = 0;
  11. all_permissions_enabled = false;
  12. device_id = 0;
  13. }
  14. ApplicationBase::~ApplicationBase()
  15. {
  16. NXURIRelease(data_path);
  17. data_path = 0;
  18. NXStringRelease(device_id);
  19. device_id = 0;
  20. }
  21. int ApplicationBase::Initialize()
  22. {
  23. return NErr_Success;
  24. }
  25. /* and call this after doing your own shutdown */
  26. void ApplicationBase::Shutdown()
  27. {
  28. NXURIRelease(data_path);
  29. data_path = 0;
  30. }
  31. void ApplicationBase::SetDataPath(nx_uri_t new_data_path)
  32. {
  33. nx_uri_t old_path = data_path;
  34. data_path = NXURIRetain(new_data_path);
  35. NXURIRelease(old_path);
  36. }
  37. void ApplicationBase::SetPermission(GUID feature)
  38. {
  39. permissions.insert(feature);
  40. }
  41. void ApplicationBase::RemovePermission(GUID permission)
  42. {
  43. permissions.erase(permission);
  44. }
  45. void ApplicationBase::EnableAllPermissions()
  46. {
  47. all_permissions_enabled=true;
  48. }
  49. void ApplicationBase::ClearPermissions()
  50. {
  51. permissions.clear();
  52. }
  53. void ApplicationBase::NotifyPermissions(api_syscb *system_callbacks)
  54. {
  55. if (system_callbacks)
  56. system_callbacks->IssueCallback(Features::event_type, Features::permissions_changed);
  57. }
  58. int ApplicationBase::Application_GetDataPath(nx_uri_t *path)
  59. {
  60. *path=NXURIRetain(data_path);
  61. if (data_path)
  62. return NErr_Success;
  63. else
  64. return NErr_Empty;
  65. }
  66. int ApplicationBase::Application_GetPermission(GUID feature)
  67. {
  68. if (all_permissions_enabled)
  69. return NErr_True;
  70. else if (permissions.find(feature) == permissions.end())
  71. return NErr_False;
  72. else
  73. return NErr_True;
  74. }
  75. int ApplicationBase::Application_GetFeature(GUID feature)
  76. {
  77. if (features.find(feature) == features.end())
  78. return NErr_False;
  79. else
  80. return NErr_True;
  81. }
  82. void ApplicationBase::Application_SetFeature(GUID feature)
  83. {
  84. features.insert(feature);
  85. }
  86. void ApplicationBase::SetDeviceID(nx_string_t device_id)
  87. {
  88. nx_string_t old = this->device_id;
  89. this->device_id = NXStringRetain(device_id);
  90. NXStringRelease(old);
  91. }
  92. static void GUIDtoCString(const GUID &guid, char *target)
  93. {
  94. //{2E9CE2F8-E26D-4629-A3FF-5DF619136B2C}
  95. sprintf(target, "{%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x}",
  96. (int)guid.Data1, (int)guid.Data2, (int)guid.Data3,
  97. (int)guid.Data4[0], (int)guid.Data4[1],
  98. (int)guid.Data4[2], (int)guid.Data4[3],
  99. (int)guid.Data4[4], (int)guid.Data4[5],
  100. (int)guid.Data4[6], (int)guid.Data4[7] );
  101. }
  102. static const char *GetFeatureName(const GUID &guid)
  103. {
  104. if (guid == Features::aac_playback)
  105. return "AAC Playback";
  106. else if (guid == Features::gapless)
  107. return "Gapless Playback";
  108. else if (guid == Features::flac_playback)
  109. return "FLAC Playback";
  110. else if (guid == Features::gracenote_autotag)
  111. return "Gracenote Autotagger";
  112. else
  113. return 0; /* the lack of of return 0 by default here was why it was crashing */
  114. }
  115. void ApplicationBase::DumpPermissions()
  116. {
  117. #ifdef __ANDROID__
  118. char guid_string[64];
  119. FeatureList::iterator itr;
  120. for (itr=features.begin();itr!=features.end();itr++)
  121. {
  122. GUIDtoCString(*itr, guid_string);
  123. const char *feature_name = GetFeatureName(*itr);
  124. if (feature_name)
  125. __android_log_print(ANDROID_LOG_INFO, "libreplicant", "[Feature] %s (%s)", guid_string, feature_name);
  126. else
  127. __android_log_print(ANDROID_LOG_INFO, "libreplicant", "[Feature] %s", guid_string);
  128. }
  129. for (itr=permissions.begin();itr!=permissions.end();itr++)
  130. {
  131. GUIDtoCString(*itr, guid_string);
  132. const char *feature_name = GetFeatureName(*itr);
  133. if (feature_name)
  134. __android_log_print(ANDROID_LOG_INFO, "libreplicant", "[Permission] %s (%s)", guid_string, feature_name);
  135. else
  136. __android_log_print(ANDROID_LOG_INFO, "libreplicant", "[Permission] %s", guid_string);
  137. }
  138. #endif
  139. }
  140. int ApplicationBase::Application_GetDeviceID(nx_string_t *value)
  141. {
  142. if (!device_id)
  143. return NErr_Empty;
  144. *value = NXStringRetain(device_id);
  145. return NErr_Success;
  146. }