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 android.app.AutomaticZenRule; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.ComponentInfo; 24 import android.content.pm.PackageManager; 25 import android.service.notification.ZenModeConfig; 26 import android.view.View; 27 import android.widget.CheckBox; 28 29 import androidx.fragment.app.Fragment; 30 import androidx.preference.Preference; 31 import androidx.preference.PreferenceViewHolder; 32 33 import com.android.settings.R; 34 import com.android.settings.utils.ManagedServiceSettings; 35 import com.android.settings.utils.ZenServiceListing; 36 import com.android.settingslib.TwoTargetPreference; 37 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 38 39 import java.util.Map; 40 41 public class ZenRulePreference extends TwoTargetPreference { 42 private static final ManagedServiceSettings.Config CONFIG = 43 ZenModeAutomationSettings.getConditionProviderConfig(); 44 final String mId; 45 final Fragment mParent; 46 final Preference mPref; 47 final Context mContext; 48 final ZenModeBackend mBackend; 49 final ZenServiceListing mServiceListing; 50 final PackageManager mPm; 51 final MetricsFeatureProvider mMetricsFeatureProvider; 52 AutomaticZenRule mRule; 53 CharSequence mName; 54 55 private Intent mIntent; 56 private boolean mChecked; 57 private CheckBox mCheckBox; 58 ZenRulePreference(Context context, final Map.Entry<String, AutomaticZenRule> ruleEntry, Fragment parent, MetricsFeatureProvider metricsProvider)59 public ZenRulePreference(Context context, 60 final Map.Entry<String, AutomaticZenRule> ruleEntry, 61 Fragment parent, MetricsFeatureProvider metricsProvider) { 62 super(context); 63 setLayoutResource(R.layout.preference_checkable_two_target); 64 mBackend = ZenModeBackend.getInstance(context); 65 mContext = context; 66 mRule = ruleEntry.getValue(); 67 mName = mRule.getName(); 68 mId = ruleEntry.getKey(); 69 mParent = parent; 70 mPm = mContext.getPackageManager(); 71 mServiceListing = new ZenServiceListing(mContext, CONFIG); 72 mServiceListing.reloadApprovedServices(); 73 mPref = this; 74 mMetricsFeatureProvider = metricsProvider; 75 mChecked = mRule.isEnabled(); 76 setAttributes(mRule); 77 setWidgetLayoutResource(getSecondTargetResId()); 78 } 79 getSecondTargetResId()80 protected int getSecondTargetResId() { 81 if (mIntent != null) { 82 return R.layout.zen_rule_widget; 83 } 84 return 0; 85 } 86 87 @Override onBindViewHolder(PreferenceViewHolder view)88 public void onBindViewHolder(PreferenceViewHolder view) { 89 super.onBindViewHolder(view); 90 View settingsWidget = view.findViewById(android.R.id.widget_frame); 91 View divider = view.findViewById(R.id.two_target_divider); 92 if (mIntent != null) { 93 divider.setVisibility(View.VISIBLE); 94 settingsWidget.setVisibility(View.VISIBLE); 95 settingsWidget.setOnClickListener(new View.OnClickListener() { 96 @Override 97 public void onClick(View v) { 98 mContext.startActivity(mIntent); 99 } 100 }); 101 } else { 102 divider.setVisibility(View.GONE); 103 settingsWidget.setVisibility(View.GONE); 104 settingsWidget.setOnClickListener(null); 105 } 106 107 View checkboxContainer = view.findViewById(R.id.checkbox_container); 108 if (checkboxContainer != null) { 109 checkboxContainer.setOnClickListener(mOnCheckBoxClickListener); 110 } 111 mCheckBox = (CheckBox) view.findViewById(com.android.internal.R.id.checkbox); 112 if (mCheckBox != null) { 113 mCheckBox.setChecked(mChecked); 114 } 115 } 116 isChecked()117 public boolean isChecked() { 118 return mChecked; 119 } 120 updatePreference(AutomaticZenRule rule)121 public void updatePreference(AutomaticZenRule rule) { 122 if (!mRule.getName().equals(rule.getName())) { 123 mName = rule.getName(); 124 setTitle(mName); 125 } 126 127 if (mRule.isEnabled() != rule.isEnabled()) { 128 setChecked(mRule.isEnabled()); 129 setSummary(computeRuleSummary(mRule)); 130 } 131 132 mRule = rule; 133 } 134 135 @Override onClick()136 public void onClick() { 137 mOnCheckBoxClickListener.onClick(null); 138 } 139 setChecked(boolean checked)140 private void setChecked(boolean checked) { 141 mChecked = checked; 142 if (mCheckBox != null) { 143 mCheckBox.setChecked(checked); 144 } 145 } 146 147 private View.OnClickListener mOnCheckBoxClickListener = new View.OnClickListener() { 148 @Override 149 public void onClick(View v) { 150 mRule.setEnabled(!mChecked); 151 mBackend.updateZenRule(mId, mRule); 152 setChecked(mRule.isEnabled()); 153 setAttributes(mRule); 154 } 155 }; 156 setAttributes(AutomaticZenRule rule)157 protected void setAttributes(AutomaticZenRule rule) { 158 final boolean isSchedule = ZenModeConfig.isValidScheduleConditionId( 159 rule.getConditionId(), true); 160 final boolean isEvent = ZenModeConfig.isValidEventConditionId(rule.getConditionId()); 161 162 setSummary(computeRuleSummary(rule)); 163 164 setTitle(mName); 165 setPersistent(false); 166 167 final String action = isSchedule ? ZenModeScheduleRuleSettings.ACTION 168 : isEvent ? ZenModeEventRuleSettings.ACTION : ""; 169 ComponentInfo si = mServiceListing.findService(rule.getOwner()); 170 ComponentName settingsActivity = AbstractZenModeAutomaticRulePreferenceController. 171 getSettingsActivity(rule, si); 172 mIntent = AbstractZenModeAutomaticRulePreferenceController.getRuleIntent(action, 173 settingsActivity, mId); 174 if (mIntent.resolveActivity(mPm) == null) { 175 mIntent = null; 176 } 177 setKey(mId); 178 } 179 computeRuleSummary(AutomaticZenRule rule)180 private String computeRuleSummary(AutomaticZenRule rule) { 181 return (rule == null || !rule.isEnabled()) 182 ? mContext.getResources().getString(R.string.switch_off_text) 183 : mContext.getResources().getString(R.string.switch_on_text); 184 } 185 } 186