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