1 /*
2  * Copyright (C) 2022 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.managedprovisioning.common;
18 
19 import static java.util.Objects.requireNonNull;
20 
21 import android.content.Context;
22 import android.content.Intent;
23 
24 /**
25  * Utilities related to error dialogs, such as constructing a resulting {@link Intent} that
26  * describes a dialog.
27  */
28 public final class ErrorDialogUtils {
29     public static final String EXTRA_DIALOG_TITLE_ID = "dialog_title_id";
30     public static final String EXTRA_ERROR_MESSAGE_RES =
31             "dialog_error_message_res";
32     public static final String EXTRA_FACTORY_RESET_REQUIRED =
33             "factory_reset_required";
34 
ErrorDialogUtils()35     private ErrorDialogUtils() {
36     }
37 
38     /**
39      * Creates a resulting intent to be returned as a result that describes an error.
40      */
createResultIntent(ErrorWrapper error, Context context)41     public static Intent createResultIntent(ErrorWrapper error, Context context) {
42         requireNonNull(error);
43         Intent intent = new Intent();
44         if (error.dialogTitleId != 0) {
45             intent.putExtra(EXTRA_DIALOG_TITLE_ID, error.dialogTitleId);
46         }
47         if (error.errorMessageRes != null) {
48             intent.putExtra(EXTRA_ERROR_MESSAGE_RES, error.errorMessageRes.valueOrNull(context));
49         }
50         intent.putExtra(EXTRA_FACTORY_RESET_REQUIRED, error.factoryResetRequired);
51         return intent;
52     }
53 }
54