diff options
Diffstat (limited to 'admin/NfcProvisioning/Application/src/main/java/com')
3 files changed, 99 insertions, 7 deletions
diff --git a/admin/NfcProvisioning/Application/src/main/java/com/example/android/nfcprovisioning/CompatUtils.java b/admin/NfcProvisioning/Application/src/main/java/com/example/android/nfcprovisioning/CompatUtils.java new file mode 100644 index 00000000..528d2da2 --- /dev/null +++ b/admin/NfcProvisioning/Application/src/main/java/com/example/android/nfcprovisioning/CompatUtils.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2016 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.example.android.nfcprovisioning; + +import android.content.res.Configuration; +import android.os.Build; +import android.os.LocaleList; + +import java.util.Locale; + + +public final class CompatUtils { + + private CompatUtils() { + // Prevent instantiation + } + + /** + * Retrieves the primary locale from the specified {@link Configuration}. + * + * @param configuration The current {@link Configuration}. + * @return The primary locale. + */ + public static Locale getPrimaryLocale(Configuration configuration) { + if (Build.VERSION.SDK_INT >= 24) { + final LocaleList locales = configuration.getLocales(); + if (locales.size() > 0) { + return locales.get(0); + } + } + //noinspection deprecation + return configuration.locale; + } + +} diff --git a/admin/NfcProvisioning/Application/src/main/java/com/example/android/nfcprovisioning/NfcProvisioningFragment.java b/admin/NfcProvisioning/Application/src/main/java/com/example/android/nfcprovisioning/NfcProvisioningFragment.java index 3aef6178..9c2d9481 100644 --- a/admin/NfcProvisioning/Application/src/main/java/com/example/android/nfcprovisioning/NfcProvisioningFragment.java +++ b/admin/NfcProvisioning/Application/src/main/java/com/example/android/nfcprovisioning/NfcProvisioningFragment.java @@ -18,10 +18,12 @@ package com.example.android.nfcprovisioning; import android.app.Activity; import android.app.admin.DevicePolicyManager; +import android.content.ComponentName; import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.nfc.NfcAdapter; import android.nfc.NfcEvent; +import android.os.Build; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; @@ -55,6 +57,7 @@ public class NfcProvisioningFragment extends Fragment implements // View references private EditText mEditPackageName; + private EditText mEditClassName; private EditText mEditLocale; private EditText mEditTimezone; private EditText mEditWifiSsid; @@ -74,6 +77,7 @@ public class NfcProvisioningFragment extends Fragment implements public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { // Retrieve view references mEditPackageName = (EditText) view.findViewById(R.id.package_name); + mEditClassName = (EditText) view.findViewById(R.id.class_name); mEditLocale = (EditText) view.findViewById(R.id.locale); mEditTimezone = (EditText) view.findViewById(R.id.timezone); mEditWifiSsid = (EditText) view.findViewById(R.id.wifi_ssid); @@ -81,12 +85,15 @@ public class NfcProvisioningFragment extends Fragment implements mEditWifiPassword = (EditText) view.findViewById(R.id.wifi_password); // Bind event handlers mEditPackageName.addTextChangedListener(new TextWatcherWrapper(R.id.package_name, this)); + mEditClassName.addTextChangedListener(new TextWatcherWrapper(R.id.class_name, this)); mEditLocale.addTextChangedListener(new TextWatcherWrapper(R.id.locale, this)); mEditTimezone.addTextChangedListener(new TextWatcherWrapper(R.id.timezone, this)); mEditWifiSsid.addTextChangedListener(new TextWatcherWrapper(R.id.wifi_ssid, this)); mEditWifiSecurityType.addTextChangedListener( new TextWatcherWrapper(R.id.wifi_security_type, this)); mEditWifiPassword.addTextChangedListener(new TextWatcherWrapper(R.id.wifi_password, this)); + // Prior to API 23, the class name is not needed + mEditClassName.setVisibility(Build.VERSION.SDK_INT >= 23 ? View.VISIBLE : View.GONE); } @Override @@ -117,9 +124,14 @@ public class NfcProvisioningFragment extends Fragment implements if (!value.startsWith("\"") || !value.endsWith("\"")) { value = "\"" + value + "\""; } - } else { - value = e.getValue(); - } + } else //noinspection deprecation + if (e.getKey().equals( + DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME) + && Build.VERSION.SDK_INT >= 23) { + continue; + } else { + value = e.getValue(); + } properties.put(e.getKey(), value); } } @@ -147,9 +159,28 @@ public class NfcProvisioningFragment extends Fragment implements } switch (id) { case R.id.package_name: + //noinspection deprecation mProvisioningValues.put( DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, s); break; + case R.id.class_name: + if (Build.VERSION.SDK_INT >= 23) { + if (TextUtils.isEmpty(s)) { + mProvisioningValues.remove( + DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME); + } else { + // On API 23 and above, we can use + // EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME to specify the receiver + // in the device owner app. If the provisioning values contain this key, + // EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME is not read. + String packageName = mEditPackageName.getText().toString(); + ComponentName name = new ComponentName(packageName, s); + mProvisioningValues.put( + DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, + name.flattenToShortString()); + } + } + break; case R.id.locale: mProvisioningValues.put(DevicePolicyManager.EXTRA_PROVISIONING_LOCALE, s); break; @@ -181,14 +212,21 @@ public class NfcProvisioningFragment extends Fragment implements public void onLoadFinished(Loader<Map<String, String>> loader, Map<String, String> values) { if (loader.getId() == LOADER_PROVISIONING_VALUES) { mProvisioningValues = values; + //noinspection deprecation mEditPackageName.setText(values.get( DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME)); + if (Build.VERSION.SDK_INT >= 23) { + ComponentName name = ComponentName.unflattenFromString(values.get( + DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME)); + mEditClassName.setText(name.getClassName()); + } mEditLocale.setText(values.get(DevicePolicyManager.EXTRA_PROVISIONING_LOCALE)); mEditTimezone.setText(values.get(DevicePolicyManager.EXTRA_PROVISIONING_TIME_ZONE)); mEditWifiSsid.setText(values.get(DevicePolicyManager.EXTRA_PROVISIONING_WIFI_SSID)); mEditWifiSecurityType.setText(values.get( DevicePolicyManager.EXTRA_PROVISIONING_WIFI_SECURITY_TYPE)); - mEditWifiPassword.setText(null); + mEditWifiPassword.setText(values.get( + DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PASSWORD)); } } diff --git a/admin/NfcProvisioning/Application/src/main/java/com/example/android/nfcprovisioning/ProvisioningValuesLoader.java b/admin/NfcProvisioning/Application/src/main/java/com/example/android/nfcprovisioning/ProvisioningValuesLoader.java index 73a60532..f3940f1d 100644 --- a/admin/NfcProvisioning/Application/src/main/java/com/example/android/nfcprovisioning/ProvisioningValuesLoader.java +++ b/admin/NfcProvisioning/Application/src/main/java/com/example/android/nfcprovisioning/ProvisioningValuesLoader.java @@ -21,6 +21,7 @@ import android.app.admin.DevicePolicyManager; import android.content.Context; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; +import android.os.Build; import android.os.Environment; import android.support.v4.content.AsyncTaskLoader; @@ -136,9 +137,8 @@ public class ProvisioningValuesLoader extends AsyncTaskLoader<Map<String, String } private void gatherAdminExtras(HashMap<String, String> values) { - HashMap<String, String> newMap = new HashMap<String, String>(); Properties props = new Properties(); - Set<String>keys = new HashSet(values.keySet()); + Set<String> keys = new HashSet<>(values.keySet()); for (String key : keys) { if (key.startsWith("android.app.extra")) { continue; @@ -160,10 +160,15 @@ public class ProvisioningValuesLoader extends AsyncTaskLoader<Map<String, String private void loadSystemValues(HashMap<String, String> values) { Context context = getContext(); + //noinspection deprecation putIfMissing(values, DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, "com.example.android.deviceowner"); + if (Build.VERSION.SDK_INT >= 23) { + putIfMissing(values, DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, + "com.example.android.deviceowner/.DeviceOwnerReceiver"); + } putIfMissing(values, DevicePolicyManager.EXTRA_PROVISIONING_LOCALE, - context.getResources().getConfiguration().locale.toString()); + CompatUtils.getPrimaryLocale(context.getResources().getConfiguration()).toString()); putIfMissing(values, DevicePolicyManager.EXTRA_PROVISIONING_TIME_ZONE, TimeZone.getDefault().getID()); if (!values.containsKey(DevicePolicyManager.EXTRA_PROVISIONING_WIFI_SSID)) { |