mobile/android/base/toolbar/ToolbarTitlePrefs.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     2  * This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 package org.mozilla.gecko.toolbar;
     8 import org.mozilla.gecko.PrefsHelper;
     9 import org.mozilla.gecko.Tab;
    10 import org.mozilla.gecko.Tabs;
    11 import org.mozilla.gecko.util.ThreadUtils;
    13 class ToolbarTitlePrefs {
    14     static final String PREF_TITLEBAR_MODE = "browser.chrome.titlebarMode";
    15     static final String PREF_TRIM_URLS = "browser.urlbar.trimURLs";
    17     interface OnChangeListener {
    18         public void onChange();
    19     }
    21     final String[] prefs = {
    22         PREF_TITLEBAR_MODE,
    23         PREF_TRIM_URLS
    24     };
    26     private boolean mShowUrl;
    27     private boolean mTrimUrls;
    29     private Integer mPrefObserverId;
    31     ToolbarTitlePrefs() {
    32         mShowUrl = false;
    33         mTrimUrls = true;
    35         mPrefObserverId = PrefsHelper.getPrefs(prefs, new TitlePrefsHandler());
    36     }
    38     boolean shouldShowUrl() {
    39         return mShowUrl;
    40     }
    42     boolean shouldTrimUrls() {
    43         return mTrimUrls;
    44     }
    46     void close() {
    47         if (mPrefObserverId != null) {
    48              PrefsHelper.removeObserver(mPrefObserverId);
    49              mPrefObserverId = null;
    50         }
    51     }
    53     private class TitlePrefsHandler extends PrefsHelper.PrefHandlerBase {
    54         @Override
    55         public void prefValue(String pref, String str) {
    56             // Handles PREF_TITLEBAR_MODE, which is always a string.
    57             int value = Integer.parseInt(str);
    58             boolean shouldShowUrl = (value == 1);
    60             if (shouldShowUrl == mShowUrl) {
    61                 return;
    62             }
    63             mShowUrl = shouldShowUrl;
    65             triggerChangeListener();
    66         }
    68         @Override
    69         public void prefValue(String pref, boolean value) {
    70             // Handles PREF_TRIM_URLS, which should usually be a boolean.
    71             if (value == mTrimUrls) {
    72                 return;
    73             }
    74             mTrimUrls = value;
    76             triggerChangeListener();
    77         }
    79         @Override
    80         public boolean isObserver() {
    81             // We want to be notified of changes to be able to switch mode
    82             // without restarting.
    83             return true;
    84         }
    86         private void triggerChangeListener() {
    87             ThreadUtils.postToUiThread(new Runnable() {
    88                 @Override
    89                 public void run() {
    90                     final Tabs tabs = Tabs.getInstance();
    91                     final Tab tab = tabs.getSelectedTab();
    92                     if (tab != null) {
    93                         tabs.notifyListeners(tab, Tabs.TabEvents.TITLE);
    94                     }
    95                 }
    96             });
    97         }
    98     }
    99 }

mercurial