• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 @RequiresApi(Build.VERSION_CODES.S)
40 public class AdServicesSettingsMainActivity extends AdServicesBaseActivity {
41     public static final String FROM_NOTIFICATION_KEY = "FROM_NOTIFICATION";
42     private MainActionDelegate mActionDelegate;
43     private MainActivityActionDelegate mActivityActionDelegate;
44 
45     /** @return the action delegate for the activity. */
getActionDelegate()46     public MainActionDelegate getActionDelegate() {
47         return mActionDelegate;
48     }
49 
50     @Override
onBackPressed()51     public void onBackPressed() {
52         // if navigated here from notification, then back button should not go back to notification.
53         if (getIntent().getBooleanExtra(FROM_NOTIFICATION_KEY, false)) {
54             moveTaskToBack(true);
55         } else {
56             super.onBackPressed();
57         }
58     }
59 
60     @Override
onCreate(Bundle savedInstanceState)61     protected void onCreate(Bundle savedInstanceState) {
62         Trace.beginSection("AdServicesSettingsMainActivity#OnCreate");
63         // Only for main view, we want to use the most up to date OTA strings on the device to
64         // create the ResourcesLoader.
65         if (FlagsFactory.getFlags().getUiOtaStringsFeatureEnabled()
66                 || FlagsFactory.getFlags().getUiOtaResourcesFeatureEnabled()) {
67             OTAResourcesManager.applyOTAResources(getApplicationContext(), true);
68             // apply to activity context as well since activity context has been created already.
69             OTAResourcesManager.applyOTAResources(this, false);
70         }
71         UiStatsLogger.logSettingsPageDisplayed();
72         super.onCreate(savedInstanceState);
73         if (!isUxStatesReady()) {
74             initMainFragment();
75         }
76         Trace.endSection();
77     }
78 
initMainFragment()79     private void initMainFragment() {
80         setContentView(R.layout.adservices_settings_main_activity);
81         getSupportFragmentManager()
82                 .beginTransaction()
83                 .replace(R.id.fragment_container_view, AdServicesSettingsMainFragment.class, null)
84                 .setReorderingAllowed(true)
85                 .commit();
86         mActionDelegate =
87                 new MainActionDelegate(this, new ViewModelProvider(this).get(MainViewModel.class));
88     }
89 
90     @Override
onResume()91     protected void onResume() {
92         super.onResume();
93         if (isUxStatesReady() && mActivityActionDelegate != null) {
94             mActivityActionDelegate.refreshState();
95         }
96     }
97 
98     @Override
initGA()99     public void initGA() {
100         initMainActivity(R.layout.main_activity);
101     }
102 
103     @Override
initU18()104     public void initU18() {
105         initMainActivity(R.layout.main_u18_activity);
106     }
107 
108     @Override
initGaUxWithPas()109     public void initGaUxWithPas() {
110         initGA();
111     }
112 
initMainActivity(int layoutResID)113     private void initMainActivity(int layoutResID) {
114         setContentView(layoutResID);
115         mActivityActionDelegate =
116                 new MainActivityActionDelegate(
117                         this, new ViewModelProvider(this).get(MainViewModel.class));
118     }
119 }
120