1 /* 2 * Copyright (C) 2009 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.content.ContentUris; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.net.Uri; 23 import android.preference.Preference; 24 import android.provider.Telephony; 25 import android.util.AttributeSet; 26 import android.util.Log; 27 import android.view.View; 28 import android.view.ViewGroup; 29 import android.view.View.OnClickListener; 30 import android.widget.CompoundButton; 31 import android.widget.RadioButton; 32 import android.widget.RelativeLayout; 33 34 public class ApnPreference extends Preference implements 35 CompoundButton.OnCheckedChangeListener, OnClickListener { 36 final static String TAG = "ApnPreference"; 37 38 /** 39 * @param context 40 * @param attrs 41 * @param defStyle 42 */ 43 public ApnPreference(Context context, AttributeSet attrs, int defStyle) { 44 super(context, attrs, defStyle); 45 init(); 46 } 47 48 /** 49 * @param context 50 * @param attrs 51 */ 52 public ApnPreference(Context context, AttributeSet attrs) { 53 super(context, attrs); 54 init(); 55 } 56 57 /** 58 * @param context 59 */ 60 public ApnPreference(Context context) { 61 super(context); 62 init(); 63 } 64 65 private static String mSelectedKey = null; 66 private static CompoundButton mCurrentChecked = null; 67 private boolean mProtectFromCheckedChange = false; 68 private boolean mSelectable = true; 69 70 @Override 71 public View getView(View convertView, ViewGroup parent) { 72 View view = super.getView(convertView, parent); 73 74 View widget = view.findViewById(R.id.apn_radiobutton); 75 if ((widget != null) && widget instanceof RadioButton) { 76 RadioButton rb = (RadioButton) widget; 77 if (mSelectable) { 78 rb.setOnCheckedChangeListener(this); 79 80 boolean isChecked = getKey().equals(mSelectedKey); 81 if (isChecked) { 82 mCurrentChecked = rb; 83 mSelectedKey = getKey(); 84 } 85 86 mProtectFromCheckedChange = true; 87 rb.setChecked(isChecked); 88 mProtectFromCheckedChange = false; 89 } else { 90 rb.setVisibility(View.GONE); 91 } 92 } 93 94 View textLayout = view.findViewById(R.id.text_layout); 95 if ((textLayout != null) && textLayout instanceof RelativeLayout) { 96 textLayout.setOnClickListener(this); 97 } 98 99 return view; 100 } 101 102 private void init() { 103 setLayoutResource(R.layout.apn_preference_layout); 104 } 105 106 public boolean isChecked() { 107 return getKey().equals(mSelectedKey); 108 } 109 110 public void setChecked() { 111 mSelectedKey = getKey(); 112 } 113 114 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 115 Log.i(TAG, "ID: " + getKey() + " :" + isChecked); 116 if (mProtectFromCheckedChange) { 117 return; 118 } 119 120 if (isChecked) { 121 if (mCurrentChecked != null) { 122 mCurrentChecked.setChecked(false); 123 } 124 mCurrentChecked = buttonView; 125 mSelectedKey = getKey(); 126 callChangeListener(mSelectedKey); 127 } else { 128 mCurrentChecked = null; 129 mSelectedKey = null; 130 } 131 } 132 133 public void onClick(android.view.View v) { 134 if ((v != null) && (R.id.text_layout == v.getId())) { 135 Context context = getContext(); 136 if (context != null) { 137 int pos = Integer.parseInt(getKey()); 138 Uri url = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI, pos); 139 context.startActivity(new Intent(Intent.ACTION_EDIT, url)); 140 } 141 } 142 } 143 144 public void setSelectable(boolean selectable) { 145 mSelectable = selectable; 146 } 147 148 public boolean getSelectable() { 149 return mSelectable; 150 } 151 } 152