Skip to content

Commit

Permalink
Merge /usr/local/google/home/ascull/projects/ub-testdpc-oc-release/ve…
Browse files Browse the repository at this point in the history
…ndor/unbundled_google/packages/TestDPC into new-version
  • Loading branch information
AndrewScull committed Dec 13, 2017
2 parents 18cea73 + 31d6f9d commit d252de9
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 24 deletions.
14 changes: 7 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ ext {
// exactly 1 digit
versionMinor = 0
// exactly 2 digits
versionBuild = 05
versionBuild = 06
}

android {
compileSdkVersion 26
buildToolsVersion '26.0.0'
compileSdkVersion 27
buildToolsVersion "27.0.1"

defaultConfig {
applicationId "com.afwsamples.testdpc"
minSdkVersion 21
targetSdkVersion 26
targetSdkVersion 27
versionCode versionMajor * 1000 + versionMinor * 100 + versionBuild
versionName "${versionMajor}.${versionMinor}.${versionBuild}"
}
Expand Down Expand Up @@ -61,9 +61,9 @@ android {
}

dependencies {
api 'com.android.support:preference-v14:26.+'
api 'com.android.support:recyclerview-v7:26.+'
api 'com.android.support:support-v13:26.+'
api 'com.android.support:preference-v14:27.0.2'
api 'com.android.support:recyclerview-v7:27.0.2'
api 'com.android.support:support-v13:27.0.2'
api 'com.google.android.gms:play-services-safetynet:+'
api(name:'setup-wizard-lib-platform-release', ext:'aar')
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.afwsamples.testdpc;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.admin.DevicePolicyManager;
Expand All @@ -33,13 +32,12 @@
import android.os.UserHandle;
import android.os.UserManager;
import android.preference.PreferenceManager;
import android.support.v7.app.NotificationCompat;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.afwsamples.testdpc.common.NotificationUtil;
import com.afwsamples.testdpc.common.Util;
import com.afwsamples.testdpc.provision.PostProvisioningTask;

import java.io.BufferedReader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.NotificationCompat;
import android.support.v7.app.NotificationCompat.Builder;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;

import com.afwsamples.testdpc.common.NotificationUtil;
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import android.support.annotation.RequiresApi;
import android.support.annotation.StringRes;
import android.support.v4.os.BuildCompat;
import android.support.v7.app.NotificationCompat;
import android.support.v4.app.NotificationCompat;

import com.afwsamples.testdpc.R;

Expand Down Expand Up @@ -37,8 +37,8 @@ public static NotificationCompat.Builder getNotificationBuilder(Context context)
if (BuildCompat.isAtLeastO()) {
createDefaultNotificationChannel(context);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setChannelId(DEFAULT_CHANNEL_ID);
NotificationCompat.Builder builder
= new NotificationCompat.Builder(context, DEFAULT_CHANNEL_ID);
return builder;
}

Expand Down
49 changes: 43 additions & 6 deletions app/src/main/java/com/afwsamples/testdpc/common/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
package com.afwsamples.testdpc.common;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.app.admin.DevicePolicyManager;
import android.content.ActivityNotFoundException;
Expand All @@ -30,12 +27,11 @@
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;
import android.support.v14.preference.PreferenceFragment;
import android.support.v4.os.BuildCompat;
import android.support.v7.app.NotificationCompat;
import android.text.format.DateUtils;
import android.util.Log;
import android.widget.ImageView;
Expand All @@ -56,7 +52,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 +195,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
Loading

0 comments on commit d252de9

Please sign in to comment.