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

Commit 75e4028b authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Clean up DarkUIInfoDialogFragment

The usage of this dialog is removed in
Change Ie2cf147de53385ae0c626c8472306f1b85317686

But this dialog is created (but not show) in DarkUIPreferenceController
each time dark mode toggle is turned on by user.

So clean this up.

Fix: 234419979
Test: make Settings
Change-Id: Icdc9d7a4fb77dc8b2a3f1a9d8e3f40fc0af4917d
parent 610538e1
Loading
Loading
Loading
Loading
+0 −6
Original line number Diff line number Diff line
@@ -11745,12 +11745,6 @@
    <!-- [CHAR LIMIT=60] Summary string on dark theme explaining why the toggle is disabled while the setting is off-->
    <string name="dark_ui_mode_disabled_summary_dark_theme_off">Temporarily turned on due to Battery Saver</string>
    <!-- [CHAR_LIMIT=NONE] Summary that is shown in the footer when dark mode is selected -->
    <string name="dark_ui_settings_dark_summary">Supported apps will also switch to dark theme</string>
    <!-- [CHAR_LIMIT=40] Positive button text in dark theme notification -->
    <string name="dark_ui_settings_dialog_acknowledge">Got it</string>
    <!-- [CHAR_LIMIT=50] Title string in the dark theme slice(suggestion) -->
    <string name="dark_theme_slice_title">Try Dark theme</string>
+0 −89
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.settings.display;

import android.app.Dialog;
import android.app.UiModeManager;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.android.settings.R;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;

public class DarkUIInfoDialogFragment extends InstrumentedDialogFragment
        implements DialogInterface.OnClickListener{

    @Override
    public int getMetricsCategory() {
        return SettingsEnums.DIALOG_DARK_UI_INFO;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Context context = getContext();
        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        LayoutInflater inflater = LayoutInflater.from(dialog.getContext());
        View titleView = inflater.inflate(R.layout.settings_dialog_title, null);
        ((ImageView) titleView.findViewById(R.id.settings_icon))
                .setImageDrawable(context.getDrawable(R.drawable.dark_theme));
        ((TextView) titleView.findViewById(R.id.settings_title)).setText(R.string.dark_ui_mode);

        dialog.setCustomTitle(titleView)
                .setMessage(R.string.dark_ui_settings_dark_summary)
                .setPositiveButton(
                        R.string.dark_ui_settings_dialog_acknowledge,
                        this);
        return dialog.create();
    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        enableDarkTheme();
        super.onDismiss(dialog);
    }

    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        // We have to manually dismiss the dialog because changing night mode causes it to
        // recreate itself.
        dialogInterface.dismiss();
        enableDarkTheme();
    }

    private void enableDarkTheme() {
        final Context context = getContext();
        if (context != null) {
            Settings.Secure.putInt(context.getContentResolver(),
                    Settings.Secure.DARK_MODE_DIALOG_SEEN,
                    DarkUIPreferenceController.DIALOG_SEEN);
            context.getSystemService(UiModeManager.class)
                    .setNightMode(UiModeManager.MODE_NIGHT_YES);
        }
    }
}
+0 −25
Original line number Diff line number Diff line
@@ -23,10 +23,8 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.os.PowerManager;
import android.provider.Settings;

import androidx.annotation.VisibleForTesting;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

@@ -39,7 +37,6 @@ import com.android.settingslib.core.lifecycle.events.OnStop;
public class DarkUIPreferenceController extends TogglePreferenceController implements
        LifecycleObserver, OnStart, OnStop {

    public static final String DARK_MODE_PREFS = "dark_mode_prefs";
    public static final String PREF_DARK_MODE_DIALOG_SEEN = "dark_mode_dialog_seen";
    public static final int DIALOG_SEEN = 1;

@@ -48,9 +45,6 @@ public class DarkUIPreferenceController extends TogglePreferenceController imple

    private UiModeManager mUiModeManager;
    private PowerManager mPowerManager;
    private Context mContext;

    private Fragment mFragment;

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
@@ -61,7 +55,6 @@ public class DarkUIPreferenceController extends TogglePreferenceController imple

    public DarkUIPreferenceController(Context context, String key) {
        super(context, key);
        mContext = context;
        mUiModeManager = context.getSystemService(UiModeManager.class);
        mPowerManager = context.getSystemService(PowerManager.class);
    }
@@ -86,12 +79,6 @@ public class DarkUIPreferenceController extends TogglePreferenceController imple

    @Override
    public boolean setChecked(boolean isChecked) {
        final boolean dialogSeen =
                Settings.Secure.getInt(mContext.getContentResolver(),
                        Settings.Secure.DARK_MODE_DIALOG_SEEN, 0) == DIALOG_SEEN;
        if (!dialogSeen && isChecked) {
            showDarkModeDialog();
        }
        return mUiModeManager.setNightModeActivated(isChecked);
    }

@@ -100,13 +87,6 @@ public class DarkUIPreferenceController extends TogglePreferenceController imple
        return R.string.menu_key_display;
    }

    private void showDarkModeDialog() {
        final DarkUIInfoDialogFragment frag = new DarkUIInfoDialogFragment();
        if (mFragment != null && mFragment.getFragmentManager() != null) {
            frag.show(mFragment.getFragmentManager(), getClass().getName());
        }
    }

    @VisibleForTesting
    void updateEnabledStateIfNeeded() {
        if (mPreference == null) {
@@ -133,11 +113,6 @@ public class DarkUIPreferenceController extends TogglePreferenceController imple
                new IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED));
    }

    // used by AccessibilitySettings
    public void setParentFragment(Fragment fragment) {
        mFragment = fragment;
    }

    @Override
    public void onStop() {
        mContext.unregisterReceiver(mReceiver);
+0 −65
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.settings.display;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.provider.Settings;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;

@RunWith(RobolectricTestRunner.class)
public class DarkUIInfoDialogFragmentTest {
    private DarkUIInfoDialogFragment mFragment;
    @Mock
    private DialogInterface dialog;


    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        mFragment = spy(new DarkUIInfoDialogFragment());
    }

    @Test
    public void dialogDismissedOnConfirmation() {
        doReturn(RuntimeEnvironment.application).when(mFragment).getContext();
        SharedPreferences prefs = RuntimeEnvironment.application.getSharedPreferences(
                DarkUIPreferenceController.DARK_MODE_PREFS,
                Context.MODE_PRIVATE);
        assertThat(prefs.getBoolean(DarkUIPreferenceController.PREF_DARK_MODE_DIALOG_SEEN, false))
                .isFalse();
        mFragment.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
        verify(dialog, times(1)).dismiss();
        assertThat(Settings.Secure.getInt(RuntimeEnvironment.application.getContentResolver(),
                Settings.Secure.DARK_MODE_DIALOG_SEEN, -1)).isEqualTo(1);
    }
}
+7 −9
Original line number Diff line number Diff line
@@ -16,35 +16,33 @@

package com.android.settings.display;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;

import android.content.Context;
import androidx.fragment.app.Fragment;

import com.android.settings.display.darkmode.DarkModePreference;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;

@RunWith(RobolectricTestRunner.class)
public class DarkUIPreferenceControllerTest {

    private DarkUIPreferenceController mController;
    private Context mContext;
    @Mock
    private Fragment mFragment;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mContext = spy(RuntimeEnvironment.application);
        mController = spy(new DarkUIPreferenceController(mContext, "dark_ui_mode"));
        mController.setParentFragment(mFragment);
        mController.mPreference = new DarkModePreference(mContext, null /* AttributeSet attrs */);
        mController.onStart();
    }