1 /*
2  * Copyright (C) 2022 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.darkmode;
18 
19 import static android.provider.Settings.ACTION_BEDTIME_SETTINGS;
20 import static android.util.FeatureFlagUtils.SETTINGS_APP_ALLOW_DARK_THEME_ACTIVATION_AT_BEDTIME;
21 
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.pm.PackageManager;
25 import android.content.pm.ResolveInfo;
26 import android.util.FeatureFlagUtils;
27 
28 import androidx.annotation.Nullable;
29 
30 /** Manages Digital Wellbeing bedtime settings' intents. */
31 public final class BedtimeSettings {
32     private final Context mContext;
33     private final PackageManager mPackageManager;
34     private final String mWellbeingPackage;
35 
BedtimeSettings(Context context)36     public BedtimeSettings(Context context) {
37         mContext = context;
38         mPackageManager = context.getPackageManager();
39         mWellbeingPackage = mContext.getResources().getString(
40                 com.android.internal.R.string.config_systemWellbeing);
41     }
42 
43     /**
44      * Returns the bedtime settings intent. If the bedtime settings isn't available, returns
45      * {@code null}.
46      */
47     @Nullable
getBedtimeSettingsIntent()48     public Intent getBedtimeSettingsIntent() {
49         if (!FeatureFlagUtils.isEnabled(mContext,
50                 SETTINGS_APP_ALLOW_DARK_THEME_ACTIVATION_AT_BEDTIME)) {
51             return null;
52         }
53         Intent bedtimeSettingsIntent = new Intent(ACTION_BEDTIME_SETTINGS).setPackage(
54                 mWellbeingPackage);
55         ResolveInfo bedtimeSettingInfo = mPackageManager.resolveActivity(bedtimeSettingsIntent,
56                 PackageManager.MATCH_DEFAULT_ONLY);
57 
58         if (bedtimeSettingInfo != null && bedtimeSettingInfo.activityInfo.isEnabled()) {
59             return bedtimeSettingsIntent;
60         } else {
61             return null;
62         }
63     }
64 }
65