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 android.app.Dialog;
20 import android.app.UiModeManager;
21 import android.app.settings.SettingsEnums;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.os.Bundle;
25 import android.provider.Settings;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.widget.ImageView;
29 import android.widget.TextView;
30 
31 import androidx.annotation.NonNull;
32 import androidx.annotation.Nullable;
33 import androidx.appcompat.app.AlertDialog;
34 
35 import com.android.settings.R;
36 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
37 
38 public class DarkUIInfoDialogFragment extends InstrumentedDialogFragment
39         implements DialogInterface.OnClickListener{
40 
41     @Override
getMetricsCategory()42     public int getMetricsCategory() {
43         return SettingsEnums.DIALOG_DARK_UI_INFO;
44     }
45 
46     @NonNull
47     @Override
onCreateDialog(@ullable Bundle savedInstanceState)48     public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
49         Context context = getContext();
50         AlertDialog.Builder dialog = new AlertDialog.Builder(context);
51         LayoutInflater inflater = LayoutInflater.from(dialog.getContext());
52         View titleView = inflater.inflate(R.layout.settings_dialog_title, null);
53         ((ImageView) titleView.findViewById(R.id.settings_icon))
54                 .setImageDrawable(context.getDrawable(R.drawable.dark_theme));
55         ((TextView) titleView.findViewById(R.id.settings_title)).setText(R.string.dark_ui_mode);
56 
57         dialog.setCustomTitle(titleView)
58                 .setMessage(R.string.dark_ui_settings_dark_summary)
59                 .setPositiveButton(
60                         R.string.dark_ui_settings_dialog_acknowledge,
61                         this);
62         return dialog.create();
63     }
64 
65     @Override
onDismiss(@onNull DialogInterface dialog)66     public void onDismiss(@NonNull DialogInterface dialog) {
67         enableDarkTheme();
68         super.onDismiss(dialog);
69     }
70 
71     @Override
onClick(DialogInterface dialogInterface, int i)72     public void onClick(DialogInterface dialogInterface, int i) {
73         // We have to manually dismiss the dialog because changing night mode causes it to
74         // recreate itself.
75         dialogInterface.dismiss();
76         enableDarkTheme();
77     }
78 
enableDarkTheme()79     private void enableDarkTheme() {
80         final Context context = getContext();
81         if (context != null) {
82             Settings.Secure.putInt(context.getContentResolver(),
83                     Settings.Secure.DARK_MODE_DIALOG_SEEN,
84                     DarkUIPreferenceController.DIALOG_SEEN);
85             context.getSystemService(UiModeManager.class)
86                     .setNightMode(UiModeManager.MODE_NIGHT_YES);
87         }
88     }
89 }
90