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 | /** |
| 26 | * A class that represents a local OTA policy set by the device owner. |
| 27 | * |
| 28 | * @see DevicePolicyManager#setOtaPolicy |
| 29 | * @see DevicePolicyManager#getOtaPolicy |
| 30 | */ |
| 31 | public class OtaPolicy { |
| 32 | |
| 33 | /** @hide */ |
| 34 | @IntDef({ |
| 35 | TYPE_INSTALL_AUTOMATIC, |
| 36 | TYPE_INSTALL_WINDOWED, |
| 37 | TYPE_POSTPONE}) |
| 38 | @Retention(RetentionPolicy.SOURCE) |
| 39 | @interface OtaPolicyType {} |
| 40 | |
| 41 | /** |
| 42 | * Install OTA update automatically as soon as one is available. |
| 43 | */ |
| 44 | public static final int TYPE_INSTALL_AUTOMATIC = 1; |
| 45 | |
| 46 | /** |
| 47 | * Install OTA update automatically within a daily maintenance window, for a maximum of two-week |
| 48 | * period. After that period the OTA will be installed automatically. |
| 49 | */ |
| 50 | public static final int TYPE_INSTALL_WINDOWED = 2; |
| 51 | |
| 52 | /** |
| 53 | * Incoming OTA will be blocked for a maximum of two weeks, after which it will be installed |
| 54 | * automatically. |
| 55 | */ |
| 56 | public static final int TYPE_POSTPONE = 3; |
| 57 | |
| 58 | private static final String KEY_POLICY_TYPE = "policy_type"; |
| 59 | private static final String KEY_INSTALL_WINDOW_START = "install_window_start"; |
| 60 | private static final String KEY_INSTALL_WINDOW_END = "install_window_end"; |
| 61 | |
| 62 | private PersistableBundle mPolicy; |
| 63 | |
| 64 | public OtaPolicy() { |
| 65 | mPolicy = new PersistableBundle(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Construct an OtaPolicy object from a bundle. |
| 70 | * @hide |
| 71 | */ |
| 72 | public OtaPolicy(PersistableBundle in) { |
| 73 | mPolicy = new PersistableBundle(in); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Retrieve the underlying bundle where the policy is stored. |
| 78 | * @hide |
| 79 | */ |
| 80 | public PersistableBundle getPolicyBundle() { |
| 81 | return new PersistableBundle(mPolicy); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Set the OTA policy to: install OTA update automatically as soon as one is available. |
| 86 | */ |
| 87 | public void setAutomaticInstallPolicy() { |
| 88 | mPolicy.clear(); |
| 89 | mPolicy.putInt(KEY_POLICY_TYPE, TYPE_INSTALL_AUTOMATIC); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Set the OTA policy to: new OTA update will only be installed automatically when the system |
| 94 | * clock is inside a daily maintenance window. If the start and end times are the same, the |
| 95 | * window is considered to include the WHOLE 24 hours, that is, OTAs can install at any time. If |
| 96 | * the given window in invalid, a {@link OtaPolicy.InvalidWindowException} will be thrown. If |
| 97 | * start time is later than end time, the window is considered spanning midnight, i.e. end time |
| 98 | * donates a time on the next day. The maintenance window will last for two weeks, after which |
| 99 | * the OTA will be installed automatically. |
| 100 | * |
| 101 | * @param startTime the start of the maintenance window, measured as the number of minutes from |
| 102 | * midnight in the device's local time. Must be in the range of [0, 1440). |
| 103 | * @param endTime the end of the maintenance window, measured as the number of minutes from |
| 104 | * midnight in the device's local time. Must be in the range of [0, 1440). |
| 105 | */ |
| 106 | public void setWindowedInstallPolicy(int startTime, int endTime) throws InvalidWindowException{ |
| 107 | if (startTime < 0 || startTime >= 1440 || endTime < 0 || endTime >= 1440) { |
| 108 | throw new InvalidWindowException("startTime and endTime must be inside [0, 1440)"); |
| 109 | } |
| 110 | mPolicy.clear(); |
| 111 | mPolicy.putInt(KEY_POLICY_TYPE, TYPE_INSTALL_WINDOWED); |
| 112 | mPolicy.putInt(KEY_INSTALL_WINDOW_START, startTime); |
| 113 | mPolicy.putInt(KEY_INSTALL_WINDOW_END, endTime); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Set the OTA policy to: block installation for a maximum period of two weeks. After the |
| 118 | * block expires the OTA will be installed automatically. |
| 119 | */ |
| 120 | public void setPostponeInstallPolicy() { |
| 121 | mPolicy.clear(); |
| 122 | mPolicy.putInt(KEY_POLICY_TYPE, TYPE_POSTPONE); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Returns the type of OTA policy. |
| 127 | * |
| 128 | * @return an integer, either one of {@link #TYPE_INSTALL_AUTOMATIC}, |
| 129 | * {@link #TYPE_INSTALL_WINDOWED} and {@link #TYPE_POSTPONE}, or -1 if no policy has been set. |
| 130 | */ |
| 131 | @OtaPolicyType |
| 132 | public int getPolicyType() { |
| 133 | return mPolicy.getInt(KEY_POLICY_TYPE, -1); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Get the start of the maintenance window. |
| 138 | * |
| 139 | * @return the start of the maintenance window measured as the number of minutes from midnight, |
| 140 | * or -1 if the policy does not have a maintenance window. |
| 141 | */ |
| 142 | public int getInstallWindowStart() { |
| 143 | if (getPolicyType() == TYPE_INSTALL_WINDOWED) { |
| 144 | return mPolicy.getInt(KEY_INSTALL_WINDOW_START, -1); |
| 145 | } else { |
| 146 | return -1; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Get the end of the maintenance window. |
| 152 | * |
| 153 | * @return the end of the maintenance window measured as the number of minutes from midnight, |
| 154 | * or -1 if the policy does not have a maintenance window. |
| 155 | */ |
| 156 | public int getInstallWindowEnd() { |
| 157 | if (getPolicyType() == TYPE_INSTALL_WINDOWED) { |
| 158 | return mPolicy.getInt(KEY_INSTALL_WINDOW_END, -1); |
| 159 | } else { |
| 160 | return -1; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public String toString() { |
| 166 | return mPolicy.toString(); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Exception thrown by {@link OtaPolicy#setWindowedInstallPolicy(int, int)} in case the |
| 171 | * specified window is invalid. |
| 172 | */ |
| 173 | public static class InvalidWindowException extends Exception { |
| 174 | public InvalidWindowException(String reason) { |
| 175 | super(reason); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |