1 /*
2  * Copyright (C) 2017 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.notification.zen;
18 
19 import static com.android.settings.widget.EntityHeaderController.PREF_KEY_APP_HEADER;
20 
21 import android.app.AutomaticZenRule;
22 import android.content.Context;
23 import android.content.pm.ApplicationInfo;
24 import android.content.pm.PackageManager;
25 import android.graphics.drawable.Drawable;
26 import android.service.notification.ZenModeConfig;
27 import android.util.Slog;
28 
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceFragmentCompat;
31 
32 import com.android.settings.R;
33 import com.android.settings.core.PreferenceControllerMixin;
34 import com.android.settings.widget.EntityHeaderController;
35 import com.android.settingslib.core.lifecycle.Lifecycle;
36 import com.android.settingslib.widget.LayoutPreference;
37 
38 public class ZenAutomaticRuleHeaderPreferenceController extends AbstractZenModePreferenceController
39         implements PreferenceControllerMixin {
40 
41     private final PreferenceFragmentCompat mFragment;
42     private AutomaticZenRule mRule;
43     private EntityHeaderController mController;
44 
ZenAutomaticRuleHeaderPreferenceController(Context context, PreferenceFragmentCompat fragment, Lifecycle lifecycle)45     public ZenAutomaticRuleHeaderPreferenceController(Context context,
46             PreferenceFragmentCompat fragment, Lifecycle lifecycle) {
47         super(context, PREF_KEY_APP_HEADER, lifecycle);
48         mFragment = fragment;
49     }
50 
51     @Override
getPreferenceKey()52     public String getPreferenceKey() {
53         return PREF_KEY_APP_HEADER;
54     }
55 
setRule(AutomaticZenRule rule)56     void setRule(AutomaticZenRule rule) {
57         mRule = rule;
58     }
59 
60     @Override
isAvailable()61     public boolean isAvailable() {
62         return mRule != null;
63     }
64 
65     @Override
updateState(Preference preference)66     public void updateState(Preference preference) {
67         if (mRule == null || mFragment == null) {
68             return;
69         }
70 
71         if (mController == null) {
72             final LayoutPreference pref = (LayoutPreference) preference;
73             mController = EntityHeaderController.newInstance(mFragment.getActivity(), mFragment,
74                     pref.findViewById(R.id.entity_header));
75         }
76 
77         mController.setIcon(getIcon())
78                 .setLabel(mRule.getName())
79                 .done(false /* rebindActions */);
80     }
81 
getIcon()82     private Drawable getIcon() {
83         try {
84             PackageManager packageManager =  mContext.getPackageManager();
85             ApplicationInfo info = packageManager.getApplicationInfo(
86                     mRule.getOwner().getPackageName(), 0);
87             if (info.isSystemApp()) {
88                 if (ZenModeConfig.isValidScheduleConditionId(mRule.getConditionId())) {
89                     return mContext.getDrawable(R.drawable.ic_timelapse);
90                 } else if (ZenModeConfig.isValidEventConditionId(mRule.getConditionId())) {
91                     return mContext.getDrawable(R.drawable.ic_event);
92                 }
93             }
94             return info.loadIcon(packageManager);
95         } catch (PackageManager.NameNotFoundException e) {
96            Slog.w(TAG, "Unable to load icon - PackageManager.NameNotFoundException");
97         }
98 
99         return null;
100     }
101 }
102