OSDN Git Service

Change dark theme screen to toggle
[android-x86/packages-apps-Settings.git] / src / com / android / settings / display / DarkUIInfoDialogFragment.java
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16
17 package com.android.settings.display;
18
19 import static com.android.settings.display.DarkUIPreferenceController.DARK_MODE_PREFS;
20 import static com.android.settings.display.DarkUIPreferenceController.PREF_DARK_MODE_DIALOG_SEEN;
21
22 import android.app.Dialog;
23 import android.app.UiModeManager;
24 import android.content.Context;
25 import android.content.DialogInterface;
26 import android.os.Bundle;
27 import android.provider.Settings;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.widget.ImageView;
31 import android.widget.TextView;
32
33 import androidx.annotation.NonNull;
34 import androidx.annotation.Nullable;
35 import androidx.appcompat.app.AlertDialog;
36
37 import com.android.settings.R;
38 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
39
40 public class DarkUIInfoDialogFragment extends InstrumentedDialogFragment
41         implements DialogInterface.OnClickListener{
42
43     @Override
44     public int getMetricsCategory() {
45         // TODO(b/130251804): Add metrics constant in followup change to avoid merge conflict in
46         // beta cherrypick
47         return 0;
48     }
49
50     @NonNull
51     @Override
52     public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
53         Context context = getContext();
54         AlertDialog.Builder dialog = new AlertDialog.Builder(context);
55         LayoutInflater inflater = LayoutInflater.from(dialog.getContext());
56         View titleView = inflater.inflate(R.layout.settings_dialog_title, null);
57         ((ImageView) titleView.findViewById(R.id.settings_icon))
58                 .setImageDrawable(context.getDrawable(R.drawable.dark_theme));
59         ((TextView) titleView.findViewById(R.id.settings_title)).setText(R.string.dark_ui_mode);
60
61         dialog.setCustomTitle(titleView)
62                 .setMessage(R.string.dark_ui_settings_dark_summary)
63                 .setPositiveButton(
64                         R.string.dark_ui_settings_dialog_acknowledge,
65                         this);
66         return dialog.create();
67     }
68
69     @Override
70     public void onDismiss(@NonNull DialogInterface dialog) {
71         enableDarkTheme();
72         super.onDismiss(dialog);
73     }
74
75     @Override
76     public void onClick(DialogInterface dialogInterface, int i) {
77         // We have to manually dismiss the dialog because changing night mode causes it to
78         // recreate itself.
79         dialogInterface.dismiss();
80         enableDarkTheme();
81     }
82
83     private void enableDarkTheme() {
84         final Context context = getContext();
85         if (context != null) {
86             Settings.Secure.putInt(context.getContentResolver(),
87                     Settings.Secure.DARK_MODE_DIALOG_SEEN,
88                     DarkUIPreferenceController.DIALOG_SEEN);
89             context.getSystemService(UiModeManager.class)
90                     .setNightMode(UiModeManager.MODE_NIGHT_YES);
91         }
92     }
93 }