[mobile] Remove password lockscreen
[apps/native/starter.git] / src / mobile / lock_mgr.c
1 /*
2  * Copyright (c) 2000 - 2015 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
17 #include <vconf.h>
18 #include <vconf-keys.h>
19
20 #include <glib.h>
21 #include <glib-object.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <feedback.h>
27 #include <time.h>
28 #include <dd-deviced.h>
29 #include <dd-display.h>
30 #include <aul.h>
31 #include <system_settings.h>
32
33 #include "lock_mgr.h"
34 #include "package_mgr.h"
35 #include "process_mgr.h"
36 #include "hw_key.h"
37 #include "dbus_util.h"
38 #include "util.h"
39 #include "status.h"
40
41
42
43 static struct {
44         int checkfd;
45         int old_lock_type;
46         int lock_pid;
47         int lcd_state;
48 } s_lock_mgr = {
49         .checkfd = 0,
50         .old_lock_type = 0,
51         .lock_pid = -1,
52         .lcd_state = -1,
53 };
54
55
56
57 int lock_mgr_lcd_state_get(void)
58 {
59         return s_lock_mgr.lcd_state;
60 }
61
62
63
64 int lock_mgr_get_lock_pid(void)
65 {
66         return s_lock_mgr.lock_pid;
67 }
68
69
70
71 void lock_mgr_sound_play(lock_sound_type_e type)
72 {
73         int val = status_passive_get()->setappl_sound_lock_bool;
74         ret_if(!val);
75
76         switch(type) {
77         case LOCK_SOUND_LOCK:
78                 feedback_play_type(FEEDBACK_TYPE_SOUND, FEEDBACK_PATTERN_LOCK);
79                 break;
80         case LOCK_SOUND_UNLOCK:
81                 feedback_play_type(FEEDBACK_TYPE_SOUND, FEEDBACK_PATTERN_UNLOCK);
82                 break;
83         case LOCK_SOUND_BTN_KEY:
84                 feedback_play_type(FEEDBACK_TYPE_SOUND, FEEDBACK_PATTERN_SIP);
85                 break;
86         case LOCK_SOUND_TAP:
87                 feedback_play_type(FEEDBACK_TYPE_SOUND, FEEDBACK_PATTERN_TAP);
88                 break;
89         default:
90                 break;
91         }
92 }
93
94
95
96 void lock_mgr_idle_lock_state_set(int lock_state)
97 {
98         _D("lock state : %d", lock_state);
99
100         int ret = 0;
101
102         ret = vconf_set_int(VCONFKEY_IDLE_LOCK_STATE, lock_state);
103         if (ret < 0) {
104                 _E("Failed to set vconfkey : VCONFKEY_IDLE_LOCK_STATE");
105         }
106
107         ret = vconf_set_int(VCONFKEY_IDLE_LOCK_STATE_READ_ONLY, lock_state);
108         if (ret < 0) {
109                 _E("Failed to set vconfkey : VCONFKEY_IDLE_LOCK_STATE_READ_ONLY");
110         }
111 }
112
113
114
115 static void _after_launch_lock(int pid)
116 {
117         int idle_lock_state = 0;
118
119         if (dbus_util_send_oomadj(pid, OOM_ADJ_VALUE_DEFAULT) < 0) {
120                 _E("cannot send oomadj for pid[%d]", pid);
121         }
122         process_mgr_set_lock_priority(pid);
123         display_unlock_state(LCD_OFF, PM_SLEEP_MARGIN);
124         s_lock_mgr.lock_pid = pid;
125
126         idle_lock_state = status_passive_get()->idle_lock_state;
127         if (idle_lock_state == VCONFKEY_IDLE_UNLOCK) {
128                 lock_mgr_idle_lock_state_set(VCONFKEY_IDLE_LOCK);
129                 lock_mgr_sound_play(LOCK_SOUND_LOCK);
130         }
131 }
132
133
134
135 static int _lock_changed_cb(const char *appid, const char *key, const char *value, void *cfn, void *afn)
136 {
137         _D("%s", __func__);
138
139         return 0;
140 }
141
142
143
144 static void _other_lockscreen_unlock(void)
145 {
146         _D("unlock other lock screen");
147 }
148
149
150
151 void lock_mgr_unlock(void)
152 {
153         int lock_type = status_active_get()->setappl_screen_lock_type_int;
154         _D("lock type(%d)", lock_type);
155
156         lock_mgr_idle_lock_state_set(VCONFKEY_IDLE_UNLOCK);
157         lock_mgr_sound_play(LOCK_SOUND_UNLOCK);
158         s_lock_mgr.lock_pid = 0;
159
160         if (lock_type == SETTING_SCREEN_LOCK_TYPE_OTHER) {
161                 _other_lockscreen_unlock();
162         }
163 }
164
165
166
167 static void _on_lcd_changed_receive(void *data, DBusMessage *msg)
168 {
169         int lcd_on = 0;
170         int lcd_off = 0;
171
172         _D("LCD signal is received");
173
174         lcd_on = dbus_message_is_signal(msg, DEVICED_INTERFACE_DISPLAY, MEMBER_LCD_ON);
175         lcd_off = dbus_message_is_signal(msg, DEVICED_INTERFACE_DISPLAY, MEMBER_LCD_OFF);
176
177         if (lcd_on) {
178                 _W("LCD on");
179                 s_lock_mgr.lcd_state = LCD_STATE_ON;
180         } else if (lcd_off) {
181                 int idle_lock_state = 0;
182                 Eina_Bool ret = EINA_FALSE;
183                 s_lock_mgr.lcd_state = LCD_STATE_OFF;
184
185                 idle_lock_state = status_passive_get()->idle_lock_state;
186                 _D("idle_lock_state(%d)", idle_lock_state);
187
188                 if (idle_lock_state == VCONFKEY_IDLE_UNLOCK) {
189                         ret = lock_mgr_lockscreen_launch();
190                         if (ret != EINA_TRUE) {
191                                 _E("Failed to launch lockscreen");
192                         }
193                 }
194         } else {
195                 _E("%s dbus_message_is_signal error", DEVICED_INTERFACE_DISPLAY);
196         }
197 }
198
199
200
201 Eina_Bool lock_mgr_lockscreen_launch(void)
202 {
203         const char *lock_appid = NULL;
204         int lock_type = 0;
205
206         lock_type = status_active_get()->setappl_screen_lock_type_int;
207         _D("lock type : %d", lock_type);
208
209         //PM LOCK - don't go to sleep
210         display_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
211
212         lock_appid = status_passive_get()->setappl_3rd_lock_pkg_name_str;
213         if (!lock_appid) {
214                 _E("set default lockscreen");
215                 lock_appid = STATUS_DEFAULT_LOCK_PKG_NAME;
216         }
217
218         _I("lockscreen appid : %s", lock_appid);
219
220         switch (lock_type) {
221         case SETTING_SCREEN_LOCK_TYPE_NONE:
222                 _D("Lockscreen type is NONE");
223                 return EINA_TRUE;
224         case SETTING_SCREEN_LOCK_TYPE_SWIPE:
225         case SETTING_SCREEN_LOCK_TYPE_SIMPLE_PASSWORD:
226         case SETTING_SCREEN_LOCK_TYPE_PASSWORD:
227         case SETTING_SCREEN_LOCK_TYPE_OTHER:
228                 process_mgr_must_launch(lock_appid, NULL, NULL, _lock_changed_cb, _after_launch_lock);
229                 goto_if(s_lock_mgr.lock_pid < 0, ERROR);
230                 break;
231         default:
232                 _E("type error(%d)", lock_type);
233                 goto ERROR;
234         }
235
236         _I("lock pid : %d", s_lock_mgr.lock_pid);
237
238         return EINA_TRUE;
239
240 ERROR:
241         _E("Failed to launch lockscreen");
242         display_unlock_state(LCD_OFF, PM_SLEEP_MARGIN);
243         return EINA_FALSE;
244 }
245
246
247
248 static void _lock_daemon_init(void)
249 {
250         _SECURE_I("lockscreen : %s", status_passive_get()->setappl_3rd_lock_pkg_name_str);
251
252         /* register lcd changed cb */
253         dbus_util_receive_lcd_status(_on_lcd_changed_receive, NULL);
254 }
255
256
257
258 static int _lock_type_changed_cb(status_active_key_e key, void *data)
259 {
260         int lock_type = status_active_get()->setappl_screen_lock_type_int;
261         retv_if(lock_type == s_lock_mgr.old_lock_type, 1);
262
263         _D("lock type is changed : %d -> %d", s_lock_mgr.old_lock_type, lock_type);
264
265         s_lock_mgr.old_lock_type = lock_type;
266
267         return 1;
268 }
269
270
271 int lock_mgr_daemon_start(void)
272 {
273         int lock_type = 0;
274         int ret = 0;
275
276         _lock_daemon_init();
277
278         lock_type = status_active_get()->setappl_screen_lock_type_int;
279         _D("lock type : %d", lock_type);
280
281         status_active_register_cb(STATUS_ACTIVE_KEY_SETAPPL_SCREEN_LOCK_TYPE_INT, _lock_type_changed_cb, NULL);
282
283         ret = lock_mgr_lockscreen_launch();
284         if (ret != EINA_TRUE) {
285                 _E("Failed to launch lockscreen");
286         }
287
288         if (feedback_initialize() != FEEDBACK_ERROR_NONE) {
289                 _E("Failed to initialize feedback");
290         }
291
292         return ret;
293 }
294
295
296
297 void lock_mgr_daemon_end(void)
298 {
299         status_active_unregister_cb(STATUS_ACTIVE_KEY_SETAPPL_SCREEN_LOCK_TYPE_INT, _lock_type_changed_cb);
300
301         feedback_deinitialize();
302 }