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