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"; |
| 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 // Make sure the screenlock state set by this gets cleared. |
| 93 ChangeState(STATE_INACTIVE); |
| 94 } |
| 95 |
| 96 void EasyUnlockScreenlockStateHandler::ChangeState(State new_state) { |
| 97 if (state_ == new_state) |
| 98 return; |
| 99 |
| 100 state_ = new_state; |
| 101 |
| 102 // If lock screen is not active, just cache the current state. |
| 103 // The screenlock state will get refreshed in |ScreenDidLock|. |
| 104 if (!screenlock_bridge_->IsLocked()) |
| 105 return; |
| 106 |
| 107 UpdateScreenlockAuthType(); |
| 108 |
| 109 ScreenlockBridge::UserPodCustomIconOptions icon_options; |
| 110 |
| 111 std::string icon_url = GetIconURLForState(state_); |
| 112 if (icon_url.empty()) { |
| 113 screenlock_bridge_->lock_handler()->HideUserPodCustomIcon(user_email_); |
| 114 return; |
| 115 } |
| 116 icon_options.SetIconAsResourceURL(icon_url); |
| 117 |
| 118 UpdateTooltipOptions(&icon_options); |
| 119 |
| 120 if (UseOpaqueIcon(state_)) |
| 121 icon_options.SetOpacity(kOpaqueIconOpacity); |
| 122 |
| 123 icon_options.SetSize(kIconSize, kIconSize); |
| 124 |
| 125 if (HasAnimation(state_)) |
| 126 icon_options.SetAnimation(kSpinnerResourceWidth, kSpinnerIntervalMs); |
| 127 |
| 128 screenlock_bridge_->lock_handler()->ShowUserPodCustomIcon(user_email_, |
| 129 icon_options); |
| 130 } |
| 131 |
| 132 void EasyUnlockScreenlockStateHandler::OnScreenDidLock() { |
| 133 State last_state = state_; |
| 134 // This should force updating screenlock state. |
| 135 state_ = STATE_INACTIVE; |
| 136 ChangeState(last_state); |
| 137 } |
| 138 |
| 139 void EasyUnlockScreenlockStateHandler::OnScreenDidUnlock() { |
| 140 } |
| 141 |
| 142 void EasyUnlockScreenlockStateHandler::UpdateTooltipOptions( |
| 143 ScreenlockBridge::UserPodCustomIconOptions* icon_options) { |
| 144 bool show_tutorial = ShouldShowTutorial(); |
| 145 |
| 146 size_t resource_id = 0; |
| 147 base::string16 device_name; |
| 148 if (show_tutorial) { |
| 149 resource_id = IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_TUTORIAL; |
| 150 } else { |
| 151 resource_id = GetTooltipResourceId(state_); |
| 152 if (state_ == STATE_AUTHENTICATED || state_ == STATE_PHONE_UNLOCKABLE) |
| 153 device_name = GetDeviceName(); |
| 154 } |
| 155 |
| 156 if (!resource_id) |
| 157 return; |
| 158 |
| 159 base::string16 tooltip; |
| 160 if (device_name.empty()) { |
| 161 tooltip = l10n_util::GetStringUTF16(resource_id); |
| 162 } else { |
| 163 tooltip = l10n_util::GetStringFUTF16(resource_id, device_name); |
| 164 } |
| 165 |
| 166 if (tooltip.empty()) |
| 167 return; |
| 168 |
| 169 if (show_tutorial) |
| 170 MarkTutorialShown(); |
| 171 |
| 172 icon_options->SetTooltip(tooltip, show_tutorial /* autoshow tooltip */); |
| 173 } |
| 174 |
| 175 bool EasyUnlockScreenlockStateHandler::ShouldShowTutorial() { |
| 176 if (state_ != STATE_AUTHENTICATED) |
| 177 return false; |
| 178 return pref_service_ && |
| 179 pref_service_->GetBoolean(prefs::kEasyUnlockShowTutorial); |
| 180 } |
| 181 |
| 182 void EasyUnlockScreenlockStateHandler::MarkTutorialShown() { |
| 183 if (!pref_service_) |
| 184 return; |
| 185 pref_service_->SetBoolean(prefs::kEasyUnlockShowTutorial, false); |
| 186 } |
| 187 |
| 188 base::string16 EasyUnlockScreenlockStateHandler::GetDeviceName() { |
| 189 #if defined(OS_CHROMEOS) |
| 190 return chromeos::GetChromeDeviceType(); |
| 191 #else |
| 192 // TODO(tbarzic): Figure out the name for non Chrome OS case. |
| 193 return base::ASCIIToUTF16("Chrome"); |
| 194 #endif |
| 195 } |
| 196 |
| 197 void EasyUnlockScreenlockStateHandler::UpdateScreenlockAuthType() { |
| 198 if (state_ == STATE_AUTHENTICATED) { |
| 199 screenlock_bridge_->lock_handler()->SetAuthType( |
| 200 user_email_, |
| 201 ScreenlockBridge::LockHandler::USER_CLICK, |
| 202 l10n_util::GetStringUTF16( |
| 203 IDS_EASY_UNLOCK_SCREENLOCK_USER_POD_AUTH_VALUE)); |
| 204 } else if (screenlock_bridge_->lock_handler()->GetAuthType(user_email_) != |
| 205 ScreenlockBridge::LockHandler::OFFLINE_PASSWORD) { |
| 206 screenlock_bridge_->lock_handler()->SetAuthType( |
| 207 user_email_, |
| 208 ScreenlockBridge::LockHandler::OFFLINE_PASSWORD, |
| 209 base::string16()); |
| 210 } |
| 211 } |
OLD | NEW |