Skip to content

Commit

Permalink
Snap for 4498373 from 1f3b2a2d39de79f131368ff005b49018f8563f92 to ub-…
Browse files Browse the repository at this point in the history
…testdpc-oc-release

Change-Id: I47eb9a1ae6c2b312f83cd84802dece7f93c03341
  • Loading branch information
android-build-team Robot committed Dec 12, 2017
2 parents 6b1e816 + b5fb2d0 commit fa349d2
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ext {
// exactly 1 digit
versionMinor = 0
// exactly 2 digits
versionBuild = 05
versionBuild = 06
}

android {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.support.v7.app.NotificationCompat;
import android.support.v7.app.NotificationCompat.Builder;
import android.text.TextUtils;

import com.afwsamples.testdpc.common.NotificationUtil;

public class PackageMonitorReceiver extends BroadcastReceiver {
Expand Down Expand Up @@ -37,6 +36,7 @@ public void onReceive(Context context, Intent intent) {
.setContentText(notificationBody)
.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationBody))
.setDefaults(Notification.DEFAULT_LIGHTS)
.setOnlyAlertOnce(true)
.build();
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Expand Down
44 changes: 43 additions & 1 deletion app/src/main/java/com/afwsamples/testdpc/common/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import android.net.Uri;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.support.annotation.RequiresApi;
Expand All @@ -56,7 +57,12 @@
*/
public class Util {
private static final String TAG = "Util";
private static final int DEFAULT_BUFFER_SIZE = 4096;
private static final int DEFAULT_BUFFER_SIZE = 4096;

private static final String BROADCAST_ACTION_FRP_CONFIG_CHANGED =
"com.google.android.gms.auth.FRP_CONFIG_CHANGED";
private static final String GMSCORE_PACKAGE = "com.google.android.gms";
private static final String PERSISTENT_DEVICE_OWNER_STATE = "persistentDeviceOwnerState";

/**
* Format a friendly datetime for the current locale according to device policy documentation.
Expand Down Expand Up @@ -194,6 +200,42 @@ public static boolean installCaCertificate(InputStream certificateInputStream,
return false;
}

/**
* Returns the persistent device owner state which has been set by the device owner as an app
* restriction on GmsCore or null if there is no such restriction set.
*/
@TargetApi(VERSION_CODES.O)
public static String getPersistentDoStateFromApplicationRestriction(
DevicePolicyManager dpm, ComponentName admin) {
Bundle restrictions = dpm.getApplicationRestrictions(admin, GMSCORE_PACKAGE);
return restrictions.getString(PERSISTENT_DEVICE_OWNER_STATE);
}

/**
* Sets the persistent device owner state by setting a special app restriction on GmsCore and
* notifies GmsCore about the change by sending a broadcast.
*
* @param state The device owner state to be preserved across factory resets. If null, the
* persistent device owner state and the corresponding restiction are cleared.
*/
@TargetApi(VERSION_CODES.O)
public static void setPersistentDoStateWithApplicationRestriction(
Context context, DevicePolicyManager dpm, ComponentName admin, String state) {
Bundle restrictions = dpm.getApplicationRestrictions(admin, GMSCORE_PACKAGE);
if (state == null) {
// Clear the restriction
restrictions.remove(PERSISTENT_DEVICE_OWNER_STATE);
} else {
// Set the restriction
restrictions.putString(PERSISTENT_DEVICE_OWNER_STATE, state);
}
dpm.setApplicationRestrictions(admin, GMSCORE_PACKAGE, restrictions);
Intent broadcastIntent = new Intent(BROADCAST_ACTION_FRP_CONFIG_CHANGED);
broadcastIntent.setPackage(GMSCORE_PACKAGE);
broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
context.sendBroadcast(broadcastIntent);
}

private static DevicePolicyManager getDevicePolicyManager(Context context) {
return (DevicePolicyManager)context.getSystemService(Service.DEVICE_POLICY_SERVICE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ public class DpcPreferenceHelper {
* not yet assigned.
*/
private int getDeviceSdkInt() {
if (BuildCompat.isAtLeastO()) {
return Build.VERSION_CODES.O;
}
return Build.VERSION.SDK_INT;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.afwsamples.testdpc.policy;

import android.annotation.TargetApi;
import android.app.Fragment;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.afwsamples.testdpc.DeviceAdminReceiver;
import com.afwsamples.testdpc.R;
import com.afwsamples.testdpc.common.Util;

/**
* Allows the user to set a test persistent device owner state.
*
* <p>For manual testing of forced re-enrollment.
*
* <p>If there is a non-empty peristent device owner state, it will survive the next factory reset,
* TestDPC will be re-installed automatically as device owner and the state will be passed to it
* during the initial device setup.
*/
public class PersistentDeviceOwnerFragment extends Fragment implements View.OnClickListener {

private DevicePolicyManager mDpm;
private ComponentName mAdminComponent;
private EditText mStateEdit;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().getActionBar().setTitle(R.string.persistent_device_owner);
mDpm = (DevicePolicyManager) getActivity().getSystemService(
Context.DEVICE_POLICY_SERVICE);
mAdminComponent = DeviceAdminReceiver.getComponentName(getActivity());
}

@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.persistent_device_owner_fragment, container, false);
root.findViewById(R.id.clear_persistent_device_owner_button).setOnClickListener(this);
root.findViewById(R.id.set_persistent_device_owner_button).setOnClickListener(this);
mStateEdit = (EditText) root.findViewById(R.id.persistent_device_owner_state_edit);
return root;
}

@Override
public void onClick(View view) {
String message = null;
switch (view.getId()) {
case R.id.clear_persistent_device_owner_button:
mStateEdit.getText().clear();
Util.setPersistentDoStateWithApplicationRestriction(
getActivity(), mDpm, mAdminComponent, null);
break;
case R.id.set_persistent_device_owner_button:
Util.setPersistentDoStateWithApplicationRestriction(
getActivity(), mDpm, mAdminComponent, mStateEdit.getText().toString());
break;
}
}

@Override
public void onResume() {
super.onResume();
String state = Util.getPersistentDoStateFromApplicationRestriction(mDpm, mAdminComponent);
mStateEdit.setText(state == null ? "" : state);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
private static final String UNHIDE_APPS_KEY = "unhide_apps";
private static final String UNSUSPEND_APPS_KEY = "unsuspend_apps";
private static final String WIPE_DATA_KEY = "wipe_data";
private static final String PERSISTENT_DEVICE_OWNER_KEY = "persistent_device_owner";
private static final String CREATE_WIFI_CONFIGURATION_KEY = "create_wifi_configuration";
private static final String CREATE_EAP_TLS_WIFI_CONFIGURATION_KEY
= "create_eap_tls_wifi_configuration";
Expand Down Expand Up @@ -409,6 +410,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
STAY_ON_WHILE_PLUGGED_IN);
mStayOnWhilePluggedInSwitchPreference.setOnPreferenceChangeListener(this);
findPreference(WIPE_DATA_KEY).setOnPreferenceClickListener(this);
findPreference(PERSISTENT_DEVICE_OWNER_KEY).setOnPreferenceClickListener(this);
findPreference(REMOVE_DEVICE_OWNER_KEY).setOnPreferenceClickListener(this);
mEnableBackupServicePreference = (SwitchPreference) findPreference(ENABLE_BACKUP_SERVICE);
mEnableBackupServicePreference.setOnPreferenceChangeListener(this);
Expand Down Expand Up @@ -482,6 +484,10 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
SET_AUTO_TIME_REQUIRED_KEY);
mSetAutoTimeRequiredPreference.setOnPreferenceChangeListener(this);

mSetDeviceOrganizationNamePreference =
(EditTextPreference) findPreference(SET_DEVICE_ORGANIZATION_NAME_KEY);
mSetDeviceOrganizationNamePreference.setOnPreferenceChangeListener(this);

constrainSpecialCasePreferences();

maybeDisableLockTaskPreferences();
Expand Down Expand Up @@ -585,6 +591,9 @@ public void onPositiveButtonClicked(String[] lockTaskArray) {
case WIPE_DATA_KEY:
showWipeDataPrompt();
return true;
case PERSISTENT_DEVICE_OWNER_KEY:
showFragment(new PersistentDeviceOwnerFragment());
return true;
case REMOVE_DEVICE_OWNER_KEY:
showRemoveDeviceOwnerPrompt();
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import android.util.Log;

import com.afwsamples.testdpc.AddAccountActivity;
import com.afwsamples.testdpc.DeviceAdminReceiver;
import com.afwsamples.testdpc.EnableDeviceOwnerActivity;
import com.afwsamples.testdpc.EnableProfileActivity;
import com.afwsamples.testdpc.common.LaunchIntentUtil;
Expand Down Expand Up @@ -66,6 +67,8 @@ public class PostProvisioningTask {
"com.afwsamples.testdpc.SetupManagementLaunchActivity";
private static final String POST_PROV_PREFS = "post_prov_prefs";
private static final String KEY_POST_PROV_DONE = "key_post_prov_done";
private static final String KEY_DEVICE_OWNER_STATE =
"android.app.extra.PERSISTENT_DEVICE_OWNER_STATE";

private final Context mContext;
private final DevicePolicyManager mDevicePolicyManager;
Expand Down Expand Up @@ -98,6 +101,18 @@ public boolean performPostProvisioningOperations(Intent intent) {
maybeSetAffiliationIds(extras);
}

// If TestDPC asked GmsCore to store its state in the FRP area before factory reset, the
// state will be handed over to it during the next device setup.
if (BuildCompat.isAtLeastOMR1()
&& extras != null
&& extras.containsKey(KEY_DEVICE_OWNER_STATE)) {
Util.setPersistentDoStateWithApplicationRestriction(
mContext,
mDevicePolicyManager,
DeviceAdminReceiver.getComponentName(mContext),
extras.getString(KEY_DEVICE_OWNER_STATE));
}

// Hide the setup launcher when this app is the admin
mContext.getPackageManager().setComponentEnabledSetting(
new ComponentName(mContext, SETUP_MANAGEMENT_LAUNCH_ACTIVITY),
Expand Down
46 changes: 46 additions & 0 deletions app/src/main/res/layout/persistent_device_owner_fragment.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2017 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.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical">
<EditText android:id="@+id/persistent_device_owner_state_edit"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:lines="5"
android:layout_gravity="center"
android:hint="@string/persistent_device_owner_state_hint"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<Button android:id="@+id/clear_persistent_device_owner_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/clear_persistent_device_owner_label"
android:layout_margin="8dp"
android:visibility="visible"/>
<Button android:id="@+id/set_persistent_device_owner_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/set_persistent_device_owner_label"
android:layout_margin="8dp"
android:visibility="visible"/>
</LinearLayout>
</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<enum name="M" value="23" />
<enum name="N" value="24" />
<enum name="O" value="26" />
<enum name="O_MR1" value="27" />
<!-- Build.VERSION_CODES.CUR_DEVELOPMENT, fill in the sdk int once it is finalized -->
</attr>

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,17 @@
<string name="wipe_data_title">Wipe data?</string>
<string name="wipe_data_confirmation">Are you sure you want to wipe the data?</string>
<string name="wipe_data_include">Also wipe:</string>
<string name="persistent_device_owner">Persistent device owner state</string>
<string name="external_storage">External storage</string>
<string name="reset_protection_data">Factory reset protection</string>
<string name="remove_device_owner">Remove this device owner</string>
<string name="remove_device_owner_title">Remove device owner?</string>
<string name="remove_device_owner_confirmation">Policies and restrictions will continue to be
active and may require a factory reset to clear.</string>
<string name="device_owner_removed">This app is no longer a device owner.</string>
<string name="persistent_device_owner_state_hint">Test state to be restored alongside the device owner after factory reset.</string>
<string name="set_persistent_device_owner_label">Set</string>
<string name="clear_persistent_device_owner_label">Clear</string>

<!-- Strings for device reboot -->
<string name="reboot">Reboot device</string>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/xml/device_policy_header.xml
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,11 @@
android:title="@string/reboot"
testdpc:admin="deviceOwner"
testdpc:minSdkVersion="N" />
<com.afwsamples.testdpc.common.preference.DpcPreference
android:key="persistent_device_owner"
android:title="@string/persistent_device_owner"
testdpc:admin="deviceOwner"
testdpc:minSdkVersion="O_MR1" />
</PreferenceCategory>

<PreferenceCategory android:title="@string/safetynet_preference_title">
Expand Down

0 comments on commit fa349d2

Please sign in to comment.