From b8008ace00a4111669422fc11b26df2e2a2a6c27 Mon Sep 17 00:00:00 2001 From: Yuichi Araki Date: Mon, 1 Aug 2016 17:26:51 +0900 Subject: NfcProvisioning, DeviceOwner: Several fixes - Use EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME on API Level 23 and above - Wrap deprecated Configuration#locale and use #getLocales(). - (Moderately) Materialize DeviceOwner app - Remove an unused variable Change-Id: Ic8288e157e3fd724201acb30e59d644a53219f53 --- .../Application/src/main/AndroidManifest.xml | 2 +- .../android/deviceowner/DeviceOwnerFragment.java | 8 ++-- .../android/deviceowner/InstructionFragment.java | 2 +- .../example/android/deviceowner/MainActivity.java | 8 ++-- .../Application/src/main/res/values/styles.xml | 25 +++++++++++ .../android/nfcprovisioning/CompatUtils.java | 49 ++++++++++++++++++++++ .../nfcprovisioning/NfcProvisioningFragment.java | 46 ++++++++++++++++++-- .../nfcprovisioning/ProvisioningValuesLoader.java | 11 +++-- .../main/res/layout/fragment_nfc_provisioning.xml | 8 ++++ .../Application/src/main/res/values/strings.xml | 1 + 10 files changed, 143 insertions(+), 17 deletions(-) create mode 100644 admin/DeviceOwner/Application/src/main/res/values/styles.xml create mode 100644 admin/NfcProvisioning/Application/src/main/java/com/example/android/nfcprovisioning/CompatUtils.java diff --git a/admin/DeviceOwner/Application/src/main/AndroidManifest.xml b/admin/DeviceOwner/Application/src/main/AndroidManifest.xml index fb61ac9a..d81d85a0 100644 --- a/admin/DeviceOwner/Application/src/main/AndroidManifest.xml +++ b/admin/DeviceOwner/Application/src/main/AndroidManifest.xml @@ -24,7 +24,7 @@ android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" - android:theme="@style/AppTheme"> + android:theme="@style/Theme.DeviceOwner"> + + + + + + 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> loader, Map 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 values) { - HashMap newMap = new HashMap(); Properties props = new Properties(); - Setkeys = new HashSet(values.keySet()); + Set 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 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)) { diff --git a/admin/NfcProvisioning/Application/src/main/res/layout/fragment_nfc_provisioning.xml b/admin/NfcProvisioning/Application/src/main/res/layout/fragment_nfc_provisioning.xml index ee79e7a3..8aebcc3c 100644 --- a/admin/NfcProvisioning/Application/src/main/res/layout/fragment_nfc_provisioning.xml +++ b/admin/NfcProvisioning/Application/src/main/res/layout/fragment_nfc_provisioning.xml @@ -32,6 +32,14 @@ limitations under the License. android:hint="@string/hint_package_name" android:inputType="textNoSuggestions"/> + + NFC provisioning sample Package name of device owner + Class name of device admin receiver Locale Timezone WiFi SSID -- cgit v1.2.3