1 /* 2 * Copyright (C) 2015 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.Activity; 20 import android.content.ComponentName; 21 import android.content.Intent; 22 import android.content.pm.ServiceInfo; 23 import android.net.Uri; 24 import android.preference.Preference; 25 import android.preference.Preference.OnPreferenceClickListener; 26 import android.preference.PreferenceScreen; 27 import android.provider.Settings; 28 import android.service.notification.ZenModeConfig.ZenRule; 29 import android.util.Log; 30 31 import com.android.internal.logging.MetricsLogger; 32 import com.android.settings.R; 33 import com.android.settings.notification.ZenRuleNameDialog.RuleInfo; 34 35 public class ZenModeExternalRuleSettings extends ZenModeRuleSettingsBase { 36 private static final String KEY_TYPE = "type"; 37 private static final String KEY_CONFIGURE = "configure"; 38 39 public static final String ACTION = Settings.ACTION_ZEN_MODE_EXTERNAL_RULE_SETTINGS; 40 private static final int REQUEST_CODE_CONFIGURE = 1; 41 42 private static final String MD_RULE_TYPE = "automatic.ruleType"; 43 private static final String MD_DEFAULT_CONDITION_ID = "automatic.defaultConditionId"; 44 private static final String MD_CONFIGURATION_ACTIVITY = "automatic.configurationActivity"; 45 private static final String EXTRA_CONDITION_ID = "automatic.conditionId"; 46 47 private Preference mType; 48 private Preference mConfigure; 49 50 @Override 51 protected boolean setRule(ZenRule rule) { 52 return rule != null; 53 } 54 55 @Override 56 protected String getZenModeDependency() { 57 return null; 58 } 59 60 @Override 61 protected int getEnabledToastText() { 62 return 0; 63 } 64 65 @Override 66 protected void onCreateInternal() { 67 addPreferencesFromResource(R.xml.zen_mode_external_rule_settings); 68 final PreferenceScreen root = getPreferenceScreen(); 69 final ServiceInfo si = ServiceListing.findService(mContext, 70 ZenModeAutomationSettings.CONFIG, mRule.component); 71 if (DEBUG) Log.d(TAG, "ServiceInfo: " + si); 72 final RuleInfo ri = getRuleInfo(si); 73 if (DEBUG) Log.d(TAG, "RuleInfo: " + ri); 74 mType = root.findPreference(KEY_TYPE); 75 if (ri == null) { 76 mType.setSummary(R.string.zen_mode_rule_type_unknown); 77 } else { 78 mType.setSummary(ri.caption); 79 } 80 81 mConfigure = root.findPreference(KEY_CONFIGURE); 82 if (ri == null || ri.configurationActivity == null) { 83 mConfigure.setEnabled(false); 84 } else { 85 mConfigure.setOnPreferenceClickListener(new OnPreferenceClickListener() { 86 @Override 87 public boolean onPreferenceClick(Preference preference) { 88 startActivityForResult(new Intent().setComponent(ri.configurationActivity), 89 REQUEST_CODE_CONFIGURE); 90 return true; 91 } 92 }); 93 } 94 } 95 96 @Override 97 public void onActivityResult(int requestCode, int resultCode, Intent data) { 98 super.onActivityResult(requestCode, resultCode, data); 99 if (requestCode == REQUEST_CODE_CONFIGURE) { 100 if (resultCode == Activity.RESULT_OK && data != null) { 101 final Uri conditionId = data.getParcelableExtra(EXTRA_CONDITION_ID); 102 if (conditionId != null && !conditionId.equals(mRule.conditionId)) { 103 updateRule(conditionId); 104 } 105 } 106 } 107 } 108 109 public static RuleInfo getRuleInfo(ServiceInfo si) { 110 if (si == null || si.metaData == null) return null; 111 final String ruleType = si.metaData.getString(MD_RULE_TYPE); 112 final String defaultConditionId = si.metaData.getString(MD_DEFAULT_CONDITION_ID); 113 final String configurationActivity = si.metaData.getString(MD_CONFIGURATION_ACTIVITY); 114 if (ruleType != null && !ruleType.trim().isEmpty() && defaultConditionId != null) { 115 final RuleInfo ri = new RuleInfo(); 116 ri.serviceComponent = new ComponentName(si.packageName, si.name); 117 ri.settingsAction = ZenModeExternalRuleSettings.ACTION; 118 ri.caption = ruleType; 119 ri.defaultConditionId = Uri.parse(defaultConditionId); 120 if (configurationActivity != null) { 121 ri.configurationActivity = ComponentName.unflattenFromString(configurationActivity); 122 } 123 return ri; 124 } 125 return null; 126 } 127 128 @Override 129 protected void updateControlsInternal() { 130 // everything done up front 131 } 132 133 @Override 134 protected int getMetricsCategory() { 135 return MetricsLogger.NOTIFICATION_ZEN_MODE_EXTERNAL_RULE; 136 } 137 138 } 139