Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1a6887bb authored by Doris Ling's avatar Doris Ling
Browse files

Set activity title from preference screen title.

- When we initialize the preference screen, if the screen title is
available, use that to set the activity title, so that it will be up to
date with dynamic language changes.
- for preference fragment that don't have preference screen xml, added a
getTitle() method for the fragment to provide the title explicitly.
- use feature flag to switch between the old and new mechanism for
handling activity title.

Bug: 64564191
Test: blaze-bin/screenshots/android/i18nscreenshots/i18nscreenshots
Change-Id: I96318a5aa81e7b8ca94118adac3245ed27a03d93
parent 1e870364
Loading
Loading
Loading
Loading
+8 −3
Original line number Diff line number Diff line
@@ -55,6 +55,7 @@ import com.android.internal.util.ArrayUtils;
import com.android.settings.Settings.WifiSettingsActivity;
import com.android.settings.applications.manageapplications.ManageApplications;
import com.android.settings.backup.BackupSettingsActivity;
import com.android.settings.core.InstrumentedPreferenceFragment;
import com.android.settings.core.gateway.SettingsGateway;
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.core.instrumentation.SharedPreferencesLogger;
@@ -209,8 +210,12 @@ public class SettingsActivity extends SettingsDrawerActivity

    @Override
    public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
        if (InstrumentedPreferenceFragment.usePreferenceScreenTitle()) {
            startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, null, null, 0);
        } else {
            startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, pref.getTitle(),
                    null, 0);
        }
        return true;
    }

@@ -629,7 +634,7 @@ public class SettingsActivity extends SettingsDrawerActivity
        if (titleRes < 0) {
            if (titleText != null) {
                title = titleText.toString();
            } else {
            } else if (!InstrumentedPreferenceFragment.usePreferenceScreenTitle()) {
                // There not much we can do in that case
                title = "";
            }
+54 −0
Original line number Diff line number Diff line
@@ -16,8 +16,15 @@

package com.android.settings.core;

import android.annotation.Nullable;
import android.annotation.StringRes;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.XmlRes;
import android.support.v7.preference.PreferenceScreen;
import android.text.TextUtils;
import android.util.FeatureFlagUtils;
import android.util.Log;

import com.android.settings.core.instrumentation.Instrumentable;
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
@@ -32,6 +39,9 @@ import com.android.settingslib.core.lifecycle.ObservablePreferenceFragment;
public abstract class InstrumentedPreferenceFragment extends ObservablePreferenceFragment
        implements Instrumentable {

    private static final String TAG = "InstrumentedPrefFrag";
    private static final String FEATURE_FLAG_USE_PREFERENCE_SCREEN_TITLE =
            "settings_use_preference_screen_title";
    protected MetricsFeatureProvider mMetricsFeatureProvider;

    // metrics placeholder value. Only use this for development.
@@ -46,6 +56,17 @@ public abstract class InstrumentedPreferenceFragment extends ObservablePreferenc
        getLifecycle().addObserver(new SurveyMixin(this, getClass().getSimpleName()));
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (usePreferenceScreenTitle()) {
            final int title = getTitle();
            if (title != -1) {
                getActivity().setTitle(title);
            }
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
@@ -62,6 +83,16 @@ public abstract class InstrumentedPreferenceFragment extends ObservablePreferenc
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    }

    @Override
    public void addPreferencesFromResource(@XmlRes int preferencesResId) {
        super.addPreferencesFromResource(preferencesResId);
        updateActivityTitleWithScreenTitle(getPreferenceScreen());
    }

    public static boolean usePreferenceScreenTitle() {
        return FeatureFlagUtils.isEnabled(FEATURE_FLAG_USE_PREFERENCE_SCREEN_TITLE);
    }

    protected final Context getPrefContext() {
        return getPreferenceManager().getContext();
    }
@@ -69,4 +100,27 @@ public abstract class InstrumentedPreferenceFragment extends ObservablePreferenc
    protected final VisibilityLoggerMixin getVisibilityLogger() {
        return mVisibilityLoggerMixin;
    }

    /**
     * Return the resource id of the title to be used for the fragment. This is for preference
     * fragments that do not have an explicit preference screen xml, and hence the title need to be
     * specified separately. Do not use this method if the title is already specified in the
     * preference screen.
     */
    @StringRes
    protected int getTitle() {
        return -1;
    }

    private void updateActivityTitleWithScreenTitle(PreferenceScreen screen) {
        if (usePreferenceScreenTitle() && screen != null) {
            final CharSequence title = screen.getTitle();
            if (!TextUtils.isEmpty(title)) {
                getActivity().setTitle(title);
            } else {
                Log.w(TAG, "Screen title missing for fragment " + this.getClass().getName());
            }
        }
    }

}