|
| 1 | +// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
1 | 5 | #include "app_settings_manager.h"
|
2 | 6 |
|
3 | 7 | #include <app_common.h>
|
4 | 8 | #include <app_control.h>
|
5 | 9 | #include <package_manager.h>
|
6 | 10 |
|
| 11 | +#include <string> |
| 12 | + |
7 | 13 | #include "log.h"
|
8 | 14 |
|
9 | 15 | namespace {
|
10 |
| -constexpr char kPackageID[] = "pkgId"; |
11 |
| -constexpr char kSettingAppID[] = "com.samsung.clocksetting.apps"; |
12 |
| - |
13 |
| -class AppPermissions { |
14 |
| - public: |
15 |
| - AppPermissions() { Init(); } |
16 |
| - |
17 |
| - ~AppPermissions() { Deinit(); } |
18 |
| - |
19 |
| - bool Launch(const std::string& package_name) { |
20 |
| - if (package_name.length() == 0) { |
21 |
| - return false; |
22 |
| - } |
23 |
| - |
24 |
| - int ret = app_control_add_extra_data(app_control_, kPackageID, |
25 |
| - package_name.c_str()); |
26 |
| - if (ret != APP_CONTROL_ERROR_NONE) { |
27 |
| - LOG_ERROR("Failed to add key[%s]:value[%s]", kPackageID, |
28 |
| - package_name.c_str()); |
29 |
| - return false; |
30 |
| - } |
31 |
| - |
32 |
| - ret = app_control_send_launch_request(app_control_, nullptr, nullptr); |
33 |
| - if (ret != APP_CONTROL_ERROR_NONE) { |
34 |
| - LOG_ERROR("Failed to send launch request setting"); |
35 |
| - return false; |
36 |
| - } |
37 |
| - return true; |
| 16 | + |
| 17 | +constexpr char kSettingAppId[] = "com.samsung.clocksetting.apps"; |
| 18 | + |
| 19 | +std::string GetPackageName() { |
| 20 | + char* app_id; |
| 21 | + int ret = app_get_id(&app_id); |
| 22 | + if (ret != APP_ERROR_NONE) { |
| 23 | + LOG_ERROR("The app ID is not found."); |
| 24 | + return ""; |
38 | 25 | }
|
39 | 26 |
|
40 |
| - private: |
41 |
| - void Init() { |
42 |
| - int ret = app_control_create(&app_control_); |
43 |
| - if (ret != APP_CONTROL_ERROR_NONE) { |
44 |
| - LOG_ERROR("Failed to create app control handle"); |
45 |
| - } |
46 |
| - |
47 |
| - ret = app_control_set_app_id(app_control_, kSettingAppID); |
48 |
| - if (ret != APP_CONTROL_ERROR_NONE) { |
49 |
| - LOG_ERROR("Failed to set app id[%s]", kSettingAppID); |
50 |
| - } |
| 27 | + package_info_h package_info; |
| 28 | + ret = package_info_create(app_id, &package_info); |
| 29 | + free(app_id); |
| 30 | + if (ret != PACKAGE_MANAGER_ERROR_NONE) { |
| 31 | + LOG_ERROR("Failed to create a package info handle."); |
| 32 | + return ""; |
51 | 33 | }
|
52 | 34 |
|
53 |
| - void Deinit() { |
54 |
| - if (app_control_) { |
55 |
| - app_control_destroy(app_control_); |
56 |
| - app_control_ = nullptr; |
57 |
| - } |
| 35 | + char* package_name; |
| 36 | + ret = package_info_get_package(package_info, &package_name); |
| 37 | + package_info_destroy(package_info); |
| 38 | + if (ret != PACKAGE_MANAGER_ERROR_NONE) { |
| 39 | + LOG_ERROR("Failed to get the package name."); |
| 40 | + return ""; |
58 | 41 | }
|
59 | 42 |
|
60 |
| - app_control_h app_control_{nullptr}; |
61 |
| -}; |
62 |
| - |
63 |
| -class PackageName { |
64 |
| - public: |
65 |
| - PackageName() { Init(); } |
66 |
| - |
67 |
| - ~PackageName() { Deinit(); } |
68 |
| - |
69 |
| - char* Get() { return package_name_; } |
70 |
| - |
71 |
| - private: |
72 |
| - void Init() { |
73 |
| - int ret = app_get_id(&app_id_); |
74 |
| - if (ret != APP_ERROR_NONE || app_id_ == nullptr) { |
75 |
| - LOG_ERROR("Failed to get app id"); |
76 |
| - return; |
77 |
| - } |
78 |
| - |
79 |
| - ret = package_info_create(app_id_, &package_info_); |
80 |
| - if (ret != PACKAGE_MANAGER_ERROR_NONE || package_info_ == nullptr) { |
81 |
| - LOG_ERROR("Failed to create package info handle"); |
82 |
| - } |
83 |
| - ret = package_info_get_package(package_info_, &package_name_); |
84 |
| - if (ret != PACKAGE_MANAGER_ERROR_NONE || package_name_ == nullptr) { |
85 |
| - LOG_ERROR("Failed to get package name"); |
86 |
| - } |
| 43 | + std::string result = std::string(package_name); |
| 44 | + free(package_name); |
| 45 | + |
| 46 | + return result; |
| 47 | +} |
| 48 | + |
| 49 | +} // namespace |
| 50 | + |
| 51 | +bool AppSettingsManager::OpenAppSettings() { |
| 52 | + std::string package_name = GetPackageName(); |
| 53 | + if (package_name.empty()) { |
| 54 | + return false; |
87 | 55 | }
|
88 | 56 |
|
89 |
| - void Deinit() { |
90 |
| - if (app_id_) { |
91 |
| - free(app_id_); |
92 |
| - app_id_ = nullptr; |
93 |
| - } |
94 |
| - |
95 |
| - if (package_info_) { |
96 |
| - package_info_destroy(package_info_); |
97 |
| - package_info_ = nullptr; |
98 |
| - } |
99 |
| - |
100 |
| - if (package_name_) { |
101 |
| - free(package_name_); |
102 |
| - package_name_ = nullptr; |
103 |
| - } |
| 57 | + app_control_h app_control; |
| 58 | + int ret = app_control_create(&app_control); |
| 59 | + if (ret != APP_CONTROL_ERROR_NONE) { |
| 60 | + LOG_ERROR("Failed to create an app control handle."); |
| 61 | + return false; |
104 | 62 | }
|
105 | 63 |
|
106 |
| - char* app_id_{nullptr}; |
107 |
| - package_info_h package_info_{nullptr}; |
108 |
| - char* package_name_{nullptr}; |
109 |
| -}; |
110 |
| -} // namespace |
| 64 | + ret = app_control_set_app_id(app_control, kSettingAppId); |
| 65 | + if (ret != APP_CONTROL_ERROR_NONE) { |
| 66 | + LOG_ERROR("Failed to set an app ID."); |
| 67 | + app_control_destroy(app_control); |
| 68 | + return false; |
| 69 | + } |
111 | 70 |
|
112 |
| -AppSettingsManager::AppSettingsManager() { |
113 |
| - app_permissions_ = std::make_unique<AppPermissions>(); |
114 |
| - PackageName pkg_name; |
115 |
| - char* name = pkg_name.Get(); |
116 |
| - if (name == nullptr) { |
117 |
| - return; |
| 71 | + ret = app_control_add_extra_data(app_control, "pkgId", package_name.c_str()); |
| 72 | + if (ret != APP_CONTROL_ERROR_NONE) { |
| 73 | + LOG_ERROR("Failed to add extra data."); |
| 74 | + app_control_destroy(app_control); |
| 75 | + return false; |
118 | 76 | }
|
119 |
| - package_name_ = std::string(name); |
120 |
| -} |
121 | 77 |
|
122 |
| -AppSettingsManager::~AppSettingsManager() {} |
| 78 | + ret = app_control_send_launch_request(app_control, nullptr, nullptr); |
| 79 | + app_control_destroy(app_control); |
| 80 | + if (ret != APP_CONTROL_ERROR_NONE) { |
| 81 | + LOG_ERROR("Failed to send a launch request."); |
| 82 | + return false; |
| 83 | + } |
123 | 84 |
|
124 |
| -bool AppSettingsManager::OpenAppSettings() { |
125 |
| - return app_permissions_->Launch(package_name_); |
| 85 | + return true; |
126 | 86 | }
|
0 commit comments