OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. 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 | |
5 #include "chrome/browser/signin/easy_unlock_screenlock_state_handler.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/prefs/pref_service.h" | |
9 #include "base/strings/string16.h" | |
10 #include "base/strings/utf_string_conversions.h" | |
11 #include "chrome/browser/chromeos/chromeos_utils.h" | |
12 #include "chrome/common/pref_names.h" | |
13 #include "grit/generated_resources.h" | |
14 #include "ui/base/l10n/l10n_util.h" | |
15 | |
16 namespace { | |
17 | |
18 size_t kIconSize = 27u; | |
19 size_t kOpaqueIconOpacity = 50u; | |
20 size_t kSpinnerResourceWidth = 1215u; | |
21 size_t kSpinnerIntervalMs = 50u; | |
22 | |
23 std::string GetIconURLForState(EasyUnlockScreenlockStateHandler::State state) { | |
24 switch (state) { | |
25 case EasyUnlockScreenlockStateHandler::STATE_NO_BLUETOOTH: | |
26 case EasyUnlockScreenlockStateHandler::STATE_NO_PHONE: | |
27 case EasyUnlockScreenlockStateHandler::STATE_PHONE_NOT_AUTHENTICATED: | |
28 case EasyUnlockScreenlockStateHandler::STATE_PHONE_LOCKED: | |
29 case EasyUnlockScreenlockStateHandler::STATE_PHONE_NOT_NEARBY: | |
30 case EasyUnlockScreenlockStateHandler::STATE_PHONE_UNLOCKABLE: | |
31 return "chrome://theme/IDR_EASY_UNLOCK_LOCKED"; | |
32 case EasyUnlockScreenlockStateHandler::STATE_BLUETOOTH_CONNECTING: | |
33 return "chrome://theme/IDR_EASY_UNLOCK_SPINNER"; | |
34 case EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED: | |
35 return "chrome://theme/IDR_EASY_UNLOCK_UNLOCKED"; | |
xiyuan
2014/08/12 18:09:47
Do you have a seperate CL for those image resource
tbarzic
2014/08/12 19:14:36
yep, I'll upload the cl today...
| |
36 default: | |
37 return ""; | |
38 } | |
39 } | |
40 | |
41 bool UseOpaqueIcon(EasyUnlockScreenlockStateHandler::State state) { | |
42 return state == EasyUnlockScreenlockStateHandler::STATE_NO_BLUETOOTH || | |
43 state == EasyUnlockScreenlockStateHandler::STATE_NO_PHONE || | |
44 state == EasyUnlockScreenlockStateHandler::STATE_PHONE_NOT_NEARBY || | |
45 state == EasyUnlockScreenlockStateHandler::STATE_PHONE_UNLOCKABLE; | |
46 } | |
47 | |
48 bool HasAnimation(EasyUnlockScreenlockStateHandler::State state) { | |
49 return state == EasyUnlockScreenlockStateHandler::STATE_BLUETOOTH_CONNECTING; | |
50 } | |
51 | |
52 size_t GetTooltipResourceId(EasyUnlockScreenlockStateHandler::State state) { | |
53 switch (state) { | |
54 case EasyUnlockScreenlockStateHandler::STATE_NO_BLUETOOTH: | |
55 return IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_NO_BLUETOOTH; | |
56 case EasyUnlockScreenlockStateHandler::STATE_NO_PHONE: | |
57 return IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_NO_PHONE; | |
58 case EasyUnlockScreenlockStateHandler::STATE_PHONE_NOT_AUTHENTICATED: | |
59 return IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_PHONE_NOT_AUTHENTICATED; | |
60 case EasyUnlockScreenlockStateHandler::STATE_PHONE_LOCKED: | |
61 return IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_PHONE_LOCKED; | |
62 case EasyUnlockScreenlockStateHandler::STATE_PHONE_UNLOCKABLE: | |
63 return IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_PHONE_UNLOCKABLE; | |
64 case EasyUnlockScreenlockStateHandler::STATE_PHONE_NOT_NEARBY: | |
65 return IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_PHONE_NOT_NEARBY; | |
66 case EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED: | |
67 // TODO(tbarzic): When hard lock is enabled change this to | |
68 // IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_HARDLOCK_INSTRUCTIONS. | |
69 return 0; | |
70 default: | |
71 return 0; | |
72 } | |
73 } | |
74 | |
75 } // namespace | |
76 | |
77 | |
78 EasyUnlockScreenlockStateHandler::EasyUnlockScreenlockStateHandler( | |
79 const std::string& user_email, | |
80 PrefService* pref_service, | |
81 ScreenlockBridge* screenlock_bridge) | |
82 : state_(STATE_INACTIVE), | |
83 user_email_(user_email), | |
84 pref_service_(pref_service), | |
85 screenlock_bridge_(screenlock_bridge) { | |
86 DCHECK(screenlock_bridge_); | |
87 screenlock_bridge_->AddObserver(this); | |
88 } | |
89 | |
90 EasyUnlockScreenlockStateHandler::~EasyUnlockScreenlockStateHandler() { | |
91 screenlock_bridge_->RemoveObserver(this); | |
92 } | |
93 | |
94 void EasyUnlockScreenlockStateHandler::ChangeState(State new_state) { | |
95 if (state_ == new_state) | |
96 return; | |
97 | |
98 state_ = screenlock_bridge_->lock_handler() ? new_state : STATE_INACTIVE; | |
dzhioev (left Google)
2014/08/12 19:36:08
screenlock_bridge_->IsLocked()?
dzhioev (left Google)
2014/08/12 19:36:08
Why do you ignore state change when screen is unlo
tbarzic
2014/08/12 21:40:57
Done.
tbarzic
2014/08/12 21:40:57
Yes. And the app would call ChangeState when scree
| |
99 | |
100 // If lock screen is still shown, make sure the previously set lockscreen | |
101 // state gets cleared. | |
102 if (!screenlock_bridge_->lock_handler()) | |
dzhioev (left Google)
2014/08/12 19:36:08
ditto
tbarzic
2014/08/12 21:40:57
Done.
| |
103 return; | |
104 | |
105 UpdateScreenlockAuthType(); | |
106 | |
107 ScreenlockBridge::UserPodCustomIconOptions icon_options; | |
108 | |
109 std::string icon_url = GetIconURLForState(state_); | |
110 if (icon_url.empty()) { | |
111 screenlock_bridge_->lock_handler()->HideUserPodCustomIcon(user_email_); | |
112 return; | |
113 } | |
114 icon_options.SetIconAsResourceURL(icon_url); | |
115 | |
116 UpdateTooltipOptions(&icon_options); | |
117 | |
118 if (UseOpaqueIcon(state_)) | |
119 icon_options.SetOpacity(kOpaqueIconOpacity); | |
120 | |
121 icon_options.SetSize(kIconSize, kIconSize); | |
122 | |
123 if (HasAnimation(state_)) | |
124 icon_options.SetAnimation(kSpinnerResourceWidth, kSpinnerIntervalMs); | |
125 | |
126 screenlock_bridge_->lock_handler()->ShowUserPodCustomIcon(user_email_, | |
127 icon_options); | |
128 } | |
129 | |
130 void EasyUnlockScreenlockStateHandler::OnScreenDidLock() {} | |
131 | |
132 void EasyUnlockScreenlockStateHandler::OnScreenDidUnlock() { | |
133 ChangeState(STATE_INACTIVE); | |
134 } | |
135 | |
136 void EasyUnlockScreenlockStateHandler::UpdateTooltipOptions( | |
137 ScreenlockBridge::UserPodCustomIconOptions* icon_options) { | |
138 bool show_tutorial = ShouldShowTutorial(); | |
139 | |
140 size_t resource_id = 0; | |
141 base::string16 device_name; | |
142 if (show_tutorial) { | |
143 resource_id = IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_TUTORIAL; | |
144 } else { | |
145 resource_id = GetTooltipResourceId(state_); | |
146 if (state_ == STATE_AUTHENTICATED || state_ == STATE_PHONE_UNLOCKABLE) | |
147 device_name = GetDeviceName(); | |
148 } | |
149 | |
150 if (!resource_id) | |
151 return; | |
152 | |
153 base::string16 tooltip; | |
154 if (device_name.empty()) { | |
155 tooltip = l10n_util::GetStringUTF16(resource_id); | |
156 } else { | |
157 tooltip = l10n_util::GetStringFUTF16(resource_id, device_name); | |
158 } | |
159 | |
160 if (tooltip.empty()) | |
161 return; | |
162 | |
163 if (show_tutorial) | |
164 MarkTutorialShown(); | |
165 | |
166 icon_options->SetTooltip(tooltip, show_tutorial /* autoshow tooltip */); | |
167 } | |
168 | |
169 bool EasyUnlockScreenlockStateHandler::ShouldShowTutorial() { | |
170 if (state_ != STATE_AUTHENTICATED) | |
171 return false; | |
172 return pref_service_ && | |
173 pref_service_->GetBoolean(prefs::kEasyUnlockShowTutorial); | |
174 } | |
175 | |
176 void EasyUnlockScreenlockStateHandler::MarkTutorialShown() { | |
177 if (!pref_service_) | |
178 return; | |
179 pref_service_->SetBoolean(prefs::kEasyUnlockShowTutorial, false); | |
180 } | |
181 | |
182 base::string16 EasyUnlockScreenlockStateHandler::GetDeviceName() { | |
183 #if defined(OS_CHROMEOS) | |
184 return chromeos::GetChromeDeviceType(); | |
185 #else | |
186 // TODO(tbarzic): Figure out the name for non Chrome OS case. | |
187 return base::ASCIIToUTF16("Chrome"); | |
188 #endif | |
189 } | |
190 | |
191 void EasyUnlockScreenlockStateHandler::UpdateScreenlockAuthType() { | |
192 if (state_ == STATE_AUTHENTICATED) { | |
193 screenlock_bridge_->lock_handler()->SetAuthType( | |
194 user_email_, | |
195 ScreenlockBridge::LockHandler::USER_CLICK, | |
196 l10n_util::GetStringUTF16( | |
197 IDS_EASY_UNLOCK_SCREENLOCK_USER_POD_AUTH_VALUE)); | |
198 } else if (screenlock_bridge_->lock_handler()->GetAuthType(user_email_) != | |
199 ScreenlockBridge::LockHandler::OFFLINE_PASSWORD) { | |
200 screenlock_bridge_->lock_handler()->SetAuthType( | |
201 user_email_, | |
202 ScreenlockBridge::LockHandler::OFFLINE_PASSWORD, | |
203 base::string16()); | |
204 } | |
205 } | |
OLD | NEW |