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; 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 import android.view.View; 29 30 import androidx.preference.Preference; 31 import androidx.preference.PreferenceFragmentCompat; 32 33 import com.android.settings.R; 34 import com.android.settings.core.PreferenceControllerMixin; 35 import com.android.settings.widget.EntityHeaderController; 36 import com.android.settingslib.core.lifecycle.Lifecycle; 37 import com.android.settingslib.widget.LayoutPreference; 38 39 public class ZenAutomaticRuleHeaderPreferenceController extends AbstractZenModePreferenceController 40 implements PreferenceControllerMixin { 41 42 private final String KEY = PREF_KEY_APP_HEADER; 43 private final PreferenceFragmentCompat mFragment; 44 private AutomaticZenRule mRule; 45 private String mId; 46 private EntityHeaderController mController; 47 ZenAutomaticRuleHeaderPreferenceController(Context context, PreferenceFragmentCompat fragment, Lifecycle lifecycle)48 public ZenAutomaticRuleHeaderPreferenceController(Context context, 49 PreferenceFragmentCompat fragment, Lifecycle lifecycle) { 50 super(context, PREF_KEY_APP_HEADER, lifecycle); 51 mFragment = fragment; 52 } 53 54 @Override getPreferenceKey()55 public String getPreferenceKey() { 56 return KEY; 57 } 58 59 @Override isAvailable()60 public boolean isAvailable() { 61 return mRule != null; 62 } 63 updateState(Preference preference)64 public void updateState(Preference preference) { 65 if (mRule == null) { 66 return; 67 } 68 69 if (mFragment != null) { 70 LayoutPreference pref = (LayoutPreference) preference; 71 72 if (mController == null) { 73 mController = EntityHeaderController 74 .newInstance(mFragment.getActivity(), mFragment, 75 pref.findViewById(R.id.entity_header)); 76 } 77 78 pref = mController.setIcon(getIcon()) 79 .setLabel(mRule.getName()) 80 .setPackageName(mRule.getOwner().getPackageName()) 81 .setUid(mContext.getUserId()) 82 .setHasAppInfoLink(false) 83 .setButtonActions(EntityHeaderController.ActionType.ACTION_EDIT_PREFERENCE, 84 EntityHeaderController.ActionType.ACTION_NONE) 85 .done(mFragment.getActivity(), mContext); 86 87 pref.findViewById(R.id.entity_header).setVisibility(View.VISIBLE); 88 } 89 } 90 getIcon()91 private Drawable getIcon() { 92 try { 93 PackageManager packageManager = mContext.getPackageManager(); 94 ApplicationInfo info = packageManager.getApplicationInfo( 95 mRule.getOwner().getPackageName(), 0); 96 if (info.isSystemApp()) { 97 if (ZenModeConfig.isValidScheduleConditionId(mRule.getConditionId())) { 98 return mContext.getDrawable(R.drawable.ic_timelapse); 99 } else if (ZenModeConfig.isValidEventConditionId(mRule.getConditionId())) { 100 return mContext.getDrawable(R.drawable.ic_event); 101 } 102 } 103 return info.loadIcon(packageManager); 104 } catch (PackageManager.NameNotFoundException e) { 105 Slog.w(TAG, "Unable to load icon - PackageManager.NameNotFoundException"); 106 } 107 108 return null; 109 } 110 onResume(AutomaticZenRule rule, String id)111 protected void onResume(AutomaticZenRule rule, String id) { 112 mRule = rule; 113 mId = id; 114 } 115 } 116