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.app.Fragment; 23 import android.content.Context; 24 import android.content.pm.ApplicationInfo; 25 import android.content.pm.PackageManager; 26 import android.graphics.drawable.Drawable; 27 import android.service.notification.ZenModeConfig; 28 import android.support.v14.preference.PreferenceFragment; 29 import android.support.v7.preference.Preference; 30 import android.util.Slog; 31 import android.view.View; 32 33 import com.android.internal.logging.nano.MetricsProto; 34 import com.android.settings.R; 35 import com.android.settings.applications.LayoutPreference; 36 import com.android.settings.core.PreferenceControllerMixin; 37 import com.android.settings.widget.EntityHeaderController; 38 import com.android.settingslib.core.lifecycle.Lifecycle; 39 40 public class ZenAutomaticRuleHeaderPreferenceController extends AbstractZenModePreferenceController 41 implements PreferenceControllerMixin { 42 43 private final String KEY = PREF_KEY_APP_HEADER; 44 private final PreferenceFragment mFragment; 45 private AutomaticZenRule mRule; 46 private String mId; 47 private EntityHeaderController mController; 48 49 public ZenAutomaticRuleHeaderPreferenceController(Context context, PreferenceFragment fragment, 50 Lifecycle lifecycle) { 51 super(context, PREF_KEY_APP_HEADER, lifecycle); 52 mFragment = fragment; 53 } 54 55 @Override 56 public String getPreferenceKey() { 57 return KEY; 58 } 59 60 @Override 61 public boolean isAvailable() { 62 return mRule != null; 63 } 64 65 public void updateState(Preference preference) { 66 if (mRule == null) { 67 return; 68 } 69 70 if (mFragment != null) { 71 LayoutPreference pref = (LayoutPreference) preference; 72 73 if (mController == null) { 74 mController = EntityHeaderController 75 .newInstance(mFragment.getActivity(), mFragment, 76 pref.findViewById(R.id.entity_header)); 77 78 mController.setEditZenRuleNameListener(new View.OnClickListener() { 79 @Override 80 public void onClick(View v) { 81 ZenRuleNameDialog.show(mFragment, mRule.getName(), null, 82 new RuleNameChangeListener()); 83 } 84 }); 85 } 86 87 pref = mController.setIcon(getIcon()) 88 .setLabel(mRule.getName()) 89 .setPackageName(mRule.getOwner().getPackageName()) 90 .setUid(mContext.getUserId()) 91 .setHasAppInfoLink(false) 92 .setButtonActions(EntityHeaderController.ActionType.ACTION_DND_RULE_PREFERENCE, 93 EntityHeaderController.ActionType.ACTION_NONE) 94 .done(mFragment.getActivity(), mContext); 95 96 pref.findViewById(R.id.entity_header).setVisibility(View.VISIBLE); 97 } 98 } 99 100 private Drawable getIcon() { 101 try { 102 PackageManager packageManager = mContext.getPackageManager(); 103 ApplicationInfo info = packageManager.getApplicationInfo( 104 mRule.getOwner().getPackageName(), 0); 105 if (info.isSystemApp()) { 106 if (ZenModeConfig.isValidScheduleConditionId(mRule.getConditionId())) { 107 return mContext.getDrawable(R.drawable.ic_timelapse); 108 } else if (ZenModeConfig.isValidEventConditionId(mRule.getConditionId())) { 109 return mContext.getDrawable(R.drawable.ic_event); 110 } 111 } 112 return info.loadIcon(packageManager); 113 } catch (PackageManager.NameNotFoundException e) { 114 Slog.w(TAG, "Unable to load icon - PackageManager.NameNotFoundException"); 115 } 116 117 return null; 118 } 119 120 protected void onResume(AutomaticZenRule rule, String id) { 121 mRule = rule; 122 mId = id; 123 } 124 125 public class RuleNameChangeListener implements ZenRuleNameDialog.PositiveClickListener { 126 public RuleNameChangeListener() {} 127 128 @Override 129 public void onOk(String ruleName, Fragment parent) { 130 mMetricsFeatureProvider.action(mContext, 131 MetricsProto.MetricsEvent.ACTION_ZEN_MODE_RULE_NAME_CHANGE_OK); 132 mRule.setName(ruleName); 133 mBackend.setZenRule(mId, mRule); 134 } 135 } 136 } 137