2 * Copyright (c) 2018 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.
21 #include <package_info.h>
22 #include <package_manager.h>
28 #include "notification-ex/ex_util.h"
34 #define LOG_TAG "NOTIFICATION_EX"
35 #define MAX_PACKAGE_STR_SIZE 512
36 #define MAX_CACHE_SIZE 100
39 namespace notification {
44 g_atomic_int_inc(&id);
49 return GetAppId(getpid());
52 string GetAppId(pid_t pid) {
53 static map<pid_t, string> appid_cache;
56 char appid_buf[MAX_PACKAGE_STR_SIZE] = {0, };
58 if (appid_cache.find(pid) != appid_cache.end()) {
59 LOGI("appid(%s)", appid_cache[pid].c_str());
60 return appid_cache[pid];
63 if (appid_cache.size() > MAX_CACHE_SIZE)
66 int ret = aul_app_get_appid_bypid(pid, appid_buf, sizeof(appid_buf));
67 if (ret == AUL_R_OK) {
68 appid = string(appid_buf);
71 int last_slash_index = 0;
72 char proc_buf[MAX_PACKAGE_STR_SIZE] = { 0, };
74 snprintf(proc_buf, sizeof(proc_buf), "/proc/%d/cmdline", pid);
76 fd = open(proc_buf, O_RDONLY);
78 LOGE("Fail to get appid (%d)", errno);
82 ret = read(fd, appid_buf, sizeof(appid_buf) - 1);
84 LOGE("Fail to get appid (%d)", errno);
90 for (i = 0 ; i < ret ; i++) {
91 if (appid_buf[i] == '/')
95 if (last_slash_index == (ret - 1)) {
96 LOGE("Fail to get appid (%s)", appid_buf);
100 if (last_slash_index == 0)
101 appid = string(appid_buf);
103 appid = string(&appid_buf[last_slash_index + 1]);
106 LOGI("appid(%s)", appid.c_str());
107 appid_cache[pid] = appid;
112 static string pkgid = "";
113 if (!pkgid.empty()) {
114 LOGI("pkgid(%s)", pkgid.c_str());
118 char pkgid_buf[MAX_PACKAGE_STR_SIZE] = {0, };
119 int ret = aul_app_get_pkgid_bypid(getpid(), pkgid_buf, sizeof(pkgid_buf));
121 pkgid = string(pkgid_buf);
126 string GetDomainName() {
127 static string domain_str = "";
128 if (!domain_str.empty()) {
129 LOGI("domain(%s)", domain_str.c_str());
133 string pkgid = GetPkgId();
137 string::size_type pos = pkgid.rfind('.');
138 if (pos != string::npos)
143 domain_str = pkgid.substr(pos);
147 string GetLocaleDirectory() {
148 static string locale_directory = "";
149 if (!locale_directory.empty()) {
150 LOGI("locale_directory(%s)", locale_directory.c_str());
151 return locale_directory;
154 string pkgid = GetPkgId();
158 package_info_h package_info;
159 int err = package_info_create(pkgid.c_str(), &package_info);
160 if (err != PACKAGE_MANAGER_ERROR_NONE) {
161 LOGE("fail to get package info");
164 unique_ptr<package_info_s, decltype(package_info_destroy)*> pkg_ptr(
165 package_info, package_info_destroy);
168 err = package_info_get_root_path(pkg_ptr.get(), &app_root_path);
169 if (err != PACKAGE_MANAGER_ERROR_NONE) {
170 LOGE("Failed to get root path err[%d] path[%p]",
175 string app_root_str(app_root_path);
178 locale_directory = app_root_str + string("/res/locale");
179 return locale_directory;
183 } // namespace notification