|
| 1 | +/* |
| 2 | + * Copyright (C) 2018 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.afwsamples.testdpc.policy; |
| 18 | + |
| 19 | +import android.annotation.SuppressLint; |
| 20 | +import android.annotation.TargetApi; |
| 21 | +import android.app.Activity; |
| 22 | +import android.app.AlertDialog; |
| 23 | +import android.app.admin.DevicePolicyManager; |
| 24 | +import android.content.ComponentName; |
| 25 | +import android.content.Context; |
| 26 | +import android.os.Build; |
| 27 | +import android.os.Bundle; |
| 28 | +import android.support.v14.preference.SwitchPreference; |
| 29 | +import android.support.v7.preference.Preference; |
| 30 | +import android.telephony.data.ApnSetting; |
| 31 | +import android.text.TextUtils; |
| 32 | +import android.util.Log; |
| 33 | +import android.view.View; |
| 34 | +import android.widget.EditText; |
| 35 | +import android.widget.Toast; |
| 36 | +import com.afwsamples.testdpc.DeviceAdminReceiver; |
| 37 | +import com.afwsamples.testdpc.R; |
| 38 | +import com.afwsamples.testdpc.common.BaseSearchablePolicyPreferenceFragment; |
| 39 | +import com.afwsamples.testdpc.common.ReflectionUtil; |
| 40 | +import java.lang.reflect.Method; |
| 41 | +import java.net.InetAddress; |
| 42 | +import java.net.MalformedURLException; |
| 43 | +import java.net.URL; |
| 44 | +import java.net.UnknownHostException; |
| 45 | +import java.util.Arrays; |
| 46 | +import java.util.List; |
| 47 | + |
| 48 | +@TargetApi(28) |
| 49 | +public class OverrideApnFragment extends BaseSearchablePolicyPreferenceFragment implements |
| 50 | + Preference.OnPreferenceClickListener, Preference.OnPreferenceChangeListener { |
| 51 | + |
| 52 | + private static String LOG_TAG = "OverrideApnFragment"; |
| 53 | + private static final String INSERT_OVERRIDE_APN_KEY = "insert_override_apn"; |
| 54 | + private static final String REMOVE_OVERRIDE_APN_KEY = "remove_override_apn"; |
| 55 | + private static final String ENABLE_OVERRIDE_APN_KEY = "enable_override_apn"; |
| 56 | + |
| 57 | + private DevicePolicyManager mDevicePolicyManager; |
| 58 | + private ComponentName mAdminComponentName; |
| 59 | + |
| 60 | + private SwitchPreference mEnableOverrideApnPreference; |
| 61 | + |
| 62 | + @Override |
| 63 | + public void onCreate(Bundle savedInstanceState) { |
| 64 | + mDevicePolicyManager = (DevicePolicyManager) getActivity().getSystemService( |
| 65 | + Context.DEVICE_POLICY_SERVICE); |
| 66 | + mAdminComponentName = DeviceAdminReceiver.getComponentName(getActivity()); |
| 67 | + getActivity().getActionBar().setTitle(R.string.override_apn_title); |
| 68 | + |
| 69 | + super.onCreate(savedInstanceState); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { |
| 74 | + addPreferencesFromResource(R.xml.override_apn_preferences); |
| 75 | + |
| 76 | + findPreference(INSERT_OVERRIDE_APN_KEY).setOnPreferenceClickListener(this); |
| 77 | + findPreference(REMOVE_OVERRIDE_APN_KEY).setOnPreferenceClickListener(this); |
| 78 | + mEnableOverrideApnPreference = (SwitchPreference) findPreference(ENABLE_OVERRIDE_APN_KEY); |
| 79 | + mEnableOverrideApnPreference.setOnPreferenceChangeListener(this); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public boolean isAvailable(Context context) { |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public boolean onPreferenceClick(Preference preference) { |
| 89 | + String key = preference.getKey(); |
| 90 | + switch (key) { |
| 91 | + case INSERT_OVERRIDE_APN_KEY: |
| 92 | + showInsertOverrideApnDialog(); |
| 93 | + return true; |
| 94 | + case REMOVE_OVERRIDE_APN_KEY: |
| 95 | + onRemoveOverrideApn(); |
| 96 | + return true; |
| 97 | + } |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + @SuppressLint("NewApi") |
| 103 | + public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 104 | + String key = preference.getKey(); |
| 105 | + |
| 106 | + switch (key) { |
| 107 | + case ENABLE_OVERRIDE_APN_KEY: |
| 108 | + boolean enabled = (boolean) newValue; |
| 109 | + try { |
| 110 | + ReflectionUtil.invoke(mDevicePolicyManager, "setOverrideApnsEnabled", |
| 111 | + new Class[]{ComponentName.class, boolean.class}, mAdminComponentName, enabled); |
| 112 | + } catch (Exception e) { |
| 113 | + Log.e(LOG_TAG, "setOverrideApnsEnabled not implemented", e); |
| 114 | + } |
| 115 | + reloadEnableOverrideApnUi(); |
| 116 | + return true; |
| 117 | + } |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + void showInsertOverrideApnDialog() { |
| 122 | + if (getActivity() == null || getActivity().isFinishing()) { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + final View dialogView = getActivity().getLayoutInflater().inflate( |
| 127 | + R.layout.insert_apn, null); |
| 128 | + final EditText entryNameEditText = (EditText) dialogView.findViewById( |
| 129 | + R.id.apn_entry_name); |
| 130 | + final EditText apnNameEditText = (EditText) dialogView.findViewById( |
| 131 | + R.id.apn_apn_name); |
| 132 | + final EditText proxyEditText = (EditText) dialogView.findViewById( |
| 133 | + R.id.apn_proxy); |
| 134 | + final EditText portEditText = (EditText) dialogView.findViewById( |
| 135 | + R.id.apn_port); |
| 136 | + final EditText mmscEditText = (EditText) dialogView.findViewById( |
| 137 | + R.id.apn_mmsc); |
| 138 | + final EditText mmsProxyEditText = (EditText) dialogView.findViewById( |
| 139 | + R.id.apn_mmsproxy); |
| 140 | + final EditText mmsPortEditText = (EditText) dialogView.findViewById( |
| 141 | + R.id.apn_mmsport); |
| 142 | + final EditText userEditText = (EditText) dialogView.findViewById( |
| 143 | + R.id.apn_user); |
| 144 | + final EditText passwordEditText = (EditText) dialogView.findViewById( |
| 145 | + R.id.apn_password); |
| 146 | + final EditText authTypeEditText = (EditText) dialogView.findViewById( |
| 147 | + R.id.apn_auth_type); |
| 148 | + final EditText typeEditText = (EditText) dialogView.findViewById( |
| 149 | + R.id.apn_type); |
| 150 | + final EditText numericEditText = (EditText) dialogView.findViewById( |
| 151 | + R.id.apn_numeric); |
| 152 | + final EditText protocolEditText = (EditText) dialogView.findViewById( |
| 153 | + R.id.apn_protocol); |
| 154 | + final EditText roamingProtocolEditText = (EditText) dialogView.findViewById( |
| 155 | + R.id.apn_roaming_protocol); |
| 156 | + final EditText carrierEnabledEditText = (EditText) dialogView.findViewById( |
| 157 | + R.id.apn_carrier_enabled); |
| 158 | + final EditText networkBitmaskEditText = (EditText) dialogView.findViewById( |
| 159 | + R.id.apn_network_bitmask); |
| 160 | + final EditText mvnoTypeEditText = (EditText) dialogView.findViewById( |
| 161 | + R.id.apn_mvno_type); |
| 162 | + |
| 163 | + authTypeEditText.setHint(R.string.apn_auth_type_hint); |
| 164 | + numericEditText.setHint(R.string.apn_numeric_hint); |
| 165 | + carrierEnabledEditText.setHint(R.string.apn_carrier_enabled_hint); |
| 166 | + |
| 167 | + new AlertDialog.Builder(getActivity()) |
| 168 | + .setTitle(R.string.insert_override_apn) |
| 169 | + .setView(dialogView) |
| 170 | + .setPositiveButton(android.R.string.ok, (dialogInterface, i) -> { |
| 171 | + final String entryName = entryNameEditText.getText().toString(); |
| 172 | + if (entryName.isEmpty()) { |
| 173 | + showToast(R.string.apn_no_entry_name); |
| 174 | + return; |
| 175 | + } |
| 176 | + final String apnName = apnNameEditText.getText().toString(); |
| 177 | + if (apnName.isEmpty()) { |
| 178 | + showToast(R.string.apn_no_apn_name); |
| 179 | + return; |
| 180 | + } |
| 181 | + int authType = parseInt(authTypeEditText.getText().toString(), 0); |
| 182 | + int enabled = parseInt(carrierEnabledEditText.getText().toString(), 0); |
| 183 | + int networkbitmask = parseInt(networkBitmaskEditText.getText().toString(), 0); |
| 184 | + |
| 185 | + ApnSetting apn = makeApnSetting( |
| 186 | + numericEditText.getText().toString(), |
| 187 | + entryName, |
| 188 | + apnName, |
| 189 | + inetAddressFromString(proxyEditText.getText().toString()), |
| 190 | + parseInt(portEditText.getText().toString(), -1), |
| 191 | + URLFromString(mmscEditText.getText().toString()), |
| 192 | + inetAddressFromString(mmsProxyEditText.getText().toString()), |
| 193 | + parseInt(mmsPortEditText.getText().toString(), -1), |
| 194 | + userEditText.getText().toString(), |
| 195 | + passwordEditText.getText().toString(), |
| 196 | + authType, |
| 197 | + Arrays.asList(parseTypes(typeEditText.getText().toString())), |
| 198 | + protocolEditText.getText().toString(), |
| 199 | + roamingProtocolEditText.getText().toString(), |
| 200 | + enabled == 1, |
| 201 | + networkbitmask, |
| 202 | + mvnoTypeEditText.getText().toString() |
| 203 | + ); |
| 204 | + int insertedId = -1; |
| 205 | + try { |
| 206 | + insertedId = (int) ReflectionUtil.invoke(mDevicePolicyManager, |
| 207 | + "addOverrideApn", |
| 208 | + new Class[]{ComponentName.class, ApnSetting.class}, |
| 209 | + mAdminComponentName, apn); |
| 210 | + } catch (Exception e) { |
| 211 | + Log.e(LOG_TAG, "addOverrideApn method not implemented", e); |
| 212 | + } |
| 213 | + if (insertedId == -1) { |
| 214 | + showToast(R.string.insert_override_apn_error); |
| 215 | + } else { |
| 216 | + showToast("Inserted APN id: " + insertedId); |
| 217 | + } |
| 218 | + }) |
| 219 | + .setNegativeButton(android.R.string.cancel, null) |
| 220 | + .show(); |
| 221 | + } |
| 222 | + |
| 223 | + private void onRemoveOverrideApn() { |
| 224 | + try { |
| 225 | + List<ApnSetting> apnSettings = (List<ApnSetting>)ReflectionUtil.invoke(mDevicePolicyManager, |
| 226 | + "getOverrideApns", |
| 227 | + new Class[]{ComponentName.class}, |
| 228 | + mAdminComponentName); |
| 229 | + for(ApnSetting apn : apnSettings) { |
| 230 | + ReflectionUtil.invoke(mDevicePolicyManager, "removeOverrideApn", |
| 231 | + new Class[]{ComponentName.class, int.class}, |
| 232 | + mAdminComponentName, apn.getId()); |
| 233 | + } |
| 234 | + } catch (Exception e) { |
| 235 | + Log.e(LOG_TAG, "removeOverrideApn method not implemented", e); |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + private int parseInt(String str, int defaultValue) { |
| 240 | + try { |
| 241 | + return Integer.parseInt(str); |
| 242 | + } catch (NumberFormatException e) { |
| 243 | + return defaultValue; |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + private ApnSetting makeApnSetting(String operatorNumeric, String entryName, String apnName, |
| 248 | + InetAddress proxy, int port, URL mmsc, InetAddress mmsProxy, int mmsPort, |
| 249 | + String user, String password, int authType, List<String> types, String protocol, |
| 250 | + String roamingProtocol, boolean carrierEnabled, int networkTypeBitmask, |
| 251 | + String mvnoType) { |
| 252 | + try { |
| 253 | + ApnSetting.Builder builder = new ApnSetting.Builder(); |
| 254 | + Method setNetworkTypeBitmask = ApnSetting.Builder.class.getMethod( |
| 255 | + "setNetworkTypeBitmask", new Class[]{int.class}); |
| 256 | + setNetworkTypeBitmask.invoke(builder, networkTypeBitmask); |
| 257 | + |
| 258 | + return builder.setOperatorNumeric(operatorNumeric) |
| 259 | + .setEntryName(entryName) |
| 260 | + .setApnName(apnName) |
| 261 | + .setProxy(proxy) |
| 262 | + .setPort(port) |
| 263 | + .setMmsc(mmsc) |
| 264 | + .setMmsProxy(mmsProxy) |
| 265 | + .setMmsPort(mmsPort) |
| 266 | + .setUser(user) |
| 267 | + .setPassword(password) |
| 268 | + .setAuthType(authType) |
| 269 | + .setTypes(types) |
| 270 | + .setProtocol(protocol) |
| 271 | + .setRoamingProtocol(roamingProtocol) |
| 272 | + .setCarrierEnabled(carrierEnabled) |
| 273 | + .setMvnoType(mvnoType) |
| 274 | + .build(); |
| 275 | + } catch (Exception e) { |
| 276 | + Log.e(LOG_TAG, "setNetworkTypeBitmask not implemented.", e); |
| 277 | + } |
| 278 | + return null; |
| 279 | + } |
| 280 | + |
| 281 | + private URL URLFromString(String url) { |
| 282 | + try { |
| 283 | + return TextUtils.isEmpty(url) ? null : new URL(url); |
| 284 | + } catch (MalformedURLException e) { |
| 285 | + Log.e(LOG_TAG, "Can't parse URL from string."); |
| 286 | + showToast(R.string.apn_wrong_url); |
| 287 | + return null; |
| 288 | + } |
| 289 | + } |
| 290 | + |
| 291 | + private InetAddress inetAddressFromString(String inetAddress) { |
| 292 | + if (TextUtils.isEmpty(inetAddress)) { |
| 293 | + return null; |
| 294 | + } |
| 295 | + try { |
| 296 | + return InetAddress.getByName(inetAddress); |
| 297 | + } catch (UnknownHostException e) { |
| 298 | + Log.e(LOG_TAG, "Can't parse InetAddress from string: unknown host."); |
| 299 | + showToast(R.string.apn_wrong_inetaddress); |
| 300 | + return null; |
| 301 | + } |
| 302 | + } |
| 303 | + |
| 304 | + private String[] parseTypes(String types) { |
| 305 | + String[] result; |
| 306 | + // If unset, set to DEFAULT. |
| 307 | + if (TextUtils.isEmpty(types)) { |
| 308 | + result = new String[1]; |
| 309 | + result[0] = "*"; |
| 310 | + } else { |
| 311 | + result = types.split(","); |
| 312 | + } |
| 313 | + return result; |
| 314 | + } |
| 315 | + |
| 316 | + private void reloadEnableOverrideApnUi() { |
| 317 | + boolean enabled = false; |
| 318 | + try { |
| 319 | + enabled = (boolean)ReflectionUtil.invoke(mDevicePolicyManager, |
| 320 | + "isOverrideApnEnabled", |
| 321 | + new Class[]{ComponentName.class}, mAdminComponentName); |
| 322 | + } catch (Exception e) { |
| 323 | + Log.e(LOG_TAG, "setOverrideApnEnabled not implemented", e); |
| 324 | + showToast("setOverrideApnEnabled not implemented"); |
| 325 | + } |
| 326 | + if (mEnableOverrideApnPreference.isEnabled()) { |
| 327 | + mEnableOverrideApnPreference.setChecked(enabled); |
| 328 | + } |
| 329 | + } |
| 330 | + |
| 331 | + private void showToast(int msgId, Object... args) { |
| 332 | + showToast(getString(msgId, args), Toast.LENGTH_SHORT); |
| 333 | + } |
| 334 | + |
| 335 | + private void showToast(String msg) { |
| 336 | + showToast(msg, Toast.LENGTH_SHORT); |
| 337 | + } |
| 338 | + |
| 339 | + private void showToast(String msg, int duration) { |
| 340 | + Activity activity = getActivity(); |
| 341 | + if (activity == null || activity.isFinishing()) { |
| 342 | + return; |
| 343 | + } |
| 344 | + Toast.makeText(activity, msg, duration).show(); |
| 345 | + } |
| 346 | +} |
0 commit comments