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.network;
18 
19 import static com.android.settings.network.InternetUpdater.INTERNET_ETHERNET;
20 
21 import android.content.Context;
22 import android.graphics.drawable.Drawable;
23 
24 import androidx.lifecycle.Lifecycle;
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceScreen;
27 
28 import com.android.settings.R;
29 import com.android.settingslib.Utils;
30 import com.android.settingslib.core.AbstractPreferenceController;
31 
32 /**
33  * PreferenceController to show the connected ethernet network.
34  */
35 public class ConnectedEthernetNetworkController extends AbstractPreferenceController
36         implements InternetUpdater.InternetChangeListener {
37 
38     public static final String KEY = "connected_ethernet_network";
39 
40     private Preference mPreference;
41     private InternetUpdater mInternetUpdater;
42     private @InternetUpdater.InternetType int mInternetType;
43 
ConnectedEthernetNetworkController(Context context, Lifecycle lifecycle)44     public ConnectedEthernetNetworkController(Context context, Lifecycle lifecycle) {
45         super(context);
46         mInternetUpdater = new InternetUpdater(context, lifecycle, this);
47         mInternetType = mInternetUpdater.getInternetType();
48     }
49 
50     @Override
isAvailable()51     public boolean isAvailable() {
52         return mInternetType == INTERNET_ETHERNET;
53     }
54 
55     @Override
getPreferenceKey()56     public String getPreferenceKey() {
57         return KEY;
58     }
59 
60     @Override
displayPreference(PreferenceScreen screen)61     public void displayPreference(PreferenceScreen screen) {
62         super.displayPreference(screen);
63         mPreference = screen.findPreference(KEY);
64         final Drawable drawable = mContext.getDrawable(R.drawable.ic_settings_ethernet);
65         if (drawable != null) {
66             drawable.setTintList(
67                     Utils.getColorAttr(mContext, android.R.attr.colorControlActivated));
68             mPreference.setIcon(drawable);
69         }
70     }
71 
72     /**
73      * Called when internet type is changed.
74      *
75      * @param internetType the internet type
76      */
onInternetTypeChanged(@nternetUpdater.InternetType int internetType)77     public void onInternetTypeChanged(@InternetUpdater.InternetType int internetType) {
78         mInternetType = internetType;
79         if (mPreference != null) {
80             mPreference.setVisible(isAvailable());
81         }
82     }
83 }
84