| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/input_method/input_method_manager_impl.h" | 5 #include "chrome/browser/chromeos/input_method/input_method_manager_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> // std::find | 7 #include <algorithm> // std::find |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 std::map<std::string, InputMethodDescriptor>::const_iterator ix = | 152 std::map<std::string, InputMethodDescriptor>::const_iterator ix = |
| 153 extra_input_methods_.find(input_method_id); | 153 extra_input_methods_.find(input_method_id); |
| 154 if (ix != extra_input_methods_.end()) | 154 if (ix != extra_input_methods_.end()) |
| 155 ime = &ix->second; | 155 ime = &ix->second; |
| 156 } | 156 } |
| 157 return ime; | 157 return ime; |
| 158 } | 158 } |
| 159 | 159 |
| 160 void InputMethodManagerImpl::EnableLoginLayouts( | 160 void InputMethodManagerImpl::EnableLoginLayouts( |
| 161 const std::string& language_code, | 161 const std::string& language_code, |
| 162 const std::string& initial_layout) { | 162 const std::vector<std::string>& initial_layouts) { |
| 163 if (state_ == STATE_TERMINATING) | 163 if (state_ == STATE_TERMINATING) |
| 164 return; | 164 return; |
| 165 | 165 |
| 166 std::vector<std::string> candidates; | 166 // First, hardware keyboard layout should be shown. |
| 167 std::vector<std::string> candidates = |
| 168 util_.GetHardwareLoginInputMethodIds(); |
| 169 |
| 170 // Seocnd, locale based input method should be shown. |
| 167 // Add input methods associated with the language. | 171 // Add input methods associated with the language. |
| 172 std::vector<std::string> layouts_from_locale; |
| 168 util_.GetInputMethodIdsFromLanguageCode(language_code, | 173 util_.GetInputMethodIdsFromLanguageCode(language_code, |
| 169 kKeyboardLayoutsOnly, | 174 kKeyboardLayoutsOnly, |
| 170 &candidates); | 175 &layouts_from_locale); |
| 171 // Add the hardware keyboard as well. We should always add this so users | 176 candidates.insert(candidates.end(), layouts_from_locale.begin(), |
| 172 // can use the hardware keyboard on the login screen and the screen locker. | 177 layouts_from_locale.end()); |
| 173 candidates.push_back(util_.GetHardwareLoginInputMethodId()); | |
| 174 | 178 |
| 175 std::vector<std::string> layouts; | 179 std::vector<std::string> layouts; |
| 176 // First, add the initial input method ID, if it's requested, to | 180 // First, add the initial input method ID, if it's requested, to |
| 177 // layouts, so it appears first on the list of active input | 181 // layouts, so it appears first on the list of active input |
| 178 // methods at the input language status menu. | 182 // methods at the input language status menu. |
| 179 if (util_.IsValidInputMethodId(initial_layout)) { | 183 for (size_t i = 0; i < initial_layouts.size(); ++i) { |
| 180 if (!IsLoginKeyboard(initial_layout)) { | 184 if (util_.IsValidInputMethodId(initial_layouts[i])) { |
| 181 DVLOG(1) | 185 if (IsLoginKeyboard(initial_layouts[i])) { |
| 182 << "EnableLoginLayouts: ignoring non-login initial keyboard layout:" | 186 layouts.push_back(initial_layouts[i]); |
| 183 << initial_layout; | 187 } else { |
| 184 } else { | 188 DVLOG(1) |
| 185 layouts.push_back(initial_layout); | 189 << "EnableLoginLayouts: ignoring non-login initial keyboard layout:" |
| 190 << initial_layouts[i]; |
| 191 } |
| 192 } else if (!initial_layouts[i].empty()) { |
| 193 DVLOG(1) << "EnableLoginLayouts: ignoring non-keyboard or invalid ID: " |
| 194 << initial_layouts[i]; |
| 186 } | 195 } |
| 187 } else if (!initial_layout.empty()) { | |
| 188 DVLOG(1) << "EnableLoginLayouts: ignoring non-keyboard or invalid ID: " | |
| 189 << initial_layout; | |
| 190 } | 196 } |
| 191 | 197 |
| 192 // Add candidates to layouts, while skipping duplicates. | 198 // Add candidates to layouts, while skipping duplicates. |
| 193 for (size_t i = 0; i < candidates.size(); ++i) { | 199 for (size_t i = 0; i < candidates.size(); ++i) { |
| 194 const std::string& candidate = candidates[i]; | 200 const std::string& candidate = candidates[i]; |
| 195 // Not efficient, but should be fine, as the two vectors are very | 201 // Not efficient, but should be fine, as the two vectors are very |
| 196 // short (2-5 items). | 202 // short (2-5 items). |
| 197 if (!Contains(layouts, candidate) && IsLoginKeyboard(candidate)) | 203 if (!Contains(layouts, candidate) && IsLoginKeyboard(candidate)) |
| 198 layouts.push_back(candidate); | 204 layouts.push_back(candidate); |
| 199 } | 205 } |
| 200 | 206 |
| 201 active_input_method_ids_.swap(layouts); | 207 active_input_method_ids_.swap(layouts); |
| 202 | 208 |
| 203 // Initialize candidate window controller and widgets such as | 209 // Initialize candidate window controller and widgets such as |
| 204 // candidate window, infolist and mode indicator. Note, mode | 210 // candidate window, infolist and mode indicator. Note, mode |
| 205 // indicator is used by only keyboard layout input methods. | 211 // indicator is used by only keyboard layout input methods. |
| 206 if (active_input_method_ids_.size() > 1) | 212 if (active_input_method_ids_.size() > 1) |
| 207 MaybeInitializeCandidateWindowController(); | 213 MaybeInitializeCandidateWindowController(); |
| 208 | 214 |
| 209 ChangeInputMethod(initial_layout); // you can pass empty |initial_layout|. | 215 // you can pass empty |initial_layout|. |
| 216 ChangeInputMethod(initial_layouts.empty() ? "" : initial_layouts[0]); |
| 210 } | 217 } |
| 211 | 218 |
| 212 // Adds new input method to given list. | 219 // Adds new input method to given list. |
| 213 bool InputMethodManagerImpl::EnableInputMethodImpl( | 220 bool InputMethodManagerImpl::EnableInputMethodImpl( |
| 214 const std::string& input_method_id, | 221 const std::string& input_method_id, |
| 215 std::vector<std::string>* new_active_input_method_ids) const { | 222 std::vector<std::string>* new_active_input_method_ids) const { |
| 216 DCHECK(new_active_input_method_ids); | 223 DCHECK(new_active_input_method_ids); |
| 217 if (!util_.IsValidInputMethodId(input_method_id)) { | 224 if (!util_.IsValidInputMethodId(input_method_id)) { |
| 218 DVLOG(1) << "EnableInputMethod: Invalid ID: " << input_method_id; | 225 DVLOG(1) << "EnableInputMethod: Invalid ID: " << input_method_id; |
| 219 return false; | 226 return false; |
| (...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 532 | 539 |
| 533 if (active_imes_changed) { | 540 if (active_imes_changed) { |
| 534 MaybeInitializeCandidateWindowController(); | 541 MaybeInitializeCandidateWindowController(); |
| 535 | 542 |
| 536 // If |current_input_method| is no longer in |active_input_method_ids_|, | 543 // If |current_input_method| is no longer in |active_input_method_ids_|, |
| 537 // switch to the first one in |active_input_method_ids_|. | 544 // switch to the first one in |active_input_method_ids_|. |
| 538 ChangeInputMethod(current_input_method_.id()); | 545 ChangeInputMethod(current_input_method_.id()); |
| 539 } | 546 } |
| 540 } | 547 } |
| 541 | 548 |
| 542 void InputMethodManagerImpl::SetInputMethodDefault() { | 549 void InputMethodManagerImpl::SetInputMethodLoginDefault() { |
| 543 // Set up keyboards. For example, when |locale| is "en-US", enable US qwerty | 550 // Set up keyboards. For example, when |locale| is "en-US", enable US qwerty |
| 544 // and US dvorak keyboard layouts. | 551 // and US dvorak keyboard layouts. |
| 545 if (g_browser_process && g_browser_process->local_state()) { | 552 if (g_browser_process && g_browser_process->local_state()) { |
| 546 const std::string locale = g_browser_process->GetApplicationLocale(); | 553 const std::string locale = g_browser_process->GetApplicationLocale(); |
| 547 // If the preferred keyboard for the login screen has been saved, use it. | 554 // If the preferred keyboard for the login screen has been saved, use it. |
| 548 PrefService* prefs = g_browser_process->local_state(); | 555 PrefService* prefs = g_browser_process->local_state(); |
| 549 std::string initial_input_method_id = | 556 std::string initial_input_method_id = |
| 550 prefs->GetString(chromeos::language_prefs::kPreferredKeyboardLayout); | 557 prefs->GetString(chromeos::language_prefs::kPreferredKeyboardLayout); |
| 558 std::vector<std::string> input_methods_to_be_enabled; |
| 551 if (initial_input_method_id.empty()) { | 559 if (initial_input_method_id.empty()) { |
| 552 // If kPreferredKeyboardLayout is not specified, use the hardware layout. | 560 // If kPreferredKeyboardLayout is not specified, use the hardware layout. |
| 553 initial_input_method_id = | 561 input_methods_to_be_enabled = util_.GetHardwareLoginInputMethodIds(); |
| 554 GetInputMethodUtil()->GetHardwareInputMethodId(); | 562 } else { |
| 563 input_methods_to_be_enabled.push_back(initial_input_method_id); |
| 555 } | 564 } |
| 556 EnableLoginLayouts(locale, initial_input_method_id); | 565 EnableLoginLayouts(locale, input_methods_to_be_enabled); |
| 557 } | 566 } |
| 558 } | 567 } |
| 559 | 568 |
| 560 bool InputMethodManagerImpl::SwitchToNextInputMethod() { | 569 bool InputMethodManagerImpl::SwitchToNextInputMethod() { |
| 561 // Sanity checks. | 570 // Sanity checks. |
| 562 if (active_input_method_ids_.empty()) { | 571 if (active_input_method_ids_.empty()) { |
| 563 DVLOG(1) << "active input method is empty"; | 572 DVLOG(1) << "active input method is empty"; |
| 564 return false; | 573 return false; |
| 565 } | 574 } |
| 566 | 575 |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 775 FOR_EACH_OBSERVER(InputMethodManager::CandidateWindowObserver, | 784 FOR_EACH_OBSERVER(InputMethodManager::CandidateWindowObserver, |
| 776 candidate_window_observers_, | 785 candidate_window_observers_, |
| 777 CandidateWindowClosed(this)); | 786 CandidateWindowClosed(this)); |
| 778 } | 787 } |
| 779 | 788 |
| 780 void InputMethodManagerImpl::OnScreenLocked() { | 789 void InputMethodManagerImpl::OnScreenLocked() { |
| 781 saved_previous_input_method_ = previous_input_method_; | 790 saved_previous_input_method_ = previous_input_method_; |
| 782 saved_current_input_method_ = current_input_method_; | 791 saved_current_input_method_ = current_input_method_; |
| 783 saved_active_input_method_ids_ = active_input_method_ids_; | 792 saved_active_input_method_ids_ = active_input_method_ids_; |
| 784 | 793 |
| 785 const std::string hardware_keyboard_id = util_.GetHardwareInputMethodId(); | 794 std::set<std::string> added_ids_; |
| 786 // We'll add the hardware keyboard if it's not included in | 795 |
| 787 // |active_input_method_list| so that the user can always use the hardware | 796 const std::vector<std::string>& hardware_keyboard_ids = |
| 788 // keyboard on the screen locker. | 797 util_.GetHardwareLoginInputMethodIds(); |
| 789 bool should_add_hardware_keyboard = true; | |
| 790 | 798 |
| 791 active_input_method_ids_.clear(); | 799 active_input_method_ids_.clear(); |
| 792 for (size_t i = 0; i < saved_active_input_method_ids_.size(); ++i) { | 800 for (size_t i = 0; i < saved_active_input_method_ids_.size(); ++i) { |
| 793 const std::string& input_method_id = saved_active_input_method_ids_[i]; | 801 const std::string& input_method_id = saved_active_input_method_ids_[i]; |
| 794 // Skip if it's not a keyboard layout. Drop input methods including | 802 // Skip if it's not a keyboard layout. Drop input methods including |
| 795 // extension ones. | 803 // extension ones. |
| 796 if (!IsLoginKeyboard(input_method_id)) | 804 if (!IsLoginKeyboard(input_method_id) || |
| 805 added_ids_.find(input_method_id) != added_ids_.end()) |
| 797 continue; | 806 continue; |
| 798 active_input_method_ids_.push_back(input_method_id); | 807 active_input_method_ids_.push_back(input_method_id); |
| 799 if (input_method_id == hardware_keyboard_id) | 808 added_ids_.insert(input_method_id); |
| 800 should_add_hardware_keyboard = false; | |
| 801 } | 809 } |
| 802 if (should_add_hardware_keyboard) | 810 |
| 803 active_input_method_ids_.push_back(hardware_keyboard_id); | 811 // We'll add the hardware keyboard if it's not included in |
| 812 // |active_input_method_ids_| so that the user can always use the hardware |
| 813 // keyboard on the screen locker. |
| 814 for (size_t i = 0; i < hardware_keyboard_ids.size(); ++i) { |
| 815 if (added_ids_.find(hardware_keyboard_ids[i]) == added_ids_.end()) { |
| 816 active_input_method_ids_.push_back(hardware_keyboard_ids[i]); |
| 817 added_ids_.insert(hardware_keyboard_ids[i]); |
| 818 } |
| 819 } |
| 804 | 820 |
| 805 ChangeInputMethod(current_input_method_.id()); | 821 ChangeInputMethod(current_input_method_.id()); |
| 806 } | 822 } |
| 807 | 823 |
| 808 void InputMethodManagerImpl::OnScreenUnlocked() { | 824 void InputMethodManagerImpl::OnScreenUnlocked() { |
| 809 previous_input_method_ = saved_previous_input_method_; | 825 previous_input_method_ = saved_previous_input_method_; |
| 810 current_input_method_ = saved_current_input_method_; | 826 current_input_method_ = saved_current_input_method_; |
| 811 active_input_method_ids_ = saved_active_input_method_ids_; | 827 active_input_method_ids_ = saved_active_input_method_ids_; |
| 812 | 828 |
| 813 ChangeInputMethod(current_input_method_.id()); | 829 ChangeInputMethod(current_input_method_.id()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 831 if (candidate_window_controller_.get()) | 847 if (candidate_window_controller_.get()) |
| 832 return; | 848 return; |
| 833 | 849 |
| 834 candidate_window_controller_.reset( | 850 candidate_window_controller_.reset( |
| 835 CandidateWindowController::CreateCandidateWindowController()); | 851 CandidateWindowController::CreateCandidateWindowController()); |
| 836 candidate_window_controller_->AddObserver(this); | 852 candidate_window_controller_->AddObserver(this); |
| 837 } | 853 } |
| 838 | 854 |
| 839 } // namespace input_method | 855 } // namespace input_method |
| 840 } // namespace chromeos | 856 } // namespace chromeos |
| OLD | NEW |