-
Notifications
You must be signed in to change notification settings - Fork 215
/
PrivacyOptionsView.java
359 lines (275 loc) · 14.5 KB
/
PrivacyOptionsView.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/* -*- 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.Manifest;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import androidx.databinding.DataBindingUtil;
import org.mozilla.geckoview.GeckoSession;
import org.mozilla.geckoview.StorageController;
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.browser.SettingsStore;
import org.mozilla.vrbrowser.browser.engine.SessionStore;
import org.mozilla.vrbrowser.databinding.OptionsPrivacyBinding;
import org.mozilla.vrbrowser.ui.views.settings.SwitchSetting;
import org.mozilla.vrbrowser.ui.widgets.WidgetManagerDelegate;
import org.mozilla.vrbrowser.ui.widgets.WidgetPlacement;
import org.mozilla.vrbrowser.utils.DeviceType;
import java.util.ArrayList;
class PrivacyOptionsView extends SettingsView {
private OptionsPrivacyBinding mBinding;
private ArrayList<Pair<SwitchSetting, String>> mPermissionButtons;
public PrivacyOptionsView(Context aContext, WidgetManagerDelegate aWidgetManager) {
super(aContext, aWidgetManager);
initialize(aContext);
}
private void initialize(Context aContext) {
updateUI();
((Application)aContext.getApplicationContext()).registerActivityLifecycleCallbacks(mLifeCycleListener);
}
@Override
protected void updateUI() {
super.updateUI();
LayoutInflater inflater = LayoutInflater.from(getContext());
// Inflate this data binding layout
mBinding = DataBindingUtil.inflate(inflater, R.layout.options_privacy, this, true);
mScrollbar = mBinding.scrollbar;
// Header
mBinding.headerLayout.setBackClickListener(view -> onDismiss());
// Footer
mBinding.footerLayout.setFooterButtonClickListener(v -> resetOptions());
// Options
mBinding.showPrivacyButton.setOnClickListener(v -> {
SessionStore.get().getActiveSession().loadUri(getContext().getString(R.string.private_policy_url));
exitWholeSettings();
});
mBinding.clearCookiesSite.setOnClickListener(v -> {
SessionStore.get().clearCache(
StorageController.ClearFlags.SITE_DATA |
StorageController.ClearFlags.COOKIES |
StorageController.ClearFlags.SITE_SETTINGS);
});
mBinding.clearWebContent.setOnClickListener(v -> {
SessionStore.get().clearCache(StorageController.ClearFlags.ALL_CACHES);
});
TextView permissionsTitleText = findViewById(R.id.permissionsTitle);
permissionsTitleText.setText(getContext().getString(R.string.security_options_permissions_title, getContext().getString(R.string.app_name)));
mPermissionButtons = new ArrayList<>();
mPermissionButtons.add(Pair.create(findViewById(R.id.cameraPermissionSwitch), Manifest.permission.CAMERA));
mPermissionButtons.add(Pair.create(findViewById(R.id.microphonePermissionSwitch), Manifest.permission.RECORD_AUDIO));
mPermissionButtons.add(Pair.create(findViewById(R.id.locationPermissionSwitch), Manifest.permission.ACCESS_FINE_LOCATION));
mPermissionButtons.add(Pair.create(findViewById(R.id.storagePermissionSwitch), Manifest.permission.READ_EXTERNAL_STORAGE));
if (DeviceType.isOculusBuild() || DeviceType.isWaveBuild() || DeviceType.isPicoVR()) {
findViewById(R.id.cameraPermissionSwitch).setVisibility(View.GONE);
}
for (Pair<SwitchSetting, String> button: mPermissionButtons) {
button.first.setChecked(mWidgetManager.isPermissionGranted(button.second));
button.first.setOnCheckedChangeListener((compoundButton, enabled, apply) ->
togglePermission(button.first, button.second));
}
mBinding.drmContentPlaybackSwitch.setOnCheckedChangeListener(mDrmContentListener);
mBinding.drmContentPlaybackSwitch.setLinkClickListener((widget, url) -> {
SessionStore.get().getActiveSession().loadUri(url);
exitWholeSettings();
});
setDrmContent(SettingsStore.getInstance(getContext()).isDrmContentPlaybackEnabled(), false);
mBinding.trackingProtectionSwitch.setOnCheckedChangeListener(mTrackingProtectionListener);
setTrackingProtection(SettingsStore.getInstance(getContext()).isTrackingProtectionEnabled(), false);
mBinding.notificationsPermissionSwitch.setOnCheckedChangeListener(mNotificationsListener);
setNotifications(SettingsStore.getInstance(getContext()).isNotificationsEnabled(), false);
mBinding.speechDataSwitch.setOnCheckedChangeListener(mSpeechDataListener);
setSpeechData(SettingsStore.getInstance(getContext()).isSpeechDataCollectionEnabled(), false);
mBinding.telemetryDataSwitch.setOnCheckedChangeListener(mTelemetryListener);
setTelemetry(SettingsStore.getInstance(getContext()).isTelemetryEnabled(), false);
mBinding.crashReportsDataSwitch.setOnCheckedChangeListener(mCrashReportsListener);
setCrashReports(SettingsStore.getInstance(getContext()).isCrashReportingEnabled(), false);
mBinding.popUpsBlockingSwitch.setOnCheckedChangeListener(mPopUpsBlockingListener);
setPopUpsBlocking(SettingsStore.getInstance(getContext()).isPopUpsBlockingEnabled(), false);
mBinding.popUpsBlockingExceptionsButton.setOnClickListener(v -> mDelegate.showView(SettingViewType.POPUP_EXCEPTIONS));
mBinding.restoreTabsSwitch.setOnCheckedChangeListener(mRestoreTabsListener);
setRestoreTabs(SettingsStore.getInstance(getContext()).isRestoreTabsEnabled(), false);
}
private void togglePermission(SwitchSetting aButton, String aPermission) {
if (mWidgetManager.isPermissionGranted(aPermission)) {
showAlert(aButton.getDescription(), getContext().getString(R.string.security_options_permissions_reject_message));
aButton.setChecked(true);
} else {
mWidgetManager.requestPermission("", aPermission, new GeckoSession.PermissionDelegate.Callback() {
@Override
public void grant() {
aButton.setChecked(true);
}
@Override
public void reject() {
}
});
}
}
private SwitchSetting.OnCheckedChangeListener mDrmContentListener = (compoundButton, value, doApply) -> {
setDrmContent(value, doApply);
};
private SwitchSetting.OnCheckedChangeListener mTrackingProtectionListener = (compoundButton, value, doApply) -> {
setTrackingProtection(value, doApply);
};
private SwitchSetting.OnCheckedChangeListener mNotificationsListener = (compoundButton, value, doApply) -> {
setNotifications(value, doApply);
};
private SwitchSetting.OnCheckedChangeListener mSpeechDataListener = (compoundButton, value, doApply) -> {
setSpeechData(value, doApply);
};
private SwitchSetting.OnCheckedChangeListener mTelemetryListener = (compoundButton, value, doApply) -> {
setTelemetry(value, doApply);
};
private SwitchSetting.OnCheckedChangeListener mCrashReportsListener = (compoundButton, value, doApply) -> {
setCrashReports(value, doApply);
};
private SwitchSetting.OnCheckedChangeListener mPopUpsBlockingListener = (compoundButton, value, doApply) -> {
setPopUpsBlocking(value, doApply);
};
private SwitchSetting.OnCheckedChangeListener mRestoreTabsListener = (compoundButton, value, doApply) -> {
setRestoreTabs(value, doApply);
};
private void resetOptions() {
if (mBinding.drmContentPlaybackSwitch.isChecked() != SettingsStore.DRM_PLAYBACK_DEFAULT) {
setDrmContent(SettingsStore.DRM_PLAYBACK_DEFAULT, true);
}
if (mBinding.trackingProtectionSwitch.isChecked() != SettingsStore.TRACKING_DEFAULT) {
setTrackingProtection(SettingsStore.TRACKING_DEFAULT, true);
}
if (mBinding.notificationsPermissionSwitch.isChecked() != SettingsStore.NOTIFICATIONS_DEFAULT) {
setNotifications(SettingsStore.NOTIFICATIONS_DEFAULT, true);
}
if (mBinding.speechDataSwitch.isChecked() != SettingsStore.SPEECH_DATA_COLLECTION_DEFAULT) {
setSpeechData(SettingsStore.SPEECH_DATA_COLLECTION_DEFAULT, true);
}
if (mBinding.telemetryDataSwitch.isChecked() != SettingsStore.TELEMETRY_DEFAULT) {
setTelemetry(SettingsStore.TELEMETRY_DEFAULT, true);
}
if (mBinding.crashReportsDataSwitch.isChecked() != SettingsStore.CRASH_REPORTING_DEFAULT) {
setCrashReports(SettingsStore.CRASH_REPORTING_DEFAULT, true);
}
if (mBinding.popUpsBlockingSwitch.isChecked() != SettingsStore.POP_UPS_BLOCKING_DEFAULT) {
setPopUpsBlocking(SettingsStore.POP_UPS_BLOCKING_DEFAULT, true);
}
if (mBinding.restoreTabsSwitch.isChecked() != SettingsStore.RESTORE_TABS_ENABLED) {
setRestoreTabs(SettingsStore.RESTORE_TABS_ENABLED, true);
}
}
private void setDrmContent(boolean value, boolean doApply) {
mBinding.drmContentPlaybackSwitch.setOnCheckedChangeListener(null);
mBinding.drmContentPlaybackSwitch.setValue(value, false);
mBinding.drmContentPlaybackSwitch.setOnCheckedChangeListener(mDrmContentListener);
if (doApply) {
SettingsStore.getInstance(getContext()).setDrmContentPlaybackEnabled(value);
// TODO Enable/Disable DRM content playback
}
}
private void setTrackingProtection(boolean value, boolean doApply) {
mBinding.trackingProtectionSwitch.setOnCheckedChangeListener(null);
mBinding.trackingProtectionSwitch.setValue(value, false);
mBinding.trackingProtectionSwitch.setOnCheckedChangeListener(mTrackingProtectionListener);
if (doApply) {
SettingsStore.getInstance(getContext()).setTrackingProtectionEnabled(value);
SessionStore.get().setTrackingProtection(value);
}
}
private void setNotifications(boolean value, boolean doApply) {
mBinding.notificationsPermissionSwitch.setOnCheckedChangeListener(null);
mBinding.notificationsPermissionSwitch.setValue(value, false);
mBinding.notificationsPermissionSwitch.setOnCheckedChangeListener(mNotificationsListener);
if (doApply) {
SettingsStore.getInstance(getContext()).setNotificationsEnabled(value);
}
}
private void setSpeechData(boolean value, boolean doApply) {
mBinding.speechDataSwitch.setOnCheckedChangeListener(null);
mBinding.speechDataSwitch.setValue(value, false);
mBinding.speechDataSwitch.setOnCheckedChangeListener(mSpeechDataListener);
if (doApply) {
SettingsStore.getInstance(getContext()).setSpeechDataCollectionEnabled(value);
}
}
private void setTelemetry(boolean value, boolean doApply) {
mBinding.telemetryDataSwitch.setOnCheckedChangeListener(null);
mBinding.telemetryDataSwitch.setValue(value, false);
mBinding.telemetryDataSwitch.setOnCheckedChangeListener(mTelemetryListener);
if (doApply) {
SettingsStore.getInstance(getContext()).setTelemetryEnabled(value);
}
}
private void setCrashReports(boolean value, boolean doApply) {
mBinding.crashReportsDataSwitch.setOnCheckedChangeListener(null);
mBinding.crashReportsDataSwitch.setValue(value, false);
mBinding.crashReportsDataSwitch.setOnCheckedChangeListener(mCrashReportsListener);
if (doApply) {
SettingsStore.getInstance(getContext()).setCrashReportingEnabled(value);
}
}
private void setPopUpsBlocking(boolean value, boolean doApply) {
mBinding.popUpsBlockingSwitch.setOnCheckedChangeListener(null);
mBinding.popUpsBlockingSwitch.setValue(value, false);
mBinding.popUpsBlockingSwitch.setOnCheckedChangeListener(mPopUpsBlockingListener);
if (doApply) {
SettingsStore.getInstance(getContext()).setPopUpsBlockingEnabled(value);
}
}
private void setRestoreTabs(boolean value, boolean doApply) {
mBinding.restoreTabsSwitch.setOnCheckedChangeListener(null);
mBinding.restoreTabsSwitch.setValue(value, false);
mBinding.restoreTabsSwitch.setOnCheckedChangeListener(mRestoreTabsListener);
if (doApply) {
SettingsStore.getInstance(getContext()).setRestoreTabsEnabled(value);
}
}
@Override
public Point getDimensions() {
return new Point( WidgetPlacement.dpDimension(getContext(), R.dimen.settings_dialog_width),
WidgetPlacement.dpDimension(getContext(), R.dimen.privacy_options_height));
}
@Override
public void releasePointerCapture() {
super.releasePointerCapture();
((Application)getContext().getApplicationContext()).unregisterActivityLifecycleCallbacks(mLifeCycleListener);
}
private Application.ActivityLifecycleCallbacks mLifeCycleListener = new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
// Refresh permission settings status after a permission has been requested
for (Pair<SwitchSetting, String> button: mPermissionButtons) {
button.first.setValue(mWidgetManager.isPermissionGranted(button.second), false);
}
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
activity.getApplication().unregisterActivityLifecycleCallbacks(mLifeCycleListener);
}
};
@Override
protected SettingViewType getType() {
return SettingViewType.PRIVACY;
}
}