1 /* 2 * Copyright (C) 2021 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.imsserviceentitlement; 18 19 import android.app.Activity; 20 import android.content.Intent; 21 import android.os.Bundle; 22 import android.os.SystemProperties; 23 import android.util.Log; 24 import android.view.KeyEvent; 25 26 import androidx.annotation.StringRes; 27 import androidx.fragment.app.FragmentActivity; 28 import androidx.fragment.app.FragmentTransaction; 29 30 import com.google.android.setupdesign.util.ThemeHelper; 31 import com.google.android.setupdesign.util.ThemeResolver; 32 33 /** The UI for WFC activation. */ 34 public class WfcActivationActivity extends FragmentActivity implements WfcActivationUi { 35 private static final String TAG = "IMSSE-WfcActivationActivity"; 36 37 // Dependencies 38 @SuppressWarnings("StaticFieldLeak") 39 private static WfcActivationController sWfcActivationController; 40 private WfcActivationController mWfcActivationController; 41 private WfcWebPortalFragment mWfcWebPortalFragment; 42 43 @Override onCreate(Bundle savedInstanceState)44 protected void onCreate(Bundle savedInstanceState) { 45 createDependeny(); 46 setSuwTheme(); 47 48 super.onCreate(savedInstanceState); 49 setContentView(R.layout.activity_wfc_activation); 50 51 if (mWfcActivationController.isSkipWfcActivation() 52 && ActivityConstants.isActivationFlow(getIntent())) { 53 Log.d(TAG, "Skip wfc activation"); 54 setResultAndFinish(Activity.RESULT_OK); 55 return; 56 } 57 58 mWfcActivationController.startFlow(); 59 } 60 61 @Override onDestroy()62 protected void onDestroy() { 63 super.onDestroy(); 64 mWfcActivationController.finish(); 65 } 66 67 @Override onKeyDown(int keyCode, KeyEvent event)68 public boolean onKeyDown(int keyCode, KeyEvent event) { 69 if (mWfcWebPortalFragment != null && mWfcWebPortalFragment.onKeyDown(keyCode, event)) { 70 return true; 71 } 72 return super.onKeyDown(keyCode, event); 73 } 74 75 @Override showActivationUi( @tringRes int title, @StringRes int text, boolean isInProgress, @StringRes int primaryButtonText, int primaryButtonResult, @StringRes int secondaryButtonText)76 public boolean showActivationUi( 77 @StringRes int title, 78 @StringRes int text, 79 boolean isInProgress, 80 @StringRes int primaryButtonText, 81 int primaryButtonResult, 82 @StringRes int secondaryButtonText) { 83 runOnUiThreadIfAlive( 84 () -> { 85 FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 86 SuwUiFragment frag = 87 SuwUiFragment.newInstance( 88 title, 89 text, 90 isInProgress, 91 primaryButtonText, 92 primaryButtonResult, 93 secondaryButtonText); 94 ft.replace(R.id.wfc_activation_container, frag); 95 // commit may be executed after activity's state is saved. 96 ft.commitAllowingStateLoss(); 97 }); 98 return true; 99 } 100 101 @Override showWebview(String url, String postData)102 public boolean showWebview(String url, String postData) { 103 runOnUiThreadIfAlive( 104 () -> { 105 FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 106 mWfcWebPortalFragment = WfcWebPortalFragment.newInstance(url, postData); 107 ft.replace(R.id.wfc_activation_container, mWfcWebPortalFragment); 108 // commit may be executed after activity's state is saved. 109 ft.commitAllowingStateLoss(); 110 }); 111 return true; 112 } 113 runOnUiThreadIfAlive(Runnable r)114 private void runOnUiThreadIfAlive(Runnable r) { 115 if (!isFinishing() && !isDestroyed()) { 116 runOnUiThread(r); 117 } 118 } 119 120 @Override setResultAndFinish(int resultCode)121 public void setResultAndFinish(int resultCode) { 122 Log.d(TAG, "setResultAndFinish: result=" + resultCode); 123 if (!isFinishing() && !isDestroyed()) { 124 setResult(resultCode); 125 finish(); 126 } 127 } 128 129 @Override getController()130 public WfcActivationController getController() { 131 return mWfcActivationController; 132 } 133 setSuwTheme()134 private void setSuwTheme() { 135 int defaultTheme = 136 ThemeHelper.isSetupWizardDayNightEnabled(this) 137 ? com.google.android.setupdesign.R.style.SudThemeGlifV3_DayNight 138 : com.google.android.setupdesign.R.style.SudThemeGlifV3_Light; 139 ThemeResolver themeResolver = 140 new ThemeResolver.Builder(ThemeResolver.getDefault()) 141 .setDefaultTheme(defaultTheme) 142 .setUseDayNight(true) 143 .build(); 144 setTheme(themeResolver.resolve( 145 SystemProperties.get("setupwizard.theme", "SudThemeGlifV3_DayNight"), 146 /* suppressDayNight= */ !ThemeHelper.isSetupWizardDayNightEnabled(this))); 147 } 148 createDependeny()149 private void createDependeny() { 150 Log.d(TAG, "Loading dependencies..."); 151 // TODO(b/177495634) Use DependencyInjector 152 if (sWfcActivationController == null) { 153 // Default initialization 154 Log.d(TAG, "Default WfcActivationController initialization"); 155 Intent startIntent = this.getIntent(); 156 int subId = ActivityConstants.getSubId(startIntent); 157 mWfcActivationController = 158 new WfcActivationController( 159 /* context = */ this, 160 /* wfcActivationUi = */ this, 161 new ImsEntitlementApi(this, subId), 162 startIntent); 163 } else { 164 mWfcActivationController = sWfcActivationController; 165 } 166 } 167 } 168