summaryrefslogtreecommitdiffstats
path: root/src/com/android/browser/preferences/SiteSpecificPreferencesFragment.java
blob: 1a764a9bbd2a965acd3f9094d4299bc22973a81f (plain)
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/*
 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *     * Neither the name of The Linux Foundation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.android.browser.preferences;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.http.SslCertificate;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import android.preference.TwoStatePreference;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.ValueCallback;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.browser.BrowserLocationListPreference;
import com.android.browser.BrowserSettings;
import com.android.browser.NavigationBarBase;
import com.android.browser.PreferenceKeys;
import com.android.browser.R;
import com.android.browser.reflect.ReflectHelper;

import org.codeaurora.swe.PermissionsServiceFactory;
import org.codeaurora.swe.WebRefiner;
import org.codeaurora.swe.util.ColorUtils;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.Formatter;
import java.util.List;
import java.util.Map;

public class SiteSpecificPreferencesFragment extends SWEPreferenceFragment
        implements Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener,
        View.OnClickListener {
    public static final String EXTRA_SITE = "website";
    public static final String EXTRA_ORIGIN = "website_origin";
    public static final String EXTRA_FAVICON = "website_favicon";
    public static final String EXTRA_WEB_REFINER_ADS_INFO = "website_refiner_ads_info";
    public static final String EXTRA_WEB_REFINER_TRACKER_INFO = "website_refiner_tracker_info";
    public static final String EXTRA_WEB_REFINER_MALWARE_INFO = "website_refiner_malware_info";
    public static final String EXTRA_SECURITY_CERT = "website_security_cert";
    public static final String EXTRA_SECURITY_CERT_BAD = "website_security_cert_bad";
    public static final String EXTRA_SECURITY_CERT_MIXED = "website_security_cert_mixed";

    private PermissionsServiceFactory.PermissionsService.OriginInfo mOriginInfo;
    private PermissionsServiceFactory.PermissionsService mPermServ;
    private ActionBar mBar;
    private List<String> mLocationValues;

    private boolean mUsingDefaultSettings = true;
    private int mOriginalActionBarOptions;
    private int mIconColor = 0;

    private SslCertificate mSslCert;
    private int mSslState;

    private static class SiteSecurityViewFactory {
        private class SiteSecurityView {
            private TextView mTextView;
            private View mContainer;
            private String mDisplayText;

            public SiteSecurityView(View parent, int resId, String text) {
                mContainer = parent.findViewById(resId);
                mTextView = (TextView) mContainer.findViewById(R.id.security_view_text);
                mTextView.setText(text);
                mDisplayText = text;
                updateVisibility();
            }

            private void updateVisibility() {
                if (TextUtils.isEmpty(mDisplayText)) {
                    mContainer.setVisibility(View.GONE);
                } else {
                    mContainer.setVisibility(View.VISIBLE);
                }
            }

            public void setText(String text) {
                mDisplayText = text;
                mTextView.setText(mDisplayText);
                updateVisibility();
            }

            public void clearText() {
                mDisplayText = null;
                updateVisibility();
            }
        }

        public enum ViewType{
            ERROR,
            WARNING,
            INFO
        };

        private Map<ViewType, SiteSecurityView> mViews =
                new EnumMap<ViewType, SiteSecurityView>(ViewType.class);
        private Map<ViewType, String> mTexts = new EnumMap<ViewType, String>(ViewType.class);

        private boolean mbEmpty = true;

        public void setText(ViewType type, String text) {
            mTexts.put(type, text);

            SiteSecurityView view = mViews.get(type);
            if (view != null) {
                view.setText(text);
            }

            mbEmpty = false;
        }

        public void appendText(ViewType type, String text) {
            String new_text = mTexts.get(type);
            if (new_text != null)
                new_text += text;
            else
                new_text = text;

            mTexts.put(type, new_text);

            SiteSecurityView view = mViews.get(type);
            if (view != null) {
                view.setText(new_text);
            }

            mbEmpty = false;
        }

        public void clearText(ViewType type) {
            mTexts.remove(type);

            SiteSecurityView view = mViews.get(type);
            if (view != null) {
                view.clearText();
            }
        }

        public void setResource(ViewType type, View parent, int resId) {
            String text = mTexts.get(type);
            mViews.remove(type);
            mViews.put(type, new SiteSecurityView(parent, resId, text));
        }
    }

    private SiteSecurityViewFactory mSecurityViews;

    private String mOriginText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.site_specific_preferences);

        mBar = getActivity().getActionBar();

        mLocationValues = Arrays.asList(
                getResources().getStringArray(R.array.geolocation_settings_choices));

        mSecurityViews = new SiteSecurityViewFactory();

        Bundle args = getArguments();
        if (args != null) {
            mOriginText = args.getString(EXTRA_ORIGIN, null);

            if (mOriginText == null) {
                mOriginText = args.getString(EXTRA_SITE);
            }
        }

        mIconColor = NavigationBarBase.getSiteIconColor(mOriginText);

        PermissionsServiceFactory.getPermissionsService(
            new ValueCallback<PermissionsServiceFactory.PermissionsService>() {
                @Override
                public void onReceiveValue(PermissionsServiceFactory.PermissionsService value) {
                    mPermServ = value;
                    Preference pref = findPreference("site_name");

                    pref.setTitle(mOriginText);
                    try {
                        URL url = new URL(mOriginText);
                        pref.setSummary("(" + url.getHost() + ")");
                    } catch (MalformedURLException e) {
                    }
                    mOriginInfo = mPermServ.getOriginInfo(mOriginText);
                    setActionBarTitle(PermissionsServiceFactory.getPrettyUrl(mOriginText));
                    updatePreferenceInfo();
                }
            }
        );

        if (!BrowserSettings.getInstance().getPreferences()
                .getBoolean(PreferenceKeys.PREF_WEB_REFINER, false)) {
            PreferenceCategory category = (PreferenceCategory) findPreference("site_pref_list");
            if (category != null) {
                Preference pref = findPreference("distracting_contents");
                category.removePreference(pref);
            }
        }

        int ads = args.getInt(EXTRA_WEB_REFINER_ADS_INFO, 0);
        String[] strings = new String[3];
        int index = 0;

        if (ads > 0) {
            strings[index++] = getResources().getQuantityString(
                    R.plurals.pref_web_refiner_advertisements, ads, ads);
        }

        int trackers = args.getInt(EXTRA_WEB_REFINER_TRACKER_INFO, 0);
        if (trackers > 0) {
            strings[index++] = getResources().getQuantityString(
                    R.plurals.pref_web_refiner_trackers, trackers, trackers);

        }

        int malware = args.getInt(EXTRA_WEB_REFINER_MALWARE_INFO, 0);
        if (malware > 0) {
            strings[index++] = getResources().getQuantityString(
                    R.plurals.pref_web_refiner_malware, malware, malware);
        }

        if (index > 0) {
            String[] formats = getResources().getStringArray(R.array.pref_web_refiner_message);
            Formatter formatter = new Formatter();
            formatter.format(formats[index - 1], strings[0], strings[1], strings[2]);
            mSecurityViews.appendText(SiteSecurityViewFactory.ViewType.INFO, formatter.toString());
        }

        Bundle parcel = args.getParcelable(EXTRA_SECURITY_CERT);
        mSslCert = (parcel != null) ? SslCertificate.restoreState(parcel) : null;

        if (mSslCert != null) {
            Preference pref = findPreference("site_security_info");
            if (pref != null) {
                pref.setSelectable(true);
            }

            boolean certBad = args.getBoolean(EXTRA_SECURITY_CERT_BAD, false);
            boolean certMix = args.getBoolean(EXTRA_SECURITY_CERT_MIXED, false);
            if (!certBad && !certMix) {
                final String string = getString(R.string.pref_valid_cert);
                mSecurityViews.appendText(SiteSecurityViewFactory.ViewType.INFO,
                        string);
                mSslState = 0;
            } else if (certMix) {
                mSecurityViews.appendText(SiteSecurityViewFactory.ViewType.WARNING,
                        getString(R.string.pref_warning_cert));
                mSslState = 1;
            } else {
                mSecurityViews.appendText(SiteSecurityViewFactory.ViewType.ERROR,
                        getString(R.string.pref_invalid_cert));
                mSslState = 2;
            }
        }
    }

    private AlertDialog.Builder createSslCertificateDialog(Context ctx,
                                                           SslCertificate certificate) {
        Object[] params = {ctx};
        Class[] type = new Class[] {Context.class};
        View certificateView = (View) ReflectHelper.invokeMethod(certificate,
                "inflateCertificateView", type, params);
        Resources res = Resources.getSystem();
        // load 'android.R.placeholder' via introspection, since it's not a public resource ID
        int placeholder_id = res.getIdentifier("placeholder", "id", "android");
        final LinearLayout placeholder =
                (LinearLayout)certificateView.findViewById(placeholder_id);

        LayoutInflater factory = LayoutInflater.from(ctx);
        int iconId = R.drawable.ic_cert_trusted;
        TextView textView;

        switch (mSslState) {
            case 0:
                iconId = R.drawable.ic_cert_trusted;
                LinearLayout table = (LinearLayout)factory.inflate(R.layout.ssl_success, placeholder);
                textView = (TextView)table.findViewById(R.id.success);
                textView.setText(R.string.ssl_certificate_is_valid);
                break;
            case 1:
                iconId = R.drawable.ic_cert_untrusted;
                textView = (TextView) factory.inflate(R.layout.ssl_warning, placeholder, false);
                textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_sp_level_warning,
                        0, 0, 0);
                textView.setText(R.string.ssl_unknown);
                placeholder.addView(textView);
                break;
            case 2:
                iconId = R.drawable.ic_cert_avoid;
                textView = (TextView) factory.inflate(R.layout.ssl_warning, placeholder, false);
                textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_sp_level_severe,
                        0, 0, 0);
                textView.setText(R.string.ssl_invalid);
                placeholder.addView(textView);
                break;
        }

        return new AlertDialog.Builder(ctx)
                .setTitle(R.string.ssl_certificate)
                .setIcon(iconId)
                .setView(certificateView);
    }

    private void setActionBarTitle(String url) {
        if (mBar != null) {
            mBar.setTitle("  " + url);
        }
    }

    private String getStorage() {
        if (mOriginInfo == null) {
            return new String("");
        }

        long value = mOriginInfo.getStoredData();
        if (value == 0) {
            return "Empty";
        }

        if (value < (1 << 10)) {
            return value + "B";
        } else if (value < (1 << 20)) {
            return (value >> 10) + "KB";
        } else if (value < (1 << 30)) {
            return (value >> 20) + "MB";
        }

        return (value >> 30) + "GB";
    }

    private long showPermission(CharSequence key, PermissionsServiceFactory.PermissionType type,
                                int defaultOnSummary, int defaultOffSummary) {
        Preference pref = findPreference(key);
        long permission = (mOriginInfo != null) ? mOriginInfo.getPermission(type) :
                PermissionsServiceFactory.Permission.NOTSET;

        pref.setOnPreferenceChangeListener(this);

        if (permission == PermissionsServiceFactory.Permission.ALLOW) {
            if (pref instanceof TwoStatePreference) {
                ((TwoStatePreference) pref).setChecked(true);
                ((TwoStatePreference) pref).setSummaryOn(R.string.pref_security_allowed);
            } else {
                pref.setSummary(R.string.pref_security_allowed);
            }
            mUsingDefaultSettings = false;
        } else if (permission == PermissionsServiceFactory.Permission.BLOCK) {
            if (pref instanceof TwoStatePreference) {
                ((TwoStatePreference) pref).setChecked(false);
            } else {
                pref.setSummary(R.string.pref_security_not_allowed);
            }
            mUsingDefaultSettings = false;
        } else if (permission == PermissionsServiceFactory.Permission.ASK) {
            if (pref instanceof TwoStatePreference) {
                ((TwoStatePreference) pref).setChecked(true);
                ((TwoStatePreference) pref).setSummaryOn(R.string.pref_security_ask_before_using);
            } else {
                pref.setSummary(R.string.pref_security_ask_before_using);
            }
            mUsingDefaultSettings = false;
        } else if (permission == PermissionsServiceFactory.Permission.NOTSET) {
            boolean defaultPerm = PermissionsServiceFactory.getDefaultPermissions(type);
            if (pref instanceof TwoStatePreference) {
                if (!defaultPerm) {
                    ((TwoStatePreference) pref).setChecked(false);
                    ((TwoStatePreference) pref).setSummaryOff(defaultOffSummary);
                    return PermissionsServiceFactory.Permission.BLOCK;
                } else {
                    ((TwoStatePreference) pref).setChecked(true);
                    ((TwoStatePreference) pref).setSummaryOn(defaultOnSummary);
                    return PermissionsServiceFactory.Permission.ASK;
                }
            } else {
                if (!defaultPerm) {
                    pref.setSummary(defaultOffSummary);
                    return PermissionsServiceFactory.Permission.BLOCK;
                } else {
                    pref.setSummary(defaultOnSummary);
                    return PermissionsServiceFactory.Permission.ASK;
                }
            }
        }
        return permission;
    }

    private void updateStorageInfo(Preference pref) {
        if (mOriginInfo != null) {
            pref.setTitle(R.string.webstorage_clear_data_title);
            pref.setSummary("(" + getStorage() + ")");
        }
    }

    private void updatePreferenceInfo() {
        Preference pref = findPreference("clear_data");
        updateStorageInfo(pref);
        pref.setOnPreferenceClickListener(this);
        String warningText = new String("");

        long permission = showPermission("select_geolocation",
                PermissionsServiceFactory.PermissionType.GEOLOCATION,
                R.string.pref_security_ask_before_using, R.string.pref_security_not_allowed);

        if (PermissionsServiceFactory.Permission.ALLOW == permission) {
            warningText = getString(R.string.pref_privacy_enable_geolocation);
        }

        ListPreference geolocation_pref = (ListPreference) findPreference("select_geolocation");
        geolocation_pref.setValueIndex(0);
        if (permission == PermissionsServiceFactory.Permission.CUSTOM) {
            pref = findPreference("select_geolocation");
            long custom = mOriginInfo.getPermissionCustomValue(
                    PermissionsServiceFactory.PermissionType.GEOLOCATION);
            float customHrs = ((float) custom) / (60 * 60);
            String customSummary = "Allowed for " + String.format("%.02f", customHrs) + " hours";
            if (pref instanceof TwoStatePreference) {
                ((TwoStatePreference) pref).setChecked(true);
                ((TwoStatePreference) pref).setSummaryOn(customSummary);
            } else {
                pref.setSummary(customSummary);
            }
            mUsingDefaultSettings = false;
            warningText = getString(R.string.pref_privacy_enable_geolocation);
            geolocation_pref.setValueIndex(1);
        } else if (permission == PermissionsServiceFactory.Permission.ALLOW) {
            geolocation_pref.setValueIndex(2);
        }

        permission = showPermission("microphone", PermissionsServiceFactory.PermissionType.VOICE,
                R.string.pref_security_ask_before_using, R.string.pref_security_not_allowed);

        if (PermissionsServiceFactory.Permission.ALLOW == permission) {
            if (!warningText.isEmpty()) {
                warningText += ", ";
            }
            warningText += getString(R.string.pref_security_allow_mic);
        }

        permission = showPermission("camera", PermissionsServiceFactory.PermissionType.VIDEO,
                R.string.pref_security_ask_before_using, R.string.pref_security_not_allowed);
        if (PermissionsServiceFactory.Permission.ALLOW == permission) {
            if (!warningText.isEmpty()) {
                warningText += ", ";
            }
            warningText += getString(R.string.pref_security_allow_camera);
        }

        if (!warningText.isEmpty()) {
            warningText += " ";
            warningText += getResources().getString(R.string.pref_security_access_is_allowed);
            mSecurityViews.appendText(SiteSecurityViewFactory.ViewType.WARNING, warningText);
        }

        pref = findPreference("distracting_contents");
        if (pref != null) {
            permission = showPermission("distracting_contents",
                    PermissionsServiceFactory.PermissionType.WEBREFINER,
                    R.string.pref_security_allowed, R.string.pref_security_not_allowed);
            if (permission == PermissionsServiceFactory.Permission.BLOCK) {
                ((TwoStatePreference) pref).setChecked(true);
            } else {
                ((TwoStatePreference) pref).setChecked(false);
            }
        }

        showPermission("popup_windows", PermissionsServiceFactory.PermissionType.POPUP,
                R.string.pref_security_allowed, R.string.pref_security_not_allowed);

        showPermission("accept_cookies", PermissionsServiceFactory.PermissionType.COOKIE,
                R.string.pref_security_allowed, R.string.pref_security_not_allowed);

        if (!mUsingDefaultSettings && mBar != null) {
            mBar.getCustomView().setVisibility(View.VISIBLE);
        }

        if (mSecurityViews.mbEmpty) {
            PreferenceScreen screen = (PreferenceScreen)
                    findPreference("site_specific_prefs");

            pref = findPreference("site_security_info_title");
            if (pref != null && screen != null) {
                screen.removePreference(pref);
            }
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        BrowserLocationListPreference pref =
                (BrowserLocationListPreference) findPreference("select_geolocation");
        if ( pref != null) pref.setEnabled(PermissionsServiceFactory.isSystemLocationEnabled());
        if (mBar != null) {
            mOriginalActionBarOptions = mBar.getDisplayOptions();
            mBar.setDisplayHomeAsUpEnabled(false);
            mBar.setHomeButtonEnabled(false);

            assignResetButton();

            Bundle args = getArguments();
            if (args != null) {
                byte[] data = args.getByteArray(EXTRA_FAVICON);
                if (data != null) {
                    Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
                    if (bm != null) {
                        Bitmap bitmap = Bitmap.createScaledBitmap(bm, 150, 150, true);
                        int color = ColorUtils.getDominantColorForBitmap(bitmap);

                        appendActionBarDisplayOptions(ActionBar.DISPLAY_SHOW_HOME |
                                ActionBar.DISPLAY_SHOW_TITLE);
                        mBar.setHomeButtonEnabled(true);
                        mBar.setIcon(new BitmapDrawable(getResources(), bitmap));
                        mBar.setBackgroundDrawable(new ColorDrawable(color));
                        NavigationBarBase.setStatusAndNavigationBarColor(getActivity(),
                                NavigationBarBase.adjustColor(color, 1, 1, 0.7f));
                    }
                } else {
                    if (mIconColor != 0) {
                        mBar.setBackgroundDrawable(new ColorDrawable(mIconColor));
                        NavigationBarBase.setStatusAndNavigationBarColor(getActivity(),
                                NavigationBarBase.adjustColor(mIconColor, 1, 1, 0.7f));
                    }
                }
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mBar != null) {
            mBar.setDisplayOptions(mOriginalActionBarOptions);
            NavigationBarBase.setStatusAndNavigationBarColor(getActivity(),
                    NavigationBarBase.getDefaultStatusBarColor());
        }

        // flush all the settings in pause to assure that writes happen
        //  as soon the user leaves the activity
        PermissionsServiceFactory.flushPendingSettings();

    }

    private void appendActionBarDisplayOptions(int extraOptions) {
        int options = mBar.getDisplayOptions();
        options |= extraOptions;
        mBar.setDisplayOptions(options);
    }

    private void assignResetButton() {
        appendActionBarDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        mBar.setCustomView(R.layout.swe_preference_custom_actionbar);
        //mBar.getCustomView().setVisibility(View.GONE);
        Button btn = (Button) mBar.getCustomView().findViewById(R.id.reset);
        if (btn == null) {
            return;
        }

        btn.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new AlertDialog.Builder(getActivity())
                        .setMessage(R.string.pref_extras_reset_default_dlg)
                        .setPositiveButton(
                            R.string.website_settings_clear_all_dialog_ok_button,
                            new AlertDialog.OnClickListener() {
                                public void onClick(DialogInterface dlg, int which) {
                                    if (mOriginInfo != null) {
                                        mOriginInfo.resetSitePermission();
                                        Preference e = findPreference("clear_data");
                                        e.setSummary("(Empty)");
                                        updatePreferenceInfo();

                                        WebRefiner refiner = WebRefiner.getInstance();
                                        if (refiner != null) {
                                            String[] origins = new String[1];
                                            origins[0] = mOriginInfo.getOrigin();
                                            refiner.useDefaultPermissionForOrigins(origins);
                                        }

                                        finish();
                                    }
                                }
                            })
                        .setNegativeButton(
                                R.string.website_settings_clear_all_dialog_cancel_button, null)
                        .setIconAttribute(android.R.attr.alertDialogIcon)
                        .show();
                }
            }
        );
    }

    private void finish() {
        Activity activity = getActivity();
        if (activity != null) {
            getActivity().getFragmentManager().popBackStack();
        }
    }

    @Override
    public void onChildViewAddedToHierarchy(View parent, View child) {
        if (child.getId() == R.id.site_security_info) {
            mSecurityViews.setResource(SiteSecurityViewFactory.ViewType.ERROR,
                    child, R.id.site_security_error);
            mSecurityViews.setResource(SiteSecurityViewFactory.ViewType.WARNING,
                    child, R.id.site_security_warning);
            mSecurityViews.setResource(SiteSecurityViewFactory.ViewType.INFO,
                    child, R.id.site_security_verbose);

            if (mSslCert != null) {
                child.setOnClickListener(this);
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.site_security_info) {
            createSslCertificateDialog(getActivity(), mSslCert)
                    .setPositiveButton(R.string.ok, null)
                    .show();
        }
    }

    private void updateTwoStatePreference(Preference pref,
                                          PermissionsServiceFactory.PermissionType type,
                                          boolean state) {
        if (state) {
            mOriginInfo.setPermission(type, PermissionsServiceFactory.Permission.ALLOW);
            ((TwoStatePreference)pref).setSummaryOn(R.string.pref_security_allowed);
        } else {
            mOriginInfo.setPermission(type, PermissionsServiceFactory.Permission.BLOCK);
        }
    }

    @Override
    public boolean onPreferenceChange(Preference pref, Object objValue) {
        if (mOriginInfo == null) {
            if (mOriginText != null) {
                mOriginInfo = mPermServ.addOriginInfo(mOriginText);
                if (mOriginInfo == null) {
                    mOriginInfo = mPermServ.getOriginInfo(mOriginText);
                    if (mOriginInfo == null) {
                        return false;
                    }
                }
            } else {
                return false;
            }
        }
        if (pref.getKey().toString().equalsIgnoreCase("select_geolocation")) {
            int index = mLocationValues.indexOf(objValue.toString());
            switch (index) {
                case 0:
                    mOriginInfo.setPermission(PermissionsServiceFactory.PermissionType.GEOLOCATION,
                            PermissionsServiceFactory.Permission.BLOCK);
                    pref.setSummary(R.string.pref_security_not_allowed);
                    break;
                case 1:
                    mOriginInfo.setPermission(PermissionsServiceFactory.PermissionType.GEOLOCATION,
                            PermissionsServiceFactory.Permission.CUSTOM);
                    pref.setSummary(R.string.geolocation_permissions_prompt_share_for_limited_time);
                    break;
                case 2:
                    mOriginInfo.setPermission(PermissionsServiceFactory.PermissionType.GEOLOCATION,
                            PermissionsServiceFactory.Permission.ALLOW);
                    pref.setSummary(R.string.pref_security_allowed);
                    break;
                default:
                    break;
            }
        } else if (pref.getKey().toString().equalsIgnoreCase("microphone")) {
            updateTwoStatePreference(pref,
                    PermissionsServiceFactory.PermissionType.VOICE, (boolean)objValue);
        } else if (pref.getKey().toString().equalsIgnoreCase("camera")) {
            updateTwoStatePreference(pref,
                    PermissionsServiceFactory.PermissionType.VIDEO, (boolean)objValue);
        } else if (pref.getKey().toString().equalsIgnoreCase("distracting_contents")) {
            WebRefiner refiner = WebRefiner.getInstance();
            if (refiner != null) {
                boolean disable = (boolean) objValue;
                String[] origins = new String[1];
                origins[0] = mOriginInfo.getOrigin();
                refiner.setPermissionForOrigins(origins, !disable);
            }
            // Distracting contents and web refiner complimentary of each other
            updateTwoStatePreference(pref,
                    PermissionsServiceFactory.PermissionType.WEBREFINER, !(boolean)objValue);
        } else if (pref.getKey().toString().equalsIgnoreCase("popup_windows")) {
            updateTwoStatePreference(pref,
                    PermissionsServiceFactory.PermissionType.POPUP, (boolean)objValue);
        } else if (pref.getKey().toString().equalsIgnoreCase("accept_cookies")) {
            updateTwoStatePreference(pref,
                    PermissionsServiceFactory.PermissionType.COOKIE, (boolean)objValue);
        }
        return true;
    }

    @Override
    public boolean onPreferenceClick(Preference pref) {
        if (pref.getKey().toString().equalsIgnoreCase("clear_data")) {
            new AlertDialog.Builder(getActivity())
                .setMessage(R.string.website_settings_clear_all_dialog_message)
                .setPositiveButton(R.string.website_settings_clear_all_dialog_ok_button,
                        new AlertDialog.OnClickListener() {
                            public void onClick(DialogInterface dlg, int which) {
                                if (mOriginInfo != null) {
                                    mOriginInfo.clearAllStoredData();
                                    Preference e = findPreference("clear_data");
                                    e.setSummary("(Empty)");
                                }
                            }
                        })
                .setNegativeButton(R.string.website_settings_clear_all_dialog_cancel_button, null)
                .setIconAttribute(android.R.attr.alertDialogIcon)
                .show();
        }
        return true;
    }

}