Home | History | Annotate | Download | only in settings
      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.provider.Telephony;
     24 import android.support.v7.preference.Preference;
     25 import android.support.v7.preference.PreferenceViewHolder;
     26 import android.util.AttributeSet;
     27 import android.util.Log;
     28 import android.view.View;
     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     public ApnPreference(Context context, AttributeSet attrs, int defStyle) {
     39         super(context, attrs, defStyle);
     40     }
     41 
     42     public ApnPreference(Context context, AttributeSet attrs) {
     43         this(context, attrs, R.attr.apnPreferenceStyle);
     44     }
     45 
     46     public ApnPreference(Context context) {
     47         this(context, null);
     48     }
     49 
     50     private static String mSelectedKey = null;
     51     private static CompoundButton mCurrentChecked = null;
     52     private boolean mProtectFromCheckedChange = false;
     53     private boolean mSelectable = true;
     54 
     55     @Override
     56     public void onBindViewHolder(PreferenceViewHolder view) {
     57         super.onBindViewHolder(view);
     58 
     59         View widget = view.findViewById(R.id.apn_radiobutton);
     60         if ((widget != null) && widget instanceof RadioButton) {
     61             RadioButton rb = (RadioButton) widget;
     62             if (mSelectable) {
     63                 rb.setOnCheckedChangeListener(this);
     64 
     65                 boolean isChecked = getKey().equals(mSelectedKey);
     66                 if (isChecked) {
     67                     mCurrentChecked = rb;
     68                     mSelectedKey = getKey();
     69                 }
     70 
     71                 mProtectFromCheckedChange = true;
     72                 rb.setChecked(isChecked);
     73                 mProtectFromCheckedChange = false;
     74                 rb.setVisibility(View.VISIBLE);
     75             } else {
     76                 rb.setVisibility(View.GONE);
     77             }
     78         }
     79 
     80         View textLayout = view.findViewById(R.id.text_layout);
     81         if ((textLayout != null) && textLayout instanceof RelativeLayout) {
     82             textLayout.setOnClickListener(this);
     83         }
     84     }
     85 
     86     public boolean isChecked() {
     87         return getKey().equals(mSelectedKey);
     88     }
     89 
     90     public void setChecked() {
     91         mSelectedKey = getKey();
     92     }
     93 
     94     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
     95         Log.i(TAG, "ID: " + getKey() + " :" + isChecked);
     96         if (mProtectFromCheckedChange) {
     97             return;
     98         }
     99 
    100         if (isChecked) {
    101             if (mCurrentChecked != null) {
    102                 mCurrentChecked.setChecked(false);
    103             }
    104             mCurrentChecked = buttonView;
    105             mSelectedKey = getKey();
    106             callChangeListener(mSelectedKey);
    107         } else {
    108             mCurrentChecked = null;
    109             mSelectedKey = null;
    110         }
    111     }
    112 
    113     public void onClick(android.view.View v) {
    114         if ((v != null) && (R.id.text_layout == v.getId())) {
    115             Context context = getContext();
    116             if (context != null) {
    117                 int pos = Integer.parseInt(getKey());
    118                 Uri url = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI, pos);
    119                 context.startActivity(new Intent(Intent.ACTION_EDIT, url));
    120             }
    121         }
    122     }
    123 
    124     public void setSelectable(boolean selectable) {
    125         mSelectable = selectable;
    126     }
    127 
    128     public boolean getSelectable() {
    129         return mSelectable;
    130     }
    131 }
    132