Change test files and modification vconf
[platform/core/api/system-settings.git] / src / sst_screen.c
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
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 #include "sst_screen.h"
17
18 #include <stdlib.h>
19 #include <dlfcn.h>
20 #include <pkgmgr-info.h>
21 #include <vconf.h>
22 #include "sst.h"
23 #include "sst_vconf.h"
24 #include "sst_exclusive.h"
25 #include "sst_utils_wrapper.h"
26
27 int sst_screen_set_backlight_time(sst_interface *iface, int value)
28 {
29         if (value <= 0 || 600 < value) {
30                 ERR("Invalid Value(%d)", value);
31                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
32         }
33
34         if (vconf_set_int(iface->vconf_key, value)) {
35                 ERR("vconf_set_int(%s, %d) Fail", iface->vconf_key, value);
36                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
37         }
38
39         return SYSTEM_SETTINGS_ERROR_NONE;
40 }
41
42 int sst_screen_set_home_wallpaper(sst_interface *iface, const char *value)
43 {
44         bool valid = sstu_is_valid_image(value);
45         if (false == valid) {
46                 ERR("Invalid image file(%s)", value);
47                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
48         }
49
50         return sst_excl_set_wallpaper(iface->vconf_key, value);
51 }
52
53 int sst_screen_set_lock_wallpaper(sst_interface *iface, const char *value)
54 {
55         bool valid = sstu_is_valid_image(value);
56         if (false == valid) {
57                 ERR("Invalid image file(%s)", value);
58                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
59         }
60
61         if (vconf_set_str(iface->vconf_key, value)) {
62                 ERR("vconf_set_str(%s, %s) Fail", iface->vconf_key, value);
63                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
64         }
65
66         return SYSTEM_SETTINGS_ERROR_NONE;
67 }
68
69 static int _category_func(const char *name, void *user_data)
70 {
71         const char *category = "lock-screen";
72         bool *found = user_data;
73
74         RETV_IF(NULL == name, PMINFO_R_OK);
75
76         if (SST_EQUAL == strcmp(name, category)) {
77                 *found = true;
78                 return PMINFO_R_ERROR; //not Error. It stops iteration.
79         } else {
80                 DBG("Unexpeced category(%s)", name);
81                 return PMINFO_R_OK;
82         }
83
84         return PMINFO_R_OK;
85 }
86
87 /**
88  * set 'swipe type' if current lockscreen app is 'com.samsung.lockscreen'
89  */
90 int sst_screen_set_lockscreen(sst_interface *iface, const char *app_id)
91 {
92         int ret;
93
94         RETV_IF(NULL == app_id, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
95         //ex) app_id = com.samsung.lockscreen
96
97         pkgmgrinfo_appinfo_h handle = NULL;
98         ret = pkgmgrinfo_appinfo_get_appinfo(app_id, &handle);
99         if (PMINFO_R_OK != ret) {
100                 ERR("pkgmgrinfo_appinfo_get_appinfo(%s) Fail(%d)", app_id, ret);
101                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
102         }
103
104         bool is_lock_screen = false;
105         ret = pkgmgrinfo_appinfo_foreach_category(handle, _category_func, &is_lock_screen);
106         if (ret != PMINFO_R_OK) {
107                 ERR("pkgmgrinfo_appinfo_foreach_category() Fail(%d)", ret);
108                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
109                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
110         }
111         pkgmgrinfo_appinfo_destroy_appinfo(handle);
112
113         if (false == is_lock_screen) {
114                 ERR("Invalid Application(%s)", app_id);
115                 //Todo: It should be enable.
116                 //return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
117         }
118
119         int locktype = -1;
120         if (vconf_get_int(VCONFKEY_SETAPPL_SCREEN_LOCK_TYPE_INT, &locktype)) {
121                 ERR("vconf_get_int(lock_type) Fail");
122                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
123         }
124
125         if (locktype == SETTING_SCREEN_LOCK_TYPE_PASSWORD) {
126                 ERR("Unsupported lock type");
127                 return SYSTEM_SETTINGS_ERROR_LOCKSCREEN_APP_PASSWORD_MODE;
128         }
129
130         if (vconf_set_str(VCONFKEY_SETAPPL_3RD_LOCK_PKG_NAME_STR, app_id)) {
131                 ERR("vconf_set_str(3rd lock pkg) Fail");
132                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
133         }
134
135         if (SST_EQUAL == strcmp(app_id, "com.samsung.lockscreen")) {
136                 const char *lock_type_key = VCONFKEY_SETAPPL_SCREEN_LOCK_TYPE_INT;
137                 if (vconf_set_int(lock_type_key, SETTING_SCREEN_LOCK_TYPE_SWIPE)) {
138                         ERR("vconf_set_int(%s) Fail", lock_type_key);
139                         return SYSTEM_SETTINGS_ERROR_IO_ERROR;
140                 }
141         }
142         return SYSTEM_SETTINGS_ERROR_NONE;
143 }
144
145 int sst_screen_get_lockscreen(sst_interface *iface, char **value)
146 {
147         char *pkg_name = NULL;
148
149         int locktype = -1;
150         if (vconf_get_int(VCONFKEY_SETAPPL_SCREEN_LOCK_TYPE_INT, &locktype)) {
151                 ERR("vconf_get_int(lock_type) Fail");
152                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
153         }
154
155         if (sst_vconf_get_str_ally(iface->vconf_key, &pkg_name)) {
156                 ERR("sst_vconf_get_str_ally(%s) Fail", iface->vconf_key);
157                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
158         }
159
160         if (SST_EQUAL == strcmp(pkg_name, "com.samsung.lockscreen")
161                         && locktype == SETTING_SCREEN_LOCK_TYPE_PASSWORD) {
162                 //if current lock type is 'password', return SYSTEM_SETTINGS_ERROR_LOCKSCREEN_APP_PASSWORD_MODE
163                 free(pkg_name);
164                 return SYSTEM_SETTINGS_ERROR_LOCKSCREEN_APP_PASSWORD_MODE;
165         }
166
167         *value = pkg_name;
168         return SYSTEM_SETTINGS_ERROR_NONE;
169 }