1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/arc/settings_bridge.h"
#include <algorithm>
#include "base/strings/stringprintf.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
using ::chromeos::system::TimezoneSettings;
namespace arc {
namespace fontsizes {
double ConvertFontSizeChromeToAndroid(int default_size,
int default_fixed_size,
int minimum_size) {
// kWebKitDefaultFixedFontSize is automatically set to be 3 pixels smaller
// than kWebKitDefaultFontSize when Chrome's settings page's main font
// dropdown control is adjusted. If the user specifically sets a higher
// fixed font size we will want to take into account the adjustment.
default_fixed_size += 3;
int max_chrome_size =
std::max(std::max(default_fixed_size, default_size), minimum_size);
double android_scale = kAndroidFontScaleSmall;
if (max_chrome_size >= kChromeFontSizeVeryLarge) {
android_scale = kAndroidFontScaleHuge;
} else if (max_chrome_size >= kChromeFontSizeLarge) {
android_scale = kAndroidFontScaleLarge;
} else if (max_chrome_size >= kChromeFontSizeNormal) {
android_scale = kAndroidFontScaleNormal;
}
return android_scale;
}
} // namespace fontsizes
SettingsBridge::SettingsBridge(SettingsBridge::Delegate* delegate)
: delegate_(delegate) {
DCHECK(delegate_);
StartObservingSettingsChanges();
SyncAllPrefs();
}
SettingsBridge::~SettingsBridge() {
StopObservingSettingsChanges();
}
void SettingsBridge::StartObservingSettingsChanges() {
Profile* profile = ProfileManager::GetActiveUserProfile();
registrar_.Init(profile->GetPrefs());
AddPrefToObserve(prefs::kWebKitDefaultFixedFontSize);
AddPrefToObserve(prefs::kWebKitDefaultFontSize);
AddPrefToObserve(prefs::kWebKitMinimumFontSize);
AddPrefToObserve(prefs::kAccessibilitySpokenFeedbackEnabled);
AddPrefToObserve(prefs::kUse24HourClock);
TimezoneSettings::GetInstance()->AddObserver(this);
}
void SettingsBridge::SyncAllPrefs() const {
SyncFontSize();
SyncLocale();
SyncSpokenFeedbackEnabled();
SyncTimeZone();
SyncUse24HourClock();
}
void SettingsBridge::StopObservingSettingsChanges() {
registrar_.RemoveAll();
TimezoneSettings::GetInstance()->RemoveObserver(this);
}
void SettingsBridge::AddPrefToObserve(const std::string& pref_name) {
registrar_.Add(pref_name, base::Bind(&SettingsBridge::OnPrefChanged,
base::Unretained(this)));
}
void SettingsBridge::OnPrefChanged(const std::string& pref_name) const {
if (pref_name == prefs::kAccessibilitySpokenFeedbackEnabled) {
SyncSpokenFeedbackEnabled();
} else if (pref_name == prefs::kWebKitDefaultFixedFontSize ||
pref_name == prefs::kWebKitDefaultFontSize ||
pref_name == prefs::kWebKitMinimumFontSize) {
SyncFontSize();
} else if (pref_name == prefs::kUse24HourClock) {
SyncUse24HourClock();
} else {
LOG(ERROR) << "Unknown pref changed.";
}
}
void SettingsBridge::TimezoneChanged(const icu::TimeZone& timezone) {
SyncTimeZone();
}
int SettingsBridge::GetIntegerPref(const std::string& pref_name) const {
const PrefService::Preference* pref =
registrar_.prefs()->FindPreference(pref_name);
DCHECK(pref);
int val = -1;
bool value_exists = pref->GetValue()->GetAsInteger(&val);
DCHECK(value_exists);
return val;
}
void SettingsBridge::SyncFontSize() const {
int default_size = GetIntegerPref(prefs::kWebKitDefaultFontSize);
int default_fixed_size = GetIntegerPref(prefs::kWebKitDefaultFixedFontSize);
int minimum_size = GetIntegerPref(prefs::kWebKitMinimumFontSize);
double android_scale = fontsizes::ConvertFontSizeChromeToAndroid(
default_size, default_fixed_size, minimum_size);
base::DictionaryValue extras;
extras.SetDouble("scale", android_scale);
SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_FONT_SCALE",
extras);
}
void SettingsBridge::SyncSpokenFeedbackEnabled() const {
const PrefService::Preference* pref = registrar_.prefs()->FindPreference(
prefs::kAccessibilitySpokenFeedbackEnabled);
DCHECK(pref);
bool enabled = false;
bool value_exists = pref->GetValue()->GetAsBoolean(&enabled);
DCHECK(value_exists);
base::DictionaryValue extras;
extras.SetBoolean("enabled", enabled);
SendSettingsBroadcast(
"org.chromium.arc.intent_helper.SET_SPOKEN_FEEDBACK_ENABLED", extras);
}
void SettingsBridge::SyncLocale() const {
const PrefService::Preference* pref =
registrar_.prefs()->FindPreference(prefs::kApplicationLocale);
DCHECK(pref);
std::string locale;
bool value_exists = pref->GetValue()->GetAsString(&locale);
DCHECK(value_exists);
base::DictionaryValue extras;
extras.SetString("locale", locale);
SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_LOCALE", extras);
}
void SettingsBridge::SyncTimeZone() const {
TimezoneSettings* timezone_settings = TimezoneSettings::GetInstance();
base::string16 timezoneID = timezone_settings->GetCurrentTimezoneID();
base::DictionaryValue extras;
extras.SetString("olsonTimeZone", timezoneID);
SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_TIME_ZONE", extras);
}
void SettingsBridge::SyncUse24HourClock() const {
const PrefService::Preference* pref =
registrar_.prefs()->FindPreference(prefs::kUse24HourClock);
DCHECK(pref);
bool use24HourClock = false;
bool value_exists = pref->GetValue()->GetAsBoolean(&use24HourClock);
DCHECK(value_exists);
base::DictionaryValue extras;
extras.SetBoolean("use24HourClock", use24HourClock);
SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_USE_24_HOUR_CLOCK",
extras);
}
void SettingsBridge::SendSettingsBroadcast(
const std::string& action,
const base::DictionaryValue& extras) const {
delegate_->OnBroadcastNeeded(action, extras);
}
} // namespace arc
|