|
| 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.networking; |
| 18 | + |
| 19 | +import android.annotation.TargetApi; |
| 20 | +import android.app.Fragment; |
| 21 | +import android.app.admin.DevicePolicyManager; |
| 22 | +import android.content.ComponentName; |
| 23 | +import android.content.Context; |
| 24 | +import android.os.Bundle; |
| 25 | +import android.util.Log; |
| 26 | +import android.view.LayoutInflater; |
| 27 | +import android.view.View; |
| 28 | +import android.view.ViewGroup; |
| 29 | +import android.widget.Button; |
| 30 | +import android.widget.EditText; |
| 31 | +import android.widget.RadioGroup; |
| 32 | +import android.widget.Toast; |
| 33 | + |
| 34 | +import com.afwsamples.testdpc.DeviceAdminReceiver; |
| 35 | +import com.afwsamples.testdpc.R; |
| 36 | +import com.afwsamples.testdpc.common.ReflectionUtil; |
| 37 | + |
| 38 | +@TargetApi(29) |
| 39 | +public class PrivateDnsModeFragment extends Fragment implements View.OnClickListener, |
| 40 | + RadioGroup.OnCheckedChangeListener { |
| 41 | + private static final String TAG = "PDNS_FRAG"; |
| 42 | + |
| 43 | + // Copied from DevicePolicyManager, should be removed when the code is migrated to Q. |
| 44 | + static final int PRIVATE_DNS_MODE_UNKNOWN = 0; |
| 45 | + static final int PRIVATE_DNS_MODE_OFF = 1; |
| 46 | + static final int PRIVATE_DNS_MODE_OPPORTUNISTIC = 2; |
| 47 | + static final int PRIVATE_DNS_MODE_PROVIDER_HOSTNAME = 3; |
| 48 | + |
| 49 | + private DevicePolicyManager mDpm; |
| 50 | + private RadioGroup mPrivateDnsModeSelection; |
| 51 | + private Button mSetButton; |
| 52 | + private EditText mCurrentResolver; |
| 53 | + // The mode that is currently selected in the radio group. |
| 54 | + private int mSelectedMode; |
| 55 | + |
| 56 | + @Override |
| 57 | + public void onCreate(Bundle savedInstanceState) { |
| 58 | + super.onCreate(savedInstanceState); |
| 59 | + mDpm = (DevicePolicyManager) getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE); |
| 60 | + mSelectedMode = PRIVATE_DNS_MODE_UNKNOWN; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void onClick(View view) { |
| 65 | + String resolver = mCurrentResolver.getText().toString(); |
| 66 | + setPrivateDnsMode(mSelectedMode, resolver); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void onCheckedChanged(RadioGroup group, int checkedId) { |
| 71 | + updateSelectedMode(checkedId); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 76 | + Bundle savedInstanceState) { |
| 77 | + View view = inflater.inflate(R.layout.private_dns_mode, null); |
| 78 | + mSetButton = view.findViewById(R.id.private_dns_mode_apply); |
| 79 | + mSetButton.setOnClickListener(this); |
| 80 | + |
| 81 | + mPrivateDnsModeSelection = view.findViewById(R.id.private_dns_mode_selection); |
| 82 | + int currentMode = getPrivateDnsMode(); |
| 83 | + switch (currentMode) { |
| 84 | + case PRIVATE_DNS_MODE_OFF: |
| 85 | + mPrivateDnsModeSelection.check(R.id.private_dns_mode_off); |
| 86 | + break; |
| 87 | + case PRIVATE_DNS_MODE_OPPORTUNISTIC: |
| 88 | + mPrivateDnsModeSelection.check(R.id.private_dns_mode_automatic); |
| 89 | + break; |
| 90 | + case PRIVATE_DNS_MODE_PROVIDER_HOSTNAME: |
| 91 | + mPrivateDnsModeSelection.check(R.id.private_dns_mode_specific_host); |
| 92 | + break; |
| 93 | + default: |
| 94 | + mPrivateDnsModeSelection.check(R.id.private_dns_mode_unknown); |
| 95 | + break; |
| 96 | + } |
| 97 | + mPrivateDnsModeSelection.setOnCheckedChangeListener(this); |
| 98 | + updateSelectedMode(mPrivateDnsModeSelection.getCheckedRadioButtonId()); |
| 99 | + |
| 100 | + mCurrentResolver = view.findViewById(R.id.private_dns_resolver); |
| 101 | + mCurrentResolver.setText(getPrivateDnsHost()); |
| 102 | + return view; |
| 103 | + } |
| 104 | + |
| 105 | + private void updateSelectedMode(int checkedId) { |
| 106 | + switch (checkedId) { |
| 107 | + case R.id.private_dns_mode_off: |
| 108 | + mSelectedMode = PRIVATE_DNS_MODE_OFF; |
| 109 | + break; |
| 110 | + case R.id.private_dns_mode_automatic: |
| 111 | + mSelectedMode = PRIVATE_DNS_MODE_OPPORTUNISTIC; |
| 112 | + break; |
| 113 | + case R.id.private_dns_mode_specific_host: |
| 114 | + mSelectedMode = PRIVATE_DNS_MODE_PROVIDER_HOSTNAME; |
| 115 | + break; |
| 116 | + case R.id.private_dns_mode_unknown: |
| 117 | + default: |
| 118 | + mSelectedMode = PRIVATE_DNS_MODE_UNKNOWN; |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private int getPrivateDnsMode() { |
| 124 | + try { |
| 125 | + return (Integer) ReflectionUtil.invoke( |
| 126 | + mDpm, "getGlobalPrivateDnsMode", |
| 127 | + DeviceAdminReceiver.getComponentName(getActivity())); |
| 128 | + } catch (ReflectionUtil.ReflectionIsTemporaryException e) { |
| 129 | + Log.w(TAG, "Failure getting current mode", e); |
| 130 | + } |
| 131 | + |
| 132 | + return PRIVATE_DNS_MODE_UNKNOWN; |
| 133 | + } |
| 134 | + |
| 135 | + private String getPrivateDnsHost() { |
| 136 | + try { |
| 137 | + return (String) ReflectionUtil.invoke( |
| 138 | + mDpm, "getGlobalPrivateDnsHost", |
| 139 | + DeviceAdminReceiver.getComponentName(getActivity())); |
| 140 | + } catch (ReflectionUtil.ReflectionIsTemporaryException e) { |
| 141 | + Log.w(TAG, "Failure getting host", e); |
| 142 | + } |
| 143 | + |
| 144 | + return "<error getting resolver>"; |
| 145 | + } |
| 146 | + |
| 147 | + private void setPrivateDnsMode(int mode, String resolver) { |
| 148 | + Log.w(TAG, String.format("Setting mode %d host %s", mSelectedMode, resolver)); |
| 149 | + try { |
| 150 | + ReflectionUtil.invoke( |
| 151 | + mDpm, "setGlobalPrivateDns", |
| 152 | + new Class[]{ComponentName.class, int.class, String.class}, |
| 153 | + DeviceAdminReceiver.getComponentName(getActivity()), |
| 154 | + mode, |
| 155 | + resolver); |
| 156 | + Toast.makeText(getActivity(), R.string.setting_private_dns_succeess, |
| 157 | + Toast.LENGTH_LONG).show(); |
| 158 | + } catch (ReflectionUtil.ReflectionIsTemporaryException e) { |
| 159 | + Log.w(TAG, "Failed to invoke, cause", e); |
| 160 | + |
| 161 | + // This is the real exception. |
| 162 | + Throwable causeOfCause = e.getCause().getCause(); |
| 163 | + |
| 164 | + Toast.makeText(getActivity(), |
| 165 | + "Failure: " + causeOfCause.getMessage(), Toast.LENGTH_LONG).show(); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments