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