blob: de56cd0e86ec7389528fd4a7dfec890d2f0c162d [file] [log] [blame]
Rubin Xu8027a4f2015-03-10 17:52:37 +00001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app.admin;
18
19import android.annotation.IntDef;
20import android.os.PersistableBundle;
21
22import java.lang.annotation.Retention;
23import java.lang.annotation.RetentionPolicy;
24
25/**
Rubin Xu5faad8e2015-04-20 17:43:48 +010026 * A class that represents a local system update policy set by the device owner.
Rubin Xu8027a4f2015-03-10 17:52:37 +000027 *
Rubin Xu5faad8e2015-04-20 17:43:48 +010028 * @see DevicePolicyManager#setSystemUpdatePolicy
29 * @see DevicePolicyManager#getSystemUpdatePolicy
Rubin Xu8027a4f2015-03-10 17:52:37 +000030 */
Rubin Xu5faad8e2015-04-20 17:43:48 +010031public class SystemUpdatePolicy {
Rubin Xu8027a4f2015-03-10 17:52:37 +000032
33 /** @hide */
34 @IntDef({
35 TYPE_INSTALL_AUTOMATIC,
36 TYPE_INSTALL_WINDOWED,
37 TYPE_POSTPONE})
38 @Retention(RetentionPolicy.SOURCE)
Rubin Xu5faad8e2015-04-20 17:43:48 +010039 @interface SystemUpdatePolicyType {}
Rubin Xu8027a4f2015-03-10 17:52:37 +000040
41 /**
Rubin Xu5faad8e2015-04-20 17:43:48 +010042 * Install system update automatically as soon as one is available.
Rubin Xu8027a4f2015-03-10 17:52:37 +000043 */
44 public static final int TYPE_INSTALL_AUTOMATIC = 1;
45
46 /**
Rubin Xu5faad8e2015-04-20 17:43:48 +010047 * Install system update automatically within a daily maintenance window, for a maximum of 30
48 * days. After the expiration the policy will no longer be effective and the system should
49 * revert back to its normal behavior as if no policy were set. The only exception is
50 * {@link #TYPE_INSTALL_AUTOMATIC} which should still take effect to install system update
51 * immediately.
Rubin Xu8027a4f2015-03-10 17:52:37 +000052 */
53 public static final int TYPE_INSTALL_WINDOWED = 2;
54
55 /**
Rubin Xu5faad8e2015-04-20 17:43:48 +010056 * Incoming system update will be blocked for a maximum of 30 days, after which the system
57 * should revert back to its normal behavior as if no policy were set. The only exception is
58 * {@link #TYPE_INSTALL_AUTOMATIC} which should still take effect to install system update
59 * immediately.
Rubin Xu8027a4f2015-03-10 17:52:37 +000060 */
61 public static final int TYPE_POSTPONE = 3;
62
63 private static final String KEY_POLICY_TYPE = "policy_type";
64 private static final String KEY_INSTALL_WINDOW_START = "install_window_start";
65 private static final String KEY_INSTALL_WINDOW_END = "install_window_end";
66
67 private PersistableBundle mPolicy;
68
Rubin Xu5faad8e2015-04-20 17:43:48 +010069 public SystemUpdatePolicy() {
Rubin Xu8027a4f2015-03-10 17:52:37 +000070 mPolicy = new PersistableBundle();
71 }
72
73 /**
Rubin Xu5faad8e2015-04-20 17:43:48 +010074 * Construct an SystemUpdatePolicy object from a bundle.
Rubin Xu8027a4f2015-03-10 17:52:37 +000075 * @hide
76 */
Rubin Xu5faad8e2015-04-20 17:43:48 +010077 public SystemUpdatePolicy(PersistableBundle in) {
Rubin Xu8027a4f2015-03-10 17:52:37 +000078 mPolicy = new PersistableBundle(in);
79 }
80
81 /**
82 * Retrieve the underlying bundle where the policy is stored.
83 * @hide
84 */
85 public PersistableBundle getPolicyBundle() {
86 return new PersistableBundle(mPolicy);
87 }
88
89 /**
Rubin Xu5faad8e2015-04-20 17:43:48 +010090 * Set the policy to: install update automatically as soon as one is available.
91 *
92 * @see #TYPE_INSTALL_AUTOMATIC
Rubin Xu8027a4f2015-03-10 17:52:37 +000093 */
94 public void setAutomaticInstallPolicy() {
95 mPolicy.clear();
96 mPolicy.putInt(KEY_POLICY_TYPE, TYPE_INSTALL_AUTOMATIC);
97 }
98
99 /**
Rubin Xu5faad8e2015-04-20 17:43:48 +0100100 * Set the policy to: new system update will only be installed automatically when the system
Rubin Xu8027a4f2015-03-10 17:52:37 +0000101 * clock is inside a daily maintenance window. If the start and end times are the same, the
Rubin Xu5faad8e2015-04-20 17:43:48 +0100102 * window is considered to include the WHOLE 24 hours, that is, updates can install at any time.
103 * If the given window in invalid, a {@link SystemUpdatePolicy.InvalidWindowException} will be
104 * thrown. If start time is later than end time, the window is considered spanning midnight,
105 * i.e. end time donates a time on the next day. The maintenance window will last for 30 days,
106 * after which the system should revert back to its normal behavior as if no policy were set.
Rubin Xu8027a4f2015-03-10 17:52:37 +0000107 *
108 * @param startTime the start of the maintenance window, measured as the number of minutes from
Rubin Xu5faad8e2015-04-20 17:43:48 +0100109 * midnight in the device's local time. Must be in the range of [0, 1440).
Rubin Xu8027a4f2015-03-10 17:52:37 +0000110 * @param endTime the end of the maintenance window, measured as the number of minutes from
Rubin Xu5faad8e2015-04-20 17:43:48 +0100111 * midnight in the device's local time. Must be in the range of [0, 1440).
112 * @see #TYPE_INSTALL_WINDOWED
Rubin Xu8027a4f2015-03-10 17:52:37 +0000113 */
114 public void setWindowedInstallPolicy(int startTime, int endTime) throws InvalidWindowException{
115 if (startTime < 0 || startTime >= 1440 || endTime < 0 || endTime >= 1440) {
116 throw new InvalidWindowException("startTime and endTime must be inside [0, 1440)");
117 }
118 mPolicy.clear();
119 mPolicy.putInt(KEY_POLICY_TYPE, TYPE_INSTALL_WINDOWED);
120 mPolicy.putInt(KEY_INSTALL_WINDOW_START, startTime);
121 mPolicy.putInt(KEY_INSTALL_WINDOW_END, endTime);
122 }
123
124 /**
Rubin Xu5faad8e2015-04-20 17:43:48 +0100125 * Set the policy to: block installation for a maximum period of 30 days. After expiration the
126 * system should revert back to its normal behavior as if no policy were set.
127 *
128 * @see #TYPE_POSTPONE
Rubin Xu8027a4f2015-03-10 17:52:37 +0000129 */
130 public void setPostponeInstallPolicy() {
131 mPolicy.clear();
132 mPolicy.putInt(KEY_POLICY_TYPE, TYPE_POSTPONE);
133 }
134
135 /**
Rubin Xu5faad8e2015-04-20 17:43:48 +0100136 * Returns the type of system update policy.
Rubin Xu8027a4f2015-03-10 17:52:37 +0000137 *
138 * @return an integer, either one of {@link #TYPE_INSTALL_AUTOMATIC},
139 * {@link #TYPE_INSTALL_WINDOWED} and {@link #TYPE_POSTPONE}, or -1 if no policy has been set.
140 */
Rubin Xu5faad8e2015-04-20 17:43:48 +0100141 @SystemUpdatePolicyType
Rubin Xu8027a4f2015-03-10 17:52:37 +0000142 public int getPolicyType() {
143 return mPolicy.getInt(KEY_POLICY_TYPE, -1);
144 }
145
146 /**
147 * Get the start of the maintenance window.
148 *
149 * @return the start of the maintenance window measured as the number of minutes from midnight,
150 * or -1 if the policy does not have a maintenance window.
151 */
152 public int getInstallWindowStart() {
153 if (getPolicyType() == TYPE_INSTALL_WINDOWED) {
154 return mPolicy.getInt(KEY_INSTALL_WINDOW_START, -1);
155 } else {
156 return -1;
157 }
158 }
159
160 /**
161 * Get the end of the maintenance window.
162 *
163 * @return the end of the maintenance window measured as the number of minutes from midnight,
164 * or -1 if the policy does not have a maintenance window.
165 */
166 public int getInstallWindowEnd() {
167 if (getPolicyType() == TYPE_INSTALL_WINDOWED) {
168 return mPolicy.getInt(KEY_INSTALL_WINDOW_END, -1);
169 } else {
170 return -1;
171 }
172 }
173
174 @Override
175 public String toString() {
176 return mPolicy.toString();
177 }
178
179 /**
Rubin Xu5faad8e2015-04-20 17:43:48 +0100180 * Exception thrown by {@link SystemUpdatePolicy#setWindowedInstallPolicy(int, int)} in case the
Rubin Xu8027a4f2015-03-10 17:52:37 +0000181 * specified window is invalid.
182 */
183 public static class InvalidWindowException extends Exception {
184 public InvalidWindowException(String reason) {
185 super(reason);
186 }
187 }
188}
189