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 package com.android.adservices.ui.settings.activities;
17 
18 import static com.android.adservices.ui.UxUtil.isUxStatesReady;
19 
20 import android.os.Build;
21 import android.os.Bundle;
22 import android.os.Trace;
23 
24 import androidx.annotation.RequiresApi;
25 import androidx.lifecycle.ViewModelProvider;
26 
27 import com.android.adservices.api.R;
28 import com.android.adservices.service.FlagsFactory;
29 import com.android.adservices.service.stats.UiStatsLogger;
30 import com.android.adservices.ui.OTAResourcesManager;
31 import com.android.adservices.ui.settings.activitydelegates.MainActivityActionDelegate;
32 import com.android.adservices.ui.settings.delegates.MainActionDelegate;
33 import com.android.adservices.ui.settings.fragments.AdServicesSettingsMainFragment;
34 import com.android.adservices.ui.settings.viewmodels.MainViewModel;
35 
36 /**
37  * Android application activity for controlling settings related to PP (Privacy Preserving) APIs.
38  */
39 // TODO(b/269798827): Enable for R.
40 @RequiresApi(Build.VERSION_CODES.S)
41 public class AdServicesSettingsMainActivity extends AdServicesBaseActivity {
42     public static final String FROM_NOTIFICATION_KEY = "FROM_NOTIFICATION";
43     private MainActionDelegate mActionDelegate;
44     private MainActivityActionDelegate mActivityActionDelegate;
45 
46     /** @return the action delegate for the activity. */
getActionDelegate()47     public MainActionDelegate getActionDelegate() {
48         return mActionDelegate;
49     }
50 
51     @Override
onBackPressed()52     public void onBackPressed() {
53         // if navigated here from notification, then back button should not go back to notification.
54         if (getIntent().getBooleanExtra(FROM_NOTIFICATION_KEY, false)) {
55             moveTaskToBack(true);
56         } else {
57             super.onBackPressed();
58         }
59     }
60 
61     @Override
onCreate(Bundle savedInstanceState)62     protected void onCreate(Bundle savedInstanceState) {
63         Trace.beginSection("AdServicesSettingsMainActivity#OnCreate");
64         // Only for main view, we want to use the most up to date OTA strings on the device to
65         // create the ResourcesLoader.
66         if (FlagsFactory.getFlags().getUiOtaStringsFeatureEnabled()
67                 || FlagsFactory.getFlags().getUiOtaResourcesFeatureEnabled()) {
68             OTAResourcesManager.applyOTAResources(getApplicationContext(), true);
69             // apply to activity context as well since activity context has been created already.
70             OTAResourcesManager.applyOTAResources(this, false);
71         }
72         UiStatsLogger.logSettingsPageDisplayed();
73         super.onCreate(savedInstanceState);
74         if (!isUxStatesReady(this)) {
75             initMainFragment();
76         }
77         Trace.endSection();
78     }
79 
initMainFragment()80     private void initMainFragment() {
81         setContentView(R.layout.adservices_settings_main_activity);
82         getSupportFragmentManager()
83                 .beginTransaction()
84                 .replace(R.id.fragment_container_view, AdServicesSettingsMainFragment.class, null)
85                 .setReorderingAllowed(true)
86                 .commit();
87         mActionDelegate =
88                 new MainActionDelegate(this, new ViewModelProvider(this).get(MainViewModel.class));
89     }
90 
91     @Override
onResume()92     protected void onResume() {
93         super.onResume();
94         if (isUxStatesReady(this) && mActivityActionDelegate != null) {
95             mActivityActionDelegate.refreshState();
96         }
97     }
98 
99     @Override
initGA()100     public void initGA() {
101         initMainActivity(R.layout.main_activity);
102     }
103 
104     @Override
initU18()105     public void initU18() {
106         initMainActivity(R.layout.main_u18_activity);
107     }
108 
109     @Override
initRvc()110     public void initRvc() {
111         initU18();
112     }
113 
114     @Override
initGaUxWithPas()115     public void initGaUxWithPas() {
116         initGA();
117     }
118 
initMainActivity(int layoutResID)119     private void initMainActivity(int layoutResID) {
120         setContentView(layoutResID);
121         mActivityActionDelegate =
122                 new MainActivityActionDelegate(
123                         this, new ViewModelProvider(this).get(MainViewModel.class));
124     }
125 }
126