Rubin Xu | 8027a4f | 2015-03-10 17:52:37 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.app.admin; |
| 18 | |
| 19 | import android.annotation.IntDef; |
| 20 | import android.os.PersistableBundle; |
| 21 | |
| 22 | import java.lang.annotation.Retention; |
| 23 | import java.lang.annotation.RetentionPolicy; |
| 24 | |
| 25 | /** |
Rubin Xu | 5faad8e | 2015-04-20 17:43:48 +0100 | [diff] [blame] | 26 | * A class that represents a local system update policy set by the device owner. |
Rubin Xu | 8027a4f | 2015-03-10 17:52:37 +0000 | [diff] [blame] | 27 | * |
Rubin Xu | 5faad8e | 2015-04-20 17:43:48 +0100 | [diff] [blame] | 28 | * @see DevicePolicyManager#setSystemUpdatePolicy |
| 29 | * @see DevicePolicyManager#getSystemUpdatePolicy |
Rubin Xu | 8027a4f | 2015-03-10 17:52:37 +0000 | [diff] [blame] | 30 | */ |
Rubin Xu | 5faad8e | 2015-04-20 17:43:48 +0100 | [diff] [blame] | 31 | public class SystemUpdatePolicy { |
Rubin Xu | 8027a4f | 2015-03-10 17:52:37 +0000 | [diff] [blame] | 32 | |
| 33 | /** @hide */ |
| 34 | @IntDef({ |
| 35 | TYPE_INSTALL_AUTOMATIC, |
| 36 | TYPE_INSTALL_WINDOWED, |
| 37 | TYPE_POSTPONE}) |
| 38 | @Retention(RetentionPolicy.SOURCE) |
Rubin Xu | 5faad8e | 2015-04-20 17:43:48 +0100 | [diff] [blame] | 39 | @interface SystemUpdatePolicyType {} |
Rubin Xu | 8027a4f | 2015-03-10 17:52:37 +0000 | [diff] [blame] | 40 | |
| 41 | /** |
Rubin Xu | 5faad8e | 2015-04-20 17:43:48 +0100 | [diff] [blame] | 42 | * Install system update automatically as soon as one is available. |
Rubin Xu | 8027a4f | 2015-03-10 17:52:37 +0000 | [diff] [blame] | 43 | */ |
| 44 | public static final int TYPE_INSTALL_AUTOMATIC = 1; |
| 45 | |
| 46 | /** |
Rubin Xu | 5faad8e | 2015-04-20 17:43:48 +0100 | [diff] [blame] | 47 | * 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 Xu | 8027a4f | 2015-03-10 17:52:37 +0000 | [diff] [blame] | 52 | */ |
| 53 | public static final int TYPE_INSTALL_WINDOWED = 2; |
| 54 | |
| 55 | /** |
Rubin Xu | 5faad8e | 2015-04-20 17:43:48 +0100 | [diff] [blame] | 56 | * 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 Xu | 8027a4f | 2015-03-10 17:52:37 +0000 | [diff] [blame] | 60 | */ |
| 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 Xu | 5faad8e | 2015-04-20 17:43:48 +0100 | [diff] [blame] | 69 | public SystemUpdatePolicy() { |
Rubin Xu | 8027a4f | 2015-03-10 17:52:37 +0000 | [diff] [blame] | 70 | mPolicy = new PersistableBundle(); |
| 71 | } |
| 72 | |
| 73 | /** |
Rubin Xu | 5faad8e | 2015-04-20 17:43:48 +0100 | [diff] [blame] | 74 | * Construct an SystemUpdatePolicy object from a bundle. |
Rubin Xu | 8027a4f | 2015-03-10 17:52:37 +0000 | [diff] [blame] | 75 | * @hide |
| 76 | */ |
Rubin Xu | 5faad8e | 2015-04-20 17:43:48 +0100 | [diff] [blame] | 77 | public SystemUpdatePolicy(PersistableBundle in) { |
Rubin Xu | 8027a4f | 2015-03-10 17:52:37 +0000 | [diff] [blame] | 78 | 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 Xu | 5faad8e | 2015-04-20 17:43:48 +0100 | [diff] [blame] | 90 | * Set the policy to: install update automatically as soon as one is available. |
| 91 | * |
| 92 | * @see #TYPE_INSTALL_AUTOMATIC |
Rubin Xu | 8027a4f | 2015-03-10 17:52:37 +0000 | [diff] [blame] | 93 | */ |
| 94 | public void setAutomaticInstallPolicy() { |
| 95 | mPolicy.clear(); |
| 96 | mPolicy.putInt(KEY_POLICY_TYPE, TYPE_INSTALL_AUTOMATIC); |
| 97 | } |
| 98 | |
| 99 | /** |
Rubin Xu | 5faad8e | 2015-04-20 17:43:48 +0100 | [diff] [blame] | 100 | * Set the policy to: new system update will only be installed automatically when the system |
Rubin Xu | 8027a4f | 2015-03-10 17:52:37 +0000 | [diff] [blame] | 101 | * clock is inside a daily maintenance window. If the start and end times are the same, the |
Rubin Xu | 5faad8e | 2015-04-20 17:43:48 +0100 | [diff] [blame] | 102 | * 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 Xu | 8027a4f | 2015-03-10 17:52:37 +0000 | [diff] [blame] | 107 | * |
| 108 | * @param startTime the start of the maintenance window, measured as the number of minutes from |
Rubin Xu | 5faad8e | 2015-04-20 17:43:48 +0100 | [diff] [blame] | 109 | * midnight in the device's local time. Must be in the range of [0, 1440). |
Rubin Xu | 8027a4f | 2015-03-10 17:52:37 +0000 | [diff] [blame] | 110 | * @param endTime the end of the maintenance window, measured as the number of minutes from |
Rubin Xu | 5faad8e | 2015-04-20 17:43:48 +0100 | [diff] [blame] | 111 | * midnight in the device's local time. Must be in the range of [0, 1440). |
| 112 | * @see #TYPE_INSTALL_WINDOWED |
Rubin Xu | 8027a4f | 2015-03-10 17:52:37 +0000 | [diff] [blame] | 113 | */ |
| 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 Xu | 5faad8e | 2015-04-20 17:43:48 +0100 | [diff] [blame] | 125 | * 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 Xu | 8027a4f | 2015-03-10 17:52:37 +0000 | [diff] [blame] | 129 | */ |
| 130 | public void setPostponeInstallPolicy() { |
| 131 | mPolicy.clear(); |
| 132 | mPolicy.putInt(KEY_POLICY_TYPE, TYPE_POSTPONE); |
| 133 | } |
| 134 | |
| 135 | /** |
Rubin Xu | 5faad8e | 2015-04-20 17:43:48 +0100 | [diff] [blame] | 136 | * Returns the type of system update policy. |
Rubin Xu | 8027a4f | 2015-03-10 17:52:37 +0000 | [diff] [blame] | 137 | * |
| 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 Xu | 5faad8e | 2015-04-20 17:43:48 +0100 | [diff] [blame] | 141 | @SystemUpdatePolicyType |
Rubin Xu | 8027a4f | 2015-03-10 17:52:37 +0000 | [diff] [blame] | 142 | 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 Xu | 5faad8e | 2015-04-20 17:43:48 +0100 | [diff] [blame] | 180 | * Exception thrown by {@link SystemUpdatePolicy#setWindowedInstallPolicy(int, int)} in case the |
Rubin Xu | 8027a4f | 2015-03-10 17:52:37 +0000 | [diff] [blame] | 181 | * specified window is invalid. |
| 182 | */ |
| 183 | public static class InvalidWindowException extends Exception { |
| 184 | public InvalidWindowException(String reason) { |
| 185 | super(reason); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |