• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.network.apn;
18 
19 import static com.android.settings.network.apn.ApnEditPageProviderKt.EDIT_URL;
20 
21 import android.content.ContentUris;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.net.Uri;
25 import android.provider.Telephony;
26 import android.telephony.SubscriptionManager;
27 import android.util.Log;
28 import android.view.View;
29 import android.widget.CompoundButton;
30 import android.widget.RadioButton;
31 import android.widget.Toast;
32 
33 import androidx.annotation.NonNull;
34 import androidx.preference.Preference;
35 import androidx.preference.PreferenceViewHolder;
36 
37 import com.android.settings.R;
38 import com.android.settings.flags.Flags;
39 import com.android.settings.spa.SpaActivity;
40 import com.android.settingslib.widget.TwoTargetPreference;
41 
42 /**
43  * Preference of APN UI entry
44  */
45 public class ApnPreference extends TwoTargetPreference
46         implements CompoundButton.OnCheckedChangeListener, Preference.OnPreferenceClickListener {
47     private static final String TAG = "ApnPreference";
48     private boolean mIsChecked = false;
49     private RadioButton mRadioButton;
50     private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
51     private boolean mProtectFromCheckedChange = false;
52     private boolean mDefaultSelectable = true;
53     private boolean mHideDetails = false;
54 
55     /**
56      * Constructor of Preference
57      */
ApnPreference(Context context)58     public ApnPreference(Context context) {
59         super(context);
60         setOnPreferenceClickListener(this);
61     }
62 
63     @Override
onBindViewHolder(PreferenceViewHolder holder)64     public void onBindViewHolder(PreferenceViewHolder holder) {
65         super.onBindViewHolder(holder);
66 
67         final RadioButton rb = (RadioButton) holder.findViewById(android.R.id.checkbox);
68         final View radioButtonFrame = holder.findViewById(android.R.id.widget_frame);
69         if (rb == null || radioButtonFrame == null) {
70             throw new RuntimeException("Failed to load system layout.");
71         }
72 
73         mRadioButton = rb;
74         radioButtonFrame.setOnClickListener(v -> rb.performClick());
75         rb.setOnCheckedChangeListener(this);
76         setIsChecked(mIsChecked);
77         rb.setVisibility(View.VISIBLE);
78     }
79 
80     @Override
shouldHideSecondTarget()81     protected boolean shouldHideSecondTarget() {
82         return !mDefaultSelectable;
83     }
84 
85     @Override
getSecondTargetResId()86     protected int getSecondTargetResId() {
87         return R.layout.preference_widget_radiobutton;
88     }
89 
90     /**
91      * Set preference isChecked.
92      */
setIsChecked(boolean isChecked)93     public void setIsChecked(boolean isChecked) {
94         mIsChecked = isChecked;
95         if (mRadioButton != null) {
96             mProtectFromCheckedChange = true;
97             mRadioButton.setChecked(mIsChecked);
98             mProtectFromCheckedChange = false;
99         }
100     }
101 
102     /**
103      * Change the preference status.
104      */
105     @Override
onCheckedChanged(CompoundButton buttonView, boolean isChecked)106     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
107         Log.i(TAG, "ID: " + getKey() + " :" + isChecked);
108         if (mProtectFromCheckedChange) {
109             return;
110         }
111 
112         if (isChecked) {
113             callChangeListener(getKey());
114         }
115     }
116 
117     @Override
onPreferenceClick(@onNull Preference preference)118     public boolean onPreferenceClick(@NonNull Preference preference) {
119         final Context context = getContext();
120         final int pos = Integer.parseInt(getKey());
121 
122         if (mHideDetails) {
123             Toast.makeText(context, context.getString(R.string.cannot_change_apn_toast),
124                     Toast.LENGTH_LONG).show();
125             return true;
126         }
127 
128         final Uri url = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI, pos);
129 
130         if (Flags.newApnPageEnabled()) {
131             String route = ApnEditPageProvider.INSTANCE.getRoute(EDIT_URL, url, mSubId);
132             SpaActivity.startSpaActivity(context, route);
133         } else {
134             final Intent editIntent = new Intent(Intent.ACTION_EDIT, url);
135             editIntent.putExtra(ApnSettings.SUB_ID, mSubId);
136             editIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
137             context.startActivity(editIntent);
138         }
139         return true;
140     }
141 
setDefaultSelectable(boolean defaultSelectable)142     public void setDefaultSelectable(boolean defaultSelectable) {
143         mDefaultSelectable = defaultSelectable;
144     }
145 
setSubId(int subId)146     public void setSubId(int subId) {
147         mSubId = subId;
148     }
149 
150     /**
151      * Hide details
152      */
setHideDetails()153     public void setHideDetails() {
154         mHideDetails = true;
155     }
156 }
157