-
Notifications
You must be signed in to change notification settings - Fork 215
/
FxAAccountOptionsView.java
226 lines (176 loc) · 8.33 KB
/
FxAAccountOptionsView.java
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.vrbrowser.ui.widgets.settings;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import androidx.databinding.DataBindingUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.VRBrowserApplication;
import org.mozilla.vrbrowser.browser.Accounts;
import org.mozilla.vrbrowser.browser.SettingsStore;
import org.mozilla.vrbrowser.databinding.OptionsFxaAccountBinding;
import org.mozilla.vrbrowser.ui.views.settings.SwitchSetting;
import org.mozilla.vrbrowser.ui.widgets.WidgetManagerDelegate;
import org.mozilla.vrbrowser.utils.SystemUtils;
import java.util.Objects;
import java.util.concurrent.Executor;
import mozilla.components.concept.sync.AccountObserver;
import mozilla.components.concept.sync.AuthType;
import mozilla.components.concept.sync.OAuthAccount;
import mozilla.components.concept.sync.Profile;
import mozilla.components.service.fxa.SyncEngine;
import mozilla.components.service.fxa.sync.SyncReason;
import mozilla.components.service.fxa.sync.SyncStatusObserver;
class FxAAccountOptionsView extends SettingsView {
private static final String LOGTAG = SystemUtils.createLogtag(FxAAccountOptionsView.class);
private OptionsFxaAccountBinding mBinding;
private Accounts mAccounts;
private Executor mUIThreadExecutor;
private boolean mInitialBookmarksState;
private boolean mInitialHistoryState;
public FxAAccountOptionsView(Context aContext, WidgetManagerDelegate aWidgetManager) {
super(aContext, aWidgetManager);
initialize(aContext);
}
private void initialize(Context aContext) {
LayoutInflater inflater = LayoutInflater.from(aContext);
// Inflate this data binding layout
mBinding = DataBindingUtil.inflate(inflater, R.layout.options_fxa_account, this, true);
mScrollbar = mBinding.scrollbar;
// Header
mBinding.headerLayout.setBackClickListener(view -> onDismiss());
mAccounts = ((VRBrowserApplication)getContext().getApplicationContext()).getAccounts();
mUIThreadExecutor = ((VRBrowserApplication)getContext().getApplicationContext()).getExecutors().mainThread();
mBinding.signButton.setOnClickListener(view -> mAccounts.logoutAsync());
mBinding.syncButton.setOnClickListener(this::sync);
mBinding.setIsSyncing(mAccounts.isSyncing());
mBinding.setLastSync(mAccounts.lastSync());
mBinding.bookmarksSyncSwitch.setOnCheckedChangeListener(mBookmarksSyncListener);
mBinding.historySyncSwitch.setOnCheckedChangeListener(mHistorySyncListener);
updateCurrentAccountState();
// Footer
mBinding.footerLayout.setFooterButtonClickListener(v -> resetOptions());
}
@Override
public void onShown() {
super.onShown();
mAccounts.addAccountListener(mAccountListener);
mAccounts.addSyncListener(mSyncListener);
mInitialBookmarksState = mAccounts.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE);
mInitialHistoryState = mAccounts.isEngineEnabled(SyncEngine.History.INSTANCE);
mBinding.bookmarksSyncSwitch.setValue(mInitialBookmarksState, false);
mBinding.historySyncSwitch.setValue(mInitialHistoryState, false);
updateSyncBindings(mAccounts.isSyncing());
}
@Override
public void onHidden() {
super.onHidden();
mAccounts.removeAccountListener(mAccountListener);
mAccounts.removeSyncListener(mSyncListener);
// We only sync engines in case the user has changed the sync engines since previous sync
if (mBinding.bookmarksSyncSwitch.isChecked() != mInitialBookmarksState ||
mBinding.historySyncSwitch.isChecked() != mInitialHistoryState) {
mAccounts.syncNowAsync(SyncReason.EngineChange.INSTANCE, false);
}
}
private SwitchSetting.OnCheckedChangeListener mBookmarksSyncListener = (compoundButton, value, apply) -> {
mAccounts.setSyncStatus(SyncEngine.Bookmarks.INSTANCE, value);
};
private SwitchSetting.OnCheckedChangeListener mHistorySyncListener = (compoundButton, value, apply) -> {
mAccounts.setSyncStatus(SyncEngine.History.INSTANCE, value);
};
private void resetOptions() {
mBinding.bookmarksSyncSwitch.setValue(SettingsStore.BOOKMARKS_SYNC_DEFAULT, true);
mBinding.historySyncSwitch.setValue(SettingsStore.HISTORY_SYNC_DEFAULT, true);
}
private SyncStatusObserver mSyncListener = new SyncStatusObserver() {
@Override
public void onStarted() {
updateSyncBindings(true);
}
@Override
public void onIdle() {
updateSyncBindings(false);
// This shouldn't be necessary but for some reason the buttons stays hovered after the sync.
// I guess Android restoring it to the latest state (hovered) before being disabled
// Probably an Android bindings bug.
mBinding.bookmarksSyncSwitch.setHovered(false);
mBinding.historySyncSwitch.setHovered(false);
mBinding.syncButton.setHovered(false);
}
@Override
public void onError(@Nullable Exception e) {
updateSyncBindings(false);
}
};
private void updateSyncBindings(boolean isSyncing) {
mBinding.setIsSyncing(isSyncing);
mBinding.setLastSync(mAccounts.lastSync());
mBinding.executePendingBindings();
}
void updateCurrentAccountState() {
switch(mAccounts.getAccountStatus()) {
case NEEDS_RECONNECT:
mBinding.signButton.setButtonText(R.string.settings_fxa_account_reconnect);
break;
case SIGNED_IN:
mBinding.signButton.setButtonText(R.string.settings_fxa_account_sign_out);
Profile profile = mAccounts.accountProfile();
if (profile != null) {
updateProfile(profile);
} else {
Objects.requireNonNull(mAccounts.updateProfileAsync()).
thenAcceptAsync((u) -> updateProfile(mAccounts.accountProfile()), mUIThreadExecutor).
exceptionally(throwable -> {
Log.d(LOGTAG, "Error getting the account profile: " + throwable.getLocalizedMessage());
throwable.printStackTrace();
return null;
});
}
break;
case SIGNED_OUT:
mBinding.signButton.setButtonText(R.string.settings_fxa_account_sign_in);
break;
default:
throw new IllegalStateException("Unexpected value: " + mAccounts.getAccountStatus());
}
}
private void updateProfile(Profile profile) {
if (profile != null) {
mBinding.accountEmail.setText(profile.getEmail());
}
}
private void sync(View view) {
if (mBinding.bookmarksSyncSwitch.isChecked() != mInitialBookmarksState ||
mBinding.historySyncSwitch.isChecked() != mInitialHistoryState) {
mAccounts.syncNowAsync(SyncReason.EngineChange.INSTANCE, false);
} else {
mAccounts.syncNowAsync(SyncReason.User.INSTANCE, false);
}
}
private AccountObserver mAccountListener = new AccountObserver() {
@Override
public void onAuthenticated(@NotNull OAuthAccount oAuthAccount, @NotNull AuthType authType) {
mBinding.signButton.setButtonText(R.string.settings_fxa_account_sign_out);
}
@Override
public void onProfileUpdated(@NotNull Profile profile) {
mBinding.accountEmail.setText(profile.getEmail());
mBinding.setLastSync(mAccounts.lastSync());
}
@Override
public void onLoggedOut() {
post(FxAAccountOptionsView.this::onDismiss);
}
@Override
public void onAuthenticationProblems() {
post(FxAAccountOptionsView.this::onDismiss);
}
};
}