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 private EditTextPreference mImageUrlPreference; 59 60 private EditTextPreference mPackageToOpen; 61 private ListPreference mOnClickPreference; 62 private ListPreference mSizePreference; 63 64 @Override onCreatePreferences(Bundle savedInstanceState, String rootKey)65 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 66 mExecutor.execute( 67 () -> { 68 Looper.prepare(); 69 setPreferencesFromResource(R.xml.banner_preferences, rootKey); 70 configurePreferences(); 71 }); 72 } 73 74 @NonNull findPreferenceOrFail(String key)75 private Preference findPreferenceOrFail(String key) { 76 final Preference preference = findPreference(key); 77 if (preference == null) { 78 throw new RuntimeException(String.format("Could not find preference '%s'", key)); 79 } 80 return preference; 81 } 82 configurePreferences()83 private void configurePreferences() { 84 mVideoUrlPreference = (EditTextPreference) findPreferenceOrFail("banner_video_url"); 85 mImageUrlPreference = (EditTextPreference) findPreferenceOrFail("banner_image_url"); 86 mOnClickPreference = (ListPreference) findPreferenceOrFail("banner_on_click"); 87 mPackageToOpen = (EditTextPreference) findPreferenceOrFail("package_to_open"); 88 mSizePreference = (ListPreference) findPreferenceOrFail("banner_ad_size"); 89 final ListPreference viewTypePreference = 90 (ListPreference) findPreferenceOrFail("banner_view_type"); 91 92 viewTypePreference.setOnPreferenceChangeListener( 93 (preference, object) -> { 94 final String selection = (String) object; 95 refreshVideoOrImagePreferenceVisibility(selection); 96 refreshOnClickEnabled(selection); 97 refreshAdSize((selection)); 98 return true; 99 }); 100 101 final String viewTypeSelection = viewTypePreference.getValue(); 102 refreshVideoOrImagePreferenceVisibility(viewTypeSelection); 103 refreshOnClickEnabled(viewTypeSelection); 104 refreshAdSize(viewTypeSelection); 105 } 106 refreshVideoOrImagePreferenceVisibility(String viewTypeSelection)107 private void refreshVideoOrImagePreferenceVisibility(String viewTypeSelection) { 108 BannerOptions.ViewType viewType = BannerOptions.ViewType.valueOf(viewTypeSelection); 109 mVideoUrlPreference.setVisible(viewType == BannerOptions.ViewType.VIDEO); 110 mImageUrlPreference.setVisible(viewType == BannerOptions.ViewType.IMAGE); 111 } 112 refreshOnClickEnabled(String viewTypeSelection)113 private void refreshOnClickEnabled(String viewTypeSelection) { 114 BannerOptions.ViewType viewType = BannerOptions.ViewType.valueOf(viewTypeSelection); 115 switch (viewType) { 116 case VIDEO: 117 { 118 mOnClickPreference.setEnabled(false); 119 mOnClickPreference.setSummaryProvider(null); 120 mOnClickPreference.setSummary("Video controls"); 121 break; 122 } 123 case WEBVIEW: 124 { 125 mOnClickPreference.setEnabled(false); 126 mOnClickPreference.setSummaryProvider(null); 127 mOnClickPreference.setSummary("WebView receives clicks"); 128 break; 129 } 130 case EDITTEXT: 131 { 132 mOnClickPreference.setEnabled(false); 133 mOnClickPreference.setSummaryProvider(null); 134 mOnClickPreference.setSummary("EditText doesn't need to be clicked"); 135 break; 136 } 137 default: 138 { 139 mOnClickPreference.setEnabled(true); 140 mOnClickPreference.setSummaryProvider( 141 ListPreference.SimpleSummaryProvider.getInstance()); 142 break; 143 } 144 } 145 } 146 refreshAdSize(String viewTypeSelection)147 private void refreshAdSize(String viewTypeSelection) { 148 BannerOptions.ViewType viewType = BannerOptions.ViewType.valueOf(viewTypeSelection); 149 switch (viewType) { 150 case WEBVIEW: 151 { 152 mSizePreference.setEnabled(false); 153 mSizePreference.setSummaryProvider(null); 154 mSizePreference.setSummary("WebView must be large"); 155 break; 156 } 157 default: 158 { 159 mSizePreference.setEnabled(true); 160 mSizePreference.setSummaryProvider( 161 ListPreference.SimpleSummaryProvider.getInstance()); 162 break; 163 } 164 } 165 } 166 } 167 } 168