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
|
/*
* Copyright (C) 2015 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.app.AppGlobals;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageManager;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.net.wifi.WifiConfiguration;
import android.os.RemoteException;
import android.os.UserHandle;
import android.preference.Preference;
import android.view.View;
import android.widget.TextView;
import com.android.settings.R;
import com.android.settingslib.wifi.AccessPoint;
public class AccessPointPreference extends Preference {
private static final int[] STATE_SECURED = {
R.attr.state_encrypted
};
private static final int[] STATE_NONE = {};
private static int[] wifi_signal_attributes = { R.attr.wifi_signal };
private TextView mSummaryView;
private boolean showSummary = true;
private AccessPoint mAccessPoint;
public AccessPointPreference(AccessPoint accessPoint, Context context) {
super(context);
mAccessPoint = accessPoint;
mAccessPoint.setTag(this);
refresh();
}
public AccessPoint getAccessPoint() {
return mAccessPoint;
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
updateIcon(mAccessPoint.getLevel(), getContext());
mSummaryView = (TextView) view.findViewById(com.android.internal.R.id.summary);
mSummaryView.setVisibility(showSummary ? View.VISIBLE : View.GONE);
notifyChanged();
}
public void showAppIcon() {
PackageManager pm = getContext().getPackageManager();
String systemName = pm.getNameForUid(android.os.Process.SYSTEM_UID);
WifiConfiguration mConfig = mAccessPoint.getConfig();
Drawable drawable = pm.getDefaultActivityIcon();
if (mConfig == null) {
drawable.setAlpha(0);
} else {
int userId = UserHandle.getUserId(mConfig.creatorUid);
ApplicationInfo appInfo = null;
if (mConfig.creatorName.equals(systemName)) {
appInfo = getContext().getApplicationInfo();
} else {
try {
IPackageManager ipm = AppGlobals.getPackageManager();
appInfo = ipm.getApplicationInfo(mConfig.creatorName, 0 /* flags */, userId);
} catch (RemoteException rex) {
// use default app icon
}
}
if (appInfo != null) {
drawable = appInfo.loadIcon(pm);
drawable = pm.getUserBadgedIcon(drawable, new UserHandle(userId));
}
}
setIcon(drawable);
}
protected void updateIcon(int level, Context context) {
if (level == -1) {
setIcon(null);
} else {
Drawable drawable = getIcon();
if (drawable == null) {
// To avoid a drawing race condition, we first set the state (SECURE/NONE) and then
// set the icon (drawable) to that state's drawable.
StateListDrawable sld = (StateListDrawable) context.getTheme()
.obtainStyledAttributes(wifi_signal_attributes).getDrawable(0);
// If sld is null then we are indexing and therefore do not have access to
// (nor need to display) the drawable.
if (sld != null) {
sld.setState((mAccessPoint.getSecurity() != AccessPoint.SECURITY_NONE)
? STATE_SECURED
: STATE_NONE);
drawable = sld.getCurrent();
setIcon(drawable);
}
}
if (drawable != null) {
drawable.setLevel(level);
}
}
}
/**
* Shows or Hides the Summary of an AccessPoint.
*
* @param showSummary true will show the summary, false will hide the summary
*/
public void setShowSummary(boolean showSummary) {
this.showSummary = showSummary;
if (mSummaryView != null) {
mSummaryView.setVisibility(showSummary ? View.VISIBLE : View.GONE);
} // otherwise, will be handled in onBindView.
}
/**
* Updates the title and summary; may indirectly call notifyChanged().
*/
public void refresh() {
setTitle(mAccessPoint.getSsid());
final Context context = getContext();
updateIcon(mAccessPoint.getLevel(), context);
// Force new summary
setSummary(null);
String summary = mAccessPoint.getSummary();
if (summary.length() > 0) {
setSummary(summary);
setShowSummary(true);
} else {
setShowSummary(false);
}
}
public void onLevelChanged() {
notifyChanged();
}
}
|