• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.car.power;
18 
19 import android.annotation.IntDef;
20 
21 import java.lang.annotation.Retention;
22 import java.lang.annotation.RetentionPolicy;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 
26 final class PolicyOperationStatus {
27     static final int OK = 0;
28     static final int ERROR_DEFINE_POWER_POLICY = 1;
29     static final int ERROR_APPLY_POWER_POLICY = 2;
30     static final int ERROR_SET_POWER_POLICY_GROUP = 3;
31     static final int ERROR_INVALID_POWER_POLICY_ID = 4;
32     static final int ERROR_DOUBLE_REGISTERED_POWER_POLICY_ID = 5;
33     static final int ERROR_NOT_REGISTERED_POWER_POLICY_ID = 6;
34     static final int ERROR_INVALID_POWER_POLICY_GROUP_ID = 7;
35     static final int ERROR_DOUBLE_REGISTERED_POWER_POLICY_GROUP_ID = 8;
36     static final int ERROR_INVALID_POWER_COMPONENT = 9;
37     static final int ERROR_DUPLICATED_POWER_COMPONENT = 10;
38 
39     @IntDef(value = {
40         OK,
41         ERROR_DEFINE_POWER_POLICY,
42         ERROR_APPLY_POWER_POLICY,
43         ERROR_SET_POWER_POLICY_GROUP,
44         ERROR_INVALID_POWER_POLICY_ID,
45         ERROR_DOUBLE_REGISTERED_POWER_POLICY_ID,
46         ERROR_NOT_REGISTERED_POWER_POLICY_ID,
47         ERROR_INVALID_POWER_POLICY_GROUP_ID,
48         ERROR_DOUBLE_REGISTERED_POWER_POLICY_GROUP_ID,
49         ERROR_INVALID_POWER_COMPONENT,
50         ERROR_DUPLICATED_POWER_COMPONENT
51     })
52     @Retention(RetentionPolicy.SOURCE)
53     @interface ErrorCode{}
54 
55     private static final ArrayList<String> ERROR_CODE_DESCRIPTION = new ArrayList<>(Arrays.asList(
56             "Power policy operation is successful", // OK
57             "Failed to define power policy",        // ERROR_DEFINE_POWER_POLICY
58             "Failed to apply power policy",         // ERROR_APPLY_POWER_POLICY
59             "Failed to set power policy group",     // ERROR_SET_POWER_POLICY_GROUP
60             "Invalid power policy ID",              // ERROR_INVALID_POWER_POLICY_ID
61             "Already registered power policy ID",   // ERROR_DOUBLE_REGISTERED_POWER_POLICY_ID
62             "Not registered power policy ID",       // ERROR_NOT_REGISTERED_POWER_POLICY_ID
63             "Invalid power policy group ID",        // ERROR_INVALID_POWER_POLICY_GROUP_ID
64             "Already registered policy group ID",   // ERROR_DOUBLE_REGISTERED_POWER_POLICY_GROUP_ID
65             "Invalid power component",              // ERROR_INVALID_POWER_COMPONENT
66             "Duplicated power component"            // ERROR_DUPLICATED_POWER_COMPONENT
67     ));
68 
PolicyOperationStatus()69     private PolicyOperationStatus() {}
70 
errorCodeToString(@rrorCode int code)71     public static String errorCodeToString(@ErrorCode int code) {
72         try {
73             return ERROR_CODE_DESCRIPTION.get(code);
74         } catch (IndexOutOfBoundsException e) {
75             return "Unknown error code: " + code;
76         }
77     }
78 
errorCodeToString(@rrorCode int code, String moreDescription)79     public static String errorCodeToString(@ErrorCode int code, String moreDescription) {
80         try {
81             return ERROR_CODE_DESCRIPTION.get(code) + ": " + moreDescription;
82         } catch (IndexOutOfBoundsException e) {
83             return "Unknown error code: " + code;
84         }
85     }
86 }
87