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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
package com.android.softap;
import android.app.ActionBar;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.UserHandle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.SwitchPreference;
import com.android.softap.model.ClientInfo;
public class ClientInfoActivity extends FragmentActivity implements SoftApManageService.StatusListener {
private SoftApManageService.SoftApManageBinder mSoftApManageBinder;
private SoftApManageConn mSoftApManageConn;
private ClientInfoFragment mFragment;
private String mMACAddress;
private ClientInfo mClientInfo;
private IClientManager mClientManager = new IClientManager() {
@Override
public boolean block(boolean val) {
if (mSoftApManageBinder != null) {
if (val) {
return mSoftApManageBinder.blockClient(mMACAddress);
} else {
return mSoftApManageBinder.unblockClient(mMACAddress);
}
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
mMACAddress = intent.getStringExtra("mac");
if (TextUtils.isEmpty(mMACAddress)) {
finish();
return;
}
setContentView(R.layout.client_info_activity);
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content, new ClientInfoFragment())
.commit();
}
ActionBar actionBar = getActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
Intent mSoftApManageService = new Intent(this, SoftApManageService.class);
mSoftApManageConn = new SoftApManageConn();
bindServiceAsUser(mSoftApManageService, mSoftApManageConn, Context.BIND_AUTO_CREATE, UserHandle.CURRENT_OR_SELF);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onDestroy() {
if (mSoftApManageConn != null) {
unbindService(mSoftApManageConn);
}
if (mFragment != null) {
mFragment.setClientManager(null);
}
super.onDestroy();
}
@Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
if (mFragment == null && fragment instanceof ClientInfoFragment) {
mFragment = (ClientInfoFragment) fragment;
mFragment.setClientManager(mClientManager);
}
}
private void updateClientInfo() {
if (mFragment == null || mSoftApManageBinder == null) {
return;
}
mClientInfo = mSoftApManageBinder.getClientByMAC(mMACAddress);
mFragment.updateClientInfo(mClientInfo);
}
@Override
public void onStatusChanged(int what) {
}
private class SoftApManageConn implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
mSoftApManageBinder = (SoftApManageService.SoftApManageBinder) iBinder;
updateClientInfo();
}
@Override
public void onServiceDisconnected(ComponentName name) {
mSoftApManageBinder = null;
finish();
}
}
public static class ClientInfoFragment extends PreferenceFragmentCompat implements
CompoundButton.OnCheckedChangeListener {
private IClientManager mClientManager;
private Preference prefName;
private Preference prefMAC;
private Preference prefIP;
private Preference prefManufacturer;
private boolean mAllowed = true;
private TextView mTextView;
private View mSwitchBar;
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.client_info_prefs, rootKey);
prefName = findPreference("name");
prefIP = findPreference("ip_address");
prefMAC = findPreference("mac_address");
prefManufacturer = findPreference("manufacturer");
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
final View view = LayoutInflater.from(getContext()).inflate(R.layout.master_setting_switch, container, false);
((ViewGroup) view).addView(super.onCreateView(inflater, container, savedInstanceState));
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mTextView = view.findViewById(R.id.switch_text);
mTextView.setText(getString(mAllowed ?
R.string.switch_on_text : R.string.switch_off_text));
mSwitchBar = view.findViewById(R.id.switch_bar);
Switch switchWidget = mSwitchBar.findViewById(android.R.id.switch_widget);
switchWidget.setChecked(mAllowed);
switchWidget.setOnCheckedChangeListener(this);
mSwitchBar.setActivated(mAllowed);
mSwitchBar.setOnClickListener(v -> {
switchWidget.setChecked(!switchWidget.isChecked());
mSwitchBar.setActivated(switchWidget.isChecked());
});
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (mClientManager != null) {
mClientManager.block(!isChecked);
}
mTextView.setText(getString(isChecked ? R.string.switch_on_text : R.string.switch_off_text));
mSwitchBar.setActivated(isChecked);
}
public void setClientManager(IClientManager manager) {
mClientManager = manager;
}
private void refreshSwitch() {
Switch switchWidget = mSwitchBar.findViewById(android.R.id.switch_widget);
switchWidget.setChecked(mAllowed);
mSwitchBar.setActivated(mAllowed);
}
public void updateClientInfo(ClientInfo info) {
if (info == null) return;
prefName.setSummary(info.getName());
prefMAC.setSummary(info.getMACAddress());
prefIP.setSummary(String.join("\n", info.getIPAddressArray()));
prefManufacturer.setSummary(info.getManufacturer());
mAllowed = !info.isBlocked();
refreshSwitch();
}
}
protected interface IClientManager {
boolean block(boolean val);
}
}
|