Views Omnibox: tolerate minor click-to-select-all dragging.
[chromium-blink-merge.git] / ui / app_list / speech_ui_model.cc
blob4d0e4a6b883ee62efb0b2efb8fbd366469685c71
1 // Copyright 2013 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.
5 #include "ui/app_list/speech_ui_model.h"
7 namespace app_list {
9 namespace {
11 // The default sound level, just gotten from the developer device.
12 const int16 kDefaultSoundLevel = 200;
14 } // namespace
16 SpeechUIModel::SpeechUIModel(SpeechRecognitionState initial_state)
17 : state_(initial_state),
18 minimum_sound_level_(kDefaultSoundLevel),
19 maximum_sound_level_(kDefaultSoundLevel) {}
21 SpeechUIModel::~SpeechUIModel() {}
23 void SpeechUIModel::SetSpeechResult(const base::string16& result,
24 bool is_final) {
25 if (result_ == result && is_final_ == is_final)
26 return;
28 result_ = result;
29 is_final_ = is_final;
30 FOR_EACH_OBSERVER(SpeechUIModelObserver,
31 observers_,
32 OnSpeechResult(result, is_final));
35 void SpeechUIModel::UpdateSoundLevel(int16 level) {
36 if (sound_level_ == level)
37 return;
39 sound_level_ = level;
41 // Tweak the sound level limits adaptively.
42 // - min is the minimum value during the speech recognition starts but speech
43 // itself hasn't started.
44 // - max is the maximum value when the user speaks.
45 if (state_ == SPEECH_RECOGNITION_IN_SPEECH)
46 maximum_sound_level_ = std::max(level, maximum_sound_level_);
47 else
48 minimum_sound_level_ = std::min(level, minimum_sound_level_);
50 if (maximum_sound_level_ < minimum_sound_level_) {
51 maximum_sound_level_ = std::max(
52 static_cast<int16>(minimum_sound_level_ + kDefaultSoundLevel),
53 kint16max);
56 int16 range = maximum_sound_level_ - minimum_sound_level_;
57 uint8 visible_level = 0;
58 if (range > 0) {
59 int16 visible_level_in_range =
60 std::min(std::max(minimum_sound_level_, sound_level_),
61 maximum_sound_level_);
62 visible_level =
63 (visible_level_in_range - minimum_sound_level_) * kuint8max / range;
66 FOR_EACH_OBSERVER(SpeechUIModelObserver,
67 observers_,
68 OnSpeechSoundLevelChanged(visible_level));
71 void SpeechUIModel::SetSpeechRecognitionState(
72 SpeechRecognitionState new_state) {
73 if (state_ == new_state)
74 return;
76 state_ = new_state;
77 // Revert the min/max sound level to the default.
78 if (state_ != SPEECH_RECOGNITION_RECOGNIZING &&
79 state_ != SPEECH_RECOGNITION_IN_SPEECH) {
80 minimum_sound_level_ = kDefaultSoundLevel;
81 maximum_sound_level_ = kDefaultSoundLevel;
84 FOR_EACH_OBSERVER(SpeechUIModelObserver,
85 observers_,
86 OnSpeechRecognitionStateChanged(new_state));
89 void SpeechUIModel::AddObserver(SpeechUIModelObserver* observer) {
90 observers_.AddObserver(observer);
93 void SpeechUIModel::RemoveObserver(SpeechUIModelObserver* observer) {
94 observers_.RemoveObserver(observer);
97 } // namespace app_list