Java Code Examples for org.chromium.chrome.browser.preferences.PrefServiceBridge#setNetworkPredictionEnabled()

The following examples show how to use org.chromium.chrome.browser.preferences.PrefServiceBridge#setNetworkPredictionEnabled() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PrivacyPreferencesManager.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
/**
 * Migrate and delete old preferences.  Note that migration has to happen in Android-specific
 * code because we need to access ALLOW_PRERENDER sharedPreference.
 * TODO(bnc) https://crbug.com/394845. This change is planned for M38. After a year or so, it
 * would be worth considering removing this migration code (also removing accessors in
 * PrefServiceBridge and pref_service_bridge), and reverting to default for users
 * who had set preferences but have not used Chrome for a year. This change would be subject to
 * privacy review.
 */
public void migrateNetworkPredictionPreferences() {
    PrefServiceBridge prefService = PrefServiceBridge.getInstance();

    // See if PREF_NETWORK_PREDICTIONS is an old boolean value.
    boolean predictionOptionIsBoolean = false;
    try {
        mSharedPreferences.getString(PREF_NETWORK_PREDICTIONS, "");
    } catch (ClassCastException ex) {
        predictionOptionIsBoolean = true;
    }

    // Nothing to do if the user or this migration code has already set the new
    // preference.
    if (!predictionOptionIsBoolean
            && prefService.obsoleteNetworkPredictionOptionsHasUserSetting()) {
        return;
    }

    // Nothing to do if the old preferences are unset.
    if (!predictionOptionIsBoolean
            && !mSharedPreferences.contains(PREF_BANDWIDTH_OLD)
            && !mSharedPreferences.contains(PREF_BANDWIDTH_NO_CELLULAR_OLD)) {
        return;
    }

    // Migrate if the old preferences are at their default values.
    // (Note that for PREF_BANDWIDTH*, if the setting is default, then there is no way to tell
    // whether the user has set it.)
    final String prefBandwidthDefault = BandwidthType.PRERENDER_ON_WIFI.title();
    final String prefBandwidth =
            mSharedPreferences.getString(PREF_BANDWIDTH_OLD, prefBandwidthDefault);
    boolean prefBandwidthNoCellularDefault = true;
    boolean prefBandwidthNoCellular = mSharedPreferences.getBoolean(
            PREF_BANDWIDTH_NO_CELLULAR_OLD, prefBandwidthNoCellularDefault);

    if (!(prefBandwidthDefault.equals(prefBandwidth))
            || (prefBandwidthNoCellular != prefBandwidthNoCellularDefault)) {
        boolean newValue = true;
        // Observe PREF_BANDWIDTH on mobile network capable devices.
        if (isMobileNetworkCapable()) {
            if (mSharedPreferences.contains(PREF_BANDWIDTH_OLD)) {
                BandwidthType prefetchBandwidthTypePref = BandwidthType.getBandwidthFromTitle(
                        prefBandwidth);
                if (BandwidthType.NEVER_PRERENDER.equals(prefetchBandwidthTypePref)) {
                    newValue = false;
                } else if (BandwidthType.PRERENDER_ON_WIFI.equals(prefetchBandwidthTypePref)) {
                    newValue = true;
                } else if (BandwidthType.ALWAYS_PRERENDER.equals(prefetchBandwidthTypePref)) {
                    newValue = true;
                }
            }
        // Observe PREF_BANDWIDTH_NO_CELLULAR on devices without mobile network.
        } else {
            if (mSharedPreferences.contains(PREF_BANDWIDTH_NO_CELLULAR_OLD)) {
                if (prefBandwidthNoCellular) {
                    newValue = true;
                } else {
                    newValue = false;
                }
            }
        }
        // Save new value in Chrome PrefService.
        prefService.setNetworkPredictionEnabled(newValue);
    }

    // Delete old sharedPreferences.
    SharedPreferences.Editor sharedPreferencesEditor = mSharedPreferences.edit();
    // Delete PREF_BANDWIDTH and PREF_BANDWIDTH_NO_CELLULAR: just migrated these options.
    if (mSharedPreferences.contains(PREF_BANDWIDTH_OLD)) {
        sharedPreferencesEditor.remove(PREF_BANDWIDTH_OLD);
    }
    if (mSharedPreferences.contains(PREF_BANDWIDTH_NO_CELLULAR_OLD)) {
        sharedPreferencesEditor.remove(PREF_BANDWIDTH_NO_CELLULAR_OLD);
    }
    // Also delete ALLOW_PRERENDER, which was updated based on PREF_BANDWIDTH[_NO_CELLULAR] and
    // network connectivity type, therefore does not carry additional information.
    if (mSharedPreferences.contains(ALLOW_PRERENDER_OLD)) {
        sharedPreferencesEditor.remove(ALLOW_PRERENDER_OLD);
    }
    // Delete bool PREF_NETWORK_PREDICTIONS so that string values can be stored. Note that this
    // SharedPreference carries no information, because it used to be overwritten by
    // kNetworkPredictionEnabled on startup, and now it is overwritten by
    // kNetworkPredictionOptions on startup.
    if (mSharedPreferences.contains(PREF_NETWORK_PREDICTIONS)) {
        sharedPreferencesEditor.remove(PREF_NETWORK_PREDICTIONS);
    }
    sharedPreferencesEditor.apply();
}
 
Example 2
Source File: PrivacyPreferencesManager.java    From 365browser with Apache License 2.0 4 votes vote down vote up
/**
 * Migrate and delete old preferences.  Note that migration has to happen in Android-specific
 * code because we need to access ALLOW_PRERENDER sharedPreference.
 * TODO(bnc) https://crbug.com/394845. This change is planned for M38. After a year or so, it
 * would be worth considering removing this migration code (also removing accessors in
 * PrefServiceBridge and pref_service_bridge), and reverting to default for users
 * who had set preferences but have not used Chrome for a year. This change would be subject to
 * privacy review.
 */
public void migrateNetworkPredictionPreferences() {
    PrefServiceBridge prefService = PrefServiceBridge.getInstance();

    // See if PREF_NETWORK_PREDICTIONS is an old boolean value.
    boolean predictionOptionIsBoolean = false;
    try {
        mSharedPreferences.getString(PREF_NETWORK_PREDICTIONS, "");
    } catch (ClassCastException ex) {
        predictionOptionIsBoolean = true;
    }

    // Nothing to do if the user or this migration code has already set the new
    // preference.
    if (!predictionOptionIsBoolean
            && prefService.obsoleteNetworkPredictionOptionsHasUserSetting()) {
        return;
    }

    // Nothing to do if the old preferences are unset.
    if (!predictionOptionIsBoolean
            && !mSharedPreferences.contains(PREF_BANDWIDTH_OLD)
            && !mSharedPreferences.contains(PREF_BANDWIDTH_NO_CELLULAR_OLD)) {
        return;
    }

    // Migrate if the old preferences are at their default values.
    // (Note that for PREF_BANDWIDTH*, if the setting is default, then there is no way to tell
    // whether the user has set it.)
    final String prefBandwidthDefault = BandwidthType.PRERENDER_ON_WIFI.title();
    final String prefBandwidth =
            mSharedPreferences.getString(PREF_BANDWIDTH_OLD, prefBandwidthDefault);
    boolean prefBandwidthNoCellularDefault = true;
    boolean prefBandwidthNoCellular = mSharedPreferences.getBoolean(
            PREF_BANDWIDTH_NO_CELLULAR_OLD, prefBandwidthNoCellularDefault);

    if (!(prefBandwidthDefault.equals(prefBandwidth))
            || (prefBandwidthNoCellular != prefBandwidthNoCellularDefault)) {
        boolean newValue = true;
        // Observe PREF_BANDWIDTH on mobile network capable devices.
        if (isMobileNetworkCapable()) {
            if (mSharedPreferences.contains(PREF_BANDWIDTH_OLD)) {
                BandwidthType prefetchBandwidthTypePref = BandwidthType.getBandwidthFromTitle(
                        prefBandwidth);
                if (BandwidthType.NEVER_PRERENDER.equals(prefetchBandwidthTypePref)) {
                    newValue = false;
                } else if (BandwidthType.PRERENDER_ON_WIFI.equals(prefetchBandwidthTypePref)) {
                    newValue = true;
                } else if (BandwidthType.ALWAYS_PRERENDER.equals(prefetchBandwidthTypePref)) {
                    newValue = true;
                }
            }
        // Observe PREF_BANDWIDTH_NO_CELLULAR on devices without mobile network.
        } else {
            if (mSharedPreferences.contains(PREF_BANDWIDTH_NO_CELLULAR_OLD)) {
                if (prefBandwidthNoCellular) {
                    newValue = true;
                } else {
                    newValue = false;
                }
            }
        }
        // Save new value in Chrome PrefService.
        prefService.setNetworkPredictionEnabled(newValue);
    }

    // Delete old sharedPreferences.
    SharedPreferences.Editor sharedPreferencesEditor = mSharedPreferences.edit();
    // Delete PREF_BANDWIDTH and PREF_BANDWIDTH_NO_CELLULAR: just migrated these options.
    if (mSharedPreferences.contains(PREF_BANDWIDTH_OLD)) {
        sharedPreferencesEditor.remove(PREF_BANDWIDTH_OLD);
    }
    if (mSharedPreferences.contains(PREF_BANDWIDTH_NO_CELLULAR_OLD)) {
        sharedPreferencesEditor.remove(PREF_BANDWIDTH_NO_CELLULAR_OLD);
    }
    // Also delete ALLOW_PRERENDER, which was updated based on PREF_BANDWIDTH[_NO_CELLULAR] and
    // network connectivity type, therefore does not carry additional information.
    if (mSharedPreferences.contains(ALLOW_PRERENDER_OLD)) {
        sharedPreferencesEditor.remove(ALLOW_PRERENDER_OLD);
    }
    // Delete bool PREF_NETWORK_PREDICTIONS so that string values can be stored. Note that this
    // SharedPreference carries no information, because it used to be overwritten by
    // kNetworkPredictionEnabled on startup, and now it is overwritten by
    // kNetworkPredictionOptions on startup.
    if (mSharedPreferences.contains(PREF_NETWORK_PREDICTIONS)) {
        sharedPreferencesEditor.remove(PREF_NETWORK_PREDICTIONS);
    }
    sharedPreferencesEditor.apply();
}