ronnie
2022-10-23 eadd9db01b24ccde96a129dafa989d4ec436cdfd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.android.settings.wifi;
 
import android.content.Context;
import android.os.Bundle;
 
import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceScreen;
 
import com.android.settings.R;
import com.android.settings.core.SubSettingLauncher;
import com.android.settings.wifi.details.WifiNetworkDetailsFragment;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.wifi.AccessPoint;
import com.android.settingslib.wifi.AccessPointPreference;
import com.android.settingslib.wifi.WifiTracker;
import com.android.settingslib.wifi.WifiTrackerFactory;
 
/**
 * This places a preference into a PreferenceGroup owned by some parent
 * controller class when there is a wifi connection present.
 */
public class WifiConnectionPreferenceController extends AbstractPreferenceController implements
        WifiTracker.WifiListener {
 
    private static final String TAG = "WifiConnPrefCtrl";
 
    private static final String KEY = "active_wifi_connection";
 
    private UpdateListener mUpdateListener;
    private Context mPrefContext;
    private String mPreferenceGroupKey;
    private PreferenceGroup mPreferenceGroup;
    private WifiTracker mWifiTracker;
    private AccessPointPreference mPreference;
    private AccessPointPreference.UserBadgeCache mBadgeCache;
    private int order;
    private int mMetricsCategory;
 
    /**
     * Used to notify a parent controller that this controller has changed in availability, or has
     * updated the content in the preference that it manages.
     */
    public interface UpdateListener {
        void onChildrenUpdated();
    }
 
    /**
     * @param context            the context for the UI where we're placing the preference
     * @param lifecycle          for listening to lifecycle events for the UI
     * @param updateListener     for notifying a parent controller of changes
     * @param preferenceGroupKey the key to use to lookup the PreferenceGroup where this controller
     *                           will add its preference
     * @param order              the order that the preference added by this controller should use -
     *                           useful when this preference needs to be ordered in a specific way
     *                           relative to others in the PreferenceGroup
     * @param metricsCategory    - the category to use as the source when handling the click on the
     *                           pref to go to the wifi connection detail page
     */
    public WifiConnectionPreferenceController(Context context, Lifecycle lifecycle,
            UpdateListener updateListener, String preferenceGroupKey, int order,
            int metricsCategory) {
        super(context);
        mUpdateListener = updateListener;
        mPreferenceGroupKey = preferenceGroupKey;
        mWifiTracker = WifiTrackerFactory.create(context, this, lifecycle, true /* includeSaved */,
                true /* includeScans */);
        this.order = order;
        mMetricsCategory = metricsCategory;
        mBadgeCache = new AccessPointPreference.UserBadgeCache(context.getPackageManager());
    }
 
    @Override
    public boolean isAvailable() {
        return mWifiTracker.isConnected() && getCurrentAccessPoint() != null;
    }
 
    @Override
    public String getPreferenceKey() {
        return KEY;
    }
 
    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        mPreferenceGroup = screen.findPreference(mPreferenceGroupKey);
        mPrefContext = screen.getContext();
        update();
    }
 
    private AccessPoint getCurrentAccessPoint() {
        for (AccessPoint accessPoint : mWifiTracker.getAccessPoints()) {
            if (accessPoint.isActive()) {
                return accessPoint;
            }
        }
        return null;
    }
 
    private void updatePreference(AccessPoint accessPoint) {
        if (mPreference != null) {
            mPreferenceGroup.removePreference(mPreference);
            mPreference = null;
        }
        if (accessPoint == null) {
            return;
        }
        if (mPrefContext != null) {
            mPreference = new AccessPointPreference(accessPoint, mPrefContext, mBadgeCache,
                    R.drawable.ic_wifi_signal_0, false /* forSavedNetworks */);
            mPreference.setKey(KEY);
            mPreference.refresh();
            mPreference.setOrder(order);
 
            mPreference.setOnPreferenceClickListener(pref -> {
                Bundle args = new Bundle();
                mPreference.getAccessPoint().saveWifiState(args);
                new SubSettingLauncher(mPrefContext)
                        .setTitleRes(R.string.pref_title_network_details)
                        .setDestination(WifiNetworkDetailsFragment.class.getName())
                        .setArguments(args)
                        .setSourceMetricsCategory(mMetricsCategory)
                        .launch();
                return true;
            });
            mPreferenceGroup.addPreference(mPreference);
        }
    }
 
    private void update() {
        AccessPoint connectedAccessPoint = null;
        if (mWifiTracker.isConnected()) {
            connectedAccessPoint = getCurrentAccessPoint();
        }
        if (connectedAccessPoint == null) {
            updatePreference(null);
        } else {
          if (mPreference == null || !mPreference.getAccessPoint().equals(connectedAccessPoint)) {
              updatePreference(connectedAccessPoint);
          } else if (mPreference != null) {
              mPreference.refresh();
          }
        }
        mUpdateListener.onChildrenUpdated();
    }
 
    @Override
    public void onWifiStateChanged(int state) {
        update();
    }
 
    @Override
    public void onConnectedChanged() {
        update();
    }
 
    @Override
    public void onAccessPointsChanged() {
        update();
    }
}