1 /*
2  * Copyright (C) 2018 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.settings.wifi.dpp;
18 
19 import android.content.res.Resources;
20 import android.graphics.drawable.Drawable;
21 import android.os.Bundle;
22 import android.util.Log;
23 import android.view.View;
24 import android.widget.TextView;
25 
26 import androidx.annotation.DrawableRes;
27 
28 import com.android.settings.R;
29 import com.android.settings.core.InstrumentedFragment;
30 
31 import com.google.android.setupcompat.template.FooterBarMixin;
32 import com.google.android.setupcompat.template.FooterButton;
33 import com.google.android.setupdesign.GlifLayout;
34 
35 /**
36  * There are below 4 fragments for Wi-Fi DPP UI flow, to reduce redundant code of UI components,
37  * this parent fragment instantiates common UI components
38  *
39  * {@code WifiDppQrCodeScannerFragment}
40  * {@code WifiDppQrCodeGeneratorFragment}
41  * {@code WifiDppChooseSavedWifiNetworkFragment}
42  * {@code WifiDppAddDeviceFragment}
43  */
44 public abstract class WifiDppQrCodeBaseFragment extends InstrumentedFragment {
45     private static final String TAG = "WifiDppQrCodeBaseFragment";
46 
47     private GlifLayout mGlifLayout;
48     protected TextView mSummary;
49     protected FooterButton mLeftButton;
50     protected FooterButton mRightButton;
51 
52     @Override
onViewCreated(View view, Bundle savedInstanceState)53     public void onViewCreated(View view, Bundle savedInstanceState) {
54         super.onViewCreated(view, savedInstanceState);
55 
56         mGlifLayout = (GlifLayout) view;
57         mSummary = view.findViewById(android.R.id.summary);
58 
59         if (isFooterAvailable()) {
60             mLeftButton = new FooterButton.Builder(getContext())
61                     .setButtonType(FooterButton.ButtonType.CANCEL)
62                     .setTheme(R.style.SudGlifButton_Secondary)
63                     .build();
64             mGlifLayout.getMixin(FooterBarMixin.class).setSecondaryButton(mLeftButton);
65 
66             mRightButton = new FooterButton.Builder(getContext())
67                     .setButtonType(FooterButton.ButtonType.NEXT)
68                     .setTheme(R.style.SudGlifButton_Primary)
69                     .build();
70             mGlifLayout.getMixin(FooterBarMixin.class).setPrimaryButton(mRightButton);
71         }
72 
73         mGlifLayout.getHeaderTextView().setAccessibilityLiveRegion(
74                 View.ACCESSIBILITY_LIVE_REGION_POLITE);
75     }
76 
setHeaderIconImageResource(@rawableRes int iconResId)77     protected void setHeaderIconImageResource(@DrawableRes int iconResId) {
78         mGlifLayout.setIcon(getDrawable(iconResId));
79     }
80 
getDrawable(@rawableRes int iconResId)81     private Drawable getDrawable(@DrawableRes int iconResId) {
82         Drawable buttonIcon = null;
83 
84         try {
85             buttonIcon = getContext().getDrawable(iconResId);
86         } catch (Resources.NotFoundException exception) {
87             Log.e(TAG, "Resource does not exist: " + iconResId);
88         }
89         return buttonIcon;
90     }
91 
setHeaderTitle(String title)92     protected void setHeaderTitle(String title) {
93         mGlifLayout.setHeaderText(title);
94     }
95 
setHeaderTitle(int resId, Object... formatArgs)96     protected void setHeaderTitle(int resId, Object... formatArgs) {
97         mGlifLayout.setHeaderText(getString(resId, formatArgs));
98     }
99 
setProgressBarShown(boolean shown)100     protected void setProgressBarShown(boolean shown) {
101         mGlifLayout.setProgressBarShown(shown);
102     }
103 
isFooterAvailable()104     protected abstract boolean isFooterAvailable();
105 }
106