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.preprovisioning.terms;
18 
19 import static android.view.View.TEXT_ALIGNMENT_TEXT_START;
20 
21 import static com.google.android.setupdesign.util.ThemeHelper.shouldApplyMaterialYouStyle;
22 
23 import android.app.Activity;
24 import android.view.View;
25 import android.widget.LinearLayout;
26 import android.widget.TextView;
27 
28 import androidx.appcompat.widget.Toolbar;
29 import androidx.recyclerview.widget.LinearLayoutManager;
30 import androidx.recyclerview.widget.RecyclerView;
31 
32 import com.android.managedprovisioning.R;
33 import com.android.managedprovisioning.common.AccessibilityContextMenuMaker;
34 import com.android.managedprovisioning.common.StylerHelper;
35 import com.android.managedprovisioning.common.TransitionHelper;
36 import com.android.managedprovisioning.common.Utils;
37 import com.android.managedprovisioning.preprovisioning.terms.adapters.TermsListAdapter;
38 
39 import com.google.auto.value.AutoValue;
40 
41 import java.util.List;
42 
43 /**
44  * A {@link TermsActivityBridge} implementation meant for the base {@code ManagedProvisioning}.
45  */
46 @AutoValue
47 abstract class TermsActivityBridgeImpl implements TermsActivityBridge {
48 
getUtils()49     abstract Utils getUtils();
50 
getStylerHelper()51     abstract StylerHelper getStylerHelper();
52 
getTransitionHelper()53     abstract TransitionHelper getTransitionHelper();
54 
55     @Override
initiateUi(final Activity activity, final List<TermsDocument> terms, final TermsDocument generalTerms)56     public void initiateUi(final Activity activity, final List<TermsDocument> terms,
57             final TermsDocument generalTerms) {
58         activity.setContentView(R.layout.terms_screen);
59         activity.setTitle(R.string.terms);
60 
61         setupHeader(activity);
62         setupRecyclerView(activity);
63         setupToolbar(activity);
64         setupTermsListForHandhelds(activity, terms, generalTerms);
65     }
66 
setupHeader(Activity activity)67     private void setupHeader(Activity activity) {
68         if (!shouldApplyMaterialYouStyle(activity)) {
69             return;
70         }
71         TextView header = activity.findViewById(R.id.header);
72         header.setVisibility(View.VISIBLE);
73         header.setText(R.string.terms);
74         getStylerHelper().applyHeaderStyling(header,
75                 new LinearLayout.LayoutParams(header.getLayoutParams()));
76         header.setTextAlignment(TEXT_ALIGNMENT_TEXT_START);
77     }
78 
setupRecyclerView(Activity activity)79     private void setupRecyclerView(Activity activity) {
80         final RecyclerView recyclerView = activity.findViewById(R.id.terms_container);
81         if (recyclerView.getItemDecorationCount() > 0) {
82             recyclerView.removeItemDecorationAt(/* index= */ 0);
83         }
84     }
85 
setupToolbar(Activity activity)86     private void setupToolbar(Activity activity) {
87         Toolbar toolbar = activity.findViewById(R.id.toolbar);
88         toolbar.setNavigationContentDescription(R.string.navigation_button_description);
89         toolbar.setNavigationIcon(activity.getDrawable(R.drawable.ic_arrow_back_24dp));
90         toolbar.setNavigationOnClickListener(v ->
91                 getTransitionHelper().finishActivity(activity));
92         if (!shouldApplyMaterialYouStyle(activity)) {
93             toolbar.setTitle(R.string.terms);
94         }
95     }
96 
setupTermsListForHandhelds(Activity activity, List<TermsDocument> terms, TermsDocument generalTerms)97     private void setupTermsListForHandhelds(Activity activity, List<TermsDocument> terms,
98             TermsDocument generalTerms) {
99         RecyclerView recyclerView = activity.findViewById(R.id.terms_container);
100         recyclerView.setLayoutManager(new LinearLayoutManager(/* context= */ activity));
101         recyclerView.setAdapter(new TermsListAdapter(
102                 activity,
103                 generalTerms,
104                 terms,
105                 activity.getLayoutInflater(),
106                 new AccessibilityContextMenuMaker(activity),
107                 (TermsListAdapter.TermsBridge) activity,
108                 getUtils(),
109                 getStylerHelper()));
110     }
111 
builder()112     static Builder builder() {
113         return new AutoValue_TermsActivityBridgeImpl.Builder();
114     }
115 
116     @AutoValue.Builder
117     abstract static class Builder {
setUtils(Utils utils)118         abstract Builder setUtils(Utils utils);
119 
setStylerHelper(StylerHelper stylerHelper)120         abstract Builder setStylerHelper(StylerHelper stylerHelper);
121 
setTransitionHelper(TransitionHelper transitionHelper)122         abstract Builder setTransitionHelper(TransitionHelper transitionHelper);
123 
build()124         abstract TermsActivityBridgeImpl build();
125     }
126 
127 }
128