1 /*
2  * Copyright (C) 2023 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.privatespace;
18 
19 import static com.android.settings.privatespace.PrivateSpaceSetupActivity.ACCOUNT_LOGIN_ACTION;
20 import static com.android.settings.privatespace.PrivateSpaceSetupActivity.EXTRA_ACTION_TYPE;
21 
22 import android.annotation.SuppressLint;
23 import android.app.settings.SettingsEnums;
24 import android.content.Intent;
25 import android.os.Bundle;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 
30 import androidx.activity.OnBackPressedCallback;
31 import androidx.annotation.Nullable;
32 import androidx.navigation.fragment.NavHostFragment;
33 
34 import com.android.settings.R;
35 import com.android.settings.core.InstrumentedFragment;
36 
37 import com.google.android.setupcompat.template.FooterBarMixin;
38 import com.google.android.setupcompat.template.FooterButton;
39 import com.google.android.setupdesign.GlifLayout;
40 
41 /** Fragment to display error screen if the profile is not signed in with a Google account. */
42 public class PrivateSpaceAccountLoginError extends InstrumentedFragment {
43     private static final String TAG = "PrivateSpaceAccLoginErr";
44 
45     @Override
onCreateView( LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)46     public View onCreateView(
47             LayoutInflater inflater,
48             @Nullable ViewGroup container,
49             @Nullable Bundle savedInstanceState) {
50         GlifLayout rootView =
51                 (GlifLayout)
52                         inflater.inflate(
53                                 R.layout.privatespace_account_login_error, container, false);
54         final FooterBarMixin mixin = rootView.getMixin(FooterBarMixin.class);
55         mixin.setPrimaryButton(
56                 new FooterButton.Builder(getContext())
57                         .setText(R.string.private_space_continue_login_label)
58                         .setListener(nextScreen())
59                         .setButtonType(FooterButton.ButtonType.NEXT)
60                         .setTheme(com.google.android.setupdesign.R.style.SudGlifButton_Primary)
61                         .build());
62         mixin.setSecondaryButton(
63                 new FooterButton.Builder(getContext())
64                         .setText(R.string.private_space_skip_login_label)
65                         .setListener(onSkip())
66                         .setButtonType(FooterButton.ButtonType.CANCEL)
67                         .setTheme(com.google.android.setupdesign.R.style.SudGlifButton_Secondary)
68                         .build());
69         OnBackPressedCallback callback =
70                 new OnBackPressedCallback(true /* enabled by default */) {
71                     @Override
72                     public void handleOnBackPressed() {
73                         // Handle the back button event
74                     }
75                 };
76         requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
77 
78         return rootView;
79     }
80 
81     @Override
getMetricsCategory()82     public int getMetricsCategory() {
83         return SettingsEnums.PRIVATE_SPACE_SETUP_ACCOUNT_LOGIN_ERROR;
84     }
85 
86     @SuppressLint("MissingPermission")
nextScreen()87     private View.OnClickListener nextScreen() {
88         return v -> {
89             mMetricsFeatureProvider.action(
90                     getContext(),
91                     SettingsEnums.ACTION_PRIVATE_SPACE_SETUP_TRY_CREATE_ACCOUNT_AGAIN);
92             startAccountLogin();
93         };
94     }
95 
onSkip()96     private View.OnClickListener onSkip() {
97         return v -> {
98             mMetricsFeatureProvider.action(
99                     getContext(), SettingsEnums.ACTION_PRIVATE_SPACE_SETUP_SKIP_ACCOUNT_LOGIN);
100             mMetricsFeatureProvider.action(
101                     getContext(),
102                     SettingsEnums.ACTION_PRIVATE_SPACE_SETUP_ACCOUNT_LOGIN_SUCCESS,
103                     false);
104             NavHostFragment.findNavController(PrivateSpaceAccountLoginError.this)
105                     .navigate(R.id.action_skip_account_login);
106         };
107     }
108 
109     /** Start new activity in private profile to add an account to private profile */
110     private void startAccountLogin() {
111         Intent intent = new Intent(getContext(), PrivateProfileContextHelperActivity.class);
112         intent.putExtra(EXTRA_ACTION_TYPE, ACCOUNT_LOGIN_ACTION);
113         mMetricsFeatureProvider.action(
114                 getContext(), SettingsEnums.ACTION_PRIVATE_SPACE_SETUP_ACCOUNT_LOGIN_START);
115         getActivity().startActivityForResult(intent, ACCOUNT_LOGIN_ACTION);
116     }
117 }
118