File: FullyManagedDeviceProvisioningParams.java

package info (click to toggle)
android-platform-frameworks-base 1%3A14~beta1-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 326,092 kB
  • sloc: java: 2,032,373; xml: 343,016; cpp: 304,181; python: 3,683; ansic: 2,090; sh: 1,871; makefile: 117; sed: 19
file content (411 lines) | stat: -rw-r--r-- 15,243 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * Copyright (C) 2021 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 android.app.admin;

import static java.util.Objects.requireNonNull;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.content.ComponentName;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.PersistableBundle;
import android.provider.Settings;
import android.stats.devicepolicy.DevicePolicyEnums;

import java.util.Locale;

/**
 * Params required to provision a fully managed device, see
 * {@link DevicePolicyManager#provisionFullyManagedDevice}.
 *
 * @hide
 */
@SystemApi
public final class FullyManagedDeviceProvisioningParams implements Parcelable {
    private static final String LEAVE_ALL_SYSTEM_APPS_ENABLED_PARAM =
            "LEAVE_ALL_SYSTEM_APPS_ENABLED";
    private static final String CAN_DEVICE_OWNER_GRANT_SENSOR_PERMISSIONS_PARAM =
            "CAN_DEVICE_OWNER_GRANT_SENSOR_PERMISSIONS";
    private static final String TIME_ZONE_PROVIDED_PARAM = "TIME_ZONE_PROVIDED";
    private static final String LOCALE_PROVIDED_PARAM = "LOCALE_PROVIDED";
    private static final String DEMO_DEVICE = "DEMO_DEVICE";

    @NonNull private final ComponentName mDeviceAdminComponentName;
    @NonNull private final String mOwnerName;
    private final boolean mLeaveAllSystemAppsEnabled;
    @Nullable private final String mTimeZone;
    private final long mLocalTime;
    @SuppressLint("UseIcu")
    @Nullable private final Locale mLocale;
    private final boolean mDeviceOwnerCanGrantSensorsPermissions;
    @NonNull private final PersistableBundle mAdminExtras;
    private final boolean mDemoDevice;


    private FullyManagedDeviceProvisioningParams(
            @NonNull ComponentName deviceAdminComponentName,
            @NonNull String ownerName,
            boolean leaveAllSystemAppsEnabled,
            @Nullable String timeZone,
            long localTime,
            @Nullable @SuppressLint("UseIcu") Locale locale,
            boolean deviceOwnerCanGrantSensorsPermissions,
            @NonNull PersistableBundle adminExtras,
            boolean demoDevice) {
        this.mDeviceAdminComponentName = requireNonNull(deviceAdminComponentName);
        this.mOwnerName = requireNonNull(ownerName);
        this.mLeaveAllSystemAppsEnabled = leaveAllSystemAppsEnabled;
        this.mTimeZone = timeZone;
        this.mLocalTime = localTime;
        this.mLocale = locale;
        this.mDeviceOwnerCanGrantSensorsPermissions =
                deviceOwnerCanGrantSensorsPermissions;
        this.mAdminExtras = adminExtras;
        this.mDemoDevice = demoDevice;
    }

    private FullyManagedDeviceProvisioningParams(
            @NonNull ComponentName deviceAdminComponentName,
            @NonNull String ownerName,
            boolean leaveAllSystemAppsEnabled,
            @Nullable String timeZone,
            long localTime,
            @Nullable String localeStr,
            boolean deviceOwnerCanGrantSensorsPermissions,
            @Nullable PersistableBundle adminExtras,
            boolean demoDevice) {
        this(deviceAdminComponentName,
                ownerName,
                leaveAllSystemAppsEnabled,
                timeZone,
                localTime,
                getLocale(localeStr),
                deviceOwnerCanGrantSensorsPermissions,
                adminExtras,
                demoDevice);
    }

    @Nullable
    private static Locale getLocale(String localeStr) {
        return localeStr == null ? null : Locale.forLanguageTag(localeStr);
    }

    /**
     * Returns the device owner's {@link ComponentName}.
     */
    @NonNull
    public ComponentName getDeviceAdminComponentName() {
        return mDeviceAdminComponentName;
    }

    /**
     * Returns the device owner's name.
     */
    @NonNull
    public String getOwnerName() {
        return mOwnerName;
    }

    /**
     * Returns {@code true} if system apps should be left enabled after provisioning.
     */
    public boolean isLeaveAllSystemAppsEnabled() {
        return mLeaveAllSystemAppsEnabled;
    }

    /**
     * If set, it returns the time zone to set for the device after provisioning, otherwise returns
     * {@code null};
     */
    @Nullable
    public String getTimeZone() {
        return mTimeZone;
    }

    /**
     * If set, it returns the local time to set for the device after provisioning, otherwise returns
     * 0.
     */
    public long getLocalTime() {
        return mLocalTime;
    }

    /**
     * If set, it returns the {@link Locale} to set for the device after provisioning, otherwise
     * returns {@code null}.
     */
    @Nullable
    public @SuppressLint("UseIcu") Locale getLocale() {
        return mLocale;
    }

    /**
     * @return true if the device owner can control sensor-related permission grants, false
     * if the device owner has opted out of it.
     */
    public boolean canDeviceOwnerGrantSensorsPermissions() {
        return mDeviceOwnerCanGrantSensorsPermissions;
    }

    /**
     * Returns a copy of the admin extras bundle.
     *
     * @see DevicePolicyManager#EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE
     */
    public @NonNull PersistableBundle getAdminExtras() {
        return new PersistableBundle(mAdminExtras);
    }

    /**
     * @return true if this device is being setup as a retail demo device, see
     * {@link Settings.Global#DEVICE_DEMO_MODE}.
     */
    public boolean isDemoDevice() {
        return mDemoDevice;
    }

    /**
     * Logs the provisioning params using {@link DevicePolicyEventLogger}.
     *
     * @hide
     */
    public void logParams(@NonNull String callerPackage) {
        requireNonNull(callerPackage);

        logParam(callerPackage, LEAVE_ALL_SYSTEM_APPS_ENABLED_PARAM, mLeaveAllSystemAppsEnabled);
        logParam(callerPackage, CAN_DEVICE_OWNER_GRANT_SENSOR_PERMISSIONS_PARAM,
                mDeviceOwnerCanGrantSensorsPermissions);
        logParam(callerPackage, TIME_ZONE_PROVIDED_PARAM, /* value= */ mTimeZone != null);
        logParam(callerPackage, LOCALE_PROVIDED_PARAM, /* value= */ mLocale != null);
        logParam(callerPackage, DEMO_DEVICE, mDemoDevice);
    }

    private void logParam(String callerPackage, String param, boolean value) {
        DevicePolicyEventLogger
                .createEvent(DevicePolicyEnums.PLATFORM_PROVISIONING_PARAM)
                .setStrings(callerPackage)
                .setAdmin(mDeviceAdminComponentName)
                .setStrings(param)
                .setBoolean(value)
                .write();
    }

    /**
     * Builder class for {@link FullyManagedDeviceProvisioningParams} objects.
     */
    public static final class Builder {
        @NonNull private final ComponentName mDeviceAdminComponentName;
        @NonNull private final String mOwnerName;
        private boolean mLeaveAllSystemAppsEnabled;
        @Nullable private String mTimeZone;
        private long mLocalTime;
        @SuppressLint("UseIcu")
        @Nullable private Locale mLocale;
        // Default to allowing control over sensor permission grants.
        boolean mDeviceOwnerCanGrantSensorsPermissions = true;
        @NonNull private PersistableBundle mAdminExtras;
        // Default is normal user devices
        boolean mDemoDevice = false;


        /**
         * Initialize a new {@link Builder} to construct a
         * {@link FullyManagedDeviceProvisioningParams}.
         * <p>
         * See {@link DevicePolicyManager#provisionFullyManagedDevice}
         *
         * @param deviceAdminComponentName The admin {@link ComponentName} to be set as the device
         * owner.
         * @param ownerName The name of the device owner.
         *
         * @throws NullPointerException if {@code deviceAdminComponentName} or
         * {@code ownerName} are null.
         */
        public Builder(
                @NonNull ComponentName deviceAdminComponentName, @NonNull String ownerName) {
            this.mDeviceAdminComponentName = requireNonNull(deviceAdminComponentName);
            this.mOwnerName = requireNonNull(ownerName);
        }

        /**
         * Sets whether non-required system apps should be installed on
         * the created profile when
         * {@link DevicePolicyManager#provisionFullyManagedDevice}
         * is called. Defaults to {@code false} if not set.
         */
        @NonNull
        public Builder setLeaveAllSystemAppsEnabled(boolean leaveAllSystemAppsEnabled) {
            this.mLeaveAllSystemAppsEnabled = leaveAllSystemAppsEnabled;
            return this;
        }

        /**
         * Sets {@code timeZone} on the device. If not set or set to {@code null},
         * {@link DevicePolicyManager#provisionFullyManagedDevice} will not set a timezone
         */
        @NonNull
        public Builder setTimeZone(@Nullable String timeZone) {
            this.mTimeZone = timeZone;
            return this;
        }

        /**
         * Sets {@code localTime} on the device, If not set or set to
         * {@code 0}, {@link DevicePolicyManager#provisionFullyManagedDevice} will not set a
         * local time.
         */
        @NonNull
        public Builder setLocalTime(long localTime) {
            this.mLocalTime = localTime;
            return this;
        }

        /**
         * Sets {@link Locale} on the device, If not set or set to {@code null},
         * {@link DevicePolicyManager#provisionFullyManagedDevice} will not set a locale.
         */
        @NonNull
        public Builder setLocale(@SuppressLint("UseIcu") @Nullable Locale locale) {
            this.mLocale = locale;
            return this;
        }

        /**
         * Marks that the Device Owner may grant permissions related to device sensors.
         * See {@link DevicePolicyManager#EXTRA_PROVISIONING_SENSORS_PERMISSION_GRANT_OPT_OUT}.
         */
        @NonNull
        public Builder setCanDeviceOwnerGrantSensorsPermissions(boolean mayGrant) {
            mDeviceOwnerCanGrantSensorsPermissions = mayGrant;
            return this;
        }

        /**
         * Sets a {@link PersistableBundle} that contains admin-specific extras.
         */
        @NonNull
        //TODO(b/235783053) The adminExtras parameter is actually @Nullable.
        public Builder setAdminExtras(@NonNull PersistableBundle adminExtras) {
            mAdminExtras = adminExtras != null
                    ? new PersistableBundle(adminExtras)
                    : new PersistableBundle();
            return this;
        }

        /**
         * Marks the device as a demo device, see {@link Settings.Global#DEVICE_DEMO_MODE}. The
         * default value if unset is {@code false}.
         */
        @NonNull
        public Builder setDemoDevice(boolean demoDevice) {
            this.mDemoDevice = demoDevice;
            return this;
        }

        /**
         * Combines all of the attributes that have been set on this {@code Builder}
         *
         * @return a new {@link FullyManagedDeviceProvisioningParams} object.
         */
        @NonNull
        public FullyManagedDeviceProvisioningParams build() {
            return new FullyManagedDeviceProvisioningParams(
                    mDeviceAdminComponentName,
                    mOwnerName,
                    mLeaveAllSystemAppsEnabled,
                    mTimeZone,
                    mLocalTime,
                    mLocale,
                    mDeviceOwnerCanGrantSensorsPermissions,
                    mAdminExtras != null ? mAdminExtras : new PersistableBundle(),
                    mDemoDevice);
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    /**
     * @hide
     */
    @Override
    public String toString() {
        return "FullyManagedDeviceProvisioningParams{"
                + "mDeviceAdminComponentName=" + mDeviceAdminComponentName
                + ", mOwnerName=" + mOwnerName
                + ", mLeaveAllSystemAppsEnabled=" + mLeaveAllSystemAppsEnabled
                + ", mTimeZone=" + (mTimeZone == null ? "null" : mTimeZone)
                + ", mLocalTime=" + mLocalTime
                + ", mLocale=" + (mLocale == null ? "null" : mLocale)
                + ", mDeviceOwnerCanGrantSensorsPermissions="
                + mDeviceOwnerCanGrantSensorsPermissions
                + ", mAdminExtras=" + mAdminExtras
                + ", mDemoDevice=" + mDemoDevice
                + '}';
    }

    @Override
    public void writeToParcel(@NonNull Parcel dest, @Nullable int flags) {
        dest.writeTypedObject(mDeviceAdminComponentName, flags);
        dest.writeString(mOwnerName);
        dest.writeBoolean(mLeaveAllSystemAppsEnabled);
        dest.writeString(mTimeZone);
        dest.writeLong(mLocalTime);
        dest.writeString(mLocale == null ? null : mLocale.toLanguageTag());
        dest.writeBoolean(mDeviceOwnerCanGrantSensorsPermissions);
        dest.writePersistableBundle(mAdminExtras);
        dest.writeBoolean(mDemoDevice);
    }

    @NonNull
    public static final Creator<FullyManagedDeviceProvisioningParams> CREATOR =
            new Creator<FullyManagedDeviceProvisioningParams>() {
                @Override
                public FullyManagedDeviceProvisioningParams createFromParcel(Parcel in) {
                    ComponentName componentName = in.readTypedObject(ComponentName.CREATOR);
                    String ownerName = in.readString();
                    boolean leaveAllSystemAppsEnabled = in.readBoolean();
                    String timeZone = in.readString();
                    long localtime = in.readLong();
                    String locale = in.readString();
                    boolean deviceOwnerCanGrantSensorsPermissions = in.readBoolean();
                    PersistableBundle adminExtras = in.readPersistableBundle();
                    boolean demoDevice = in.readBoolean();

                    return new FullyManagedDeviceProvisioningParams(
                            componentName,
                            ownerName,
                            leaveAllSystemAppsEnabled,
                            timeZone,
                            localtime,
                            locale,
                            deviceOwnerCanGrantSensorsPermissions,
                            adminExtras,
                            demoDevice);
                }

                @Override
                public FullyManagedDeviceProvisioningParams[] newArray(int size) {
                    return new FullyManagedDeviceProvisioningParams[size];
                }
            };
}