Home | History | Annotate | Download | only in settings
      1 /*
      2  * Copyright (C) 2013 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;
     18 
     19 import android.app.Activity;
     20 import android.app.AlertDialog.Builder;
     21 import android.content.Context;
     22 import android.graphics.drawable.Drawable;
     23 import android.preference.ListPreference;
     24 import android.util.AttributeSet;
     25 import android.view.LayoutInflater;
     26 import android.view.View;
     27 import android.view.ViewGroup;
     28 import android.widget.ArrayAdapter;
     29 import android.widget.CheckedTextView;
     30 import android.widget.ImageView;
     31 import android.widget.ListAdapter;
     32 
     33 /**
     34  * Extends ListPreference to allow us to show the icons for the available SMS applications. We do
     35  * this because the names of SMS applications are very similar and the user may not be able to
     36  * determine what app they are selecting without an icon.
     37  */
     38 public class SmsListPreference extends ListPreference {
     39     private Drawable[] mEntryDrawables;
     40 
     41     public class SmsArrayAdapter extends ArrayAdapter<CharSequence> {
     42         private Drawable[] mImageDrawables = null;
     43         private int mSelectedIndex = 0;
     44 
     45         public SmsArrayAdapter(Context context, int textViewResourceId,
     46                 CharSequence[] objects, Drawable[] imageDrawables, int selectedIndex) {
     47             super(context, textViewResourceId, objects);
     48             mSelectedIndex = selectedIndex;
     49             mImageDrawables = imageDrawables;
     50         }
     51 
     52         public View getView(int position, View convertView, ViewGroup parent) {
     53             LayoutInflater inflater = ((Activity)getContext()).getLayoutInflater();
     54             View view = inflater.inflate(R.layout.sms_preference_item, parent, false);
     55             CheckedTextView checkedTextView = (CheckedTextView)view.findViewById(R.id.sms_text);
     56             checkedTextView.setText(getItem(position));
     57             if (position == mSelectedIndex) {
     58                 checkedTextView.setChecked(true);
     59             }
     60             ImageView imageView = (ImageView)view.findViewById(R.id.sms_image);
     61             imageView.setImageDrawable(mImageDrawables[position]);
     62             return view;
     63         }
     64     }
     65 
     66     public SmsListPreference(Context context, AttributeSet attrs) {
     67         super(context, attrs);
     68     }
     69 
     70     public void setEntryDrawables(Drawable[] entries) {
     71         mEntryDrawables = entries;
     72     }
     73 
     74     public Drawable[] getEntryDrawables() {
     75         return mEntryDrawables;
     76     }
     77 
     78     @Override
     79     protected void onPrepareDialogBuilder(Builder builder) {
     80         int selectedIndex = findIndexOfValue(getValue());
     81         ListAdapter adapter = new SmsArrayAdapter(getContext(),
     82             R.layout.sms_preference_item, getEntries(), mEntryDrawables, selectedIndex);
     83         builder.setAdapter(adapter, this);
     84         super.onPrepareDialogBuilder(builder);
     85     }
     86 }