1 /* 2 * Copyright (C) 2014 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.phone.settings; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.SharedPreferences; 22 import android.content.pm.ActivityInfo; 23 import android.content.pm.PackageManager; 24 import android.content.pm.ResolveInfo; 25 import android.database.Cursor; 26 import android.preference.ListPreference; 27 import android.preference.PreferenceManager; 28 import android.text.TextUtils; 29 import android.util.AttributeSet; 30 import android.util.Log; 31 32 import com.android.internal.telephony.Phone; 33 import com.android.phone.CallFeaturesSetting; 34 import com.android.phone.PhoneGlobals; 35 import com.android.phone.R; 36 37 import java.util.ArrayList; 38 import java.util.HashMap; 39 import java.util.List; 40 import java.util.Map; 41 42 public class VoicemailProviderListPreference extends ListPreference { 43 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2); 44 private static final String LOG_TAG = VoicemailProviderListPreference.class.getSimpleName(); 45 46 // Key identifying the default voice mail provider 47 public static final String DEFAULT_KEY = ""; 48 49 public class VoicemailProvider { 50 public String name; 51 public Intent intent; 52 VoicemailProvider(String name, Intent intent)53 public VoicemailProvider(String name, Intent intent) { 54 this.name = name; 55 this.intent = intent; 56 } 57 toString()58 public String toString() { 59 return "[ Name: " + name + ", Intent: " + intent + " ]"; 60 } 61 } 62 63 private Phone mPhone; 64 65 /** 66 * Data about discovered voice mail settings providers. 67 * Is populated by querying which activities can handle ACTION_CONFIGURE_VOICEMAIL. 68 * They key in this map is package name + activity name. 69 * We always add an entry for the default provider with a key of empty 70 * string and intent value of null. 71 * @see #initVoicemailProviders() 72 */ 73 private final Map<String, VoicemailProvider> mVmProvidersData = 74 new HashMap<String, VoicemailProvider>(); 75 76 VoicemailProviderListPreference(Context context, AttributeSet attrs)77 public VoicemailProviderListPreference(Context context, AttributeSet attrs) { 78 super(context, attrs); 79 } 80 init(Phone phone, Intent intent)81 public void init(Phone phone, Intent intent) { 82 mPhone = phone; 83 84 initVoicemailProviders(intent); 85 } 86 87 /** 88 * Enumerates existing VM providers and puts their data into the list and populates 89 * the preference list objects with their names. 90 * In case we are called with ACTION_ADD_VOICEMAIL intent the intent may have 91 * an extra string called IGNORE_PROVIDER_EXTRA with "package.activityName" of the provider 92 * which should be hidden when we bring up the list of possible VM providers to choose. 93 */ initVoicemailProviders(Intent activityIntent)94 private void initVoicemailProviders(Intent activityIntent) { 95 if (DBG) log("initVoicemailProviders()"); 96 97 String providerToIgnore = null; 98 if (activityIntent.getAction().equals(CallFeaturesSetting.ACTION_ADD_VOICEMAIL) 99 && activityIntent.hasExtra(CallFeaturesSetting.IGNORE_PROVIDER_EXTRA)) { 100 // Remove this provider from the list. 101 if (DBG) log("Found ACTION_ADD_VOICEMAIL."); 102 providerToIgnore = 103 activityIntent.getStringExtra(CallFeaturesSetting.IGNORE_PROVIDER_EXTRA); 104 VoicemailProviderSettingsUtil.delete(mPhone.getContext(), providerToIgnore); 105 } 106 107 mVmProvidersData.clear(); 108 109 List<String> entries = new ArrayList<String>(); 110 List<String> values = new ArrayList<String>(); 111 112 // Add default voicemail provider. 113 final String myCarrier = 114 mPhone.getContext().getResources().getString(R.string.voicemail_default); 115 mVmProvidersData.put(VoicemailProviderListPreference.DEFAULT_KEY, 116 new VoicemailProvider(myCarrier, null)); 117 entries.add(myCarrier); 118 values.add(VoicemailProviderListPreference.DEFAULT_KEY); 119 120 // Add other voicemail providers. 121 PackageManager pm = mPhone.getContext().getPackageManager(); 122 Intent intent = new Intent(CallFeaturesSetting.ACTION_CONFIGURE_VOICEMAIL); 123 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0); 124 for (int i = 0; i < resolveInfos.size(); i++) { 125 final ResolveInfo ri= resolveInfos.get(i); 126 final ActivityInfo currentActivityInfo = ri.activityInfo; 127 final String key = currentActivityInfo.name; 128 129 if (key.equals(providerToIgnore)) { 130 continue; 131 } 132 133 if (DBG) log("Loading key: " + key); 134 CharSequence label = ri.loadLabel(pm); 135 if (TextUtils.isEmpty(label)) { 136 Log.w(LOG_TAG, "Adding voicemail provider with no name for display."); 137 } 138 String nameForDisplay = (label != null) ? label.toString() : ""; 139 Intent providerIntent = new Intent(); 140 providerIntent.setAction(CallFeaturesSetting.ACTION_CONFIGURE_VOICEMAIL); 141 providerIntent.setClassName(currentActivityInfo.packageName, currentActivityInfo.name); 142 VoicemailProvider vmProvider = new VoicemailProvider(nameForDisplay, providerIntent); 143 144 if (DBG) log("Store VoicemailProvider. Key: " + key + " -> " + vmProvider.toString()); 145 mVmProvidersData.put(key, vmProvider); 146 entries.add(vmProvider.name); 147 values.add(key); 148 } 149 150 setEntries(entries.toArray(new String[0])); 151 setEntryValues(values.toArray(new String[0])); 152 } 153 154 @Override getValue()155 public String getValue() { 156 final String providerKey = super.getValue(); 157 return (providerKey != null) ? providerKey : DEFAULT_KEY; 158 } 159 getVoicemailProvider(String key)160 public VoicemailProvider getVoicemailProvider(String key) { 161 return mVmProvidersData.get(key); 162 } 163 hasMoreThanOneVoicemailProvider()164 public boolean hasMoreThanOneVoicemailProvider() { 165 return mVmProvidersData.size() > 1; 166 } 167 log(String msg)168 private static void log(String msg) { 169 Log.d(LOG_TAG, msg); 170 } 171 } 172