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.sdksandboxclient;
18 
19 import android.os.Bundle;
20 import android.os.Looper;
21 
22 import androidx.annotation.NonNull;
23 import androidx.appcompat.app.ActionBar;
24 import androidx.appcompat.app.AppCompatActivity;
25 import androidx.preference.EditTextPreference;
26 import androidx.preference.ListPreference;
27 import androidx.preference.Preference;
28 import androidx.preference.PreferenceFragmentCompat;
29 
30 import java.util.concurrent.Executor;
31 import java.util.concurrent.Executors;
32 
33 public class BannerOptionsActivity extends AppCompatActivity {
34 
BannerOptionsActivity()35     public BannerOptionsActivity() {
36         super(R.layout.activity_options);
37     }
38 
39     @Override
onCreate(Bundle savedInstanceState)40     protected void onCreate(Bundle savedInstanceState) {
41         super.onCreate(savedInstanceState);
42         if (savedInstanceState == null) {
43             ActionBar actionBar = getSupportActionBar();
44             if (actionBar != null) {
45                 actionBar.setDisplayHomeAsUpEnabled(true);
46             }
47             getSupportFragmentManager()
48                     .beginTransaction()
49                     .add(R.id.options_container, BannerOptionsFragment.class, null)
50                     .commit();
51         }
52     }
53 
54     public static class BannerOptionsFragment extends PreferenceFragmentCompat {
55 
56         private final Executor mExecutor = Executors.newSingleThreadExecutor();
57         private EditTextPreference mVideoUrlPreference;
58 
59         private EditTextPreference mPackageToOpen;
60         private ListPreference mOnClickPreference;
61         private ListPreference mSizePreference;
62 
63         @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)64         public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
65             mExecutor.execute(
66                     () -> {
67                         Looper.prepare();
68                         setPreferencesFromResource(R.xml.banner_preferences, rootKey);
69                         configurePreferences();
70                     });
71         }
72 
73         @NonNull
findPreferenceOrFail(String key)74         private Preference findPreferenceOrFail(String key) {
75             final Preference preference = findPreference(key);
76             if (preference == null) {
77                 throw new RuntimeException(String.format("Could not find preference '%s'", key));
78             }
79             return preference;
80         }
81 
configurePreferences()82         private void configurePreferences() {
83             mVideoUrlPreference = (EditTextPreference) findPreferenceOrFail("banner_video_url");
84             mOnClickPreference = (ListPreference) findPreferenceOrFail("banner_on_click");
85             mPackageToOpen = (EditTextPreference) findPreferenceOrFail("package_to_open");
86             mSizePreference = (ListPreference) findPreferenceOrFail("banner_ad_size");
87             final ListPreference viewTypePreference =
88                     (ListPreference) findPreferenceOrFail("banner_view_type");
89 
90             viewTypePreference.setOnPreferenceChangeListener(
91                     (preference, object) -> {
92                         final String selection = (String) object;
93                         refreshVideoPreferenceVisibility(selection);
94                         refreshOnClickEnabled(selection);
95                         refreshAdSize((selection));
96                         return true;
97                     });
98 
99             final String viewTypeSelection = viewTypePreference.getValue();
100             refreshVideoPreferenceVisibility(viewTypeSelection);
101             refreshOnClickEnabled(viewTypeSelection);
102             refreshAdSize(viewTypeSelection);
103         }
104 
refreshVideoPreferenceVisibility(String viewTypeSelection)105         private void refreshVideoPreferenceVisibility(String viewTypeSelection) {
106             BannerOptions.ViewType viewType = BannerOptions.ViewType.valueOf(viewTypeSelection);
107             mVideoUrlPreference.setVisible(viewType == BannerOptions.ViewType.VIDEO);
108         }
109 
refreshOnClickEnabled(String viewTypeSelection)110         private void refreshOnClickEnabled(String viewTypeSelection) {
111             BannerOptions.ViewType viewType = BannerOptions.ViewType.valueOf(viewTypeSelection);
112             switch (viewType) {
113                 case VIDEO:
114                 {
115                     mOnClickPreference.setEnabled(false);
116                     mOnClickPreference.setSummaryProvider(null);
117                     mOnClickPreference.setSummary("Video controls");
118                     break;
119                 }
120                 case WEBVIEW:
121                 {
122                     mOnClickPreference.setEnabled(false);
123                     mOnClickPreference.setSummaryProvider(null);
124                     mOnClickPreference.setSummary("WebView receives clicks");
125                     break;
126                 }
127                 case EDITTEXT:
128                 {
129                     mOnClickPreference.setEnabled(false);
130                     mOnClickPreference.setSummaryProvider(null);
131                     mOnClickPreference.setSummary("EditText doesn't need to be clicked");
132                     break;
133                 }
134                 default:
135                 {
136                     mOnClickPreference.setEnabled(true);
137                     mOnClickPreference.setSummaryProvider(
138                             ListPreference.SimpleSummaryProvider.getInstance());
139                     break;
140                 }
141             }
142         }
143 
refreshAdSize(String viewTypeSelection)144         private void refreshAdSize(String viewTypeSelection) {
145             BannerOptions.ViewType viewType = BannerOptions.ViewType.valueOf(viewTypeSelection);
146             switch (viewType) {
147                 case WEBVIEW:
148                 {
149                     mSizePreference.setEnabled(false);
150                     mSizePreference.setSummaryProvider(null);
151                     mSizePreference.setSummary("WebView must be large");
152                     break;
153                 }
154                 default:
155                 {
156                     mSizePreference.setEnabled(true);
157                     mSizePreference.setSummaryProvider(
158                             ListPreference.SimpleSummaryProvider.getInstance());
159                     break;
160                 }
161             }
162         }
163     }
164 }
165