2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FApp_AppResourceImpl.cpp
19 * @brief This is the implementation for the _AppResourceImpl class.
22 #include <unique_ptr.h>
24 #include <FBaseResult.h>
25 #include <FBaseSysLog.h>
26 #include <FBaseColHashMap.h>
27 #include <FBaseRt_LibraryImpl.h>
29 #include <FAppAppResource.h>
30 #include "FApp_AppManagerImpl.h"
31 #include "FApp_AppResourceString.h"
32 #include "FApp_IAppResourceBitmap.h"
34 #include "FApp_AppResourceImpl.h"
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Base::Runtime;
40 using namespace Tizen::Graphics;
41 using namespace Tizen::Io;
44 namespace Tizen { namespace App
47 #define BITMAP_PIXEL_FORMAT_INVALID (BITMAP_PIXEL_FORMAT_MIN)
50 static _IAppResourceBitmap*(* p_Create_IAppResourceBitmapInstanceN)(int type, const String& value) = null;
52 Tizen::Base::Collection::HashMap* _AppResourceImpl::__pContainer = null;
54 _AppResourceImpl::_AppResourceImpl(void)
55 : __p_AppResourceString(null)
56 , __p_IAppResourceBitmap(null)
57 , __type(APP_RESOURCE_DEFAULT)
63 _AppResourceImpl::ReleaseInstance(const Tizen::Base::String& key)
65 if (__pContainer != null)
67 AppResource* pAppResource = static_cast<AppResource*> (__pContainer->GetValue(key));
70 return __pContainer->Remove(key);
76 // Exception: E_OUT_OF_MEMORY, E_SYSTEM, E_INVALID_ARG, E_OBJ_NOT_FOUND, E_APP_NOT_INSTALLED [Get_AppResourceStringN], Get_IAppResourceBitmapN
78 _AppResourceImpl::GetInstanceN(AppResourceBy type, const Tizen::Base::String& value)
81 if (__pContainer == null)
83 std::unique_ptr< HashMap > pContainer(new (std::nothrow) HashMap());
84 SysTryReturn(NID_APP, pContainer != null,
85 null, E_OUT_OF_MEMORY, "[%s] Creating the container is failed.", GetErrorMessage(E_OUT_OF_MEMORY));
87 r = pContainer->Construct();
88 SysTryReturn(NID_APP, !IsFailed(r), null, E_SYSTEM, "[E_SYSTEM] Creating the container is failed [%s].", GetErrorMessage(r));
90 __pContainer = pContainer.release();
93 bool hasAppResource = false;
94 r = __pContainer->ContainsKey(value, hasAppResource);
95 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] Checking to contain is failed.", GetErrorMessage(r));
99 return static_cast<AppResource*> (__pContainer->GetValue(value));
103 std::unique_ptr< _AppResourceImpl > pAppResourceImpl(new (std::nothrow) _AppResourceImpl());
104 SysTryReturn(NID_APP, pAppResourceImpl != null,
105 null, E_OUT_OF_MEMORY, "[%s] Unable to allocate memory for AppResourceImpl.", GetErrorMessage(E_OUT_OF_MEMORY));
107 std::unique_ptr< _AppResourceString > pAppResourceString(_AppResourceString::Get_AppResourceStringN(type, value));
109 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] Unable to get _AppResourceString.", GetErrorMessage(r));
111 std::unique_ptr< _IAppResourceBitmap > pAppResourceBitmap(Get_IAppResourceBitmapN(type, value));
113 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] Unable to get _IAppResourceBitmap.", GetErrorMessage(r));
115 pAppResourceImpl->__p_AppResourceString = pAppResourceString.release();
116 pAppResourceImpl->__p_IAppResourceBitmap = pAppResourceBitmap.release();
117 pAppResourceImpl->__type = type;
118 pAppResourceImpl->__value = value;
120 std::unique_ptr< String > pStr(new (std::nothrow) String(value));
121 SysTryReturn(NID_APP, pStr != null, null,
122 E_OUT_OF_MEMORY, "[%s] Creating a key is failed.", GetErrorMessage(E_OUT_OF_MEMORY));
124 AppResource* pAppResource = new (std::nothrow) AppResource;
125 SysTryReturn(NID_APP, pAppResource != null, null,
126 E_OUT_OF_MEMORY, "[%s] Unable to allocate memory for AppResource", GetErrorMessage(E_OUT_OF_MEMORY));
128 pAppResource->__pAppResourceImpl = pAppResourceImpl.release();
129 r = __pContainer->Add(*pStr, *pAppResource);
133 SysTryReturn(NID_APP, false, null,
134 E_SYSTEM, "[E_SYSTEM] Adding an element to the container is failed [%s].", GetErrorMessage(r));
144 _AppResourceImpl::Get_IAppResourceBitmapN(AppResourceBy type, const Tizen::Base::String& value)
146 if (p_Create_IAppResourceBitmapInstanceN == null)
148 _LibraryImpl& lib = _AppManagerImpl::GetInstance()->GetUiLibraryImpl();
149 p_Create_IAppResourceBitmapInstanceN =
150 reinterpret_cast<_IAppResourceBitmap*(*)(int, const String&)>(lib.GetProcAddress(L"_Create_IAppResourceBitmapInstanceN"));
151 SysTryReturn(NID_APP, p_Create_IAppResourceBitmapInstanceN != null,
152 null, E_SYSTEM, "[E_SYSTEM] Failed to get Bitmap resource [%s].", GetErrorMessage(E_SYSTEM));
155 std::unique_ptr< _IAppResourceBitmap > pAppResourceBitmap(p_Create_IAppResourceBitmapInstanceN(type, value));
156 SysTryReturn(NID_APP, pAppResourceBitmap != null, null,
157 E_SYSTEM, "[%s] Unable to get IAppResourceBitmap.", GetErrorMessage(E_SYSTEM));
159 return pAppResourceBitmap.release();
162 _AppResourceImpl::~_AppResourceImpl(void)
164 delete __p_AppResourceString;
165 delete __p_IAppResourceBitmap;
169 _AppResourceImpl::GetString(const String& resourceId, String& loadedString)
171 if (__p_AppResourceString == null)
173 __p_AppResourceString = _AppResourceString::Get_AppResourceStringN(__type, __value);
174 SysTryReturnResult(NID_APP, __p_AppResourceString == null, E_FAILURE, "Failed to get String resource.");
177 return __p_AppResourceString->GetString(resourceId, loadedString);
181 _AppResourceImpl::GetBitmapN(const String& imgFilePath, BitmapPixelFormat pixelFormat)
183 if (__p_IAppResourceBitmap == null)
185 __p_IAppResourceBitmap = Get_IAppResourceBitmapN(__type, __value);
186 SysTryReturn(NID_APP, __p_IAppResourceBitmap == null,
187 null, E_SYSTEM, "[%s] Failed to get Bitmap resource.", GetErrorMessage(E_SYSTEM));
190 return __p_IAppResourceBitmap->GetBitmapN(imgFilePath, pixelFormat);
194 _AppResourceImpl::GetBitmapN(const String& imgFilePath)
196 return GetBitmapN(imgFilePath, BITMAP_PIXEL_FORMAT_INVALID);
200 _AppResourceImpl::GetInstance(void)
202 AppResource* pAppResource = AppResource::GetInstance();
205 return pAppResource->__pAppResourceImpl;
211 _AppResourceImpl::Reinitialize(void)
213 _AppResourceImpl* pThis = _AppResourceImpl::GetInstance();
216 SysLog(NID_APP, "No _AppResourceImpl instance.");
220 if (pThis->__pContainer != null)
222 std::unique_ptr< IMapEnumerator > pMapEnum(pThis->__pContainer->GetMapEnumeratorN());
225 while(pMapEnum->MoveNext() == E_SUCCESS)
227 AppResource* pAppResource = static_cast<AppResource*> (pMapEnum->GetValue());
228 if (pAppResource && pAppResource->__pAppResourceImpl)
230 _AppResourceImpl* pAppResourceImpl = pAppResource->__pAppResourceImpl;
231 delete pAppResourceImpl->__p_AppResourceString;
232 pAppResourceImpl->__p_AppResourceString =
233 _AppResourceString::Get_AppResourceStringN(pAppResourceImpl->__type, pAppResourceImpl->__value);
235 // This function will be called on Language change event
236 // Need not to re-initialize Bitmap resource as language change does not affect bitmap resource path
244 _AppResourceImpl::GetNonScalingBitmapN(const Tizen::Base::String& imagePath)
246 _IAppResourceBitmap* pAppResourceBitmap = Tizen::App::_AppResourceImpl::Get_IAppResourceBitmapN(APP_RESOURCE_BITMAP);
248 if (pAppResourceBitmap != null)
250 return pAppResourceBitmap->GetBitmapN(imagePath, BITMAP_PIXEL_FORMAT_INVALID, false);
254 SysLog(NID_APP, "Getting pAppResourceBitmap instance is failed");