6
6
package org .mozilla .vrbrowser .ui .widgets ;
7
7
8
8
import android .content .Context ;
9
+ import android .content .SharedPreferences ;
9
10
import android .net .Uri ;
11
+ import android .preference .PreferenceManager ;
10
12
import android .support .annotation .NonNull ;
11
13
import android .util .AttributeSet ;
12
14
import android .util .Log ;
33
35
public class NavigationBarWidget extends UIWidget implements GeckoSession .NavigationDelegate ,
34
36
GeckoSession .ProgressDelegate , GeckoSession .ContentDelegate ,
35
37
WidgetManagerDelegate .UpdateListener , SessionStore .SessionChangeListener ,
36
- NavigationURLBar .NavigationURLBarDelegate , VoiceSearchWidget .VoiceSearchDelegate {
38
+ NavigationURLBar .NavigationURLBarDelegate , VoiceSearchWidget .VoiceSearchDelegate ,
39
+ SharedPreferences .OnSharedPreferenceChangeListener {
37
40
38
41
private static final String LOGTAG = "VRB" ;
39
42
@@ -42,6 +45,7 @@ public class NavigationBarWidget extends UIWidget implements GeckoSession.Naviga
42
45
private UIButton mForwardButton ;
43
46
private UIButton mReloadButton ;
44
47
private UIButton mHomeButton ;
48
+ private UIButton mServoButton ;
45
49
private NavigationURLBar mURLBar ;
46
50
private ViewGroup mNavigationContainer ;
47
51
private ViewGroup mFocusModeContainer ;
@@ -60,6 +64,8 @@ public class NavigationBarWidget extends UIWidget implements GeckoSession.Naviga
60
64
private ArrayList <CustomUIButton > mButtons ;
61
65
private int mURLBarLayoutIndex ;
62
66
private VoiceSearchWidget mVoiceSearchWidget ;
67
+ private Context mAppContext ;
68
+ private SharedPreferences mPrefs ;
63
69
64
70
public NavigationBarWidget (Context aContext ) {
65
71
super (aContext );
@@ -77,12 +83,14 @@ public NavigationBarWidget(Context aContext, AttributeSet aAttrs, int aDefStyle)
77
83
}
78
84
79
85
private void initialize (Context aContext ) {
86
+ mAppContext = aContext .getApplicationContext ();
80
87
inflate (aContext , R .layout .navigation_bar , this );
81
88
mAudio = AudioEngine .fromContext (aContext );
82
89
mBackButton = findViewById (R .id .backButton );
83
90
mForwardButton = findViewById (R .id .forwardButton );
84
91
mReloadButton = findViewById (R .id .reloadButton );
85
92
mHomeButton = findViewById (R .id .homeButton );
93
+ mServoButton = findViewById (R .id .servoButton );
86
94
mURLBar = findViewById (R .id .urlBar );
87
95
mNavigationContainer = findViewById (R .id .navigationBarContainer );
88
96
mFocusModeContainer = findViewById (R .id .focusModeContainer );
@@ -147,6 +155,17 @@ public void onClick(View v) {
147
155
}
148
156
});
149
157
158
+ mServoButton .setOnClickListener (new OnClickListener () {
159
+ @ Override
160
+ public void onClick (View v ) {
161
+ v .requestFocusFromTouch ();
162
+ SessionStore .get ().toggleServo ();
163
+ if (mAudio != null ) {
164
+ mAudio .playSound (AudioEngine .Sound .CLICK );
165
+ }
166
+ }
167
+ });
168
+
150
169
mResizeEnterButton = findViewById (R .id .resizeEnterButton );
151
170
mResizeExitButton = findViewById (R .id .resizeExitButton );
152
171
mPreset0 = findViewById (R .id .resizePreset0 );
@@ -223,7 +242,7 @@ public void onClick(View view) {
223
242
mButtons = new ArrayList <>();
224
243
mButtons .addAll (Arrays .<CustomUIButton >asList (
225
244
mBackButton , mForwardButton , mReloadButton , mHomeButton , mResizeEnterButton , mResizeExitButton ,
226
- mPreset0 , mPreset1 , mPreset2 , mPreset3 ));
245
+ mServoButton , mPreset0 , mPreset1 , mPreset2 , mPreset3 ));
227
246
228
247
mURLBar .setDelegate (this );
229
248
@@ -236,11 +255,16 @@ public void onClick(View view) {
236
255
mVoiceSearchWidget .setDelegate (this );
237
256
238
257
SessionStore .get ().addSessionChangeListener (this );
258
+
259
+ mPrefs = PreferenceManager .getDefaultSharedPreferences (mAppContext );
260
+ mPrefs .registerOnSharedPreferenceChangeListener (this );
261
+ updateServoButton ();
239
262
}
240
263
241
264
@ Override
242
265
public void releaseWidget () {
243
266
mWidgetManager .removeUpdateListener (this );
267
+ mPrefs .unregisterOnSharedPreferenceChangeListener (this );
244
268
SessionStore .get ().removeNavigationListener (this );
245
269
SessionStore .get ().removeProgressListener (this );
246
270
SessionStore .get ().removeContentListener (this );
@@ -365,6 +389,14 @@ public void showVoiceSearch() {
365
389
mURLBar .showVoiceSearch (true );
366
390
}
367
391
392
+ public void updateServoButton () {
393
+ if (SettingsStore .getInstance (mAppContext ).isServoEnabled ()) {
394
+ mServoButton .setVisibility (View .VISIBLE );
395
+ } else {
396
+ mServoButton .setVisibility (View .GONE );
397
+ }
398
+ }
399
+
368
400
@ Override
369
401
public GeckoResult <GeckoSession > onNewSession (@ NonNull GeckoSession aSession , @ NonNull String aUri ) {
370
402
return null ;
@@ -606,4 +638,11 @@ public void OnVoiceSearchCanceled() {
606
638
public void OnVoiceSearchError () {
607
639
// Nothing to do yet
608
640
}
641
+
642
+ @ Override
643
+ public void onSharedPreferenceChanged (SharedPreferences sharedPreferences , String key ) {
644
+ if (key == mAppContext .getString (R .string .settings_key_servo )) {
645
+ updateServoButton ();
646
+ }
647
+ }
609
648
}
0 commit comments