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.imsserviceentitlement;
18 
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.view.WindowManager;
25 import android.widget.TextView;
26 
27 import androidx.annotation.StringRes;
28 import androidx.fragment.app.Fragment;
29 
30 import com.google.android.setupcompat.template.FooterBarMixin;
31 import com.google.android.setupcompat.template.FooterButton;
32 import com.google.android.setupdesign.GlifLayout;
33 
34 /** A {@link Fragment} with SuW GlifLayout. */
35 public class SuwUiFragment extends Fragment {
36     private static final String TITLE_RES_ID_KEY = "TITLE_RES_ID_KEY";
37     private static final String TEXT_RES_ID_KEY = "TEXT_RES_ID_KEY";
38     private static final String PROGRESS_BAR_SHOWN_KEY = "PROGRESS_BAR_SHOWN_KEY";
39     private static final String PRIMARY_BUTTON_TEXT_ID_KEY = "PRIMARY_BUTTON_TEXT_ID_KEY";
40     private static final String PRIMARY_BUTTON_RESULT_KEY = "PRIMARY_BUTTON_RESULT_KEY";
41     private static final String SECONDARY_BUTTON_TEXT_ID_KEY = "SECONDARY_BUTTON_TEXT_ID_KEY";
42 
43     /** Static constructor */
newInstance( @tringRes int title, @StringRes int text, boolean progressBarShown, @StringRes int primaryButtonText, int primaryResult, @StringRes int secondaryButtonText)44     public static SuwUiFragment newInstance(
45             @StringRes int title,
46             @StringRes int text,
47             boolean progressBarShown,
48             @StringRes int primaryButtonText,
49             int primaryResult,
50             @StringRes int secondaryButtonText) {
51         SuwUiFragment frag = new SuwUiFragment();
52         Bundle args = new Bundle();
53         args.putInt(TITLE_RES_ID_KEY, title);
54         args.putInt(TEXT_RES_ID_KEY, text);
55         args.putBoolean(PROGRESS_BAR_SHOWN_KEY, progressBarShown);
56         args.putInt(PRIMARY_BUTTON_TEXT_ID_KEY, primaryButtonText);
57         // Action for primaryButton is: finishActivity(primaryResult)
58         args.putInt(PRIMARY_BUTTON_RESULT_KEY, primaryResult);
59         args.putInt(SECONDARY_BUTTON_TEXT_ID_KEY, secondaryButtonText);
60         // Action for secondaryButton is: finishActivity(Activity.RESULT_CANCELED)
61         frag.setArguments(args);
62         return frag;
63     }
64 
65     @Override
onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)66     public View onCreateView(
67             LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
68         View view = inflater.inflate(R.layout.fragment_suw_ui, container, false);
69 
70         Bundle arguments = getArguments();
71         int titleResId = arguments.getInt(TITLE_RES_ID_KEY, 0);
72         int textResId = arguments.getInt(TEXT_RES_ID_KEY, 0);
73         boolean progressBarShown = arguments.getBoolean(PROGRESS_BAR_SHOWN_KEY, false);
74         int primaryButtonText = arguments.getInt(PRIMARY_BUTTON_TEXT_ID_KEY, 0);
75         int primaryResult = arguments.getInt(PRIMARY_BUTTON_RESULT_KEY, Activity.RESULT_CANCELED);
76         int secondaryButtonText = arguments.getInt(SECONDARY_BUTTON_TEXT_ID_KEY, 0);
77 
78         GlifLayout layout = view.findViewById(R.id.setup_wizard_layout);
79         if (titleResId != 0) {
80             layout.setHeaderText(titleResId);
81         }
82 
83         layout.setProgressBarShown(progressBarShown);
84         if (progressBarShown) {
85             // Keep screen on if something in progress. And remove the flag on destroy view.
86             getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
87         }
88 
89         if (textResId != 0) {
90             TextView text = view.findViewById(R.id.entry_text);
91             text.setText(textResId);
92         }
93 
94         final FooterBarMixin buttonFooterMixin = layout.getMixin(FooterBarMixin.class);
95 
96         if (primaryButtonText != 0) {
97             buttonFooterMixin.setPrimaryButton(
98                     new FooterButton.Builder(getContext())
99                             .setListener(v -> finishActivity(primaryResult))
100                             .setText(primaryButtonText)
101                             .setTheme(com.google.android.setupdesign.R.style.SudGlifButton_Primary)
102                             .build());
103         }
104 
105         if (secondaryButtonText != 0) {
106             buttonFooterMixin.setSecondaryButton(
107                     new FooterButton.Builder(getContext())
108                             .setListener(v -> finishActivity(Activity.RESULT_CANCELED))
109                             .setText(secondaryButtonText)
110                             .setTheme(com.google.android.setupdesign.R.style.SudGlifButton_Primary)
111                             .build());
112         }
113 
114         return view;
115     }
116 
117     @Override
onDestroyView()118     public void onDestroyView() {
119         boolean progressBarShown = getArguments().getBoolean(PROGRESS_BAR_SHOWN_KEY, false);
120         if (progressBarShown) {
121             getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
122         }
123         super.onDestroyView();
124     }
125 
126     /** Finishes the associated activity with {@code result}; no-op if no activity associated. */
finishActivity(int result)127     private void finishActivity(int result) {
128         final Activity activity = getActivity();
129         if (activity == null) {
130             return;
131         }
132         ((WfcActivationUi) activity).setResultAndFinish(result);
133     }
134 }
135