Skip to content

Commit d252de9

Browse files
committed
Merge /usr/local/google/home/ascull/projects/ub-testdpc-oc-release/vendor/unbundled_google/packages/TestDPC into new-version
2 parents 18cea73 + 31d6f9d commit d252de9

13 files changed

+213
-24
lines changed

app/build.gradle

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ ext {
77
// exactly 1 digit
88
versionMinor = 0
99
// exactly 2 digits
10-
versionBuild = 05
10+
versionBuild = 06
1111
}
1212

1313
android {
14-
compileSdkVersion 26
15-
buildToolsVersion '26.0.0'
14+
compileSdkVersion 27
15+
buildToolsVersion "27.0.1"
1616

1717
defaultConfig {
1818
applicationId "com.afwsamples.testdpc"
1919
minSdkVersion 21
20-
targetSdkVersion 26
20+
targetSdkVersion 27
2121
versionCode versionMajor * 1000 + versionMinor * 100 + versionBuild
2222
versionName "${versionMajor}.${versionMinor}.${versionBuild}"
2323
}
@@ -61,9 +61,9 @@ android {
6161
}
6262

6363
dependencies {
64-
api 'com.android.support:preference-v14:26.+'
65-
api 'com.android.support:recyclerview-v7:26.+'
66-
api 'com.android.support:support-v13:26.+'
64+
api 'com.android.support:preference-v14:27.0.2'
65+
api 'com.android.support:recyclerview-v7:27.0.2'
66+
api 'com.android.support:support-v13:27.0.2'
6767
api 'com.google.android.gms:play-services-safetynet:+'
6868
api(name:'setup-wizard-lib-platform-release', ext:'aar')
6969
}

app/src/main/java/com/afwsamples/testdpc/DeviceAdminReceiver.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.afwsamples.testdpc;
1818

1919
import android.annotation.TargetApi;
20-
import android.app.Notification;
2120
import android.app.NotificationManager;
2221
import android.app.PendingIntent;
2322
import android.app.admin.DevicePolicyManager;
@@ -33,13 +32,12 @@
3332
import android.os.UserHandle;
3433
import android.os.UserManager;
3534
import android.preference.PreferenceManager;
36-
import android.support.v7.app.NotificationCompat;
35+
import android.support.v4.app.NotificationCompat;
3736
import android.text.TextUtils;
3837
import android.util.Log;
3938
import android.widget.Toast;
4039

4140
import com.afwsamples.testdpc.common.NotificationUtil;
42-
import com.afwsamples.testdpc.common.Util;
4341
import com.afwsamples.testdpc.provision.PostProvisioningTask;
4442

4543
import java.io.BufferedReader;

app/src/main/java/com/afwsamples/testdpc/PackageMonitorReceiver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
import android.content.BroadcastReceiver;
66
import android.content.Context;
77
import android.content.Intent;
8-
import android.support.v7.app.NotificationCompat;
9-
import android.support.v7.app.NotificationCompat.Builder;
8+
import android.support.v4.app.NotificationCompat;
109
import android.text.TextUtils;
1110

1211
import com.afwsamples.testdpc.common.NotificationUtil;
@@ -37,6 +36,7 @@ public void onReceive(Context context, Intent intent) {
3736
.setContentText(notificationBody)
3837
.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationBody))
3938
.setDefaults(Notification.DEFAULT_LIGHTS)
39+
.setOnlyAlertOnce(true)
4040
.build();
4141
NotificationManager notificationManager =
4242
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

app/src/main/java/com/afwsamples/testdpc/common/NotificationUtil.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import android.support.annotation.RequiresApi;
99
import android.support.annotation.StringRes;
1010
import android.support.v4.os.BuildCompat;
11-
import android.support.v7.app.NotificationCompat;
11+
import android.support.v4.app.NotificationCompat;
1212

1313
import com.afwsamples.testdpc.R;
1414

@@ -37,8 +37,8 @@ public static NotificationCompat.Builder getNotificationBuilder(Context context)
3737
if (BuildCompat.isAtLeastO()) {
3838
createDefaultNotificationChannel(context);
3939
}
40-
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
41-
builder.setChannelId(DEFAULT_CHANNEL_ID);
40+
NotificationCompat.Builder builder
41+
= new NotificationCompat.Builder(context, DEFAULT_CHANNEL_ID);
4242
return builder;
4343
}
4444

app/src/main/java/com/afwsamples/testdpc/common/Util.java

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
package com.afwsamples.testdpc.common;
1818

1919
import android.annotation.TargetApi;
20-
import android.app.Notification;
21-
import android.app.NotificationChannel;
22-
import android.app.NotificationManager;
2320
import android.app.Service;
2421
import android.app.admin.DevicePolicyManager;
2522
import android.content.ActivityNotFoundException;
@@ -30,12 +27,11 @@
3027
import android.net.Uri;
3128
import android.os.Build;
3229
import android.os.Build.VERSION_CODES;
30+
import android.os.Bundle;
3331
import android.os.UserHandle;
3432
import android.os.UserManager;
35-
import android.support.annotation.RequiresApi;
3633
import android.support.v14.preference.PreferenceFragment;
3734
import android.support.v4.os.BuildCompat;
38-
import android.support.v7.app.NotificationCompat;
3935
import android.text.format.DateUtils;
4036
import android.util.Log;
4137
import android.widget.ImageView;
@@ -56,7 +52,12 @@
5652
*/
5753
public class Util {
5854
private static final String TAG = "Util";
59-
private static final int DEFAULT_BUFFER_SIZE = 4096;
55+
private static final int DEFAULT_BUFFER_SIZE = 4096;
56+
57+
private static final String BROADCAST_ACTION_FRP_CONFIG_CHANGED =
58+
"com.google.android.gms.auth.FRP_CONFIG_CHANGED";
59+
private static final String GMSCORE_PACKAGE = "com.google.android.gms";
60+
private static final String PERSISTENT_DEVICE_OWNER_STATE = "persistentDeviceOwnerState";
6061

6162
/**
6263
* Format a friendly datetime for the current locale according to device policy documentation.
@@ -194,6 +195,42 @@ public static boolean installCaCertificate(InputStream certificateInputStream,
194195
return false;
195196
}
196197

198+
/**
199+
* Returns the persistent device owner state which has been set by the device owner as an app
200+
* restriction on GmsCore or null if there is no such restriction set.
201+
*/
202+
@TargetApi(VERSION_CODES.O)
203+
public static String getPersistentDoStateFromApplicationRestriction(
204+
DevicePolicyManager dpm, ComponentName admin) {
205+
Bundle restrictions = dpm.getApplicationRestrictions(admin, GMSCORE_PACKAGE);
206+
return restrictions.getString(PERSISTENT_DEVICE_OWNER_STATE);
207+
}
208+
209+
/**
210+
* Sets the persistent device owner state by setting a special app restriction on GmsCore and
211+
* notifies GmsCore about the change by sending a broadcast.
212+
*
213+
* @param state The device owner state to be preserved across factory resets. If null, the
214+
* persistent device owner state and the corresponding restiction are cleared.
215+
*/
216+
@TargetApi(VERSION_CODES.O)
217+
public static void setPersistentDoStateWithApplicationRestriction(
218+
Context context, DevicePolicyManager dpm, ComponentName admin, String state) {
219+
Bundle restrictions = dpm.getApplicationRestrictions(admin, GMSCORE_PACKAGE);
220+
if (state == null) {
221+
// Clear the restriction
222+
restrictions.remove(PERSISTENT_DEVICE_OWNER_STATE);
223+
} else {
224+
// Set the restriction
225+
restrictions.putString(PERSISTENT_DEVICE_OWNER_STATE, state);
226+
}
227+
dpm.setApplicationRestrictions(admin, GMSCORE_PACKAGE, restrictions);
228+
Intent broadcastIntent = new Intent(BROADCAST_ACTION_FRP_CONFIG_CHANGED);
229+
broadcastIntent.setPackage(GMSCORE_PACKAGE);
230+
broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
231+
context.sendBroadcast(broadcastIntent);
232+
}
233+
197234
private static DevicePolicyManager getDevicePolicyManager(Context context) {
198235
return (DevicePolicyManager)context.getSystemService(Service.DEVICE_POLICY_SERVICE);
199236
}

app/src/main/java/com/afwsamples/testdpc/common/preference/DpcPreferenceHelper.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,6 @@ public class DpcPreferenceHelper {
9393
* not yet assigned.
9494
*/
9595
private int getDeviceSdkInt() {
96-
if (BuildCompat.isAtLeastO()) {
97-
return Build.VERSION_CODES.O;
98-
}
9996
return Build.VERSION.SDK_INT;
10097
}
10198

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.afwsamples.testdpc.policy;
2+
3+
import android.annotation.TargetApi;
4+
import android.app.Fragment;
5+
import android.app.admin.DevicePolicyManager;
6+
import android.content.ComponentName;
7+
import android.content.Context;
8+
import android.content.Intent;
9+
import android.os.Build;
10+
import android.os.Bundle;
11+
import android.view.LayoutInflater;
12+
import android.view.View;
13+
import android.view.ViewGroup;
14+
import android.widget.Button;
15+
import android.widget.EditText;
16+
17+
import com.afwsamples.testdpc.DeviceAdminReceiver;
18+
import com.afwsamples.testdpc.R;
19+
import com.afwsamples.testdpc.common.Util;
20+
21+
/**
22+
* Allows the user to set a test persistent device owner state.
23+
*
24+
* <p>For manual testing of forced re-enrollment.
25+
*
26+
* <p>If there is a non-empty peristent device owner state, it will survive the next factory reset,
27+
* TestDPC will be re-installed automatically as device owner and the state will be passed to it
28+
* during the initial device setup.
29+
*/
30+
public class PersistentDeviceOwnerFragment extends Fragment implements View.OnClickListener {
31+
32+
private DevicePolicyManager mDpm;
33+
private ComponentName mAdminComponent;
34+
private EditText mStateEdit;
35+
36+
@Override
37+
public void onCreate(Bundle savedInstanceState) {
38+
super.onCreate(savedInstanceState);
39+
getActivity().getActionBar().setTitle(R.string.persistent_device_owner);
40+
mDpm = (DevicePolicyManager) getActivity().getSystemService(
41+
Context.DEVICE_POLICY_SERVICE);
42+
mAdminComponent = DeviceAdminReceiver.getComponentName(getActivity());
43+
}
44+
45+
@Override
46+
public View onCreateView(
47+
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
48+
View root = inflater.inflate(R.layout.persistent_device_owner_fragment, container, false);
49+
root.findViewById(R.id.clear_persistent_device_owner_button).setOnClickListener(this);
50+
root.findViewById(R.id.set_persistent_device_owner_button).setOnClickListener(this);
51+
mStateEdit = (EditText) root.findViewById(R.id.persistent_device_owner_state_edit);
52+
return root;
53+
}
54+
55+
@Override
56+
public void onClick(View view) {
57+
String message = null;
58+
switch (view.getId()) {
59+
case R.id.clear_persistent_device_owner_button:
60+
mStateEdit.getText().clear();
61+
Util.setPersistentDoStateWithApplicationRestriction(
62+
getActivity(), mDpm, mAdminComponent, null);
63+
break;
64+
case R.id.set_persistent_device_owner_button:
65+
Util.setPersistentDoStateWithApplicationRestriction(
66+
getActivity(), mDpm, mAdminComponent, mStateEdit.getText().toString());
67+
break;
68+
}
69+
}
70+
71+
@Override
72+
public void onResume() {
73+
super.onResume();
74+
String state = Util.getPersistentDoStateFromApplicationRestriction(mDpm, mAdminComponent);
75+
mStateEdit.setText(state == null ? "" : state);
76+
}
77+
}

app/src/main/java/com/afwsamples/testdpc/policy/PolicyManagementFragment.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
298298
private static final String UNHIDE_APPS_KEY = "unhide_apps";
299299
private static final String UNSUSPEND_APPS_KEY = "unsuspend_apps";
300300
private static final String WIPE_DATA_KEY = "wipe_data";
301+
private static final String PERSISTENT_DEVICE_OWNER_KEY = "persistent_device_owner";
301302
private static final String CREATE_WIFI_CONFIGURATION_KEY = "create_wifi_configuration";
302303
private static final String CREATE_EAP_TLS_WIFI_CONFIGURATION_KEY
303304
= "create_eap_tls_wifi_configuration";
@@ -409,6 +410,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
409410
STAY_ON_WHILE_PLUGGED_IN);
410411
mStayOnWhilePluggedInSwitchPreference.setOnPreferenceChangeListener(this);
411412
findPreference(WIPE_DATA_KEY).setOnPreferenceClickListener(this);
413+
findPreference(PERSISTENT_DEVICE_OWNER_KEY).setOnPreferenceClickListener(this);
412414
findPreference(REMOVE_DEVICE_OWNER_KEY).setOnPreferenceClickListener(this);
413415
mEnableBackupServicePreference = (SwitchPreference) findPreference(ENABLE_BACKUP_SERVICE);
414416
mEnableBackupServicePreference.setOnPreferenceChangeListener(this);
@@ -482,6 +484,10 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
482484
SET_AUTO_TIME_REQUIRED_KEY);
483485
mSetAutoTimeRequiredPreference.setOnPreferenceChangeListener(this);
484486

487+
mSetDeviceOrganizationNamePreference =
488+
(EditTextPreference) findPreference(SET_DEVICE_ORGANIZATION_NAME_KEY);
489+
mSetDeviceOrganizationNamePreference.setOnPreferenceChangeListener(this);
490+
485491
constrainSpecialCasePreferences();
486492

487493
maybeDisableLockTaskPreferences();
@@ -585,6 +591,9 @@ public void onPositiveButtonClicked(String[] lockTaskArray) {
585591
case WIPE_DATA_KEY:
586592
showWipeDataPrompt();
587593
return true;
594+
case PERSISTENT_DEVICE_OWNER_KEY:
595+
showFragment(new PersistentDeviceOwnerFragment());
596+
return true;
588597
case REMOVE_DEVICE_OWNER_KEY:
589598
showRemoveDeviceOwnerPrompt();
590599
return true;

app/src/main/java/com/afwsamples/testdpc/provision/PostProvisioningTask.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import android.util.Log;
3838

3939
import com.afwsamples.testdpc.AddAccountActivity;
40+
import com.afwsamples.testdpc.DeviceAdminReceiver;
4041
import com.afwsamples.testdpc.EnableDeviceOwnerActivity;
4142
import com.afwsamples.testdpc.EnableProfileActivity;
4243
import com.afwsamples.testdpc.common.LaunchIntentUtil;
@@ -66,6 +67,8 @@ public class PostProvisioningTask {
6667
"com.afwsamples.testdpc.SetupManagementLaunchActivity";
6768
private static final String POST_PROV_PREFS = "post_prov_prefs";
6869
private static final String KEY_POST_PROV_DONE = "key_post_prov_done";
70+
private static final String KEY_DEVICE_OWNER_STATE =
71+
"android.app.extra.PERSISTENT_DEVICE_OWNER_STATE";
6972

7073
private final Context mContext;
7174
private final DevicePolicyManager mDevicePolicyManager;
@@ -98,6 +101,18 @@ public boolean performPostProvisioningOperations(Intent intent) {
98101
maybeSetAffiliationIds(extras);
99102
}
100103

104+
// If TestDPC asked GmsCore to store its state in the FRP area before factory reset, the
105+
// state will be handed over to it during the next device setup.
106+
if (BuildCompat.isAtLeastOMR1()
107+
&& extras != null
108+
&& extras.containsKey(KEY_DEVICE_OWNER_STATE)) {
109+
Util.setPersistentDoStateWithApplicationRestriction(
110+
mContext,
111+
mDevicePolicyManager,
112+
DeviceAdminReceiver.getComponentName(mContext),
113+
extras.getString(KEY_DEVICE_OWNER_STATE));
114+
}
115+
101116
// Hide the setup launcher when this app is the admin
102117
mContext.getPackageManager().setComponentEnabledSetting(
103118
new ComponentName(mContext, SETUP_MANAGEMENT_LAUNCH_ACTIVITY),
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright (C) 2017 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
18+
android:layout_width="match_parent"
19+
android:layout_height="match_parent"
20+
android:padding="10dp"
21+
android:orientation="vertical">
22+
<EditText android:id="@+id/persistent_device_owner_state_edit"
23+
android:layout_height="wrap_content"
24+
android:layout_width="match_parent"
25+
android:lines="5"
26+
android:layout_gravity="center"
27+
android:hint="@string/persistent_device_owner_state_hint"/>
28+
<LinearLayout
29+
android:layout_height="wrap_content"
30+
android:layout_width="wrap_content"
31+
android:layout_gravity="center"
32+
android:orientation="horizontal">
33+
<Button android:id="@+id/clear_persistent_device_owner_button"
34+
android:layout_width="wrap_content"
35+
android:layout_height="wrap_content"
36+
android:text="@string/clear_persistent_device_owner_label"
37+
android:layout_margin="8dp"
38+
android:visibility="visible"/>
39+
<Button android:id="@+id/set_persistent_device_owner_button"
40+
android:layout_width="wrap_content"
41+
android:layout_height="wrap_content"
42+
android:text="@string/set_persistent_device_owner_label"
43+
android:layout_margin="8dp"
44+
android:visibility="visible"/>
45+
</LinearLayout>
46+
</LinearLayout>

0 commit comments

Comments
 (0)