• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.vpn2;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.text.TextUtils;
23 import android.util.AttributeSet;
24 import android.view.View;
25 
26 import androidx.preference.PreferenceViewHolder;
27 
28 import com.android.settings.R;
29 import com.android.settingslib.HelpUtils;
30 import com.android.settingslib.RestrictedPreference;
31 
32 
33 /**
34  * A preference with an Info icon on the side
35  */
36 public class VpnInfoPreference extends RestrictedPreference implements View.OnClickListener {
37 
38     private boolean mIsInsecureVpn = false;
39     private String mHelpUrl;
40 
VpnInfoPreference(Context context, AttributeSet attrs)41     public VpnInfoPreference(Context context, AttributeSet attrs) {
42         super(context, attrs);
43         mHelpUrl = context.getString(R.string.help_url_insecure_vpn);
44     }
45 
46     @Override
getSecondTargetResId()47     protected int getSecondTargetResId() {
48         // Note: in the future, we will probably want to provide a configuration option
49         // for this info to not be the warning color.
50         return R.layout.preference_widget_warning;
51     }
52 
53     @Override
shouldHideSecondTarget()54     protected boolean shouldHideSecondTarget() {
55         return false;
56     }
57 
58     @Override
onBindViewHolder(PreferenceViewHolder holder)59     public void onBindViewHolder(PreferenceViewHolder holder) {
60         super.onBindViewHolder(holder);
61         final View icon = holder.findViewById(R.id.warning_button);
62         if (mIsInsecureVpn && !TextUtils.isEmpty(mHelpUrl)) {
63             icon.setVisibility(View.VISIBLE);
64             icon.setOnClickListener(this);
65             icon.setEnabled(true);
66         } else {
67             icon.setVisibility(View.GONE);
68             icon.setOnClickListener(this);
69             icon.setEnabled(false);
70         }
71 
72         // Hide the divider from view
73         final View divider = holder.findViewById(R.id.two_target_divider);
74         divider.setVisibility(View.GONE);
75     }
76 
77     @Override
onClick(View v)78     public void onClick(View v) {
79         if (v.getId() == R.id.warning_button) {
80             final Intent intent = HelpUtils.getHelpIntent(
81                     getContext(), mHelpUrl, this.getClass().getName());
82 
83             if (intent != null) {
84                 ((Activity) getContext()).startActivityForResult(intent, 0);
85             }
86         }
87     }
88 
89     /**
90      * Sets whether this preference corresponds to an insecure VPN. This will also affect whether
91      * the warning icon appears to the user.
92      */
setInsecureVpn(boolean isInsecureVpn)93     public void setInsecureVpn(boolean isInsecureVpn) {
94         if (mIsInsecureVpn != isInsecureVpn) {
95             mIsInsecureVpn = isInsecureVpn;
96             notifyChanged();
97         }
98     }
99 }
100