1 /*
2  * Copyright (C) 2014 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 com.android.server.pm;
18 
19 import android.annotation.IntDef;
20 import android.content.pm.PackageManager;
21 
22 import com.android.server.pm.Installer.InstallerException;
23 
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 
27 /** {@hide} */
28 public class PackageManagerException extends Exception {
29     public static final int INTERNAL_ERROR_NATIVE_LIBRARY_COPY = -1;
30     public static final int INTERNAL_ERROR_MOVE = -2;
31     public static final int INTERNAL_ERROR_MISSING_SETTING_FOR_MOVE = -3;
32     public static final int INTERNAL_ERROR_DERIVING_ABI = -4;
33     public static final int INTERNAL_ERROR_VERITY_SETUP = -5;
34     public static final int INTERNAL_ERROR_SHARED_LIB_INSTALLED_TWICE = -6;
35     public static final int INTERNAL_ERROR_STORAGE_INVALID_PACKAGE_UNKNOWN = -7;
36     public static final int INTERNAL_ERROR_STORAGE_INVALID_VOLUME_UNKNOWN = -8;
37     public static final int INTERNAL_ERROR_STORAGE_INVALID_NOT_INSTALLED_FOR_USER = -9;
38     public static final int INTERNAL_ERROR_STORAGE_INVALID_SHOULD_NOT_HAVE_STORAGE = -10;
39     public static final int INTERNAL_ERROR_DECOMPRESS_STUB = -11;
40     public static final int INTERNAL_ERROR_UPDATED_VERSION_BETTER_THAN_SYSTEM = -12;
41     public static final int INTERNAL_ERROR_DUP_STATIC_SHARED_LIB_PROVIDER = -13;
42     public static final int INTERNAL_ERROR_STATIC_SHARED_LIB_VERSION_CODES_ORDER = -14;
43     public static final int INTERNAL_ERROR_SYSTEM_OVERLAY_STATIC = -15;
44     public static final int INTERNAL_ERROR_OVERLAY_LOW_TARGET_SDK = -16;
45     public static final int INTERNAL_ERROR_OVERLAY_SIGNATURE1 = -17;
46     public static final int INTERNAL_ERROR_OVERLAY_SIGNATURE2 = -18;
47     public static final int INTERNAL_ERROR_NOT_PRIV_SHARED_USER = -19;
48     public static final int INTERNAL_ERROR_INSTALL_MISSING_CHILD_SESSIONS = -20;
49     public static final int INTERNAL_ERROR_VERIFY_MISSING_CHILD_SESSIONS = -21;
50     public static final int INTERNAL_ERROR_STATIC_SHARED_LIB_LOW_SDK = -22;
51     public static final int INTERNAL_ERROR_STATIC_SHARED_LIB_INSTANT = -23;
52     public static final int INTERNAL_ERROR_STATIC_SHARED_LIB_RENAMED = -24;
53     public static final int INTERNAL_ERROR_STATIC_SHARED_LIB_DYNAMIC = -25;
54     public static final int INTERNAL_ERROR_STATIC_SHARED_LIB_SHARED_USER = -26;
55     public static final int INTERNAL_ERROR_STATIC_SHARED_LIB_ACTIVITY = -27;
56     public static final int INTERNAL_ERROR_STATIC_SHARED_LIB_SERVICE = -28;
57     public static final int INTERNAL_ERROR_STATIC_SHARED_LIB_CONTENT_PROVIDER = -29;
58     public static final int INTERNAL_ERROR_STATIC_SHARED_LIB_BROADCAST_RECEIVER = -30;
59     public static final int INTERNAL_ERROR_STATIC_SHARED_LIB_PERMISSION_GROUP = -31;
60     public static final int INTERNAL_ERROR_STATIC_SHARED_LIB_FEATURE = -32;
61     public static final int INTERNAL_ERROR_STATIC_SHARED_LIB_PERMISSION = -33;
62     public static final int INTERNAL_ERROR_STATIC_SHARED_LIB_PROTECTED_BROADCAST = -34;
63     public static final int INTERNAL_ERROR_STATIC_SHARED_LIB_OVERLAY_TARGETS = -35;
64     public static final int INTERNAL_ERROR_APEX_NOT_DIRECTORY = -36;
65     public static final int INTERNAL_ERROR_APEX_MORE_THAN_ONE_FILE = -37;
66 
67     @IntDef(prefix = { "INTERNAL_ERROR_" }, value = {
68             INTERNAL_ERROR_NATIVE_LIBRARY_COPY,
69             INTERNAL_ERROR_MOVE,
70             INTERNAL_ERROR_MISSING_SETTING_FOR_MOVE,
71             INTERNAL_ERROR_DERIVING_ABI,
72             INTERNAL_ERROR_VERITY_SETUP,
73             INTERNAL_ERROR_SHARED_LIB_INSTALLED_TWICE,
74             INTERNAL_ERROR_STORAGE_INVALID_PACKAGE_UNKNOWN,
75             INTERNAL_ERROR_STORAGE_INVALID_VOLUME_UNKNOWN,
76             INTERNAL_ERROR_STORAGE_INVALID_NOT_INSTALLED_FOR_USER,
77             INTERNAL_ERROR_STORAGE_INVALID_SHOULD_NOT_HAVE_STORAGE,
78             INTERNAL_ERROR_DECOMPRESS_STUB,
79             INTERNAL_ERROR_UPDATED_VERSION_BETTER_THAN_SYSTEM,
80             INTERNAL_ERROR_DUP_STATIC_SHARED_LIB_PROVIDER,
81             INTERNAL_ERROR_STATIC_SHARED_LIB_VERSION_CODES_ORDER,
82             INTERNAL_ERROR_SYSTEM_OVERLAY_STATIC,
83             INTERNAL_ERROR_OVERLAY_LOW_TARGET_SDK,
84             INTERNAL_ERROR_OVERLAY_SIGNATURE1,
85             INTERNAL_ERROR_OVERLAY_SIGNATURE2,
86             INTERNAL_ERROR_NOT_PRIV_SHARED_USER,
87             INTERNAL_ERROR_INSTALL_MISSING_CHILD_SESSIONS,
88             INTERNAL_ERROR_VERIFY_MISSING_CHILD_SESSIONS,
89             INTERNAL_ERROR_STATIC_SHARED_LIB_LOW_SDK,
90             INTERNAL_ERROR_STATIC_SHARED_LIB_INSTANT,
91             INTERNAL_ERROR_STATIC_SHARED_LIB_RENAMED,
92             INTERNAL_ERROR_STATIC_SHARED_LIB_DYNAMIC,
93             INTERNAL_ERROR_STATIC_SHARED_LIB_SHARED_USER,
94             INTERNAL_ERROR_STATIC_SHARED_LIB_ACTIVITY,
95             INTERNAL_ERROR_STATIC_SHARED_LIB_SERVICE,
96             INTERNAL_ERROR_STATIC_SHARED_LIB_CONTENT_PROVIDER,
97             INTERNAL_ERROR_STATIC_SHARED_LIB_BROADCAST_RECEIVER,
98             INTERNAL_ERROR_STATIC_SHARED_LIB_PERMISSION_GROUP,
99             INTERNAL_ERROR_STATIC_SHARED_LIB_FEATURE,
100             INTERNAL_ERROR_STATIC_SHARED_LIB_PERMISSION,
101             INTERNAL_ERROR_STATIC_SHARED_LIB_PROTECTED_BROADCAST,
102             INTERNAL_ERROR_STATIC_SHARED_LIB_OVERLAY_TARGETS,
103             INTERNAL_ERROR_APEX_NOT_DIRECTORY,
104             INTERNAL_ERROR_APEX_MORE_THAN_ONE_FILE
105     })
106     @Retention(RetentionPolicy.SOURCE)
107     public @interface InternalErrorCode {}
108 
109     public final int error;
110     public final int internalErrorCode;
111 
112     /**
113      * Default constructor without specifying the public return code of this exception.
114      * The public error code will be {@link PackageManager.INSTALL_FAILED_INTERNAL_ERROR}.
115      *
116      * Note for developers: if you use this constructor, assuming you have a different case where
117      * the exception should be thrown with {@link PackageManager.INSTALL_FAILED_INTERNAL_ERROR},
118      * please create a new {@link InternalErrorCode} constant.
119      *
120      * @param detailMessage Details about the cause of the exception.
121      * @param internalErrorCode Used for logging and analysis.
122      */
ofInternalError(String detailMessage, @InternalErrorCode int internalErrorCode)123     public static PackageManagerException ofInternalError(String detailMessage,
124             @InternalErrorCode int internalErrorCode) {
125         return new PackageManagerException(
126                 PackageManager.INSTALL_FAILED_INTERNAL_ERROR, detailMessage, internalErrorCode);
127     }
128 
PackageManagerException(int error, String detailMessage, int internalErrorCode)129     protected PackageManagerException(int error, String detailMessage, int internalErrorCode) {
130         super(detailMessage);
131         this.error = error;
132         this.internalErrorCode = internalErrorCode;
133     }
134 
PackageManagerException(int error, String detailMessage)135     public PackageManagerException(int error, String detailMessage) {
136         super(detailMessage);
137         this.error = error;
138         this.internalErrorCode = 0;
139     }
140 
PackageManagerException(int error, String detailMessage, Throwable throwable)141     public PackageManagerException(int error, String detailMessage, Throwable throwable) {
142         super(detailMessage, throwable);
143         this.error = error;
144         this.internalErrorCode = 0;
145     }
146 
PackageManagerException(Throwable e)147     public PackageManagerException(Throwable e) {
148         super(e);
149         this.error = PackageManager.INSTALL_FAILED_INTERNAL_ERROR;
150         this.internalErrorCode = 0;
151     }
152 
from(InstallerException e)153     public static PackageManagerException from(InstallerException e)
154             throws PackageManagerException {
155         throw new PackageManagerException(PackageManager.INSTALL_FAILED_INTERNAL_ERROR,
156                 e.getMessage(), e.getCause());
157     }
158 }
159